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