Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | "stability": "experimental", |
michael@0 | 9 | "engines": { |
michael@0 | 10 | "Firefox": "*" |
michael@0 | 11 | } |
michael@0 | 12 | }; |
michael@0 | 13 | |
michael@0 | 14 | const { Cc, Ci } = require('chrome'); |
michael@0 | 15 | const browserHistory = Cc["@mozilla.org/browser/nav-history-service;1"]. |
michael@0 | 16 | getService(Ci.nsIBrowserHistory); |
michael@0 | 17 | const asyncHistory = Cc["@mozilla.org/browser/history;1"]. |
michael@0 | 18 | getService(Ci.mozIAsyncHistory); |
michael@0 | 19 | const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. |
michael@0 | 20 | getService(Ci.nsINavBookmarksService); |
michael@0 | 21 | const taggingService = Cc["@mozilla.org/browser/tagging-service;1"]. |
michael@0 | 22 | getService(Ci.nsITaggingService); |
michael@0 | 23 | const ios = Cc['@mozilla.org/network/io-service;1']. |
michael@0 | 24 | getService(Ci.nsIIOService); |
michael@0 | 25 | const { query } = require('./host-query'); |
michael@0 | 26 | const { |
michael@0 | 27 | defer, all, resolve, promised, reject |
michael@0 | 28 | } = require('../../core/promise'); |
michael@0 | 29 | const { request, response } = require('../../addon/host'); |
michael@0 | 30 | const { send } = require('../../addon/events'); |
michael@0 | 31 | const { on, emit } = require('../../event/core'); |
michael@0 | 32 | const { filter } = require('../../event/utils'); |
michael@0 | 33 | const { URL, isValidURI } = require('../../url'); |
michael@0 | 34 | const { newURI } = require('../../url/utils'); |
michael@0 | 35 | |
michael@0 | 36 | const DEFAULT_INDEX = bmsrv.DEFAULT_INDEX; |
michael@0 | 37 | const UNSORTED_ID = bmsrv.unfiledBookmarksFolder; |
michael@0 | 38 | const ROOT_FOLDERS = [ |
michael@0 | 39 | bmsrv.unfiledBookmarksFolder, bmsrv.toolbarFolder, |
michael@0 | 40 | bmsrv.tagsFolder, bmsrv.bookmarksMenuFolder |
michael@0 | 41 | ]; |
michael@0 | 42 | |
michael@0 | 43 | const EVENT_MAP = { |
michael@0 | 44 | 'sdk-places-bookmarks-create': createBookmarkItem, |
michael@0 | 45 | 'sdk-places-bookmarks-save': saveBookmarkItem, |
michael@0 | 46 | 'sdk-places-bookmarks-last-updated': getBookmarkLastUpdated, |
michael@0 | 47 | 'sdk-places-bookmarks-get': getBookmarkItem, |
michael@0 | 48 | 'sdk-places-bookmarks-remove': removeBookmarkItem, |
michael@0 | 49 | 'sdk-places-bookmarks-get-all': getAllBookmarks, |
michael@0 | 50 | 'sdk-places-bookmarks-get-children': getChildren |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | function typeMap (type) { |
michael@0 | 54 | if (typeof type === 'number') { |
michael@0 | 55 | if (bmsrv.TYPE_BOOKMARK === type) return 'bookmark'; |
michael@0 | 56 | if (bmsrv.TYPE_FOLDER === type) return 'group'; |
michael@0 | 57 | if (bmsrv.TYPE_SEPARATOR === type) return 'separator'; |
michael@0 | 58 | } else { |
michael@0 | 59 | if ('bookmark' === type) return bmsrv.TYPE_BOOKMARK; |
michael@0 | 60 | if ('group' === type) return bmsrv.TYPE_FOLDER; |
michael@0 | 61 | if ('separator' === type) return bmsrv.TYPE_SEPARATOR; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | function getBookmarkLastUpdated ({id}) |
michael@0 | 66 | resolve(bmsrv.getItemLastModified(id)) |
michael@0 | 67 | exports.getBookmarkLastUpdated; |
michael@0 | 68 | |
michael@0 | 69 | function createBookmarkItem (data) { |
michael@0 | 70 | let error; |
michael@0 | 71 | |
michael@0 | 72 | if (data.group == null) data.group = UNSORTED_ID; |
michael@0 | 73 | if (data.index == null) data.index = DEFAULT_INDEX; |
michael@0 | 74 | |
michael@0 | 75 | if (data.type === 'group') |
michael@0 | 76 | data.id = bmsrv.createFolder( |
michael@0 | 77 | data.group, data.title, data.index |
michael@0 | 78 | ); |
michael@0 | 79 | else if (data.type === 'separator') |
michael@0 | 80 | data.id = bmsrv.insertSeparator( |
michael@0 | 81 | data.group, data.index |
michael@0 | 82 | ); |
michael@0 | 83 | else |
michael@0 | 84 | data.id = bmsrv.insertBookmark( |
michael@0 | 85 | data.group, newURI(data.url), data.index, data.title |
michael@0 | 86 | ); |
michael@0 | 87 | |
michael@0 | 88 | // In the event where default or no index is provided (-1), |
michael@0 | 89 | // query the actual index for the response |
michael@0 | 90 | if (data.index === -1) |
michael@0 | 91 | data.index = bmsrv.getItemIndex(data.id); |
michael@0 | 92 | |
michael@0 | 93 | data.updated = bmsrv.getItemLastModified(data.id); |
michael@0 | 94 | |
michael@0 | 95 | return tag(data, true).then(() => data); |
michael@0 | 96 | } |
michael@0 | 97 | exports.createBookmarkItem = createBookmarkItem; |
michael@0 | 98 | |
michael@0 | 99 | function saveBookmarkItem (data) { |
michael@0 | 100 | let id = data.id; |
michael@0 | 101 | if (!id) |
michael@0 | 102 | reject('Item is missing id'); |
michael@0 | 103 | |
michael@0 | 104 | let group = bmsrv.getFolderIdForItem(id); |
michael@0 | 105 | let index = bmsrv.getItemIndex(id); |
michael@0 | 106 | let type = bmsrv.getItemType(id); |
michael@0 | 107 | let title = typeMap(type) !== 'separator' ? |
michael@0 | 108 | bmsrv.getItemTitle(id) : |
michael@0 | 109 | undefined; |
michael@0 | 110 | let url = typeMap(type) === 'bookmark' ? |
michael@0 | 111 | bmsrv.getBookmarkURI(id).spec : |
michael@0 | 112 | undefined; |
michael@0 | 113 | |
michael@0 | 114 | if (url != data.url) |
michael@0 | 115 | bmsrv.changeBookmarkURI(id, newURI(data.url)); |
michael@0 | 116 | else if (typeMap(type) === 'bookmark') |
michael@0 | 117 | data.url = url; |
michael@0 | 118 | |
michael@0 | 119 | if (title != data.title) |
michael@0 | 120 | bmsrv.setItemTitle(id, data.title); |
michael@0 | 121 | else if (typeMap(type) !== 'separator') |
michael@0 | 122 | data.title = title; |
michael@0 | 123 | |
michael@0 | 124 | if (data.group && data.group !== group) |
michael@0 | 125 | bmsrv.moveItem(id, data.group, data.index || -1); |
michael@0 | 126 | else if (data.index != null && data.index !== index) { |
michael@0 | 127 | // We use moveItem here instead of setItemIndex |
michael@0 | 128 | // so we don't have to manage the indicies of the siblings |
michael@0 | 129 | bmsrv.moveItem(id, group, data.index); |
michael@0 | 130 | } else if (data.index == null) |
michael@0 | 131 | data.index = index; |
michael@0 | 132 | |
michael@0 | 133 | data.updated = bmsrv.getItemLastModified(data.id); |
michael@0 | 134 | |
michael@0 | 135 | return tag(data).then(() => data); |
michael@0 | 136 | } |
michael@0 | 137 | exports.saveBookmarkItem = saveBookmarkItem; |
michael@0 | 138 | |
michael@0 | 139 | function removeBookmarkItem (data) { |
michael@0 | 140 | let id = data.id; |
michael@0 | 141 | |
michael@0 | 142 | if (!id) |
michael@0 | 143 | reject('Item is missing id'); |
michael@0 | 144 | |
michael@0 | 145 | bmsrv.removeItem(id); |
michael@0 | 146 | return resolve(null); |
michael@0 | 147 | } |
michael@0 | 148 | exports.removeBookmarkItem = removeBookmarkItem; |
michael@0 | 149 | |
michael@0 | 150 | function getBookmarkItem (data) { |
michael@0 | 151 | let id = data.id; |
michael@0 | 152 | |
michael@0 | 153 | if (!id) |
michael@0 | 154 | reject('Item is missing id'); |
michael@0 | 155 | |
michael@0 | 156 | let type = bmsrv.getItemType(id); |
michael@0 | 157 | |
michael@0 | 158 | data.type = typeMap(type); |
michael@0 | 159 | |
michael@0 | 160 | if (type === bmsrv.TYPE_BOOKMARK || type === bmsrv.TYPE_FOLDER) |
michael@0 | 161 | data.title = bmsrv.getItemTitle(id); |
michael@0 | 162 | |
michael@0 | 163 | if (type === bmsrv.TYPE_BOOKMARK) { |
michael@0 | 164 | data.url = bmsrv.getBookmarkURI(id).spec; |
michael@0 | 165 | // Should be moved into host-tags as a method |
michael@0 | 166 | data.tags = taggingService.getTagsForURI(newURI(data.url), {}); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | data.group = bmsrv.getFolderIdForItem(id); |
michael@0 | 170 | data.index = bmsrv.getItemIndex(id); |
michael@0 | 171 | data.updated = bmsrv.getItemLastModified(data.id); |
michael@0 | 172 | |
michael@0 | 173 | return resolve(data); |
michael@0 | 174 | } |
michael@0 | 175 | exports.getBookmarkItem = getBookmarkItem; |
michael@0 | 176 | |
michael@0 | 177 | function getAllBookmarks () { |
michael@0 | 178 | return query({}, { queryType: 1 }).then(bookmarks => |
michael@0 | 179 | all(bookmarks.map(getBookmarkItem))); |
michael@0 | 180 | } |
michael@0 | 181 | exports.getAllBookmarks = getAllBookmarks; |
michael@0 | 182 | |
michael@0 | 183 | function getChildren ({ id }) { |
michael@0 | 184 | if (typeMap(bmsrv.getItemType(id)) !== 'group') return []; |
michael@0 | 185 | let ids = []; |
michael@0 | 186 | for (let i = 0; ids[ids.length - 1] !== -1; i++) |
michael@0 | 187 | ids.push(bmsrv.getIdForItemAt(id, i)); |
michael@0 | 188 | ids.pop(); |
michael@0 | 189 | return all(ids.map(id => getBookmarkItem({ id: id }))); |
michael@0 | 190 | } |
michael@0 | 191 | exports.getChildren = getChildren; |
michael@0 | 192 | |
michael@0 | 193 | /* |
michael@0 | 194 | * Hook into host |
michael@0 | 195 | */ |
michael@0 | 196 | |
michael@0 | 197 | let reqStream = filter(request, function (data) /sdk-places-bookmarks/.test(data.event)); |
michael@0 | 198 | on(reqStream, 'data', function ({event, id, data}) { |
michael@0 | 199 | if (!EVENT_MAP[event]) return; |
michael@0 | 200 | |
michael@0 | 201 | let resData = { |
michael@0 | 202 | id: id, |
michael@0 | 203 | event: event |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | promised(EVENT_MAP[event])(data).then(res => { |
michael@0 | 207 | resData.data = res; |
michael@0 | 208 | respond(resData); |
michael@0 | 209 | }, reason => { |
michael@0 | 210 | resData.error = reason; |
michael@0 | 211 | respond(resData); |
michael@0 | 212 | }); |
michael@0 | 213 | }); |
michael@0 | 214 | |
michael@0 | 215 | function respond (data) { |
michael@0 | 216 | emit(response, 'data', data); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | function tag (data, isNew) { |
michael@0 | 220 | // If a new item, we can skip checking what other tags |
michael@0 | 221 | // are on the item |
michael@0 | 222 | if (data.type !== 'bookmark') { |
michael@0 | 223 | return resolve(); |
michael@0 | 224 | } else if (!isNew) { |
michael@0 | 225 | return send('sdk-places-tags-get-tags-by-url', { url: data.url }) |
michael@0 | 226 | .then(tags => { |
michael@0 | 227 | return send('sdk-places-tags-untag', { |
michael@0 | 228 | tags: tags.filter(tag => !~data.tags.indexOf(tag)), |
michael@0 | 229 | url: data.url |
michael@0 | 230 | }); |
michael@0 | 231 | }).then(() => send('sdk-places-tags-tag', { |
michael@0 | 232 | url: data.url, tags: data.tags |
michael@0 | 233 | })); |
michael@0 | 234 | } |
michael@0 | 235 | else if (data.tags && data.tags.length) { |
michael@0 | 236 | return send('sdk-places-tags-tag', { url: data.url, tags: data.tags }); |
michael@0 | 237 | } |
michael@0 | 238 | else |
michael@0 | 239 | return resolve(); |
michael@0 | 240 | } |
michael@0 | 241 |