aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/chromeapi.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/chromeapi.js')
-rw-r--r--js/io/system/chromeapi.js386
1 files changed, 386 insertions, 0 deletions
diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js
new file mode 100644
index 00000000..fb141687
--- /dev/null
+++ b/js/io/system/chromeapi.js
@@ -0,0 +1,386 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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////////////////////////////////////////////////////////////////////////
9NOTES:
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//
15var Montage = require("montage/core/core").Montage;
16////////////////////////////////////////////////////////////////////////
17//
18exports.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: {
73 enumerable: true,
74 value: function(directoryPath, callback) {
75 //
76 this.fileSystem.getDirectory(directoryPath, {}, function(dirEntry) {
77 //
78 dirEntry.removeRecursively(function() {
79 callback(true);
80 });
81 }, function (e) {callback(false)});
82 }
83 },
84 ////////////////////////////////////////////////////////////////////
85 //Returns the directory contents to a callback function
86 directoryContents: {
87 enumerable: true,
88 value: function(directory, callback) {
89 //Creating instance of directory reader
90 this.fileSystem.directoryReader = directory.createReader();
91 //Getting directory contents and sending results to callback
92 this.fileSystem.directoryReader.readEntries(function(results) {
93 //Calling callback with results (null if invalid directory)
94 callback(results);
95 }, function (e) {callback(null)});
96 }
97 },
98 ////////////////////////////////////////////////////////////////////
99 //
100 directoryCopy: {
101 enumerable: true,
102 value: function() {
103 }
104 },
105 ////////////////////////////////////////////////////////////////////
106 //
107 directoryRename: {
108 enumerable: true,
109 value: function() {
110 }
111 },
112 ////////////////////////////////////////////////////////////////////
113 //
114 directoryMove: {
115 enumerable: true,
116 value: function() {
117 }
118 },
119 ////////////////////////////////////////////////////////////////////
120 //
121 _listNinjaChromeLibrary: {
122 enumerable: false,
123 value: function () {
124 function parseLibrary (contents) {
125 //
126 var lib = [];
127 //
128
129
130
131 //Dispatching action ready event
132 var libraryEvent = document.createEvent("CustomEvent");
133 libraryEvent.initEvent('library', true, true);
134 libraryEvent.ninjaChromeLibrary = lib;
135 this.dispatchEvent(libraryEvent);
136 };
137 //
138 this.directoryContents(this.fileSystem.root, parseLibrary.bind(this));
139 }
140 }
141 ////////////////////////////////////////////////////////////////////
142 ////////////////////////////////////////////////////////////////////
143});
144////////////////////////////////////////////////////////////////////////
145////////////////////////////////////////////////////////////////////////
146
147
148
149//window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 /*10MB*/, function (fs) {
150
151
152
153 /*
154for (var i=1; i<50; i++) {
155 fs.root.getDirectory('montage0.0.0.'+i, {}, function(dirEntry) {
156 //
157 dirEntry.removeRecursively(function() {
158 console.log('Directory removed.');
159 });
160 });
161 }
162*/
163
164
165
166
167
168
169 //
170 /*
171var xhr = new XMLHttpRequest(), dir, mjs;
172 //
173 xhr.open("GET", '/ninja-internal/node_modules/descriptor.json', false);
174 xhr.send();
175 //
176 if (xhr.readyState === 4) {
177 //
178 mjs = JSON.parse(xhr.response);
179 //
180 if (mjs.version) {
181 //Checking for version to exist
182 fs.root.getDirectory('montage'+mjs.version, {}, function(dirEntry) {
183 //Already copied, nothing
184 console.log('montage'+mjs.version+' has already been created');
185 }, function (e) {
186 //Not present, should be copied
187 createFolder(false, {name: 'montage'+mjs.version});
188 //
189 for (var i in mjs.directories) {
190 createFolder('montage'+mjs.version, mjs.directories[i]);
191 }
192 //
193
194
195 for (var j in mjs.files) {
196
197 var frqst = new XMLHttpRequest();
198 frqst.open("GET", '/ninja-internal/node_modules/montage/'+mjs.files[j], false);
199 frqst.send();
200
201 if (frqst.readyState === 4) {
202 createFile('montage'+mjs.version+'/'+mjs.files[j], frqst.response);
203 }
204 }
205
206 //
207 console.log('montage'+mjs.version+' was created');
208 }, folderError);
209 }
210
211 }
212 //
213 function createFile (path, content) {
214 //
215 fs.root.getFile(path, {create: true}, function(fileEntry) {
216 //
217 fileEntry.createWriter(function(fileWriter) {
218 //
219 //console.log(path);
220 var bb = new window.WebKitBlobBuilder;
221 bb.append(content);
222 fileWriter.write(bb.getBlob('text/plain'));
223 }, fileError);
224
225 }, fileError);
226 }
227 //
228 function createFolder(root, folder) {
229 if (folder.name) {
230 if (root) {
231 dir = root+'/'+folder.name;
232 } else {
233 dir = folder.name;
234 }
235