-
Notifications
You must be signed in to change notification settings - Fork 221
/
Places.swift
815 lines (757 loc) · 35.2 KB
/
Places.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* 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 os.log
internal typealias UniffiPlacesApi = PlacesApi
internal typealias UniffiPlacesConnection = PlacesConnection
/**
* This is specifically for throwing when there is
* API misuse and/or connection issues with PlacesReadConnection
*/
public enum PlacesConnectionError: Error {
case connUseAfterApiClosed
}
/**
* This is something like a places connection manager. It primarialy exists to
* ensure that only a single write connection is active at once.
*
* If it helps, you can think of this as something like a connection pool
* (although it does not actually perform any pooling).
*/
public class PlacesAPI {
private let writeConn: PlacesWriteConnection
private let api: UniffiPlacesApi
private let queue = DispatchQueue(label: "com.mozilla.places.api")
/**
* Initialize a PlacesAPI
*
* - Parameter path: an absolute path to a file that will be used for the internal database.
*
* - Throws: `PlacesApiError` if initializing the database failed.
*/
public init(path: String) throws {
try api = placesApiNew(dbPath: path)
let uniffiConn = try api.newConnection(connType: ConnectionType.readWrite)
writeConn = try PlacesWriteConnection(conn: uniffiConn)
writeConn.api = self
}
/**
* Open a new reader connection.
*
* - Throws: `PlacesApiError` if a connection could not be opened.
*/
open func openReader() throws -> PlacesReadConnection {
return try queue.sync {
let uniffiConn = try api.newConnection(connType: ConnectionType.readOnly)
return try PlacesReadConnection(conn: uniffiConn, api: self)
}
}
/**
* Get the writer connection.
*
* - Note: There is only ever a single writer connection,
* and it's opened when the database is constructed,
* so this function does not throw
*/
open func getWriter() -> PlacesWriteConnection {
return queue.sync {
self.writeConn
}
}
open func registerWithSyncManager() {
queue.sync {
self.api.registerWithSyncManager()
}
}
}
/**
* A read-only connection to the places database.
*/
public class PlacesReadConnection {
fileprivate let queue = DispatchQueue(label: "com.mozilla.places.conn")
fileprivate var conn: UniffiPlacesConnection
fileprivate weak var api: PlacesAPI?
private let interruptHandle: SqlInterruptHandle
fileprivate init(conn: UniffiPlacesConnection, api: PlacesAPI? = nil) throws {
self.conn = conn
self.api = api
interruptHandle = self.conn.newInterruptHandle()
}
// Note: caller synchronizes!
fileprivate func checkApi() throws {
if api == nil {
throw PlacesConnectionError.connUseAfterApiClosed
}
}
/**
* Returns the bookmark subtree rooted at `rootGUID`.
*
* This differs from `getBookmark` in that it populates folder children
* recursively (specifically, any `BookmarkFolder`s in the returned value
* will have their `children` list populated, and not just `childGUIDs`.
*
* However, if `recursive: false` is passed, only a single level of child
* nodes are returned for folders.
*
* - Parameter rootGUID: the GUID where to start the tree.
*
* - Parameter recursive: Whether or not to return more than a single
* level of children for folders. If false, then
* any folders which are children of the requested
* node will *only* have their `childGUIDs`
* populated, and *not* their `children`.
*
* - Returns: The bookmarks tree starting from `rootGUID`, or null if the
* provided guid didn't refer to a known bookmark item.
* - Throws:
* - `PlacesApiError.databaseCorrupt`: If corruption is encountered when fetching
* the tree
* - `PlacesApiError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func getBookmarksTree(rootGUID: Guid, recursive: Bool) throws -> BookmarkNodeData? {
return try queue.sync {
try self.checkApi()
if recursive {
return try self.conn.bookmarksGetTree(itemGuid: rootGUID)?.asBookmarkNodeData
} else {
return try self.conn.bookmarksGetByGuid(guid: rootGUID, getDirectChildren: true)?.asBookmarkNodeData
}
}
}
/**
* Returns the information about the bookmark with the provided id.
*
* This differs from `getBookmarksTree` in that it does not populate the `children` list
* if `guid` refers to a folder (However, its `childGUIDs` list will be
* populated).
*
* - Parameter guid: the guid of the bookmark to fetch.
*
* - Returns: The bookmark node, or null if the provided guid didn't refer to a
* known bookmark item.
* - Throws:
* - `PlacesApiError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func getBookmark(guid: Guid) throws -> BookmarkNodeData? {
return try queue.sync {
try self.checkApi()
return try self.conn.bookmarksGetByGuid(guid: guid, getDirectChildren: false)?.asBookmarkNodeData
}
}
/**
* Returns the list of bookmarks with the provided URL.
*
* - Note: If the URL is not percent-encoded/punycoded, that will be performed
* internally, and so the returned bookmarks may not have an identical
* URL to the one passed in, however, it will be the same according to
* https://url.spec.whatwg.org
*
* - Parameter url: The url to search for.
*
* - Returns: A list of bookmarks that have the requested URL.
*
* - Throws:
* - `PlacesApiError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func getBookmarksWithURL(url: Url) throws -> [BookmarkItemData] {
return try queue.sync {
try self.checkApi()
let items = try self.conn.bookmarksGetAllWithUrl(url: url)
return toBookmarkItemDataList(items: items)
}
}
/**
* Returns the URL for the provided search keyword, if one exists.
*
* - Parameter keyword: The search keyword.
* - Returns: The bookmarked URL for the keyword, if set.
* - Throws:
* - `PlacesApiError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func getBookmarkURLForKeyword(keyword: String) throws -> Url? {
return try queue.sync {
try self.checkApi()
return try self.conn.bookmarksGetUrlForKeyword(keyword: keyword)
}
}
/**
* Returns the list of bookmarks that match the provided search string.
*
* The order of the results is unspecified.
*
* - Parameter query: The search query
* - Parameter limit: The maximum number of items to return.
* - Returns: A list of bookmarks where either the URL or the title
* contain a word (e.g. space separated item) from the
* query.
* - Throws:
* - `PlacesApiError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func searchBookmarks(query: String, limit: UInt) throws -> [BookmarkItemData] {
return try queue.sync {
try self.checkApi()
let items = try self.conn.bookmarksSearch(query: query, limit: Int32(limit))
return toBookmarkItemDataList(items: items)
}
}
/**
* Returns the list of most recently added bookmarks.
*
* The result list be in order of time of addition, descending (more recent
* additions first), and will contain no folder or separator nodes.
*
* - Parameter limit: The maximum number of items to return.
* - Returns: A list of recently added bookmarks.
* - Throws:
* - `PlacesApiError.databaseInterrupted`: If a call is made to
* `interrupt()` on this object
* from another thread.
* - `PlacesConnectionError.connUseAfterAPIClosed`: If the PlacesAPI that returned
* this connection object has
* been closed. This indicates
* API misuse.
* - `PlacesApiError.databaseBusy`: If this query times out with a
* SQLITE_BUSY error.
* - `PlacesApiError.unexpected`: When an error that has not specifically
* been exposed to Swift is encountered (for
* example IO errors from the database code,
* etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us
* know).
*/
open func getRecentBookmarks(limit: UInt) throws -> [BookmarkItemData] {
return try queue.sync {
try self.checkApi()
let items = try self.conn.bookmarksGetRecent(limit: Int32(limit))
return toBookmarkItemDataList(items: items)
}
}
open func getLatestHistoryMetadataForUrl(url: Url) throws -> HistoryMetadata? {
return try queue.sync {
try self.checkApi()
return try self.conn.getLatestHistoryMetadataForUrl(url: url)
}
}
open func getHistoryMetadataSince(since: Int64) throws -> [HistoryMetadata] {
return try queue.sync {
try self.checkApi()
return try self.conn.getHistoryMetadataSince(since: since)
}
}
open func getHistoryMetadataBetween(start: Int64, end: Int64) throws -> [HistoryMetadata] {
return try queue.sync {
try self.checkApi()
return try self.conn.getHistoryMetadataBetween(start: start, end: end)
}
}
open func getHighlights(weights: HistoryHighlightWeights, limit: Int32) throws -> [HistoryHighlight] {
return try queue.sync {
try self.checkApi()
return try self.conn.getHistoryHighlights(weights: weights, limit: limit)
}
}
open func queryHistoryMetadata(query: String, limit: Int32) throws -> [HistoryMetadata] {
return try queue.sync {
try self.checkApi()
return try self.conn.queryHistoryMetadata(query: query, limit: limit)
}
}
// MARK: History Read APIs
open func matchUrl(query: String) throws -> Url? {
return try queue.sync {
try self.checkApi()
return try self.conn.matchUrl(query: query)
}
}
open func queryAutocomplete(search: String, limit: Int32) throws -> [SearchResult] {
return try queue.sync {
try self.checkApi()
return try self.conn.queryAutocomplete(search: search, limit: limit)
}
}
open func getVisitUrlsInRange(start: PlacesTimestamp, end: PlacesTimestamp, includeRemote: Bool)
throws -> [Url]
{
return try queue.sync {
try self.checkApi()
return try self.conn.getVisitedUrlsInRange(start: start, end: end, includeRemote: includeRemote)
}
}
open func getVisitInfos(start: PlacesTimestamp, end: PlacesTimestamp, excludeTypes: VisitTransitionSet)
throws -> [HistoryVisitInfo]
{
return try queue.sync {
try self.checkApi()
return try self.conn.getVisitInfos(startDate: start, endDate: end, excludeTypes: excludeTypes)
}
}
open func getVisitCount(excludedTypes: VisitTransitionSet) throws -> Int64 {
return try queue.sync {
try self.checkApi()
return try self.conn.getVisitCount(excludeTypes: excludedTypes)
}
}
open func getVisitPageWithBound(
bound: Int64,
offset: Int64,
count: Int64,
excludedTypes: VisitTransitionSet
)
throws -> HistoryVisitInfosWithBound
{
return try queue.sync {
try self.checkApi()
return try self.conn.getVisitPageWithBound(
bound: bound, offset: offset, count: count, excludeTypes: excludedTypes
)
}
}
open func getVisited(urls: [String]) throws -> [Bool] {
return try queue.sync {
try self.checkApi()
return try self.conn.getVisited(urls: urls)
}
}
open func getTopFrecentSiteInfos(numItems: Int32, thresholdOption: FrecencyThresholdOption)
throws -> [TopFrecentSiteInfo]
{
return try queue.sync {
try self.checkApi()
return try self.conn.getTopFrecentSiteInfos(
numItems: numItems,
thresholdOption: thresholdOption
)
}
}
/**
* Attempt to interrupt a long-running operation which may be
* happening concurrently. If the operation is interrupted,
* it will fail.
*
* - Note: Not all operations can be interrupted, and no guarantee is
* made that a concurrent interrupt call will be respected
* (as we may miss it).
*/
open func interrupt() {
interruptHandle.interrupt()
}
}
/**
* A read-write connection to the places database.
*/
public class PlacesWriteConnection: PlacesReadConnection {
/**
* Run periodic database maintenance. This might include, but is
* not limited to:
*
* - `VACUUM`ing.
* - Requesting that the indices in our tables be optimized.
* - Periodic repair or deletion of corrupted records.
* - Deleting older visits when the database exceeds dbSizeLimit
* - etc.
*
* Maintanance in performed in small chunks at a time to avoid blocking the
* DB connection for too long. This means that this should be called
* regularly when the app is idle.
*
* - Parameter dbSizeLimit: Maximum DB size to aim for, in bytes. If the
* database exceeds this size, we will prune a small number of visits. For
* reference, desktop normally uses 75 MiB (78643200). If it determines
* that either the disk or memory is constrained then it halves the amount.
* The default of 0 disables pruning.
*
* - Throws:
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*
*/
open func runMaintenance(dbSizeLimit: UInt32 = 0) throws {
return try queue.sync {
try self.checkApi()
_ = try self.conn.runMaintenancePrune(dbSizeLimit: dbSizeLimit)
try self.conn.runMaintenanceVacuum()
try self.conn.runMaintenanceOptimize()
try self.conn.runMaintenanceCheckpoint()
}
}
/**
* Delete the bookmark with the provided GUID.
*
* If the requested bookmark is a folder, all children of
* bookmark are deleted as well, recursively.
*
* - Parameter guid: The GUID of the bookmark to delete
*
* - Returns: Whether or not the bookmark existed.
*
* - Throws:
* - `PlacesApiError.cannotUpdateRoot`: if `guid` is one of the bookmark roots.
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
@discardableResult
open func deleteBookmarkNode(guid: Guid) throws -> Bool {
return try queue.sync {
try self.checkApi()
return try self.conn.bookmarksDelete(id: guid)
}
}
/**
* Create a bookmark folder, returning its guid.
*
* - Parameter parentGUID: The GUID of the (soon to be) parent of this bookmark.
*
* - Parameter title: The title of the folder.
*
* - Parameter position: The index where to insert the record inside
* its parent. If not provided, this item will
* be appended.
*
* - Returns: The GUID of the newly inserted bookmark folder.
*
* - Throws:
* - `PlacesApiError.cannotUpdateRoot`: If `parentGUID` is `BookmarkRoots.RootGUID`.
* - `PlacesApiError.noSuchItem`: If `parentGUID` does not refer to a known bookmark.
* - `PlacesApiError.invalidParent`: If `parentGUID` refers to a bookmark which is
* not a folder.
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
@discardableResult
open func createFolder(parentGUID: Guid,
title: String,
position: UInt32? = nil) throws -> Guid
{
return try queue.sync {
try self.checkApi()
let p = position == nil ? BookmarkPosition.append : BookmarkPosition.specific(pos: position ?? 0)
let f = InsertableBookmarkFolder(parentGuid: parentGUID, position: p, title: title, children: [])
return try doInsert(item: InsertableBookmarkItem.folder(f: f))
}
}
/**
* Create a bookmark separator, returning its guid.
*
* - Parameter parentGUID: The GUID of the (soon to be) parent of this bookmark.
*
* - Parameter position: The index where to insert the record inside
* its parent. If not provided, this item will
* be appended.
*
* - Returns: The GUID of the newly inserted bookmark separator.
* - Throws:
* - `PlacesApiError.cannotUpdateRoot`: If `parentGUID` is `BookmarkRoots.RootGUID`.
* - `PlacesApiError.noSuchItem`: If `parentGUID` does not refer to a known bookmark.
* - `PlacesApiError.invalidParent`: If `parentGUID` refers to a bookmark which is
* not a folder.
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
@discardableResult
open func createSeparator(parentGUID: Guid, position: UInt32? = nil) throws -> Guid {
return try queue.sync {
try self.checkApi()
let p = position == nil ? BookmarkPosition.append : BookmarkPosition.specific(pos: position ?? 0)
let s = InsertableBookmarkSeparator(parentGuid: parentGUID, position: p)
return try doInsert(item: InsertableBookmarkItem.separator(s: s))
}
}
/**
* Create a bookmark item, returning its guid.
*
* - Parameter parentGUID: The GUID of the (soon to be) parent of this bookmark.
*
* - Parameter position: The index where to insert the record inside
* its parent. If not provided, this item will
* be appended.
*
* - Parameter url: The URL to bookmark
*
* - Parameter title: The title of the new bookmark, if any.
*
* - Returns: The GUID of the newly inserted bookmark item.
*
* - Throws:
* - `PlacesApiError.urlParseError`: If `url` is not a valid URL.
* - `PlacesApiError.urlTooLong`: If `url` is more than 65536 bytes after
* punycoding and hex encoding.
* - `PlacesApiError.cannotUpdateRoot`: If `parentGUID` is `BookmarkRoots.RootGUID`.
* - `PlacesApiError.noSuchItem`: If `parentGUID` does not refer to a known bookmark.
* - `PlacesApiError.invalidParent`: If `parentGUID` refers to a bookmark which is
* not a folder.
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
@discardableResult
open func createBookmark(parentGUID: String,
url: String,
title: String?,
position: UInt32? = nil) throws -> Guid
{
return try queue.sync {
try self.checkApi()
let p = position == nil ? BookmarkPosition.append : BookmarkPosition.specific(pos: position ?? 0)
let bm = InsertableBookmark(parentGuid: parentGUID, position: p, url: url, title: title)
return try doInsert(item: InsertableBookmarkItem.bookmark(b: bm))
}
}
/**
* Update a bookmark to the provided info.
*
* - Parameters:
* - guid: Guid of the bookmark to update
*
* - parentGUID: If the record should be moved to another folder, the guid
* of the folder it should be moved to. Interacts with
* `position`, see the note below for details.
*
* - position: If the record should be moved, the 0-based index where it
* should be moved to. Interacts with `parentGUID`, see the note
* below for details
*
* - title: If the record is a `BookmarkNodeType.bookmark` or a `BookmarkNodeType.folder`,
* and its title should be changed, then the new value of the title.
*
* - url: If the record is a `BookmarkNodeType.bookmark` node, and its `url`
* should be changed, then the new value for the url.
*
* - Note: The `parentGUID` and `position` parameters interact with eachother
* as follows:
*
* - If `parentGUID` is not provided and `position` is, we treat this
* a move within the same folder.
*
* - If `parentGUID` and `position` are both provided, we treat this as
* a move to / within that folder, and we insert at the requested
* position.
*
* - If `position` is not provided (and `parentGUID` is) then its
* treated as a move to the end of that folder.
* - Throws:
* - `PlacesApiError.illegalChange`: If the change requested is impossible given the
* type of the item in the DB. For example, on
* attempts to update the title of a separator.
* - `PlacesApiError.cannotUpdateRoot`: If `guid` is a member of `BookmarkRoots.All`, or
* `parentGUID` is is `BookmarkRoots.RootGUID`.
* - `PlacesApiError.noSuchItem`: If `guid` or `parentGUID` (if specified) do not refer
* to known bookmarks.
* - `PlacesApiError.invalidParent`: If `parentGUID` is specified and refers to a bookmark
* which is not a folder.
* - `PlacesConnectionError.connUseAfterAPIClosed`: if the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesApiError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesApiError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func updateBookmarkNode(guid: Guid,
parentGUID: Guid? = nil,
position: UInt32? = nil,
title: String? = nil,
url: Url? = nil) throws
{
try queue.sync {
try self.checkApi()
let data = BookmarkUpdateInfo(
guid: guid,
title: title,
url: url,
parentGuid: parentGUID,
position: position
)
try self.conn.bookmarksUpdate(data: data)
}
}
// Helper for the various creation functions.
// Note: Caller synchronizes
private func doInsert(item: InsertableBookmarkItem) throws -> Guid {
return try conn.bookmarksInsert(bookmark: item)
}
// MARK: History metadata write APIs
open func noteHistoryMetadataObservation(
observation: HistoryMetadataObservation
) throws {
try queue.sync {
try self.checkApi()
try self.conn.noteHistoryMetadataObservation(data: observation)
}
}
// Keeping these three functions inline with what Kotlin (PlacesConnection.kt)
// to make future work more symmetrical
open func noteHistoryMetadataObservationViewTime(key: HistoryMetadataKey, viewTime: Int32?) throws {
let obs = HistoryMetadataObservation(
url: key.url,
referrerUrl: key.referrerUrl,
searchTerm: key.searchTerm,
viewTime: viewTime
)
try noteHistoryMetadataObservation(observation: obs)
}
open func noteHistoryMetadataObservationDocumentType(key: HistoryMetadataKey, documentType: DocumentType) throws {
let obs = HistoryMetadataObservation(
url: key.url,
referrerUrl: key.referrerUrl,
searchTerm: key.searchTerm,
documentType: documentType
)
try noteHistoryMetadataObservation(observation: obs)
}
open func noteHistoryMetadataObservationTitle(key: HistoryMetadataKey, title: String) throws {
let obs = HistoryMetadataObservation(
url: key.url,
referrerUrl: key.referrerUrl,
searchTerm: key.searchTerm,
title: title
)
try noteHistoryMetadataObservation(observation: obs)
}
open func deleteHistoryMetadataOlderThan(olderThan: Int64) throws {
try queue.sync {
try self.checkApi()
try self.conn.metadataDeleteOlderThan(olderThan: olderThan)
}
}
open func deleteHistoryMetadata(key: HistoryMetadataKey) throws {
try queue.sync {
try self.checkApi()
try self.conn.metadataDelete(
url: key.url,
referrerUrl: key.referrerUrl,
searchTerm: key.searchTerm
)
}
}
// MARK: History Write APIs
open func deleteVisitsFor(url: Url) throws {
try queue.sync {
try self.checkApi()
try self.conn.deleteVisitsFor(url: url)
}
}
open func deleteVisitsBetween(start: PlacesTimestamp, end: PlacesTimestamp) throws {
try queue.sync {
try self.checkApi()
try self.conn.deleteVisitsBetween(start: start, end: end)
}
}
open func deleteVisit(url: Url, timestamp: PlacesTimestamp) throws {
try queue.sync {
try self.checkApi()
try self.conn.deleteVisit(url: url, timestamp: timestamp)
}
}
open func deleteEverythingHistory() throws {
try queue.sync {
try self.checkApi()
try self.conn.deleteEverythingHistory()
}
}
open func pruneDestructively() throws {
try queue.sync {
try self.checkApi()
try self.conn.pruneDestructively()
}
}
open func acceptResult(searchString: String, url: String) throws {
return try queue.sync {
try self.checkApi()
return try self.conn.acceptResult(searchString: searchString, url: url)
}
}
open func applyObservation(visitObservation: VisitObservation) throws {
return try queue.sync {
try self.checkApi()
return try self.conn.applyObservation(visit: visitObservation)
}
}
open func migrateHistoryFromBrowserDb(path: String, lastSyncTimestamp: Int64) throws -> HistoryMigrationResult {
return try queue.sync {
try self.checkApi()
return try self.conn.placesHistoryImportFromIos(dbPath: path, lastSyncTimestamp: lastSyncTimestamp)
}
}
}