1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/abouthealthreport/abouthealth.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/Preferences.jsm"); 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +const reporter = Cc["@mozilla.org/datareporting/service;1"] 1.16 + .getService(Ci.nsISupports) 1.17 + .wrappedJSObject 1.18 + .healthReporter; 1.19 + 1.20 +const policy = Cc["@mozilla.org/datareporting/service;1"] 1.21 + .getService(Ci.nsISupports) 1.22 + .wrappedJSObject 1.23 + .policy; 1.24 + 1.25 +const prefs = new Preferences("datareporting.healthreport."); 1.26 + 1.27 + 1.28 +let healthReportWrapper = { 1.29 + init: function () { 1.30 + if (!reporter) { 1.31 + healthReportWrapper.handleInitFailure(); 1.32 + return; 1.33 + } 1.34 + 1.35 + reporter.onInit().then(healthReportWrapper.refreshPayload, 1.36 + healthReportWrapper.handleInitFailure); 1.37 + 1.38 + let iframe = document.getElementById("remote-report"); 1.39 + iframe.addEventListener("load", healthReportWrapper.initRemotePage, false); 1.40 + let report = this._getReportURI(); 1.41 + iframe.src = report.spec; 1.42 + prefs.observe("uploadEnabled", this.updatePrefState, healthReportWrapper); 1.43 + }, 1.44 + 1.45 + uninit: function () { 1.46 + prefs.ignore("uploadEnabled", this.updatePrefState, healthReportWrapper); 1.47 + }, 1.48 + 1.49 + _getReportURI: function () { 1.50 + let url = Services.urlFormatter.formatURLPref("datareporting.healthreport.about.reportUrl"); 1.51 + return Services.io.newURI(url, null, null); 1.52 + }, 1.53 + 1.54 + onOptIn: function () { 1.55 + policy.recordHealthReportUploadEnabled(true, 1.56 + "Health report page sent opt-in command."); 1.57 + this.updatePrefState(); 1.58 + }, 1.59 + 1.60 + onOptOut: function () { 1.61 + policy.recordHealthReportUploadEnabled(false, 1.62 + "Health report page sent opt-out command."); 1.63 + this.updatePrefState(); 1.64 + }, 1.65 + 1.66 + updatePrefState: function () { 1.67 + try { 1.68 + let prefs = { 1.69 + enabled: policy.healthReportUploadEnabled, 1.70 + } 1.71 + this.injectData("prefs", prefs); 1.72 + } catch (e) { 1.73 + this.reportFailure(this.ERROR_PREFS_FAILED); 1.74 + } 1.75 + }, 1.76 + 1.77 + refreshPayload: function () { 1.78 + reporter.collectAndObtainJSONPayload().then(healthReportWrapper.updatePayload, 1.79 + healthReportWrapper.handlePayloadFailure); 1.80 + }, 1.81 + 1.82 + updatePayload: function (data) { 1.83 + healthReportWrapper.injectData("payload", data); 1.84 + }, 1.85 + 1.86 + injectData: function (type, content) { 1.87 + let report = this._getReportURI(); 1.88 + 1.89 + // file URIs can't be used for targetOrigin, so we use "*" for this special case 1.90 + // in all other cases, pass in the URL to the report so we properly restrict the message dispatch 1.91 + let reportUrl = report.scheme == "file" ? "*" : report.spec; 1.92 + 1.93 + let data = { 1.94 + type: type, 1.95 + content: content 1.96 + } 1.97 + 1.98 + let iframe = document.getElementById("remote-report"); 1.99 + iframe.contentWindow.postMessage(data, reportUrl); 1.100 + }, 1.101 + 1.102 + handleRemoteCommand: function (evt) { 1.103 + switch (evt.detail.command) { 1.104 + case "DisableDataSubmission": 1.105 + this.onOptOut(); 1.106 + break; 1.107 + case "EnableDataSubmission": 1.108 + this.onOptIn(); 1.109 + break; 1.110 + case "RequestCurrentPrefs": 1.111 + this.updatePrefState(); 1.112 + break; 1.113 + case "RequestCurrentPayload": 1.114 + this.refreshPayload(); 1.115 + break; 1.116 + default: 1.117 + Cu.reportError("Unexpected remote command received: " + evt.detail.command + ". Ignoring command."); 1.118 + break; 1.119 + } 1.120 + }, 1.121 + 1.122 + initRemotePage: function () { 1.123 + let iframe = document.getElementById("remote-report").contentDocument; 1.124 + iframe.addEventListener("RemoteHealthReportCommand", 1.125 + function onCommand(e) {healthReportWrapper.handleRemoteCommand(e);}, 1.126 + false); 1.127 + healthReportWrapper.updatePrefState(); 1.128 + }, 1.129 + 1.130 + // error handling 1.131 + ERROR_INIT_FAILED: 1, 1.132 + ERROR_PAYLOAD_FAILED: 2, 1.133 + ERROR_PREFS_FAILED: 3, 1.134 + 1.135 + reportFailure: function (error) { 1.136 + let details = { 1.137 + errorType: error, 1.138 + } 1.139 + healthReportWrapper.injectData("error", details); 1.140 + }, 1.141 + 1.142 + handleInitFailure: function () { 1.143 + healthReportWrapper.reportFailure(healthReportWrapper.ERROR_INIT_FAILED); 1.144 + }, 1.145 + 1.146 + handlePayloadFailure: function () { 1.147 + healthReportWrapper.reportFailure(healthReportWrapper.ERROR_PAYLOAD_FAILED); 1.148 + }, 1.149 +} 1.150 + 1.151 +window.addEventListener("load", function () { healthReportWrapper.init(); }); 1.152 +window.addEventListener("unload", function () { healthReportWrapper.uninit(); });