|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = [ |
|
8 "RotaryEngine", |
|
9 "RotaryRecord", |
|
10 "RotaryStore", |
|
11 "RotaryTracker", |
|
12 ]; |
|
13 |
|
14 const {utils: Cu} = Components; |
|
15 |
|
16 Cu.import("resource://services-sync/engines.js"); |
|
17 Cu.import("resource://services-sync/record.js"); |
|
18 Cu.import("resource://services-sync/util.js"); |
|
19 |
|
20 /* |
|
21 * A fake engine implementation. |
|
22 * This is used all over the place. |
|
23 * |
|
24 * Complete with record, store, and tracker implementations. |
|
25 */ |
|
26 |
|
27 this.RotaryRecord = function RotaryRecord(collection, id) { |
|
28 CryptoWrapper.call(this, collection, id); |
|
29 } |
|
30 RotaryRecord.prototype = { |
|
31 __proto__: CryptoWrapper.prototype |
|
32 }; |
|
33 Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]); |
|
34 |
|
35 this.RotaryStore = function RotaryStore(engine) { |
|
36 Store.call(this, "Rotary", engine); |
|
37 this.items = {}; |
|
38 } |
|
39 RotaryStore.prototype = { |
|
40 __proto__: Store.prototype, |
|
41 |
|
42 create: function create(record) { |
|
43 this.items[record.id] = record.denomination; |
|
44 }, |
|
45 |
|
46 remove: function remove(record) { |
|
47 delete this.items[record.id]; |
|
48 }, |
|
49 |
|
50 update: function update(record) { |
|
51 this.items[record.id] = record.denomination; |
|
52 }, |
|
53 |
|
54 itemExists: function itemExists(id) { |
|
55 return (id in this.items); |
|
56 }, |
|
57 |
|
58 createRecord: function createRecord(id, collection) { |
|
59 let record = new RotaryRecord(collection, id); |
|
60 |
|
61 if (!(id in this.items)) { |
|
62 record.deleted = true; |
|
63 return record; |
|
64 } |
|
65 |
|
66 record.denomination = this.items[id] || "Data for new record: " + id; |
|
67 return record; |
|
68 }, |
|
69 |
|
70 changeItemID: function changeItemID(oldID, newID) { |
|
71 if (oldID in this.items) { |
|
72 this.items[newID] = this.items[oldID]; |
|
73 } |
|
74 |
|
75 delete this.items[oldID]; |
|
76 }, |
|
77 |
|
78 getAllIDs: function getAllIDs() { |
|
79 let ids = {}; |
|
80 for (let id in this.items) { |
|
81 ids[id] = true; |
|
82 } |
|
83 return ids; |
|
84 }, |
|
85 |
|
86 wipe: function wipe() { |
|
87 this.items = {}; |
|
88 } |
|
89 }; |
|
90 |
|
91 this.RotaryTracker = function RotaryTracker(engine) { |
|
92 Tracker.call(this, "Rotary", engine); |
|
93 } |
|
94 RotaryTracker.prototype = { |
|
95 __proto__: Tracker.prototype |
|
96 }; |
|
97 |
|
98 |
|
99 this.RotaryEngine = function RotaryEngine(service) { |
|
100 SyncEngine.call(this, "Rotary", service); |
|
101 // Ensure that the engine starts with a clean slate. |
|
102 this.toFetch = []; |
|
103 this.previousFailed = []; |
|
104 } |
|
105 RotaryEngine.prototype = { |
|
106 __proto__: SyncEngine.prototype, |
|
107 _storeObj: RotaryStore, |
|
108 _trackerObj: RotaryTracker, |
|
109 _recordObj: RotaryRecord, |
|
110 |
|
111 _findDupe: function _findDupe(item) { |
|
112 // This is a semaphore used for testing proper reconciling on dupe |
|
113 // detection. |
|
114 if (item.id == "DUPE_INCOMING") { |
|
115 return "DUPE_LOCAL"; |
|
116 } |
|
117 |
|
118 for (let [id, value] in Iterator(this._store.items)) { |
|
119 if (item.denomination == value) { |
|
120 return id; |
|
121 } |
|
122 } |
|
123 } |
|
124 }; |