working towards sync on firefox

This commit is contained in:
Chris Karlof 2013-01-23 22:29:06 -08:00
Родитель ddc0ced0b4
Коммит 858f5c03ed
15 изменённых файлов: 150 добавлений и 58 удалений

1
TODO
Просмотреть файл

@ -1,3 +1,4 @@
- handle exceptions in FX tld.js when that service returns an exception, e.g, for intranet domains
- figure out API URL for different releases
- revisit tobmog packaging
- make sure linking creds are deleted if no logged in user or user dismisses but wait until after notification appears and user reacts

Просмотреть файл

@ -3,8 +3,8 @@
<script src="../lib/jsuri.js"></script>
<script src="../lib/tldjs.js"></script>
<script src="../lib/underscore.js"></script>
<script src="../lib/backbone-min.js"></script>
<script src="../lib/backbone.localStorage.js"></script>
<script src="../lib/backbone.js"></script>
<script src="storage.js"></script>
<script src="../infobar/manager.js"></script>
<script src="/server/client/sjcl-with-cbc.js"></script>
<script src="/server/client/urlparse.js"></script>

Просмотреть файл

@ -1,7 +1,7 @@
var UserCollection = function(Backbone, _, User, LocalStorage) {
var UserCollection = function(Backbone, _, Gombot, LocalStorage) {
var UserCollection = Backbone.Collection.extend({
model: User,
model: Gombot.User,
localStorage: LocalStorage
});

Просмотреть файл

@ -0,0 +1,23 @@
var FirefoxLocalStorage = function() {
var ss = require("simple-storage");
var timers = require("timers");
// this module uses setTimeouts to roughly simulate the async interface in chrome
return {
getItem: function(key, callback) {
timers.setTimeout(function() {
callback(ss.storage[key]);
}, 0);
},
setItem: function(key, data, callback) {
ss.storage[key] = data;
timers.setTimeout(callback,0);
},
removeItem: function(key, callback) {
delete ss.storage[key];
timers.setTimeout(callback, 0);
}
}
};
module.exports = FirefoxLocalStorage;

Просмотреть файл

@ -61,20 +61,22 @@ var gombotModules = {
Backbone: require("../lib/backbone"),
_ : require("../lib/underscore"),
Messaging: require("./firefox_messaging"),
LocalStorage: function() {}, // TODO
LocalStorage: require("./firefox_local_storage"),
Tld: require("./tld.js"),
Uri: require("../lib/jsuri"),
TldService: require("../tld_service"),
SiteConfigs: require("../site_configs"),
Realms: require("../realms"),
Storage: function() {}, // TODO
Storage: require("../storage"),
GombotClient: require("../client/client"),
GombotSync: require("../gombot_sync"),
LoginCredential: require("../models/login_credential"),
LoginCredentialCollection: require("../collections/login_credential_collection"),
CapturedCredentialStorage: require("../captured_credential_storage"),
Linker: require("../linker"),
CommandHandler: require("../command_handler")
CommandHandler: require("../command_handler"),
User: require("../models/user"),
UserCollection: require("../collections/user_collection")
};
var Gombot = require("../gombot")(gombotModules);

Просмотреть файл

@ -24,7 +24,7 @@ var _Gombot = function(importedModules, Gombot) {
}
}
var Backbone = getModule("Backbone");
var Backbone = getModule("Backbone")();
var _ = getModule("_");
// mixin guid creation into underscore
@ -71,11 +71,11 @@ var _Gombot = function(importedModules, Gombot) {
currentUser.destroy({ localOnly: true, success: function() { currentUser = null; callback(); }});
};
// new Gombot.Storage("users", function(store) {
// Gombot.User = User(Backbone, _, Gombot.LoginCredentialCollection, Gombot.Sync, store);
// Gombot.UserCollection = UserCollection(Backbone, _, Gombot.User, store);
// checkFirstRun();
// });
new Gombot.Storage("users", function(store) {
Gombot.User = getModule("User")(Backbone, _, Gombot, store);
Gombot.UserCollection = getModule("UserCollection")(Backbone, _, Gombot, store);
checkFirstRun();
});
function checkFirstRun() {
Gombot.LocalStorage.getItem("firstRun", function(firstRun) {
@ -88,13 +88,34 @@ var _Gombot = function(importedModules, Gombot) {
Gombot.users.fetch({
success: function() {
if (!firstRun) {
if (window.startFirstRunFlow) {
if (typeof startFirstRunFlow === 'function') {
startFirstRunFlow(false /* showSignInPage */); // shows signup page on first run
Gombot.LocalStorage.setItem("firstRun", true);
} // TODO: fix this
}
var loggedInUser = Gombot.users.find(function(user) { return user.isAuthenticated() });
if (loggedInUser) Gombot.setCurrentUser(loggedInUser);
if (Gombot.users.size() === 0) {
console.log("No logged in user. Number of users:"+Gombot.users.size());
var user = new Gombot.User({
'email': 'ckarlof+'+Math.floor(10000*Math.random(10000))+'@mozilla.com',
'pin': '1111'
});
user.save(null,{
success: function() {
Gombot.setCurrentUser(user);
},
error: function(args) {
console.log("ERROR", JSON.stringify(args));
if (args.response && args.response.errorMessage.match(/That email has already been used/)) {
}
},
password: "foobar",
newsletter: false
});
}
}});
}
return Gombot;

Просмотреть файл

@ -31,8 +31,9 @@ var GombotSync = function(Gombot, Backbone, _) {
var self = this;
client.account({
email: model.get('email'),
model: model,
pass: options.password,
newsletter: options.newsletter
newsletter: options.newsletter,
}, function(err, result) {
if (maybeHandleError(options.error, err, result)) return;
update(client, model, options);

1
background/lib/backbone-min.js поставляемый
Просмотреть файл

@ -1 +0,0 @@
../../lib/backbone-min.js

Просмотреть файл

@ -1,9 +1,11 @@
var User = function(Backbone, _, LoginCredentialCollection, GombotSync, LocalStorage) {
var User = function(Backbone, _, Gombot, LocalStorage) {
const USER_DATA_VERSIONS = [
"identity.mozilla.com/gombot/v1/userData"
];
var GombotSync = Gombot.Sync,
LoginCredentialCollection = Gombot.LoginCredentialCollection;
// attributes should be something like:
// {
@ -146,4 +148,7 @@ var User = function(Backbone, _, LoginCredentialCollection, GombotSync, LocalSto
return User;
}
if (typeof module !== "undefined" && module.exports) {
module.exports = User;
}

1
background/storage.js Symbolic link
Просмотреть файл

@ -0,0 +1 @@
../lib/backbone.localStorage.js

Просмотреть файл

@ -1,2 +1,34 @@
// Like polyfill hack for gombot_client.js on Firefox Jetpack
module.exports = require('xhr');
var Request = require('request').Request;
var Xhrequest = function(url, req) {
var method = req.method,
data = req.data,
headers = req.headers,
success = req.success,
error = req.error,
reqObj = {
url: url,
content: data,
headers: headers,
contentType: 'application/json'
};
reqObj.onComplete = function(response) {
//console.log(JSON.stringify(response.json))
if (response.status === 200) {
success(response.text);
} else {
error({ error: { responseText: response.text }, status: response.status }, {}, response.status);
}
};
var request = Request(reqObj);
switch (method) {
case 'PUT': request.put(); break;
case 'POST': request.post(); break;
case 'GET': request.get(); break;
default: console.log("Xhrequest: unknown method: "+method); break;
}
};
module.exports = Xhrequest;

Просмотреть файл

@ -18,7 +18,6 @@ var ContentMessaging = function() {
console.log("ContentMessaging: error can't find callback for callbackId="+callbackId);
return;
}
console.log("ContentMessaging: found callback for callbackId="+callbackId);
callback(addonMessage.message);
delete callbacks[callbackId];
});
@ -29,7 +28,6 @@ var ContentMessaging = function() {
function messageToChrome(message, callback) {
callback = callback || function() {};
console.log("messageToChrome", JSON.stringify(message), callback);
var callbackId = getCallbackId();
var messageWrapper = { message: message, callbackId: callbackId };
callbacks[callbackId] = callback;

Просмотреть файл

@ -5,7 +5,9 @@
// For all details and documentation:
// http://backbonejs.org
(function(){
var _Backbone = function() {
var Backbone;
(function(){
// Initial Setup
// -------------
@ -26,12 +28,12 @@
// The top-level namespace. All public Backbone classes and modules will
// be attached to this. Exported for both CommonJS and the browser.
var Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
Backbone = {};
// if (typeof exports !== 'undefined') {
// Backbone = exports;
// } else {
// Backbone = root.Backbone = {};
// }
// Current version of the library. Keep in sync with `package.json`.
Backbone.VERSION = '0.9.10';
@ -43,9 +45,6 @@
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
Backbone.$ = root.jQuery || root.Zepto || root.ender;
// Hack to allow Jetpack modules to dynamically change Backbone
Backbone.mutable = {};
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
Backbone.noConflict = function() {
@ -1382,15 +1381,7 @@
// instead of `application/json` with the model in a param named `model`.
// Useful when interfacing with server-side languages like **PHP** that make
// it difficult to read the body of `PUT` requests.
// Hack to address Jetpack which won't let us modify top-level objects on
// the Backbone module
Backbone.sync = function(method, model, options) {
return Backbone.mutable.sync.apply(this, arguments);
}
Backbone.mutable.sync = function(method, model, options) {
var type = methodMap[method];
// Default options, unless specified.
@ -1505,5 +1496,12 @@
var urlError = function() {
throw new Error('A "url" property or function must be specified');
};
}).call(this);
return Backbone;
};
}).call(this);
var Backbone = _Backbone;
if (typeof module !== "undefined" && module.exports) {
module.exports = _Backbone;
}

Просмотреть файл

@ -3,18 +3,18 @@
*
* https://github.com/jeromegn/Backbone.localStorage
*/
var Storage = function(Backbone, _, Storage) {
var Storage = function(Backbone, _, LocalStorage) {
return (function (root, factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["underscore","backbone"], function(_, Backbone) {
// Use global variables if the locals is undefined.
return factory(_ || root._, Backbone || root.Backbone);
});
} else {
// if (typeof define === "function" && define.amd) {
// // AMD. Register as an anonymous module.
// defin(["underscore","backbone"], function(_, Backbone) {
// // Use global variables if the locals is undefined.
// return factory(_ || root._, Backbone || root.Backbone);
// });
// } else {
// RequireJS isn't being used. Assume underscore and backbone is loaded in <script> tags
return factory(_, Backbone);
}
return factory(_, Backbone);
//}
}(this, function(_, Backbone) {
// A simple module to replace `Backbone.sync` with *localStorage*-based
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
@ -36,7 +36,7 @@ function guid() {
// Our Store is represented by a single JS object in *localStorage*. Create it
// with a meaningful name, like the name you'd give a table.
// window.Store is deprectated, use Backbone.LocalStorage instead
Backbone.LocalStorage = window.Store = function(name, callback) {
Backbone.LocalStorage = function(name, callback) {
this.name = name;
var cb = function(store) {
this.records = (store && store.split(",")) || [];
@ -110,7 +110,7 @@ _.extend(Backbone.LocalStorage.prototype, {
},
localStorage: function() {
return Storage;
return LocalStorage;
},
// fix for "illegal access" error on Android when JSON.parse is passed null
@ -123,17 +123,24 @@ _.extend(Backbone.LocalStorage.prototype, {
// localSync delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
// window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options) {
Backbone.LocalStorage.sync = Backbone.localSync = function(method, model, options) {
var store = model.localStorage || model.collection.localStorage;
var resp, syncDfd = $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
var syncDfd = (typeof $ !== "undefined") && $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
var callback = function(resp) {
if (resp) {
if (options && options.success) options.success(resp);
if (options && options.success) options.success(model, resp, options);
if (syncDfd) syncDfd.resolve();
} else {
if (options && options.error) options.error("Record not found");
if (options && options.error) options.error(model, "Record not found", options);
if (syncDfd) syncDfd.reject();
}
@ -171,4 +178,8 @@ Backbone.sync = function(method, model, options) {
return Backbone.LocalStorage;
}));
};
};
if (typeof module !== "undefined" && module.exports) {
module.exports = Storage;
}

2
server

@ -1 +1 @@
Subproject commit 23cb21a3a873be669e2a587c3074e32850bc43e1
Subproject commit 91ef645a0c28d0891ec23d193ac995d6fc1165b7