Merge lp:~urbanape/bindwood/thorough-debugging into lp:bindwood

Proposed by Zachery Bir
Status: Merged
Approved by: John O'Brien
Approved revision: 14
Merged at revision: not available
Proposed branch: lp:~urbanape/bindwood/thorough-debugging
Merge into: lp:bindwood
Diff against target: None lines
To merge this branch: bzr merge lp:~urbanape/bindwood/thorough-debugging
Reviewer Review Type Date Requested Status
John O'Brien (community) Approve
dobey (community) Approve
Review via email: mp+10928@code.launchpad.net

This proposal supersedes a proposal from 2009-08-28.

Commit message

This branch adds thorough debugging to the Bindwood functionality. However, it is only surfaced when there is a BINDWOOD_DEBUG key in the user's environment.

To post a comment you must log in.
Revision history for this message
Zachery Bir (urbanape) wrote : Posted in a previous version of this proposal

This branch adds copious debugging info to the error log, but only in the environmental presence of
$BINDWOOD_DEBUG. With this branch installed, and launching Firefox normally, you'll see precious
little info in the Error console (errors only, in fact).

If you launch Firefox from the CLI, setting BINDWOOD_DEBUG, thus:

  $ BINDWOOD_DEBUG=1 firefox &

And you look in the error console under Messages, you'll see all the gory details of pushing and pulling bookmarks, as well as event handling their creation and modification.

Revision history for this message
dobey (dobey) wrote : Posted in a previous version of this proposal

The code/logging looks ok, but I think the packaging should go in a separate branch... like a source package branch at ~ubuntuone-control-tower/ubuntu/karmic/bindwood/karmic (similar to what we have for ubuntuone-storage-protocol and ubuntuone-client currently).

review: Needs Fixing
Revision history for this message
dobey (dobey) wrote : Posted in a previous version of this proposal

Pull the packaging stuff, and then change the proposal's status to "Resubmit" to create a new proposal which supersedes this one. (Make sure it's the proposal status, and not a review requesting a resubmission, which people seem to get confused by.)

review: Needs Resubmitting
Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
John O'Brien (jdobrien) wrote :

Looks good.

As mentioned in irc you may want to fix that scheduled pullBookmarks delay

review: Approve
15. By Zachery Bir

