Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | this.EXPORTED_SYMBOLS = [ |
michael@0 | 8 | "SyncProvider", |
michael@0 | 9 | ]; |
michael@0 | 10 | |
michael@0 | 11 | const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
michael@0 | 12 | |
michael@0 | 13 | Cu.import("resource://gre/modules/Metrics.jsm", this); |
michael@0 | 14 | Cu.import("resource://gre/modules/Promise.jsm", this); |
michael@0 | 15 | Cu.import("resource://gre/modules/Services.jsm", this); |
michael@0 | 16 | Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); |
michael@0 | 17 | |
michael@0 | 18 | const DAILY_LAST_NUMERIC_FIELD = {type: Metrics.Storage.FIELD_DAILY_LAST_NUMERIC}; |
michael@0 | 19 | const DAILY_LAST_TEXT_FIELD = {type: Metrics.Storage.FIELD_DAILY_LAST_TEXT}; |
michael@0 | 20 | const DAILY_COUNTER_FIELD = {type: Metrics.Storage.FIELD_DAILY_COUNTER}; |
michael@0 | 21 | |
michael@0 | 22 | XPCOMUtils.defineLazyModuleGetter(this, "Weave", |
michael@0 | 23 | "resource://services-sync/main.js"); |
michael@0 | 24 | |
michael@0 | 25 | function SyncMeasurement1() { |
michael@0 | 26 | Metrics.Measurement.call(this); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | SyncMeasurement1.prototype = Object.freeze({ |
michael@0 | 30 | __proto__: Metrics.Measurement.prototype, |
michael@0 | 31 | |
michael@0 | 32 | name: "sync", |
michael@0 | 33 | version: 1, |
michael@0 | 34 | |
michael@0 | 35 | fields: { |
michael@0 | 36 | enabled: DAILY_LAST_NUMERIC_FIELD, |
michael@0 | 37 | preferredProtocol: DAILY_LAST_TEXT_FIELD, |
michael@0 | 38 | activeProtocol: DAILY_LAST_TEXT_FIELD, |
michael@0 | 39 | syncStart: DAILY_COUNTER_FIELD, |
michael@0 | 40 | syncSuccess: DAILY_COUNTER_FIELD, |
michael@0 | 41 | syncError: DAILY_COUNTER_FIELD, |
michael@0 | 42 | }, |
michael@0 | 43 | }); |
michael@0 | 44 | |
michael@0 | 45 | function SyncDevicesMeasurement1() { |
michael@0 | 46 | Metrics.Measurement.call(this); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | SyncDevicesMeasurement1.prototype = Object.freeze({ |
michael@0 | 50 | __proto__: Metrics.Measurement.prototype, |
michael@0 | 51 | |
michael@0 | 52 | name: "devices", |
michael@0 | 53 | version: 1, |
michael@0 | 54 | |
michael@0 | 55 | fields: {}, |
michael@0 | 56 | |
michael@0 | 57 | shouldIncludeField: function (name) { |
michael@0 | 58 | return true; |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | fieldType: function (name) { |
michael@0 | 62 | return Metrics.Storage.FIELD_DAILY_COUNTER; |
michael@0 | 63 | }, |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | this.SyncProvider = function () { |
michael@0 | 67 | Metrics.Provider.call(this); |
michael@0 | 68 | }; |
michael@0 | 69 | SyncProvider.prototype = Object.freeze({ |
michael@0 | 70 | __proto__: Metrics.Provider.prototype, |
michael@0 | 71 | |
michael@0 | 72 | name: "org.mozilla.sync", |
michael@0 | 73 | |
michael@0 | 74 | measurementTypes: [ |
michael@0 | 75 | SyncDevicesMeasurement1, |
michael@0 | 76 | SyncMeasurement1, |
michael@0 | 77 | ], |
michael@0 | 78 | |
michael@0 | 79 | _OBSERVERS: [ |
michael@0 | 80 | "weave:service:sync:start", |
michael@0 | 81 | "weave:service:sync:finish", |
michael@0 | 82 | "weave:service:sync:error", |
michael@0 | 83 | ], |
michael@0 | 84 | |
michael@0 | 85 | postInit: function () { |
michael@0 | 86 | for (let o of this._OBSERVERS) { |
michael@0 | 87 | Services.obs.addObserver(this, o, false); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | return Promise.resolve(); |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | onShutdown: function () { |
michael@0 | 94 | for (let o of this._OBSERVERS) { |
michael@0 | 95 | Services.obs.removeObserver(this, o); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | return Promise.resolve(); |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | observe: function (subject, topic, data) { |
michael@0 | 102 | let field; |
michael@0 | 103 | switch (topic) { |
michael@0 | 104 | case "weave:service:sync:start": |
michael@0 | 105 | field = "syncStart"; |
michael@0 | 106 | break; |
michael@0 | 107 | |
michael@0 | 108 | case "weave:service:sync:finish": |
michael@0 | 109 | field = "syncSuccess"; |
michael@0 | 110 | break; |
michael@0 | 111 | |
michael@0 | 112 | case "weave:service:sync:error": |
michael@0 | 113 | field = "syncError"; |
michael@0 | 114 | break; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | let m = this.getMeasurement(SyncMeasurement1.prototype.name, |
michael@0 | 118 | SyncMeasurement1.prototype.version); |
michael@0 | 119 | return this.enqueueStorageOperation(function recordSyncEvent() { |
michael@0 | 120 | return m.incrementDailyCounter(field); |
michael@0 | 121 | }); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | collectDailyData: function () { |
michael@0 | 125 | return this.storage.enqueueTransaction(this._populateDailyData.bind(this)); |
michael@0 | 126 | }, |
michael@0 | 127 | |
michael@0 | 128 | _populateDailyData: function* () { |
michael@0 | 129 | let m = this.getMeasurement(SyncMeasurement1.prototype.name, |
michael@0 | 130 | SyncMeasurement1.prototype.version); |
michael@0 | 131 | |
michael@0 | 132 | let svc = Cc["@mozilla.org/weave/service;1"] |
michael@0 | 133 | .getService(Ci.nsISupports) |
michael@0 | 134 | .wrappedJSObject; |
michael@0 | 135 | |
michael@0 | 136 | let enabled = svc.enabled; |
michael@0 | 137 | yield m.setDailyLastNumeric("enabled", enabled ? 1 : 0); |
michael@0 | 138 | |
michael@0 | 139 | // preferredProtocol is constant and only changes as the client |
michael@0 | 140 | // evolves. |
michael@0 | 141 | yield m.setDailyLastText("preferredProtocol", "1.5"); |
michael@0 | 142 | |
michael@0 | 143 | let protocol = svc.fxAccountsEnabled ? "1.5" : "1.1"; |
michael@0 | 144 | yield m.setDailyLastText("activeProtocol", protocol); |
michael@0 | 145 | |
michael@0 | 146 | if (!enabled) { |
michael@0 | 147 | return; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Before grabbing more information, be sure the Sync service |
michael@0 | 151 | // is fully initialized. This has the potential to initialize |
michael@0 | 152 | // Sync on the spot. This may be undesired if Sync appears to |
michael@0 | 153 | // be enabled but it really isn't. That responsibility should |
michael@0 | 154 | // be up to svc.enabled to not return false positives, however. |
michael@0 | 155 | yield svc.whenLoaded(); |
michael@0 | 156 | |
michael@0 | 157 | if (Weave.Status.service != Weave.STATUS_OK) { |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | // Device types are dynamic. So we need to dynamically create fields if |
michael@0 | 162 | // they don't exist. |
michael@0 | 163 | let dm = this.getMeasurement(SyncDevicesMeasurement1.prototype.name, |
michael@0 | 164 | SyncDevicesMeasurement1.prototype.version); |
michael@0 | 165 | let devices = Weave.Service.clientsEngine.deviceTypes; |
michael@0 | 166 | for (let [field, count] of devices) { |
michael@0 | 167 | let hasField = this.storage.hasFieldFromMeasurement(dm.id, field, |
michael@0 | 168 | this.storage.FIELD_DAILY_LAST_NUMERIC); |
michael@0 | 169 | let fieldID; |
michael@0 | 170 | if (hasField) { |
michael@0 | 171 | fieldID = this.storage.fieldIDFromMeasurement(dm.id, field); |
michael@0 | 172 | } else { |
michael@0 | 173 | fieldID = yield this.storage.registerField(dm.id, field, |
michael@0 | 174 | this.storage.FIELD_DAILY_LAST_NUMERIC); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | yield this.storage.setDailyLastNumericFromFieldID(fieldID, count); |
michael@0 | 178 | } |
michael@0 | 179 | }, |
michael@0 | 180 | }); |