mobile/android/chrome/content/aboutHealthReport.js

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

michael@0 1 #filter substitution
michael@0 2 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 "use strict";
michael@0 8
michael@0 9 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12 Cu.import("resource://gre/modules/Messaging.jsm");
michael@0 13 Cu.import("resource://gre/modules/SharedPreferences.jsm");
michael@0 14
michael@0 15 // Name of Android SharedPreference controlling whether to upload
michael@0 16 // health reports.
michael@0 17 const PREF_UPLOAD_ENABLED = "android.not_a_preference.healthreport.uploadEnabled";
michael@0 18
michael@0 19 // Name of Gecko Pref specifying report content location.
michael@0 20 const PREF_REPORTURL = "datareporting.healthreport.about.reportUrl";
michael@0 21
michael@0 22 // Monotonically increasing wrapper API version number.
michael@0 23 const WRAPPER_VERSION = 1;
michael@0 24
michael@0 25 const EVENT_HEALTH_REQUEST = "HealthReport:Request";
michael@0 26 const EVENT_HEALTH_RESPONSE = "HealthReport:Response";
michael@0 27
michael@0 28 // about:healthreport prefs are stored in Firefox's default Android
michael@0 29 // SharedPreferences.
michael@0 30 let sharedPrefs = new SharedPreferences();
michael@0 31
michael@0 32 let healthReportWrapper = {
michael@0 33 init: function () {
michael@0 34 let iframe = document.getElementById("remote-report");
michael@0 35 iframe.addEventListener("load", healthReportWrapper.initRemotePage, false);
michael@0 36 let report = this._getReportURI();
michael@0 37 iframe.src = report.spec;
michael@0 38 console.log("AboutHealthReport: loading content from " + report.spec);
michael@0 39
michael@0 40 sharedPrefs.addObserver(PREF_UPLOAD_ENABLED, this, false);
michael@0 41 Services.obs.addObserver(this, EVENT_HEALTH_RESPONSE, false);
michael@0 42 },
michael@0 43
michael@0 44 observe: function (subject, topic, data) {
michael@0 45 if (topic == PREF_UPLOAD_ENABLED) {
michael@0 46 this.updatePrefState();
michael@0 47 } else if (topic == EVENT_HEALTH_RESPONSE) {
michael@0 48 this.updatePayload(data);
michael@0 49 }
michael@0 50 },
michael@0 51
michael@0 52 uninit: function () {
michael@0 53 sharedPrefs.removeObserver(PREF_UPLOAD_ENABLED, this);
michael@0 54 Services.obs.removeObserver(this, EVENT_HEALTH_RESPONSE);
michael@0 55 },
michael@0 56
michael@0 57 _getReportURI: function () {
michael@0 58 let url = Services.urlFormatter.formatURLPref(PREF_REPORTURL);
michael@0 59 // This handles URLs that already have query parameters.
michael@0 60 let uri = Services.io.newURI(url, null, null).QueryInterface(Ci.nsIURL);
michael@0 61 uri.query += ((uri.query != "") ? "&v=" : "v=") + WRAPPER_VERSION;
michael@0 62 return uri;
michael@0 63 },
michael@0 64
michael@0 65 onOptIn: function () {
michael@0 66 console.log("AboutHealthReport: page sent opt-in command.");
michael@0 67 sharedPrefs.setBoolPref(PREF_UPLOAD_ENABLED, true);
michael@0 68 this.updatePrefState();
michael@0 69 },
michael@0 70
michael@0 71 onOptOut: function () {
michael@0 72 console.log("AboutHealthReport: page sent opt-out command.");
michael@0 73 sharedPrefs.setBoolPref(PREF_UPLOAD_ENABLED, false);
michael@0 74 this.updatePrefState();
michael@0 75 },
michael@0 76
michael@0 77 updatePrefState: function () {
michael@0 78 console.log("AboutHealthReport: sending pref state to page.");
michael@0 79 try {
michael@0 80 let prefs = {
michael@0 81 enabled: sharedPrefs.getBoolPref(PREF_UPLOAD_ENABLED),
michael@0 82 };
michael@0 83 this.injectData("prefs", prefs);
michael@0 84 } catch (e) {
michael@0 85 this.reportFailure(this.ERROR_PREFS_FAILED);
michael@0 86 }
michael@0 87 },
michael@0 88
michael@0 89 refreshPayload: function () {
michael@0 90 console.log("AboutHealthReport: page requested fresh payload.");
michael@0 91 sendMessageToJava({
michael@0 92 type: EVENT_HEALTH_REQUEST,
michael@0 93 });
michael@0 94 },
michael@0 95
michael@0 96 updatePayload: function (data) {
michael@0 97 healthReportWrapper.injectData("payload", data);
michael@0 98 // Data is supposed to be a string, so the length should be
michael@0 99 // defined. Just in case, we do this after injecting the data.
michael@0 100 console.log("AboutHealthReport: sending payload to page " +
michael@0 101 "(" + typeof(data) + " of length " + data.length + ").");
michael@0 102 },
michael@0 103
michael@0 104 injectData: function (type, content) {
michael@0 105 let report = this._getReportURI();
michael@0 106
michael@0 107 // file: URIs can't be used for targetOrigin, so we use "*" for
michael@0 108 // this special case. In all other cases, pass in the URL to the
michael@0 109 // report so we properly restrict the message dispatch.
michael@0 110 let reportUrl = (report.scheme == "file") ? "*" : report.spec;
michael@0 111
michael@0 112 let data = {
michael@0 113 type: type,
michael@0 114 content: content,
michael@0 115 };
michael@0 116
michael@0 117 let iframe = document.getElementById("remote-report");
michael@0 118 iframe.contentWindow.postMessage(data, reportUrl);
michael@0 119 },
michael@0 120
michael@0 121 showSettings: function () {
michael@0 122 console.log("AboutHealthReport: showing settings.");
michael@0 123 sendMessageToJava({
michael@0 124 type: "Settings:Show",
michael@0 125 resource: "preferences_vendor",
michael@0 126 });
michael@0 127 },
michael@0 128
michael@0 129 launchUpdater: function () {
michael@0 130 console.log("AboutHealthReport: launching updater.");
michael@0 131 sendMessageToJava({
michael@0 132 type: "Updater:Launch",
michael@0 133 });
michael@0 134 },
michael@0 135
michael@0 136 handleRemoteCommand: function (evt) {
michael@0 137 switch (evt.detail.command) {
michael@0 138 case "DisableDataSubmission":
michael@0 139 this.onOptOut();
michael@0 140 break;
michael@0 141 case "EnableDataSubmission":
michael@0 142 this.onOptIn();
michael@0 143 break;
michael@0 144 case "RequestCurrentPrefs":
michael@0 145 this.updatePrefState();
michael@0 146 break;
michael@0 147 case "RequestCurrentPayload":
michael@0 148 this.refreshPayload();
michael@0 149 break;
michael@0 150 case "ShowSettings":
michael@0 151 this.showSettings();
michael@0 152 break;
michael@0 153 case "LaunchUpdater":
michael@0 154 this.launchUpdater();
michael@0 155 break;
michael@0 156 default:
michael@0 157 Cu.reportError("Unexpected remote command received: " + evt.detail.command +
michael@0 158 ". Ignoring command.");
michael@0 159 break;
michael@0 160 }
michael@0 161 },
michael@0 162
michael@0 163 initRemotePage: function () {
michael@0 164 let iframe = document.getElementById("remote-report").contentDocument;
michael@0 165 iframe.addEventListener("RemoteHealthReportCommand",
michael@0 166 function onCommand(e) {healthReportWrapper.handleRemoteCommand(e);},
michael@0 167 false);
michael@0 168 healthReportWrapper.injectData("begin", null);
michael@0 169 },
michael@0 170
michael@0 171 // error handling
michael@0 172 ERROR_INIT_FAILED: 1,
michael@0 173 ERROR_PAYLOAD_FAILED: 2,
michael@0 174 ERROR_PREFS_FAILED: 3,
michael@0 175
michael@0 176 reportFailure: function (error) {
michael@0 177 let details = {
michael@0 178 errorType: error,
michael@0 179 };
michael@0 180 healthReportWrapper.injectData("error", details);
michael@0 181 },
michael@0 182
michael@0 183 handleInitFailure: function () {
michael@0 184 healthReportWrapper.reportFailure(healthReportWrapper.ERROR_INIT_FAILED);
michael@0 185 },
michael@0 186
michael@0 187 handlePayloadFailure: function () {
michael@0 188 healthReportWrapper.reportFailure(healthReportWrapper.ERROR_PAYLOAD_FAILED);
michael@0 189 },
michael@0 190 };
michael@0 191
michael@0 192 window.addEventListener("load", healthReportWrapper.init.bind(healthReportWrapper), false);
michael@0 193 window.addEventListener("unload", healthReportWrapper.uninit.bind(healthReportWrapper), false);

mercurial