Skip to content

Commit

Permalink
Bug 855604 - SearchCountMeasurement2 should wait for nsSearchService …
Browse files Browse the repository at this point in the history
…to initialize asynchronously. r=gps
  • Loading branch information
Richard Newman committed Mar 29, 2013
1 parent e1a86c8 commit 64e918b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
22 changes: 17 additions & 5 deletions services/healthreport/providers.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ AppInfoProvider.prototype = Object.freeze({
xpcomabi: "XPCOMABI",
},

onInit: function () {
return Task.spawn(this._onInit.bind(this));
postInit: function () {
return Task.spawn(this._postInit.bind(this));
},

_onInit: function () {
_postInit: function () {
let recordEmptyAppInfo = function () {
this._setCurrentAppVersion("");
this._setCurrentPlatformVersion("");
Expand Down Expand Up @@ -728,7 +728,7 @@ AddonsProvider.prototype = Object.freeze({
AddonCountsMeasurement,
],

onInit: function () {
postInit: function () {
let listener = {};

for (let method of this.ADDON_LISTENER_CALLBACKS) {
Expand Down Expand Up @@ -1196,7 +1196,7 @@ SearchCountMeasurement2.prototype = Object.freeze({
* data.
*/
shouldIncludeField: function (name) {
return name.indexOf(".") != -1;
return name.contains(".");
},

/**
Expand Down Expand Up @@ -1328,6 +1328,18 @@ this.SearchesProvider.prototype = Object.freeze({
SearchCountMeasurement2,
],

/**
* Initialize the search service before our measurements are touched.
*/
preInit: function (storage) {
// Initialize search service.
let deferred = Promise.defer();
Services.search.init(function onInitComplete () {
deferred.resolve();
});
return deferred.promise;
},

/**
* Record that a search occurred.
*
Expand Down
4 changes: 0 additions & 4 deletions services/healthreport/tests/xpcshell/test_provider_appinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ add_task(function test_record_telemetry() {
setTelemetry(true);
provider = new AppInfoProvider();
yield provider.init(storage);
yield provider.onInit();
yield provider.collectConstantData();

let m = provider.getMeasurement("appinfo", 2);
Expand All @@ -182,7 +181,6 @@ add_task(function test_record_telemetry() {
setTelemetry(false);
provider = new AppInfoProvider();
yield provider.init(storage);
yield provider.onInit();
yield provider.collectConstantData();

m = provider.getMeasurement("appinfo", 2);
Expand All @@ -202,7 +200,6 @@ add_task(function test_record_blocklist() {
Services.prefs.setBoolPref("extensions.blocklist.enabled", true);
let provider = new AppInfoProvider();
yield provider.init(storage);
yield provider.onInit();
yield provider.collectConstantData();

let m = provider.getMeasurement("appinfo", 2);
Expand All @@ -214,7 +211,6 @@ add_task(function test_record_blocklist() {
Services.prefs.setBoolPref("extensions.blocklist.enabled", false);
provider = new AppInfoProvider();
yield provider.init(storage);
yield provider.onInit();
yield provider.collectConstantData();

m = provider.getMeasurement("appinfo", 2);
Expand Down
35 changes: 27 additions & 8 deletions services/metrics/dataprovider.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ Provider.prototype = Object.freeze({

let self = this;
return Task.spawn(function init() {
let pre = self.preInit();
if (!pre || typeof(pre.then) != "function") {
throw new Error("preInit() does not return a promise.");
}
yield pre;

for (let measurementType of self.measurementTypes) {
let measurement = new measurementType();

Expand All @@ -573,13 +579,11 @@ Provider.prototype = Object.freeze({
measurement);
}

let promise = self.onInit();

if (!promise || typeof(promise.then) != "function") {
throw new Error("onInit() does not return a promise.");
let post = self.postInit();
if (!post || typeof(post.then) != "function") {
throw new Error("postInit() does not return a promise.");
}

yield promise;
yield post;
});
},

Expand All @@ -594,15 +598,30 @@ Provider.prototype = Object.freeze({
},

/**
* Hook point for implementations to perform initialization activity.
* Hook point for implementations to perform pre-initialization activity.
*
* This method will be called before measurement registration.
*
* Implementations should return a promise which is resolved when
* initialization activities have completed.
*/
preInit: function () {
return CommonUtils.laterTickResolvingPromise();
},

/**
* Hook point for implementations to perform post-initialization activity.
*
* This method will be called after `preInit` and measurement registration,
* but before initialization is finished.
*
* If a `Provider` instance needs to register observers, etc, it should
* implement this function.
*
* Implementations should return a promise which is resolved when
* initialization activities have completed.
*/
onInit: function () {
postInit: function () {
return CommonUtils.laterTickResolvingPromise();
},

Expand Down

0 comments on commit 64e918b

Please sign in to comment.