Moved all of the bookmark-share stuff out of the bookmarkEngine class into a new BookmarksSharingManager class.

This commit is contained in:
jonathandicarlo@jonathan-dicarlos-macbook-pro.local 2008-06-24 21:15:14 -07:00
parent bc4d891c3a
commit b24d0c7180

View File

@ -52,7 +52,6 @@ const SHARED_BOOKMARK_FILE_NAME = "shared_bookmarks";
const INCOMING_SHARE_ROOT_ANNO = "weave/mounted-shares-folder";
const INCOMING_SHARE_ROOT_NAME = "Shared Folders";
Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/dav.js");
Cu.import("resource://weave/util.js");
@ -63,44 +62,14 @@ Cu.import("resource://weave/syncCores.js");
Cu.import("resource://weave/stores.js");
Cu.import("resource://weave/trackers.js");
Cu.import("resource://weave/identity.js");
/* LONGTERM TODO: when we start working on the ability to share other types
of data besides bookmarks, the xmppClient instance should be moved to hang
off of Weave.Service instead of hanging off the BookmarksEngine. But for
now this is the easiest place to deal with it. */
Cu.import("resource://weave/xmpp/xmppClient.js");
Function.prototype.async = Async.sugar;
function BookmarksEngine(pbeId) {
this._init(pbeId);
function BookmarksSharingManager(engine) {
this._init(engine);
}
BookmarksEngine.prototype = {
get name() { return "bookmarks"; },
get logName() { return "BmkEngine"; },
get serverPrefix() { return "user-data/bookmarks/"; },
__core: null,
get _core() {
if (!this.__core)
this.__core = new BookmarksSyncCore();
return this.__core;
},
__store: null,
get _store() {
if (!this.__store)
this.__store = new BookmarksStore();
return this.__store;
},
__tracker: null,
get _tracker() {
if (!this.__tracker)
this.__tracker = new BookmarksTracker();
return this.__tracker;
},
BookmarksSharingManager.prototype = {
__annoSvc: null,
get _annoSvc() {
if (!this.__anoSvc)
@ -109,15 +78,16 @@ BookmarksEngine.prototype = {
return this.__annoSvc;
},
_init: function BmkEngine__init( pbeId ) {
this.__proto__.__proto__._init.call( this, pbeId );
_init: function SharingManager__init(engine) {
this._engine = engine;
this._log = Log4Moz.Service.getLogger("Bookmark Share");
if ( Utils.prefs.getBoolPref( "xmpp.enabled" ) ) {
dump( "Starting XMPP client for bookmark engine..." );
this._startXmppClient.async(this);
}
},
_startXmppClient: function BmkEngine__startXmppClient() {
_startXmppClient: function BmkSharing__startXmppClient() {
// To be called asynchronously.
let self = yield;
@ -140,7 +110,7 @@ BookmarksEngine.prototype = {
clientPassword,
transport,
auth );
let bmkEngine = this;
let bmkSharing = this;
let messageHandler = {
handle: function ( messageText, from ) {
/* The callback function for incoming xmpp messages.
@ -162,9 +132,9 @@ BookmarksEngine.prototype = {
let serverPath = words[1];
let directoryName = words.slice(2).join(" ");
if ( commandWord == "share" ) {
bmkEngine._incomingShareOffer(from, serverPath, folderName);
bmkSharing._incomingShareOffer(from, serverPath, folderName);
} else if ( commandWord == "stop" ) {
bmkEngine._incomingShareWithdrawn(from, serverPath, folderName);
bmkSharing._incomingShareWithdrawn(from, serverPath, folderName);
}
}
};
@ -180,7 +150,7 @@ BookmarksEngine.prototype = {
self.done();
},
_incomingShareOffer: function BmkEngine__incomingShareOffer(user,
_incomingShareOffer: function BmkSharing__incomingShareOffer(user,
serverPath,
folderName) {
/* Called when we receive an offer from another user to share a
@ -195,10 +165,10 @@ BookmarksEngine.prototype = {
right ahead to creating the incoming share.
*/
this._log.info("User " + user + " offered to share folder " + folderName);
this._createIncomingShare( user, serverPath, folderName );
this._createIncomingShare(user, serverPath, folderName);
},
_incomingShareWithdrawn: function BmkEngine__incomingShareStop(user,
_incomingShareWithdrawn: function BmkSharing__incomingShareStop(user,
serverPath,
folderName) {
/* Called when we receive a message telling us that a user who has
@ -208,21 +178,7 @@ BookmarksEngine.prototype = {
this._log.info("User " + user + " stopped sharing folder " + folderName);
this._stopIncomingShare(user, serverPath, folderName);
},
_sync: function BmkEngine__sync() {
/* After syncing, also call syncMounts to get the
incoming shared bookmark folder contents. */
let self = yield;
this.__proto__.__proto__._sync.async(this, self.cb );
yield;
this.updateAllOutgoingShares(self.cb);
yield;
this.updateAllIncomingShares(self.cb);
yield;
self.done();
},
_share: function BmkEngine__share( selectedFolder, username ) {
_share: function BmkSharing__share( selectedFolder, username ) {
// Return true if success, false if failure.
let ret = false;
let self = yield;
@ -266,7 +222,7 @@ BookmarksEngine.prototype = {
self.done( ret );
},
_stopSharing: function BmkEngine__stopSharing( selectedFolder, username ) {
_stopSharing: function BmkSharing__stopSharing( selectedFolder, username ) {
let self = yield;
let folderName = selectedFolder.getAttribute( "label" );
let serverPath = this._annoSvc.getItemAnnotation(folderNode,
@ -296,10 +252,10 @@ BookmarksEngine.prototype = {
self.done( true );
},
updateAllIncomingShares: function BmkEngine_updateAllIncoming(onComplete) {
updateAllIncomingShares: function BmkSharing_updateAllIncoming(onComplete) {
this._updateAllIncomingShares.async(this, onComplete);
},
_updateAllIncomingShares: function BmkEngine__updateAllIncoming() {
_updateAllIncomingShares: function BmkSharing__updateAllIncoming() {
/* For every bookmark folder in my tree that has the annotation
marking it as an incoming shared folder, pull down its latest
contents from its owner's account on the server. (This is
@ -308,7 +264,7 @@ BookmarksEngine.prototype = {
to the folder contents are simply wiped out by the latest
server contents.) */
let self = yield;
let mounts = this._store.findIncomingShares();
let mounts = this._engine._store.findIncomingShares();
for (let i = 0; i < mounts.length; i++) {
try {
@ -321,10 +277,10 @@ BookmarksEngine.prototype = {
}
},
updateAllOutgoingShares: function BmkEngine_updateAllOutgoing(onComplete) {
updateAllOutgoingShares: function BmkSharing_updateAllOutgoing(onComplete) {
this._updateAllOutgoingShares.async(this, onComplete);
},
_updateAllOutgoingShares: function BmkEngine__updateAllOutgoing() {
_updateAllOutgoingShares: function BmkSharing__updateAllOutgoing() {
let self = yield;
let shares = this._annoSvc.getItemsWithAnnotation(OUTGOING_SHARED_ANNO,
{});
@ -338,7 +294,7 @@ BookmarksEngine.prototype = {
self.done();
},
_createOutgoingShare: function BmkEngine__createOutgoing(folder, username) {
_createOutgoingShare: function BmkSharing__createOutgoing(folder, username) {
/* To be called asynchronously. Folder is a node indicating the bookmark
folder that is being shared; username is a string indicating the user
that it is to be shared with. This function creates the directory and
@ -406,7 +362,8 @@ BookmarksEngine.prototype = {
keys.ring[username] = encryptedForYou;
let keyringFile = new Resource( serverPath + "/" + KEYRING_FILE_NAME );
keyringFile.put( self.cb, this._json.encode( keys ) );
let jsonService = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
keyringFile.put( self.cb, jsonService.encode( keys ) );
yield;
// Call Atul's js api for setting htaccess:
@ -418,7 +375,7 @@ BookmarksEngine.prototype = {
self.done( serverPath );
},
_updateOutgoingShare: function BmkEngine__updateOutgoing(folderNode) {
_updateOutgoingShare: function BmkSharing__updateOutgoing(folderNode) {
/* Puts all the bookmark data from the specified bookmark folder,
encrypted, onto the shared directory on the server (replacing
anything that was already there).
@ -445,7 +402,7 @@ BookmarksEngine.prototype = {
let bulkIV = keys.bulkIV;
// Get the json-wrapped contents of everything in the folder:
let json = this._store._wrapMount( folderNode, myUserName );
let json = this._engine._store._wrapMount( folderNode, myUserName );
/* TODO what does wrapMount do with this username? Should I be passing
in my own or that of the person I share with? */
@ -463,11 +420,10 @@ BookmarksEngine.prototype = {
self.done();
},
_stopOutgoingShare: function BmkEngine__stopOutgoingShare(folderNode) {
_stopOutgoingShare: function BmkSharing__stopOutgoingShare(folderNode) {
/* Stops sharing the specified folder. Deletes its data from the
server, deletes the annotations that mark it as shared, and sends
a message to the shar-ee to let them know it's been withdrawn. */
// TODO: currently not called from anywhere.
let self = yield;
let serverPath = this._annoSvc.getItemAnnotation( folderNode,
SERVER_PATH_ANNO );
@ -562,7 +518,7 @@ BookmarksEngine.prototype = {
}
},
_updateIncomingShare: function BmkEngine__updateIncomingShare(mountData) {
_updateIncomingShare: function BmkSharing__updateIncomingShare(mountData) {
/* Pull down bookmarks from the server for a single incoming
shared folder, obliterating whatever was in that folder before.
@ -609,18 +565,18 @@ BookmarksEngine.prototype = {
/* Create diff between the json from server and the current contents;
then apply the diff. */
this._log.trace("Got bookmarks from " + user + ", comparing with local copy");
this._core.detectUpdates(self.cb, mountData.snapshot, snap.data);
this._engine._core.detectUpdates(self.cb, mountData.snapshot, snap.data);
let diff = yield;
// FIXME: should make sure all GUIDs here live under the mountpoint
this._log.trace("Applying changes to folder from " + user);
this._store.applyCommands.async(this._store, self.cb, diff);
this._engine._store.applyCommands.async(this._engine._store, self.cb, diff);
yield;
this._log.trace("Shared folder from " + user + " successfully synced!");
},
_stopIncomingShare: function BmkEngine__stopIncomingShare(user,
_stopIncomingShare: function BmkSharing__stopIncomingShare(user,
serverPath,
folderName)
{
@ -640,6 +596,59 @@ BookmarksEngine.prototype = {
}
}
}
}
function BookmarksEngine(pbeId) {
this._init(pbeId);
}
BookmarksEngine.prototype = {
get name() { return "bookmarks"; },
get logName() { return "BmkEngine"; },
get serverPrefix() { return "user-data/bookmarks/"; },
__core: null,
get _core() {
if (!this.__core)
this.__core = new BookmarksSyncCore();
return this.__core;
},
__store: null,
get _store() {
if (!this.__store)
this.__store = new BookmarksStore();
return this.__store;
},
__tracker: null,
get _tracker() {
if (!this.__tracker)
this.__tracker = new BookmarksTracker();
return this.__tracker;
},
__sharing: null,
get _sharing() {
if (!this.__sharing)
this.__sharing = new BookmarksSharingManager(this);
return this.__sharing;
},
_sync: function BmkEngine__sync() {
/* After syncing the regular bookmark folder contents,
* also update both the incoming and outgoing shared folders. */
let self = yield;
this.__proto__.__proto__._sync.async(this, self.cb );
yield;
this._sharing.updateAllOutgoingShares(self.cb);
yield;
this._sharing.updateAllIncomingShares(self.cb);
yield;
self.done();
}
};
BookmarksEngine.prototype.__proto__ = new Engine();