-
Notifications
You must be signed in to change notification settings - Fork 27
/
Bookmarks.swift
494 lines (425 loc) · 18.3 KB
/
Bookmarks.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import UIKit
import Shared
import Deferred
import SwiftyJSON
private let log = Logger.syncLogger
public protocol SearchableBookmarks: class {
func bookmarksByURL(_ url: URL) -> Deferred<Maybe<Cursor<BookmarkItem>>>
}
public protocol SyncableBookmarks: class, ResettableSyncStorage, AccountRemovalDelegate {
// TODO
func isUnchanged() -> Deferred<Maybe<Bool>>
func getLocalDeletions() -> Deferred<Maybe<[(GUID, Timestamp)]>>
func treesForEdges() -> Deferred<Maybe<(local: BookmarkTree, buffer: BookmarkTree)>>
func treeForMirror() -> Deferred<Maybe<BookmarkTree>>
func applyLocalOverrideCompletionOp(_ op: LocalOverrideCompletionOp, itemSources: ItemSources) -> Success
}
public let NotificationBookmarkBufferValidated = "NotificationBookmarkBufferValidated"
public protocol BookmarkBufferStorage: class {
func isEmpty() -> Deferred<Maybe<Bool>>
func applyRecords(_ records: [BookmarkMirrorItem]) -> Success
func doneApplyingRecordsAfterDownload() -> Success
func validate() -> Success
func getBufferedDeletions() -> Deferred<Maybe<[(GUID, Timestamp)]>>
func applyBufferCompletionOp(_ op: BufferCompletionOp, itemSources: ItemSources) -> Success
// Only use for diagnostics.
func synchronousBufferCount() -> Int?
}
public protocol MirrorItemSource: class {
func getMirrorItemWithGUID(_ guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>>
func getMirrorItemsWithGUIDs<T: Collection>(_ guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> where T.Iterator.Element == GUID
func prefetchMirrorItemsWithGUIDs<T: Collection>(_ guids: T) -> Success where T.Iterator.Element == GUID
}
public protocol BufferItemSource: class {
func getBufferItemWithGUID(_ guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>>
func getBufferItemsWithGUIDs<T: Collection>(_ guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> where T.Iterator.Element == GUID
func prefetchBufferItemsWithGUIDs<T: Collection>(_ guids: T) -> Success where T.Iterator.Element == GUID
}
public protocol LocalItemSource: class {
func getLocalItemWithGUID(_ guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>>
func getLocalItemsWithGUIDs<T: Collection>(_ guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> where T.Iterator.Element == GUID
func prefetchLocalItemsWithGUIDs<T: Collection>(_ guids: T) -> Success where T.Iterator.Element == GUID
}
open class ItemSources {
open let local: LocalItemSource
open let mirror: MirrorItemSource
open let buffer: BufferItemSource
public init(local: LocalItemSource, mirror: MirrorItemSource, buffer: BufferItemSource) {
self.local = local
self.mirror = mirror
self.buffer = buffer
}
open func prefetchWithGUIDs<T: Collection>(_ guids: T) -> Success where T.Iterator.Element == GUID {
return (self.local.prefetchLocalItemsWithGUIDs(guids)
>>> { self.mirror.prefetchMirrorItemsWithGUIDs(guids) })
>>> { self.buffer.prefetchBufferItemsWithGUIDs(guids) }
}
}
public struct BookmarkRoots {
// These match Places on desktop.
public static let RootGUID = "root________"
public static let MobileFolderGUID = "mobile______"
public static let MenuFolderGUID = "menu________"
public static let ToolbarFolderGUID = "toolbar_____"
public static let UnfiledFolderGUID = "unfiled_____"
public static let FakeDesktopFolderGUID = "desktop_____" // Pseudo. Never mentioned in a real record.
// This is the order we use.
public static let RootChildren: [GUID] = [
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
BookmarkRoots.MobileFolderGUID,
]
public static let DesktopRoots: [GUID] = [
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
]
public static let Real = Set<GUID>([
BookmarkRoots.RootGUID,
BookmarkRoots.MobileFolderGUID,
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
])
public static let All = Set<GUID>([
BookmarkRoots.RootGUID,
BookmarkRoots.MobileFolderGUID,
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
BookmarkRoots.FakeDesktopFolderGUID,
])
/**
* Sync records are a horrible mess of Places-native GUIDs and Sync-native IDs.
* For example:
* {"id":"places",
* "type":"folder",
* "title":"",
* "description":null,
* "children":["menu________","toolbar_____",
* "tags________","unfiled_____",
* "jKnyPDrBQSDg","T6XK5oJMU8ih"],
* "parentid":"2hYxKgBwvkEH"}"
*
* We thus normalize on the extended Places IDs (with underscores) for
* local storage, and translate to the Sync IDs when creating an outbound
* record.
* We translate the record's ID and also its parent. Evidence suggests that
* we don't need to translate children IDs.
*
* TODO: We don't create outbound records yet, so that's why there's no
* translation in that direction yet!
*/
public static func translateIncomingRootGUID(_ guid: GUID) -> GUID {
return [
"places": RootGUID,
"root": RootGUID,
"mobile": MobileFolderGUID,
"menu": MenuFolderGUID,
"toolbar": ToolbarFolderGUID,
"unfiled": UnfiledFolderGUID
][guid] ?? guid
}
public static func translateOutgoingRootGUID(_ guid: GUID) -> GUID {
return [
RootGUID: "places",
MobileFolderGUID: "mobile",
MenuFolderGUID: "menu",
ToolbarFolderGUID: "toolbar",
UnfiledFolderGUID: "unfiled"
][guid] ?? guid
}
/*
public static let TagsFolderGUID = "tags________"
public static let PinnedFolderGUID = "pinned______"
*/
static let RootID = 0
static let MobileID = 1
static let MenuID = 2
static let ToolbarID = 3
static let UnfiledID = 4
}
/**
* This partly matches Places's nsINavBookmarksService, just for sanity.
*
* It is further extended to support the types that exist in Sync, so we can use
* this to store mirrored rows.
*
* These are only used at the DB layer.
*/
public enum BookmarkNodeType: Int {
case bookmark = 1
case folder = 2
case separator = 3
case dynamicContainer = 4
case livemark = 5
case query = 6
// No microsummary: those turn into bookmarks.
}
public func == (lhs: BookmarkMirrorItem, rhs: BookmarkMirrorItem) -> Bool {
if lhs.type != rhs.type ||
lhs.guid != rhs.guid ||
lhs.serverModified != rhs.serverModified ||
lhs.isDeleted != rhs.isDeleted ||
lhs.hasDupe != rhs.hasDupe ||
lhs.pos != rhs.pos ||
lhs.faviconID != rhs.faviconID ||
lhs.localModified != rhs.localModified ||
lhs.parentID != rhs.parentID ||
lhs.parentName != rhs.parentName ||
lhs.feedURI != rhs.feedURI ||
lhs.siteURI != rhs.siteURI ||
lhs.title != rhs.title ||
lhs.description != rhs.description ||
lhs.bookmarkURI != rhs.bookmarkURI ||
lhs.tags != rhs.tags ||
lhs.keyword != rhs.keyword ||
lhs.folderName != rhs.folderName ||
lhs.queryID != rhs.queryID {
return false
}
if let lhsChildren = lhs.children, let rhsChildren = rhs.children {
return lhsChildren == rhsChildren
}
return lhs.children == nil && rhs.children == nil
}
public struct BookmarkMirrorItem: Equatable {
public let guid: GUID
public let type: BookmarkNodeType
public var serverModified: Timestamp
public let isDeleted: Bool
public let hasDupe: Bool
public let parentID: GUID?
public let parentName: String?
// Livemarks.
public let feedURI: String?
public let siteURI: String?
// Separators.
let pos: Int?
// Folders, livemarks, bookmarks and queries.
public let title: String?
let description: String?
// Bookmarks and queries.
let bookmarkURI: String?
let tags: String?
let keyword: String?
// Queries.
let folderName: String?
let queryID: String?
// Folders.
public let children: [GUID]?
// Internal stuff.
let faviconID: Int?
public let localModified: Timestamp?
let syncStatus: SyncStatus?
public func copyWithParentID(_ parentID: GUID, parentName: String?) -> BookmarkMirrorItem {
return BookmarkMirrorItem(
guid: self.guid,
type: self.type,
serverModified: self.serverModified,
isDeleted: self.isDeleted,
hasDupe: self.hasDupe,
parentID: parentID,
parentName: parentName,
feedURI: self.feedURI,
siteURI: self.siteURI,
pos: self.pos,
title: self.title,
description: self.description,
bookmarkURI: self.bookmarkURI,
tags: self.tags,
keyword: self.keyword,
folderName: self.folderName,
queryID: self.queryID,
children: self.children,
faviconID: self.faviconID,
localModified: self.localModified,
syncStatus: self.syncStatus)
}
// Ignores internal metadata and GUID; a pure value comparison.
// Does compare child GUIDs!
public func sameAs(_ rhs: BookmarkMirrorItem) -> Bool {
if self.type != rhs.type ||
self.isDeleted != rhs.isDeleted ||
self.pos != rhs.pos ||
self.parentID != rhs.parentID ||
self.parentName != rhs.parentName ||
self.feedURI != rhs.feedURI ||
self.siteURI != rhs.siteURI ||
self.title != rhs.title ||
(self.description ?? "") != (rhs.description ?? "") ||
self.bookmarkURI != rhs.bookmarkURI ||
self.tags != rhs.tags ||
self.keyword != rhs.keyword ||
self.folderName != rhs.folderName ||
self.queryID != rhs.queryID {
return false
}
if let lhsChildren = self.children, let rhsChildren = rhs.children {
return lhsChildren == rhsChildren
}
return self.children == nil && rhs.children == nil
}
public func asJSON() -> JSON {
return self.asJSONWithChildren(self.children)
}
public func asJSONWithChildren(_ children: [GUID]?) -> JSON {
var out: [String: AnyObject] = [:]
out["id"] = BookmarkRoots.translateOutgoingRootGUID(self.guid) as AnyObject
func take(_ key: String, _ val: String?) {
guard let val = val else {
return
}
out[key] = val as AnyObject?
}
if self.isDeleted {
out["deleted"] = true as AnyObject?
return JSON(out as AnyObject)
}
out["hasDupe"] = self.hasDupe as AnyObject?
// TODO: this should never be nil!
if let parentID = self.parentID {
out["parentid"] = BookmarkRoots.translateOutgoingRootGUID(parentID) as AnyObject?
take("parentName", titleForSpecialGUID(parentID) ?? self.parentName)
}
func takeBookmarkFields() {
take("title", self.title)
take("bmkUri", self.bookmarkURI)
take("description", self.description)
if let tags = self.tags {
let tagsJSON = JSON.parse(tags)
if let tagsArray = tagsJSON.array, tagsArray.every({ $0.type == SwiftyJSON.Type.string }) {
out["tags"] = tagsArray as AnyObject?
} else {
out["tags"] = [] as AnyObject
}
} else {
out["tags"] = [] as AnyObject
}
take("keyword", self.keyword)
}
func takeFolderFields() {
take("title", titleForSpecialGUID(self.guid) ?? self.title)
take("description", self.description)
if let children = children {
if BookmarkRoots.RootGUID == self.guid {
// Only the root contains roots, and so only its children
// need to be translated.
out["children"] = children.map(BookmarkRoots.translateOutgoingRootGUID) as AnyObject
} else {
out["children"] = children as AnyObject?
}
}
}
switch self.type {
case .query:
out["type"] = "query" as AnyObject?
take("folderName", self.folderName)
take("queryId", self.queryID)
takeBookmarkFields()
case .bookmark:
out["type"] = "bookmark" as AnyObject?
takeBookmarkFields()
case .livemark:
out["type"] = "livemark" as AnyObject?
take("siteUri", self.siteURI)
take("feedUri", self.feedURI)
takeFolderFields()
case .folder:
out["type"] = "folder" as AnyObject?
takeFolderFields()
case .separator:
out["type"] = "separator" as AnyObject?
if let pos = self.pos {
out["pos"] = pos as AnyObject?
}
case .dynamicContainer:
// Sigh.
preconditionFailure("DynamicContainer not supported.")
}
return JSON(out as AnyObject)
}
// The places root is a folder but has no parentName.
public static func folder(_ guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String?, title: String, description: String?, children: [GUID]) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .folder, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: children,
faviconID: nil, localModified: nil, syncStatus: nil)
}
public static func livemark(_ guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String?, title: String?, description: String?, feedURI: String, siteURI: String) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .livemark, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: feedURI, siteURI: siteURI,
pos: nil,
title: title, description: description,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil,
faviconID: nil, localModified: nil, syncStatus: nil)
}
public static func separator(_ guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, pos: Int) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .separator, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: pos,
title: nil, description: nil,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil,
faviconID: nil, localModified: nil, syncStatus: nil)
}
public static func bookmark(_ guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, title: String, description: String?, URI: String, tags: String, keyword: String?) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .bookmark, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: URI, tags: tags, keyword: keyword,
folderName: nil, queryID: nil,
children: nil,
faviconID: nil, localModified: nil, syncStatus: nil)
}
public static func query(_ guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, title: String, description: String?, URI: String, tags: String, keyword: String?, folderName: String?, queryID: String?) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .query, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: URI, tags: tags, keyword: keyword,
folderName: folderName, queryID: queryID,
children: nil,
faviconID: nil, localModified: nil, syncStatus: nil)
}
public static func deleted(_ type: BookmarkNodeType, guid: GUID, modified: Timestamp) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
return BookmarkMirrorItem(guid: id, type: type, serverModified: modified,
isDeleted: true, hasDupe: false, parentID: nil, parentName: nil,
feedURI: nil, siteURI: nil,
pos: nil,
title: nil, description: nil,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil,
faviconID: nil, localModified: nil, syncStatus: nil)
}
}