-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.storage.js
72 lines (62 loc) · 2.12 KB
/
system.storage.js
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
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var exec = require('cordova/exec');
var Event = require('cordova-plugin-chrome-apps-common.events');
var channel = require('cordova/channel');
var helpers = require('cordova-plugin-chrome-apps-common.helpers');
var eventsToFireOnStartUp = [];
exports.getInfo = function(callback) {
exec(callback, callback, 'ChromeSystemStorage', 'getInfo', []);
};
exports.ejectDevice = function(id, callback) {
exec(callback, callback, 'ChromeSystemStorage', 'ejectDevice', [id]);
};
exports.getAvailableCapacity = function(id, callback) {
var convertResult = callback && function(info) {
if (info === null) {
info = undefined;
}
callback(info);
};
exec(convertResult, convertResult, 'ChromeSystemStorage', 'getAvailableCapacity', [id]);
};
exports.onAttached = new Event('onAttached');
exports.onDetached = new Event('onDetached');
function firePendingEvents() {
var msg;
while (msg = eventsToFireOnStartUp.shift()) {
processMessage(msg);
}
eventsToFireOnStartUp = null;
}
function onMessageFromNative(msg) {
if (eventsToFireOnStartUp) {
eventsToFireOnStartUp.push(msg);
} else {
processMessage(msg);
}
}
function processMessage(msg) {
var action = msg.action;
if (action == 'attached') {
exports.onAttached.fire(msg.info);
} else if (action == 'detached') {
exports.onDetached.fire(msg.id);
} else {
throw new Error('Unknown system storage action' + msg.action);
}
}
channel.createSticky('onChromeSystemStorageReady');
channel.waitForInitialization('onChromeSystemStorageReady');
channel.onCordovaReady.subscribe(function() {
exec(onMessageFromNative, undefined, 'ChromeSystemStorage', 'messageChannel', []);
helpers.runAtStartUp(function() {
if (eventsToFireOnStartUp.length) {
helpers.queueLifeCycleEvent(firePendingEvents);
} else {
eventsToFireOnStartUp = null;
}
});
channel.initializationComplete('onChromeSystemStorageReady');
});