forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from MozCloudStorage/unidisk_gecko
Unidisk gecko
- Loading branch information
Showing
11 changed files
with
2,019 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,3 @@ if CONFIG['GAIADIR']: | |
DIRS += ['gaia'] | ||
|
||
DIRS += ['app'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.