diff options
Diffstat (limited to 'js/io/system/chromeapi.js')
-rw-r--r-- | js/io/system/chromeapi.js | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js new file mode 100644 index 00000000..6df41fd3 --- /dev/null +++ b/js/io/system/chromeapi.js | |||
@@ -0,0 +1,419 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | ||
9 | NOTES: | ||
10 | The init function starts up the file system API, and a size must be | ||
11 | set, no unlimited available as of now. | ||
12 | |||
13 | Core API reference in NINJA: this.application.ninja.coreIoApi | ||
14 | |||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | ///////////////////////////////////////////////////////////////////// */ | ||
17 | // | ||
18 | var Montage = require("montage/core/core").Montage; | ||
19 | //////////////////////////////////////////////////////////////////////// | ||
20 | // | ||
21 | exports.ChromeApi = Montage.create(Object.prototype, { | ||
22 | //////////////////////////////////////////////////////////////////// | ||
23 | //Needs size in MBs for fileSystem init | ||
24 | init: { | ||
25 | enumerable: true, | ||
26 | value: function(size) { | ||
27 | // | ||
28 | if (window.webkitRequestFileSystem) { | ||
29 | //Current way to init Chrome's fileSystem API | ||
30 | window.webkitRequestFileSystem(window.PERSISTENT, size*1024*1024, function (fs) { | ||
31 | //Storing reference to instance | ||
32 | this.fileSystem = fs; | ||
33 | //Dispatching action ready event | ||
34 | var readyEvent = document.createEvent("CustomEvent"); | ||
35 | readyEvent.initEvent('ready', true, true); | ||
36 | this.dispatchEvent(readyEvent); | ||
37 | //Building data of local Ninja Library | ||
38 | this._listNinjaChromeLibrary(); | ||
39 | }.bind(this), function (e) {return false}); //Returns false on error (not able to init) | ||
40 | // | ||
41 | return true; | ||
42 | } else { | ||
43 | //No fileSystem API | ||
44 | return false; | ||
45 | } | ||
46 | } | ||
47 | }, | ||
48 | //////////////////////////////////////////////////////////////////// | ||
49 | // | ||
50 | _fileSystem: { | ||
51 | enumerable: false, | ||
52 | value: null | ||
53 | }, | ||
54 | //////////////////////////////////////////////////////////////////// | ||
55 | // | ||
56 | fileSystem: { | ||
57 | enumerable: false, | ||
58 | get: function() { | ||
59 | return this._fileSystem; | ||
60 | }, | ||
61 | set: function(value) { | ||
62 | this._fileSystem = value; | ||
63 | } | ||
64 | }, | ||
65 | //////////////////////////////////////////////////////////////////// | ||
66 | // | ||
67 | fileNew: { | ||
68 | enumerable: true, | ||
69 | value: function(filePath, content, mime, callback) { | ||
70 | // | ||
71 | this.fileSystem.root.getFile(filePath, {create: true}, function(f) { | ||
72 | // | ||
73 | f.createWriter(function(writer) { | ||
74 | // | ||
75 | var b = new window.WebKitBlobBuilder; | ||
76 | b.append(content); | ||
77 | writer.write(b.getBlob(mime)); | ||
78 | // | ||
79 | if (callback) callback(true); | ||
80 | }, function (e) {if (callback) callback(false)}); | ||
81 | }, function (e) {if (callback) callback(false)}); | ||
82 | } | ||
83 | }, | ||
84 | //////////////////////////////////////////////////////////////////// | ||
85 | //Creating directory from path, callback optional | ||
86 | directoryNew: { | ||
87 | enumerable: true, | ||
88 | value: function(directoryPath, callback) { | ||
89 | //Checking for directory not to already exist | ||
90 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
91 | if (callback) callback(false); | ||
92 | return; //Directory already exists | ||
93 | }); | ||
94 | //Creating new directory | ||
95 | this.fileSystem.root.getDirectory(directoryPath, {create: true}, function(dir) { | ||
96 | if (callback) callback(true); | ||
97 | }, function (e) {if (callback) callback(false)}); | ||
98 | } | ||
99 | }, | ||
100 | //////////////////////////////////////////////////////////////////// | ||
101 | // | ||
102 | directoryDelete: { | ||
103 | enumerable: true, | ||
104 | value: function(directoryPath, callback) { | ||
105 | // | ||
106 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
107 | // | ||
108 | dir.removeRecursively(function() { | ||
109 | if (callback) callback(true); | ||
110 | }); | ||
111 | }, function (e) {if (callback) callback(false)}); | ||
112 | } | ||
113 | }, | ||
114 | //////////////////////////////////////////////////////////////////// | ||
115 | //Returns the directory contents to a callback function | ||
116 | directoryContents: { | ||
117 | enumerable: true, | ||
118 | value: function(directory, callback) { | ||
119 | //Creating instance of directory reader | ||
120 | this.fileSystem.directoryReader = directory.createReader(); | ||
121 | //Getting directory contents and sending results to callback | ||
122 | this.fileSystem.directoryReader.readEntries(function(results) { | ||
123 | //Calling callback with results (null if invalid directory) | ||
124 | callback(results); | ||
125 | }, function (e) {callback(null)}); | ||
126 | } | ||
127 | }, | ||
128 | //////////////////////////////////////////////////////////////////// | ||
129 | // | ||
130 | directoryCopy: { | ||
131 | enumerable: true, | ||
132 | value: function() { | ||
133 | } | ||
134 | }, | ||
135 | //////////////////////////////////////////////////////////////////// | ||
136 | // | ||
137 | directoryRename: { | ||
138 | enumerable: true, | ||
139 | value: function() { | ||
140 | } | ||
141 | }, | ||
142 | //////////////////////////////////////////////////////////////////// | ||
143 | // | ||
144 | directoryMove: { | ||
145 | enumerable: true, | ||
146 | value: function() { | ||
147 | } | ||
148 | }, | ||
149 | //////////////////////////////////////////////////////////////////// | ||
150 | // | ||
151 | _listNinjaChromeLibrary: { | ||
152 | enumerable: false, | ||
153 | value: function () { | ||
154 | function parseLibrary (contents) { | ||
155 | // | ||
156 | var lib = []; | ||
157 | // | ||
158 | for(var i=0; contents[i]; i++) { | ||
159 | // | ||
160 | if (contents[i].isDirectory) { | ||
161 | lib.push(contents[i].name); | ||
162 | } | ||
163 | } | ||
164 | //Dispatching action ready event | ||
165 | var libraryEvent = document.createEvent("CustomEvent"); | ||
166 | libraryEvent.initEvent('library', true, true); | ||
167 | libraryEvent.ninjaChromeLibrary = lib; | ||
168 | this.dispatchEvent(libraryEvent); | ||
169 | }; | ||
170 | // | ||
171 | this.directoryContents(this.fileSystem.root, parseLibrary.bind(this)); | ||
172 | } | ||
173 | } | ||
174 | //////////////////////////////////////////////////////////////////// | ||
175 | //////////////////////////////////////////////////////////////////// | ||
176 | }); | ||
177 | //////////////////////////////////////////////////////////////////////// | ||
178 | //////////////////////////////////////////////////////////////////////// | ||
179 | |||
180 | |||
181 | |||
182 | //window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 /*10MB*/, function (fs) { | ||
183 | |||
184 | |||
185 | |||
186 | /* | ||
187 | for (var i=1; i<50; i++) { | ||
188 | fs.root.getDirectory('montage0.0.0.'+i, {}, function(dirEntry) { | ||
189 | // | ||
190 | dirEntry.removeRecursively(function() { | ||
191 | console.log('Directory removed.'); | ||
192 | }); | ||
193 | }); | ||
194 | } | ||
195 | */ | ||
196 | |||
197 | |||
198 | |||
199 | |||
200 | |||
201 | |||
202 | // | ||
203 | /* | ||
204 | var xhr = new XMLHttpRequest(), dir, mjs; | ||
205 | // | ||
206 | xhr.open("GET", '/ninja-internal/node_modules/descriptor.json', false); | ||
207 | xhr.send(); | ||
208 | // | ||
209 | if (xhr.readyState === 4) { | ||
210 | // | ||
211 | mjs = JSON.parse(xhr.response); | ||
212 | // | ||
213 | if (mjs.version) { | ||
214 | //Checking for version to exist | ||
215 | fs.root.getDirectory('montage'+mjs.version, {}, function(dirEntry) { | ||
216 | //Already copied, nothing | ||
217 | console.log('montage'+mjs.version+' has already been created'); | ||
218 | }, function (e) { | ||
219 | //Not present, should be copied | ||
220 | createFolder(false, {name: 'montage'+mjs.version}); | ||
221 | // | ||
222 | for (var i in mjs.directories) { | ||
223 | createFolder('montage'+mjs.version, mjs.directories[i]); | ||
224 | } | ||
225 | // | ||
226 | |||
227 | |||
228 | for (var j in mjs.files) { | ||
229 | |||
230 | var frqst = new XMLHttpRequest(); | ||
231 | frqst.open("GET", '/ninja-internal/node_modules/montage/'+mjs.files[j], false); | ||
232 | frqst.send(); | ||
233 | |||
234 | if (frqst.readyState === 4) { | ||
235 | createFile('montage'+mjs.version+'/'+mjs.files[j], frqst.response); | ||