summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/storage.js
blob: 3ed13d0189bf177f0d65f710b2c370b413a44319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { Cc, Ci, Cu, components } = require('chrome');
const { id } = require('sdk/self');
const unload = require('sdk/system/unload');
const { defer } = require('sdk/core/promise');

const { Instances } = require('./chrome/instances');
const { Services } = require('./chrome/services');
const { NetUtil } = require('./chrome/net-utils');

const { FileUtils } = Cu.import('resource://gre/modules/FileUtils.jsm', {});

const JETPACK_DIR_BASENAME = "jetpack";

let saving = false;

function getStorageFile() {
  const file = Services.dirsvc.get('ProfD', Ci.nsIFile);
  file.append(JETPACK_DIR_BASENAME);
  file.append(id);
  file.append('pathfinder');
  file.append('storage');

  if (!file.exists())
    file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0755', 8));

  file.append('storage.blob');
  return file;
}

function get(options) {
  options = options || {};
  let charset = options.charset || 'UTF-8';

  const { promise, resolve } = defer();
  const file = getStorageFile();
  const channel = NetUtil.newChannel(file);

  if (!file.exists()) {
    resolve({ data: '' });
  }
  else {
    NetUtil.asyncFetch(channel, function(iStream, aResult) {
      if (!components.isSuccessCode(aResult)) {
        reject();
      }
      else {
        let text = NetUtil.readInputStreamToString(iStream, iStream.available());

        let conv = Instances.suc;
        conv.charset = charset;

        text = conv.ConvertToUnicode(text);

        resolve({
          data: text,
          charset: charset
        });
      }
    });
  }

  return promise;
}
exports.get = get;

function set({ data, charset }) {
  charset = charset || 'UTF-8';
  data = data || '';
  const { promise, resolve, reject } = defer();
  const file = getStorageFile();

  if (data == '') {
    if (file.exists()) {
      file.remove(false);
    }

    resolve({
      data: '',
      charset: charset
    });
  }
  else {
    const converter = Instances.suc;
    converter.charset = "UTF-8";

    if (isSaving()) {
      throw Error('Storage is currently in the process of saving..');
    }
    saving = true;

    let iStream = converter.convertToInputStream(data);
    let oStream = FileUtils.openSafeFileOutputStream(file);

    NetUtil.asyncCopy(
      iStream,
      oStream,
      function(aResult) {
        FileUtils.closeSafeFileOutputStream(oStream);
        saving = false;

        if (!components.isSuccessCode(aResult)) {
          reject();
        }
        else {
          resolve({
            data: data,
            charset: charset
          });
        }
      }
    );
  }

  return promise;
}
exports.set = set;

function isSaving() {
  return saving;
}
exports.isSaving = isSaving;