Skip to content

Commit

Permalink
Merge pull request #5 from MozCloudStorage/unidisk_gecko
Browse files Browse the repository at this point in the history
Unidisk gecko
  • Loading branch information
weilonge committed May 19, 2015
2 parents 6411dda + 55511da commit e906129
Show file tree
Hide file tree
Showing 11 changed files with 2,019 additions and 1 deletion.
7 changes: 7 additions & 0 deletions b2g/chrome/content/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
XPCOMUtils.defineLazyModuleGetter(this, "Screenshot",
"resource://gre/modules/Screenshot.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "CloudStorage",
"resource://gre/modules/CloudStorage.jsm");

Cu.import('resource://gre/modules/Webapps.jsm');
DOMApplicationRegistry.allAppsLaunchable = true;

Expand Down Expand Up @@ -354,6 +357,7 @@ var shell = {
WebappsHelper.init();
UserAgentOverrides.init();
CaptivePortalLoginHelper.init();
CloudStorage.init();

this.contentBrowser.src = homeURL;
this.isHomeLoaded = false;
Expand Down Expand Up @@ -829,6 +833,7 @@ window.addEventListener('ContentStart', function ss_onContentStart() {
if (e.detail.type !== 'take-screenshot')
return;

/*
try {
shell.sendChromeEvent({
type: 'take-screenshot-success',
Expand All @@ -841,6 +846,8 @@ window.addEventListener('ContentStart', function ss_onContentStart() {
error: String(e)
});
}
*/
CloudStorage.toggle();
});
});

Expand Down
1 change: 0 additions & 1 deletion b2g/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ if CONFIG['GAIADIR']:
DIRS += ['gaia']

DIRS += ['app']

64 changes: 64 additions & 0 deletions dom/system/gonk/cloudstorage/CloudStorage.jsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* 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 { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Sample.jsm');
Cu.import('resource://gre/modules/Dropbox.jsm');
Cu.import('resource://gre/modules/udManager.jsm');
Cu.import('resource://gre/modules/MetaCache.jsm');
Cu.import('resource://gre/modules/DataCache.jsm');
const { console } = Cu.import("resource://gre/modules/devtools/Console.jsm", {});

let CloudStorage = {
init: function cloudstorage_init(){
console.log('cloudstorage_init');
this.status = false;

udManager.init({
accessToken:
'SAMPLE_ACCESS_TOKEN',
webStorageModule: Dropbox,
metaCacheModule: MetaCache,
dataCacheModule: DataCache
});

},
enable: function cloudstorage_enable() {
console.log('cloudstorage_enable');
if (!this.status) {
this.status = true;

udManager.getFileMeta('/', function (error, response) {
console.log(JSON.stringify(response.data));
});

udManager.getFileList('/', function (error, response) {
console.log(JSON.stringify(response.data));
});

console.log('CloudStorage is enabled.');
} else {
console.log('CloudStorage had already been enabled.');
}
},
disable: function cloudstorage_disable() {
console.log('cloudstorage_disable');
if (this.status) {
this.status = false;
console.log('CloudStorage is disabled.');
} else {
console.log('CloudStorage had already been disabled.');
}
},
toggle: function cloudstorage_toggle() {
console.log('cloudstorage_toggle');
this.status ? this.disable() : this.enable();
}
};
this.EXPORTED_SYMBOLS = ['CloudStorage'];
this.CloudStorage = CloudStorage;
122 changes: 122 additions & 0 deletions dom/system/gonk/cloudstorage/DataCache.jsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import('resource://gre/modules/MemoryDataStore.jsm');

var DataCache = {};

DataCache.init = function (blockSize) {
this._MAX_DATA_CACHE_ENTRY = 25;
this._BLOCK_SIZE = blockSize;

this._dataStore = MemoryDataStore;
this._dataStore.init();

this._fileDataCache = {};
this._priorityQueue = [];
};

DataCache.update = function (key, data) {
if (this._fileDataCache.hasOwnProperty(key) ) {
// Update an exist cache entry.
this._updateEntry(key, data);
} else {
// Add a new data cache.
if (this._priorityQueue.length < this._MAX_DATA_CACHE_ENTRY) {
// Entry is sufficient to add a new one.
this._pushEntry(key, data);
} else {
// Remove an entry and delete cache file.
var deletingCandidateKey = this._popLowPriorityKey();
this._removeEntry(deletingCandidateKey);
this._pushEntry(key, data);
}
}
};

DataCache._updateEntry = function (key, data) {
this._fileDataCache[key] = data;
};

DataCache._pushEntry = function (key, data) {
this._fileDataCache[key] = data;
this._priorityQueue.push(key);
};

DataCache._removeEntry = function (key) {
delete this._fileDataCache[key];
this._dataStore.deleteEntry(key);
};

DataCache._popLowPriorityKey = function () {
return this._priorityQueue.shift();
};

DataCache.updateStatus = function (md5sum, status) {
this._fileDataCache[md5sum].status = status;
};

DataCache.get = function (md5sum) {
if (this._fileDataCache.hasOwnProperty(md5sum)) {
return this._fileDataCache[md5sum];
}
return null;
};

DataCache.writeCache = function (task, data, cb){
var self = this;
this._dataStore.writeEntry(task.md5sum, data, function(err) {
if(err) {
console.log(err);
} else {
self.updateStatus(task.md5sum, 'DONE');
console.log('The file was saved!');
}
cb();
});
};

DataCache.readCache = function (path, buffer, offset, size, requestList, cb){
var seek = 0,
writeSize = 0,
cursor_moved = 0;

for (var i in requestList) {
var task = requestList[i];
if (task.priority === 'PREFETCH') {
continue;
}
if (this._fileDataCache[task.md5sum] &&
this._fileDataCache[task.md5sum].status === 'DONE') {
seek = ( offset + cursor_moved ) % this._BLOCK_SIZE;
writeSize = this._BLOCK_SIZE - seek;
if ((writeSize + cursor_moved ) > size) {
writeSize = size - cursor_moved;
}

this._dataStore.readEntry(task.md5sum,
buffer, cursor_moved, seek, writeSize);

cursor_moved += writeSize ;
} else {
console.error('======= Critical Error =======');
console.error(path);
console.error(offset);
console.error(size);
console.error(requestList);
console.error(this._fileDataCache);

throw Error('data is not finished.');
}
}
cb();
};

DataCache.generateKey = function (task){
var crypto = require('crypto');
var name = task.path + '' + task.offset + '';
var hash = crypto.createHash('md5').update(name).digest('hex');
return hash;
};

this.EXPORTED_SYMBOLS = ['DataCache'];
this.DataCache = DataCache;
Loading

0 comments on commit e906129

Please sign in to comment.