Resetting the time interval back to 30s

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'content/sync.js'
--- content/sync.js 2009-08-18 14:17:30 +0000
+++ content/sync.js 2009-08-27 18:45:48 +0000
@@ -4,16 +4,17 @@
4 * This program is free software: you can redistribute it and/or modify it4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.6 * by the Free Software Foundation.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, but8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.11 * PURPOSE. See the GNU General Public License for more details.
12 * 12 *
13 * You should have received a copy of the GNU General Public License along13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */15 */
16/* Lots and lots of debugging information */16/* Lots and lots of debugging information */
17
17var Bindwood = {18var Bindwood = {
18 bookmarksService: Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]19 bookmarksService: Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
19 .getService(Components.interfaces.nsINavBookmarksService),20 .getService(Components.interfaces.nsINavBookmarksService),
@@ -27,16 +28,20 @@
27 .getService(Components.interfaces.nsINavHistoryService),28 .getService(Components.interfaces.nsINavHistoryService),
28 ioService: Components.classes["@mozilla.org/network/io-service;1"]29 ioService: Components.classes["@mozilla.org/network/io-service;1"]
29 .getService(Components.interfaces.nsIIOService),30 .getService(Components.interfaces.nsIIOService),
31 envService: Components.classes["@mozilla.org/process/environment;1"]
32 .getService(Components.interfaces.nsIEnvironment),
3033
31 annotationKey: "bindwood/uuid",34 annotationKey: "bindwood/uuid",
32 uuidItemIdMap: {},35 uuidItemIdMap: {},
3336
34 generateUUIDString: function() {37 generateUUIDString: function() {
35 return Bindwood.uuidService.generateUUID().toString();38 var uuid = Bindwood.uuidService.generateUUID().toString();
39 Bindwood.writeMessage("Generated UUID: " + uuid);
40 return uuid;
36 },41 },
3742
38 annotateItemWithUUID: function(itemId, uuid) {43 annotateItemWithUUID: function(itemId, seed_uuid) {
39 var uuid = uuid ? uuid : Bindwood.generateUUIDString();44 var uuid = seed_uuid ? seed_uuid : Bindwood.generateUUIDString();
40 Bindwood.annotationService.setItemAnnotation(itemId, Bindwood.annotationKey, uuid, 0, Bindwood.annotationService.EXPIRE_NEVER);45 Bindwood.annotationService.setItemAnnotation(itemId, Bindwood.annotationKey, uuid, 0, Bindwood.annotationService.EXPIRE_NEVER);
41 // Whenever we create a new UUID, stash it and the itemId in46 // Whenever we create a new UUID, stash it and the itemId in
42 // our local cache.47 // our local cache.
@@ -47,16 +52,24 @@
47 itemIdForUUID: function(uuid) {52 itemIdForUUID: function(uuid) {
48 // First, try to look it up in our local cache, barring that53 // First, try to look it up in our local cache, barring that
49 // (which shouldn't happen), look it up slowly.54 // (which shouldn't happen), look it up slowly.
55 Bindwood.writeMessage("Looking up itemId for uuid: " + uuid);
50 var itemId = Bindwood.uuidItemIdMap[uuid];56 var itemId = Bindwood.uuidItemIdMap[uuid];
5157
52 if (!itemId) {58 if (!itemId) {
59 Bindwood.writeMessage("Didn't find itemId, looking up annotations");
53 var items = Bindwood.annotationService.getItemsWithAnnotation(Bindwood.annotationKey, {});60 var items = Bindwood.annotationService.getItemsWithAnnotation(Bindwood.annotationKey, {});
61 var num_items = items.length;
62 Bindwood.writeMessage("Looking through " + num_items + "item" + (num_items != 1 ? "s" : ""));
54 for (var i = 0; i < items.length; i++) {63 for (var i = 0; i < items.length; i++) {
55 if (Bindwood.annotationService.getItemAnnotation(items[i], Bindwood.annotationKey) == uuid) {64 if (Bindwood.annotationService.getItemAnnotation(items[i], Bindwood.annotationKey) == uuid) {
56 Bindwood.uuidItemIdMap[uuid] = itemId = items[i];65 Bindwood.uuidItemIdMap[uuid] = itemId = items[i];
66 Bindwood.writeMessage("Found the matching itemId: " + itemId);
57 break;67 break;
58 }68 }
59 }69 }
70 if (!itemId) {
71 Bindwood.writeMessage("XXX: Still haven't found the right itemId!");
72 }
60 }73 }
61 return itemId;74 return itemId;
62 },75 },
@@ -64,13 +77,15 @@
64 uuidForItemId: function(itemId) {77 uuidForItemId: function(itemId) {
65 // Try to look up the uuid, and failing that, assign a new one78 // Try to look up the uuid, and failing that, assign a new one
66 // and return it.79 // and return it.
80 Bindwood.writeMessage("Looking up a UUID for itemId: " + itemId);
67 var uuid;81 var uuid;
68 var found = false;
69 try {82 try {
70 uuid = Bindwood.annotationService.getItemAnnotation(itemId, Bindwood.annotationKey);83 uuid = Bindwood.annotationService.getItemAnnotation(itemId, Bindwood.annotationKey);
71 found = true;84 Bindwood.writeMessage("Found it: " + uuid);
72 } catch(e) {85 } catch(e) {
86 Bindwood.writeError("Didn't find a UUID for itemId: " + itemId, e);
73 uuid = Bindwood.annotateItemWithUUID(itemId, null);87 uuid = Bindwood.annotateItemWithUUID(itemId, null);
88 Bindwood.writeMessage("Didn't find it, so made it: " + uuid);
74 }89 }
7590
76 return uuid;91 return uuid;
@@ -78,15 +93,16 @@
7893
79 writeMessage: function(aMessage) {94 writeMessage: function(aMessage) {
80 // convenience method for logging. Way better than alert()s.95 // convenience method for logging. Way better than alert()s.
81 Bindwood.consoleService.logStringMessage("Bindwood: " + aMessage);96 if (Bindwood.envService.exists('BINDWOOD_DEBUG')) {
97 Bindwood.consoleService.logStringMessage("Bindwood: " + aMessage);
98 }
82 },99 },
83100
84 writeError: function(aMessage, e) {101 writeError: function(aMessage, e) {
85 Bindwood.writeMessage(aMessage + "message: '" + e.message + "', reason: '" + e.reason + "', description: '" + e.description + "', error: '" + e.error + "'");102 // This should fire whether we're in DEBUG or not
103 Bindwood.consoleService.logStringMessage("Bindwood: " + aMessage + " message: '" + e.message + "', reason: '" + e.reason + "', description: '" + e.description + "', error: '" + e.error + "'");
86 },104 },
87105
88
89
90 init: function() {106 init: function() {
91 // Start the process and de-register ourself107 // Start the process and de-register ourself
92 // http://forums.mozillazine.org/viewtopic.php?f=19&t=657911&start=0108 // http://forums.mozillazine.org/viewtopic.php?f=19&t=657911&start=0
@@ -95,7 +111,7 @@
95 if(Components.classes["@mozilla.org/appshell/window-mediator;1"]111 if(Components.classes["@mozilla.org/appshell/window-mediator;1"]
96 .getService(Components.interfaces.nsIWindowMediator)112 .getService(Components.interfaces.nsIWindowMediator)
97 .getEnumerator("").getNext() == window) {113 .getEnumerator("").getNext() == window) {
98 Bindwood.writeMessage("Getting a Couch Port");114 Bindwood.writeMessage("First window opened. Getting a Couch Port");
99 Bindwood.getCouchPortNumber(Bindwood.startProcess);115 Bindwood.getCouchPortNumber(Bindwood.startProcess);
100 }116 }
101 },117 },
@@ -107,10 +123,12 @@
107123
108 // find OS temp dir to put the tempfile in124 // find OS temp dir to put the tempfile in
109 // https://developer.mozilla.org/index.php?title=File_I%2F%2FO#Getting_special_files125 // https://developer.mozilla.org/index.php?title=File_I%2F%2FO#Getting_special_files
126 Bindwood.writeMessage("Creating temp dir");
110 var tmpdir = Components.classes["@mozilla.org/file/directory_service;1"]127 var tmpdir = Components.classes["@mozilla.org/file/directory_service;1"]
111 .getService(Components.interfaces.nsIProperties)128 .getService(Components.interfaces.nsIProperties)
112 .get("TmpD", Components.interfaces.nsIFile);129 .get("TmpD", Components.interfaces.nsIFile);
113 // create a randomly named tempfile in the tempdir130 // create a randomly named tempfile in the tempdir
131 Bindwood.writeMessage("Creating temp file");
114 var tmpfile = Components.classes["@mozilla.org/file/local;1"]132 var tmpfile = Components.classes["@mozilla.org/file/local;1"]
115 .createInstance(Components.interfaces.nsILocalFile);133 .createInstance(Components.interfaces.nsILocalFile);
116 tmpfile.initWithPath(tmpdir.path + "/desktopcouch." + Math.random());134 tmpfile.initWithPath(tmpdir.path + "/desktopcouch." + Math.random());
@@ -130,13 +148,14 @@
130148
131 // create an nsIProcess2 to execute this bash script149 // create an nsIProcess2 to execute this bash script
132 var process = Components.classes["@mozilla.org/process/util;1"]150 var process = Components.classes["@mozilla.org/process/util;1"]
133 .createInstance(Components.interfaces.nsIProcess2);151 .createInstance(Components.interfaces.nsIProcess2);
134 process.init(nsifile);152 process.init(nsifile);
135153
136 // Run the process, passing the tmpfile path154 // Run the process, passing the tmpfile path
137 var args = [tmpfile.path];155 var args = [tmpfile.path];
156 Bindwood.writeMessage("Running dbus script");
138 process.runAsync(args, args.length, {157 process.runAsync(args, args.length, {
139 observe: function(process, finishState, data) {158 observe: function(process, finishState, unused_data) {
140 var port = 5984;159 var port = 5984;
141 if (finishState == "process-finished") {160 if (finishState == "process-finished") {
142 // read temp file to find port number161 // read temp file to find port number
@@ -151,7 +170,7 @@
151 let (str = {}) {170 let (str = {}) {
152 cstream.readString(-1, str); // read the whole file and put it in str.value171 cstream.readString(-1, str); // read the whole file and put it in str.value
153 data = str.value;172 data = str.value;
154 }173 };
155 cstream.close(); // this closes fstream174 cstream.close(); // this closes fstream
156 data = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');175 data = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
157 if (/^[0-9]+$/.test(data)) {176 if (/^[0-9]+$/.test(data)) {
@@ -173,11 +192,14 @@
173 Bindwood.writeMessage("Starting process with Couch on port " + couchPortNumber);192 Bindwood.writeMessage("Starting process with Couch on port " + couchPortNumber);
174 CouchDB.PORT_NUMBER = couchPortNumber;193 CouchDB.PORT_NUMBER = couchPortNumber;
175 try {194 try {
195 Bindwood.writeMessage("Pushing bookmarks");
176 Bindwood.pushBookmarks();196 Bindwood.pushBookmarks();
177 } catch(e) {197 } catch(e) {
178 Bindwood.writeError("Error when calling pushBookmarks: ", e);198 Bindwood.writeError("Error when calling pushBookmarks: ", e);
179 }199 }
200 Bindwood.writeMessage("Creating view, if it's not there already");
180 Bindwood.createView();201 Bindwood.createView();
202 Bindwood.writeMessage("Pulling initial bookmarks");
181 Bindwood.pullBookmarks();203 Bindwood.pullBookmarks();
182 },204 },
183205
@@ -231,7 +253,11 @@
231 try {253 try {
232 couch.createDb();254 couch.createDb();
233 } catch (e) {255 } catch (e) {
234 Bindwood.writeError("Error when creating database in pushBookmarks (file_exists is OK here): ", e);256 if (e.error == 'file_exists') {
257 Bindwood.writeMessage("Database already exists. We're okay.");
258 } else {
259 Bindwood.writeError("Error when creating database in pushBookmarks: ", e);
260 }
235 }261 }
236262
237 Bindwood.pushBookmarksFromList(Bindwood.bookmarksService.toolbarFolder,263 Bindwood.pushBookmarksFromList(Bindwood.bookmarksService.toolbarFolder,
@@ -252,10 +278,12 @@
252 var node = rootNode.getChild(i);278 var node = rootNode.getChild(i);
253 if (Bindwood.bookmarksService.getItemType(node.itemId) !=279 if (Bindwood.bookmarksService.getItemType(node.itemId) !=
254 Bindwood.bookmarksService.TYPE_BOOKMARK) {280 Bindwood.bookmarksService.TYPE_BOOKMARK) {
281 Bindwood.writeMessage("Got something that isn't a bookmark");
255 continue;282 continue;
256 }283 }
257284
258 var title = Bindwood.bookmarksService.getItemTitle(node.itemId);285 var title = Bindwood.bookmarksService.getItemTitle(node.itemId);
286 Bindwood.writeMessage("Got title: " + title);
259 try {287 try {
260 var metadata = Bindwood.bookmarksService.getBookmarkURI(node.itemId);288 var metadata = Bindwood.bookmarksService.getBookmarkURI(node.itemId);
261 } catch(e) {289 } catch(e) {
@@ -263,6 +291,7 @@
263 continue;291 continue;
264 }292 }
265 var uuid = Bindwood.uuidForItemId(node.itemId);293 var uuid = Bindwood.uuidForItemId(node.itemId);
294 Bindwood.writeMessage("Got uuid: " + uuid);
266 retval.push({295 retval.push({
267 title: title,296 title: title,
268 metadata: metadata,297 metadata: metadata,
@@ -274,13 +303,23 @@
274 },303 },
275304
276 pushBookmarksFromList: function(bookmarksList, bookmarksListName, db) {305 pushBookmarksFromList: function(bookmarksList, bookmarksListName, db) {
306 Bindwood.writeMessage("Pushing bookmarks from " + bookmarksListName);
277 var bookmarkData = Bindwood.getBookmarksFromList(bookmarksList);307 var bookmarkData = Bindwood.getBookmarksFromList(bookmarksList);
278 for (var i = 0; i < bookmarkData.length; i++) {308 for (var i = 0; i < bookmarkData.length; i++) {
279 // find this bookmark in CouchDB309 // find this bookmark in CouchDB
280 var uuid = Bindwood.uuidForItemId(bookmarkData[i].id);310 Bindwood.writeMessage("Looking up this record in Couch");
311 var itemId = bookmarkData[i].id;
312 var uuid = bookmarkData[i].uuid;
281 var uri = bookmarkData[i].metadata.spec;313 var uri = bookmarkData[i].metadata.spec;
282 var title = bookmarkData[i].title;314 var title = bookmarkData[i].title;
283315
316 Bindwood.writeMessage("Bookmark itemId: " + itemId +
317 " has uuid: " + uuid +
318 " uri: " + uri +
319 " and title: \"" + title + "\"");
320
321 Bindwood.uuidItemIdMap[uuid] = itemId;
322
284 var results = db.query(function(doc) {323 var results = db.query(function(doc) {
285 if (doc.application_annotations &&324 if (doc.application_annotations &&
286 doc.application_annotations.Firefox &&325 doc.application_annotations.Firefox &&
@@ -293,6 +332,7 @@
293332
294 if (results.rows.length === 0) {333 if (results.rows.length === 0) {
295 // this bookmark is not in CouchDB, so write it334 // this bookmark is not in CouchDB, so write it
335 Bindwood.writeMessage("No bookmark with uuid: " + uuid + ", so we're making one");
296 var record = {336 var record = {
297 record_type: "http://example.com/bookmark",337 record_type: "http://example.com/bookmark",
298 uri: uri,338 uri: uri,
@@ -306,10 +346,12 @@
306 };346 };
307 try {347 try {
308 db.save(record);348 db.save(record);
349 Bindwood.writeMessage("Saved new bookmark to Couch.");
309 } catch(e) {350 } catch(e) {
310 Bindwood.writeError("Problem saving bookmark to CouchDB; bookmark is " + JSON.stringify(record) + ": ", e);351 Bindwood.writeError("Problem saving bookmark to CouchDB; bookmark is " + JSON.stringify(record) + ": ", e);
311 }352 }
312 } else {353 } else {
354 Bindwood.writeMessage("This bookmark (" + uuid + ") is already in Couch, skipping");
313 // bookmark is already in CouchDB, so do nothing355 // bookmark is already in CouchDB, so do nothing
314 }356 }
315 }357 }
@@ -326,12 +368,14 @@
326 if (doc.record_type == "http://example.com/bookmark") {368 if (doc.record_type == "http://example.com/bookmark") {
327 emit(doc._id,doc);369 emit(doc._id,doc);
328 }370 }
329 });371 });
372 Bindwood.writeMessage("Pulled all bookmark records from Couch");
330 } catch(e) {373 } catch(e) {
331 Bindwood.writeError("Problem fetching all bookmarks from Couch: ", e);374 Bindwood.writeError("Problem fetching all bookmarks from Couch: ", e);
332 }375 }
333 for (var i = 0; i < rows.rows.length; i++) {376 for (var i = 0; i < rows.rows.length; i++) {
334 var recordid = rows.rows[i].id;377 var recordid = rows.rows[i].id;
378 Bindwood.writeMessage("Pulling record: " + recordid);
335 var bm = rows.rows[i].value;379 var bm = rows.rows[i].value;
336 if (bm.application_annotations &&380 if (bm.application_annotations &&
337 bm.application_annotations.Firefox &&381 bm.application_annotations.Firefox &&
@@ -339,7 +383,7 @@
339 // this bookmark has a uuid, so check its values haven't changed383 // this bookmark has a uuid, so check its values haven't changed
340 // find the bookmark with this uuid384 // find the bookmark with this uuid
341 var couch_uuid = bm.application_annotations.Firefox.uuid;385 var couch_uuid = bm.application_annotations.Firefox.uuid;
342 Bindwood.writeMessage("Row uuid: " + couch_uuid);386 Bindwood.writeMessage("This bookmark has a uuid: " + couch_uuid + " so we're looking it up locally");
343 var itemId = Bindwood.itemIdForUUID(couch_uuid);387 var itemId = Bindwood.itemIdForUUID(couch_uuid);
344 if (!itemId) {388 if (!itemId) {
345 // This bookmark has a uuid, but it's not one of ours.389 // This bookmark has a uuid, but it's not one of ours.
@@ -348,24 +392,32 @@
348 // make the uuids the same), or (b) it's a new one392 // make the uuids the same), or (b) it's a new one
349 // that happens to have been created on a different393 // that happens to have been created on a different
350 // machine.394 // machine.
395 Bindwood.writeMessage("We didn't find that uuid: " + couch_uuid + " in the ItemId map, so we'll need to search for it by URI");
351 try {396 try {
352 var uri = Bindwood.ioService.newURI(bm.uri, null, null);397 var uri = Bindwood.ioService.newURI(bm.uri, null, null);
398 Bindwood.writeMessage("Made a new URI from the bookmark's uri: " + bm.uri);
353 } catch(e) {399 } catch(e) {
354 Bindwood.writeError("Problem creating URI (" + bm.uri + ") for bookmark: ", e);400 Bindwood.writeError("Problem creating URI (" + bm.uri + ") for bookmark, skipping: ", e);
355 continue;401 continue;
356 }402 }
357 var ids = Bindwood.bookmarksService.getBookmarkIdsForURI(uri, {});403 var ids = Bindwood.bookmarksService.getBookmarkIdsForURI(uri, {});
358 if (ids.length > 1) {404 if (ids.length > 1) {
405 Bindwood.writeMessage("Wow, didn't expect that. Multiple (" + ids.length + ") bookmarks found locally for the same URI: " + bm.uri);
359 // punt for now, too many problems.406 // punt for now, too many problems.
360 } else if (ids.length) {407 } else if (ids.length) {
361 // Found one local bookmark. Replace its uuid to408 // Found one local bookmark. Replace its uuid to
362 // be the one from Couch.409 // be the one from Couch.
363 var itemId = ids[0];410 Bindwood.writeMessage("We found one bookmark with the URI from Couch, so we're going to stamp Couch's uuid on it");
411 itemId = ids[0];
364 var old_uuid = Bindwood.uuidForItemId(itemId);412 var old_uuid = Bindwood.uuidForItemId(itemId);
413 Bindwood.writeMessage("Got the old uuid: " + old_uuid);
365 delete Bindwood.uuidItemIdMap[old_uuid];414 delete Bindwood.uuidItemIdMap[old_uuid];
415 Bindwood.writeMessage("Deleted the old uuid from the ItemId map");
366 Bindwood.annotateItemWithUUID(itemId, couch_uuid);416 Bindwood.annotateItemWithUUID(itemId, couch_uuid);
417 Bindwood.writeMessage("Annotated the local bookmark with the uuid from Couch: " + couch_uuid);
367 } else {418 } else {
368 /// No local bookmarks419 /// No local bookmarks
420 Bindwood.writeMessage("No local bookmark found, must be a new entry in Couch. Creating locally.");
369 Bindwood.addLocalBookmark(bm, recordid, couch_uuid);421 Bindwood.addLocalBookmark(bm, recordid, couch_uuid);
370 }422 }
371 } else {423 } else {
@@ -374,17 +426,22 @@
374 // flagged for deletion by another Client. We426 // flagged for deletion by another Client. We
375 // want to respect that, and delete it427 // want to respect that, and delete it
376 // locally.428 // locally.
429 Bindwood.writeMessage("This bookmark exists on Couch, but has been flagged for deletion, so we're deleting it locally");
377 Bindwood.bookmarksService.removeItem(itemId);430 Bindwood.bookmarksService.removeItem(itemId);
431 Bindwood.writeMessage("Local copy deleted");
378 } else {432 } else {
379 var title = Bindwood.bookmarksService.getItemTitle(itemId);433 var title = Bindwood.bookmarksService.getItemTitle(itemId);
380 var metadata = Bindwood.bookmarksService.getBookmarkURI(itemId);434 var metadata = Bindwood.bookmarksService.getBookmarkURI(itemId);
381 if (title != bm.title) {435 if (title != bm.title) {
436 Bindwood.writeMessage("Resetting local title to title from Couch");
382 Bindwood.bookmarksService.setItemTitle(itemId, bm.title);437 Bindwood.bookmarksService.setItemTitle(itemId, bm.title);
383 }438 }
384 if (metadata.spec != bm.uri) {439 if (metadata.spec != bm.uri) {
440 Bindwood.writeMessage("The URI from Couch (" + bm.uri + ") is different from local (" + metadata.spec + ")");
385 try {441 try {
386 metadata = Bindwood.ioService.newURI(bm.uri, null, null);442 var new_uri = Bindwood.ioService.newURI(bm.uri, null, null);
387 Bindwood.bookmarksService.changeBookmarkURI(itemId, metadata);443 Bindowod.writeMessage("Creating a new URI for our local bookmark");
444 Bindwood.bookmarksService.changeBookmarkURI(itemId, new_uri);
388 } catch(e) {445 } catch(e) {
389 Bindwood.writeError("Problem creating a new URI for bookmark: ", e);446 Bindwood.writeError("Problem creating a new URI for bookmark: ", e);
390 }447 }
@@ -393,18 +450,20 @@
393 }450 }
394 } else {451 } else {
395 // This bookmark has no uuid, so create it from fresh452 // This bookmark has no uuid, so create it from fresh
396 // in Firefox. Passing in null to addLocalBookmar will453 // in Firefox. Passing in null to addLocalBookmark will
397 // generate a new uuid.454 // generate a new uuid.
455 Bindwood.writeMessage("This bookmark from Couch has no uuid (not sure how that happened - manual creation in Couch?), so we're creating it locally, and saving it back.");
398 Bindwood.addLocalBookmark(bm, recordid, null);456 Bindwood.addLocalBookmark(bm, recordid, null);
399 }457 }
400 }458 }
401 // reschedule ourself459 // reschedule ourself
402 setTimeout(Bindwood.pullBookmarks, 30000);460 Bindwood.writeMessage("Successful run, rescheduling ourself");
461 setTimeout(Bindwood.pullBookmarks, 300000);
403 },462 },
404463
405 addLocalBookmark: function(bm, recordid, uuid) {464 addLocalBookmark: function(bm, recordid, uuid) {
406 var couch = new CouchDB('bookmarks');465 var couch = new CouchDB('bookmarks');
407 var list;466 var list = Bindwood.bookmarksService.toolbarFolder;
408 if (bm.application_annotations &&467 if (bm.application_annotations &&
409 bm.application_annotations.Firefox &&468 bm.application_annotations.Firefox &&
410 bm.application_annotations.Firefox.list) {469 bm.application_annotations.Firefox.list) {
@@ -416,28 +475,39 @@
416 list = Bindwood.bookmarksService.bookmarksMenuFolder;475 list = Bindwood.bookmarksService.bookmarksMenuFolder;
417 break;476 break;
418 default:477 default:
419 list = Bindwood.bookmarksService.toolbarFolder;
420 break;478 break;
421 }479 }
422 } else {480 }
423 list = Bindwood.bookmarksService.toolbarFolder;481 Bindwood.writeMessage("Established the correct list for ths bookmark to reside");
424 }482
425 var metadata = Bindwood.ioService.newURI(bm.uri, null, null);483 try {
426484 var new_uri = Bindwood.ioService.newURI(bm.uri, null, null);
427 var itemId = Bindwood.bookmarksService.insertBookmark(list,485 Bindowod.writeMessage("Creating a new URI for our local bookmark");
428 metadata, -1, bm.title);486 Bindwood.bookmarksService.changeBookmarkURI(itemId, new_uri);
487 } catch(e) {
488 Bindwood.writeError("Problem creating a new URI for bookmark: ", e);
489 }
490
491 var itemId = Bindwood.bookmarksService.insertBookmark(
492 list, new_uri, -1, bm.title);
493 Bindwood.writeMessage("Inserting the new bookmark, locally");
429 // and then write the new uuid back to the record494 // and then write the new uuid back to the record
430 var uuid = uuid ? uuid : Bindwood.uuidForItemId(itemId);495 var new_uuid = uuid ? uuid : Bindwood.uuidForItemId(itemId);
496 Bindwood.writeMessage("Since it's a new bookmark, we have a new uuid: " + new_uuid);
431 var doc = couch.open(recordid);497 var doc = couch.open(recordid);
432 if (!doc.application_annotations) {498 if (!doc.application_annotations) {
499 Bindwood.writeMessage("Adding a new Applications annotation");
433 doc.application_annotations = {};500 doc.application_annotations = {};
434 }501 }
435 if (!doc.application_annotations.Firefox) {502 if (!doc.application_annotations.Firefox) {
503 Bindwood.writeMessage("Adding a new Firefox annotation");
436 doc.application_annotations.Firefox = {};504 doc.application_annotations.Firefox = {};
437 }505 }
438 doc.application_annotations.Firefox.uuid = uuid;506 Bindwood.writeMessage("Adding the new uuid to the Firefox annotation");
507 doc.application_annotations.Firefox.uuid = new_uuid;
439 try {508 try {
440 couch.save(doc);509 couch.save(doc);
510 Bindwood.writeMessage("Saved the doc back to Couch");
441 } catch(e) {511 } catch(e) {
442 Bindwood.writeError("Problem writing record for new bookmark: ",e);512 Bindwood.writeError("Problem writing record for new bookmark: ",e);
443 }513 }
@@ -448,13 +518,17 @@
448 onItemAdded: function(aItemId, aFolder, aIndex) {518 onItemAdded: function(aItemId, aFolder, aIndex) {
449 // A bookmark has been added, so we create a blank entry519 // A bookmark has been added, so we create a blank entry
450 // in Couch with our local itemId attached.520 // in Couch with our local itemId attached.
521 Bindwood.writeMessage("A new bookmark was created. Its id is: " + aItemId +
522 " at location: " + aIndex +
523 " in folder: " + aFolder );
451 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");524 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");
452525
453 var couch = new CouchDB('bookmarks');526 var couch = new CouchDB('bookmarks');
454527
455 var uuid = Bindwood.uuidForItemId(aItemId);528 var uuid = Bindwood.uuidForItemId(aItemId);
529 Bindwood.writeMessage("Determined uuid for new bookmark: " + uuid);
456530
457 var list;531 var list = "toolbarFolder";
458 switch(aFolder) {532 switch(aFolder) {
459 case Bindwood.bookmarksService.toolbarFolder:533 case Bindwood.bookmarksService.toolbarFolder:
460 list = "toolbarFolder";534 list = "toolbarFolder";
@@ -463,7 +537,6 @@
463 list = "bookmarksMenuFolder";537 list = "bookmarksMenuFolder";
464 break;538 break;
465 default:539 default:
466 list = "toolbarFolder";
467 break;540 break;
468 }541 }
469542
@@ -477,8 +550,11 @@
477 }550 }
478 };551 };
479552
553 Bindwood.writeMessage("Created a minimal record document with our uuid");
554
480 try {555 try {
481 var result = couch.save(doc);556 var result = couch.save(doc);
557 Bindwood.writeMessage("Saved new, bare record to Couch.");
482 } catch(e) {558 } catch(e) {
483 Bindwood.writeError("Problem saving new bookmark to Couch: ", e);559 Bindwood.writeError("Problem saving new bookmark to Couch: ", e);
484 }560 }
@@ -487,6 +563,7 @@
487 // A bookmark has been removed. This is called before it's563 // A bookmark has been removed. This is called before it's
488 // been removed locally, though we're passed the itemId,564 // been removed locally, though we're passed the itemId,
489 // which we use to delete from Couch.565 // which we use to delete from Couch.
566 Bindwood.writeMessage("Record " + aItemId + " is about to be removed locally.");
490 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");567 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");
491568
492 var couch = new CouchDB('bookmarks');569 var couch = new CouchDB('bookmarks');
@@ -515,25 +592,30 @@
515592
516 try {593 try {
517 var result = couch.save(doc);594 var result = couch.save(doc);
595 Bindwood.writeMessage("Saved document back to Couch with deleted flag set.");
518 // Also remove from our local cache and remove596 // Also remove from our local cache and remove
519 // annotation from service.597 // annotation from service.
520 delete Bindwood.uuidItemIdMap[uuid];598 delete Bindwood.uuidItemIdMap[uuid];
599 Bindwood.writeMessage("Deleted local reference in the uuid-itemId mapping.");
521 } catch(e) {600 } catch(e) {
522 Bindwood.writeError("Problem pushing deleted record to Couch: ", e);601 Bindwood.writeError("Problem pushing deleted record to Couch: ", e);
523 }602 }
524 },603 },
525 onItemRemoved: function(aItemId, aFolder, aIndex) {604 onItemRemoved: function(aItemId, aFolder, aIndex) {
526 Bindwood.annotationService.removeItemAnnotation(aItemId, Bindwood.annotationKey);605 Bindwood.annotationService.removeItemAnnotation(aItemId, Bindwood.annotationKey);
606 Bindwood.writeMessage("Removed annotations from bookmark identified by: " + aItemId);
527 },607 },
528 onItemChanged: function(aBookmarkId, aProperty, aIsAnnotationProperty, aValue) {608 onItemChanged: function(aBookmarkId, aProperty, aIsAnnotationProperty, aValue) {
529 // A property of a bookmark has changed. On multiple609 // A property of a bookmark has changed. On multiple
530 // property updates, this will be called multiple times,610 // property updates, this will be called multiple times,
531 // once per property (i.e., for title and URI)611 // once per property (i.e., for title and URI)
612 Bindwood.writeMessage("A property (" + aProperty + ") on bookmark id: " + aBookmarkId + " has been set to: " + aValue);
532 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");613 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead UniversalBrowserWrite");
533614
534 var couch = new CouchDB('bookmarks');615 var couch = new CouchDB('bookmarks');
535616
536 var uuid = Bindwood.uuidForItemId(aBookmarkId);617 var uuid = Bindwood.uuidForItemId(aBookmarkId);
618 Bindwood.writeMessage("Determined uuid for this bookmark: " + aBookmarkId + " is: " + uuid);
537619
538 var results = couch.query(function(doc) {620 var results = couch.query(function(doc) {
539 if (doc.application_annotations &&621 if (doc.application_annotations &&
@@ -552,9 +634,11 @@
552634
553 var doc = couch.open(results.rows[0].id);635 var doc = couch.open(results.rows[0].id);
554 doc[aProperty.toString()] = aValue.toString();636 doc[aProperty.toString()] = aValue.toString();
637 Bindwood.writeMessage("Set the new property on the document from Couch.");
555638
556 try {639 try {
557 var result = couch.save(doc);640 var result = couch.save(doc);
641 Bindwood.writeMessage("Saved the document back to Couch");
558 } catch(e) {642 } catch(e) {
559 Bindwood.writeError("Problem saving updated bookmark to Couch: ", e);643 Bindwood.writeError("Problem saving updated bookmark to Couch: ", e);
560 }644 }

Subscribers

People subscribed via source and target branches