Bug 838877 - Change FHR policy handling to permit direct pref modification. r=gps

This commit is contained in:
Richard Newman 2013-02-13 16:32:44 -08:00
parent 1445ab8932
commit c96e916919
2 changed files with 51 additions and 7 deletions

View File

@ -254,7 +254,7 @@ Object.freeze(DataSubmissionRequest.prototype);
* (Preferences) Handle on preferences branch on which state will be
* queried and stored.
* @param healthReportPrefs
* (Preferences) Handle on preferences branch hold Health Report state.
* (Preferences) Handle on preferences branch holding Health Report state.
* @param listener
* (object) Object with callbacks that will be invoked at certain key
* events.
@ -279,6 +279,24 @@ this.DataReportingPolicy = function (prefs, healthReportPrefs, listener) {
this.firstRunDate = this.now();
}
// Install an observer so that we can act on changes from external
// code (such as Android UI).
// Use a function because this is the only place where the Preferences
// abstraction is way less usable than nsIPrefBranch.
//
// Hang on to the observer here so that tests can reach it.
this.uploadEnabledObserver = function onUploadEnabledChanged() {
if (this.pendingDeleteRemoteData || this.healthReportUploadEnabled) {
// Nothing to do: either we're already deleting because the caller
// came through the front door (rHRUE), or they set the flag to true.
return;
}
this._log.info("uploadEnabled pref changed. Scheduling deletion.");
this.deleteRemoteData();
}.bind(this);
healthReportPrefs.observe("uploadEnabled", this.uploadEnabledObserver);
// Ensure we are scheduled to submit.
if (!this.nextDataSubmissionDate.getTime()) {
this.nextDataSubmissionDate = this._futureDate(MILLISECONDS_PER_DAY);
@ -668,7 +686,7 @@ DataReportingPolicy.prototype = Object.freeze({
/**
* Record the user's intent for whether FHR should upload data.
*
* This is the preferred way for the application to record a user's
* This is the preferred way for XUL applications to record a user's
* preference on whether Firefox Health Report should upload data to
* a server.
*
@ -689,13 +707,13 @@ DataReportingPolicy.prototype = Object.freeze({
* purposes only.
*/
recordHealthReportUploadEnabled: function (flag, reason="no-reason") {
this.healthReportUploadEnabled = flag;
if (flag) {
return null;
let result = null;
if (!flag) {
result = this.deleteRemoteData(reason);
}
return this.deleteRemoteData(reason);
this.healthReportUploadEnabled = flag;
return result;
},
/**

View File

@ -298,6 +298,9 @@ add_test(function test_upload_kill_switch() {
policy.recordUserAcceptance();
defineNow(policy, policy.nextDataSubmissionDate);
// So that we don't trigger deletions, which cause uploads to be delayed.
hrPrefs.ignore("uploadEnabled", policy.uploadEnabledObserver);
policy.healthReportUploadEnabled = false;
policy.checkStateAndTrigger();
do_check_eq(listener.requestDataUploadCount, 0);
@ -760,3 +763,26 @@ add_test(function test_record_health_report_upload_enabled() {
run_next_test();
});
add_test(function test_pref_change_initiates_deletion() {
let [policy, policyPrefs, hrPrefs, listener] = getPolicy("record_health_report_upload_enabled");
// Preconditions.
do_check_false(policy.pendingDeleteRemoteData);
do_check_true(policy.healthReportUploadEnabled);
do_check_eq(listener.requestRemoteDeleteCount, 0);
// User intent to disable should indirectly result in a pending
// delete request, because the policy is watching for the pref
// to change.
Object.defineProperty(policy, "deleteRemoteData", {
value: function deleteRemoteDataProxy() {
do_check_false(policy.healthReportUploadEnabled);
do_check_false(policy.pendingDeleteRemoteData); // Just called.
run_next_test();
},
});
hrPrefs.set("uploadEnabled", false);
});