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