services/sync/modules/healthreport.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial