browser/base/content/abouthealthreport/abouthealth.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 8
michael@0 9 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11
michael@0 12 const reporter = Cc["@mozilla.org/datareporting/service;1"]
michael@0 13 .getService(Ci.nsISupports)
michael@0 14 .wrappedJSObject
michael@0 15 .healthReporter;
michael@0 16
michael@0 17 const policy = Cc["@mozilla.org/datareporting/service;1"]
michael@0 18 .getService(Ci.nsISupports)
michael@0 19 .wrappedJSObject
michael@0 20 .policy;
michael@0 21
michael@0 22 const prefs = new Preferences("datareporting.healthreport.");
michael@0 23
michael@0 24
michael@0 25 let healthReportWrapper = {
michael@0 26 init: function () {
michael@0 27 if (!reporter) {
michael@0 28 healthReportWrapper.handleInitFailure();
michael@0 29 return;
michael@0 30 }
michael@0 31
michael@0 32 reporter.onInit().then(healthReportWrapper.refreshPayload,
michael@0 33 healthReportWrapper.handleInitFailure);
michael@0 34
michael@0 35 let iframe = document.getElementById("remote-report");
michael@0 36 iframe.addEventListener("load", healthReportWrapper.initRemotePage, false);
michael@0 37 let report = this._getReportURI();
michael@0 38 iframe.src = report.spec;
michael@0 39 prefs.observe("uploadEnabled", this.updatePrefState, healthReportWrapper);
michael@0 40 },
michael@0 41
michael@0 42 uninit: function () {
michael@0 43 prefs.ignore("uploadEnabled", this.updatePrefState, healthReportWrapper);
michael@0 44 },
michael@0 45
michael@0 46 _getReportURI: function () {
michael@0 47 let url = Services.urlFormatter.formatURLPref("datareporting.healthreport.about.reportUrl");
michael@0 48 return Services.io.newURI(url, null, null);
michael@0 49 },
michael@0 50
michael@0 51 onOptIn: function () {
michael@0 52 policy.recordHealthReportUploadEnabled(true,
michael@0 53 "Health report page sent opt-in command.");
michael@0 54 this.updatePrefState();
michael@0 55 },
michael@0 56
michael@0 57 onOptOut: function () {
michael@0 58 policy.recordHealthReportUploadEnabled(false,
michael@0 59 "Health report page sent opt-out command.");
michael@0 60 this.updatePrefState();
michael@0 61 },
michael@0 62
michael@0 63 updatePrefState: function () {
michael@0 64 try {
michael@0 65 let prefs = {
michael@0 66 enabled: policy.healthReportUploadEnabled,
michael@0 67 }
michael@0 68 this.injectData("prefs", prefs);
michael@0 69 } catch (e) {
michael@0 70 this.reportFailure(this.ERROR_PREFS_FAILED);
michael@0 71 }
michael@0 72 },
michael@0 73
michael@0 74 refreshPayload: function () {
michael@0 75 reporter.collectAndObtainJSONPayload().then(healthReportWrapper.updatePayload,
michael@0 76 healthReportWrapper.handlePayloadFailure);
michael@0 77 },
michael@0 78
michael@0 79 updatePayload: function (data) {
michael@0 80 healthReportWrapper.injectData("payload", data);
michael@0 81 },
michael@0 82
michael@0 83 injectData: function (type, content) {
michael@0 84 let report = this._getReportURI();
michael@0 85
michael@0 86 // file URIs can't be used for targetOrigin, so we use "*" for this special case
michael@0 87 // in all other cases, pass in the URL to the report so we properly restrict the message dispatch
michael@0 88 let reportUrl = report.scheme == "file" ? "*" : report.spec;
michael@0 89
michael@0 90 let data = {
michael@0 91 type: type,
michael@0 92 content: content
michael@0 93 }
michael@0 94
michael@0 95 let iframe = document.getElementById("remote-report");
michael@0 96 iframe.contentWindow.postMessage(data, reportUrl);
michael@0 97 },
michael@0 98
michael@0 99 handleRemoteCommand: function (evt) {
michael@0 100 switch (evt.detail.command) {
michael@0 101 case "DisableDataSubmission":
michael@0 102 this.onOptOut();
michael@0 103 break;
michael@0 104 case "EnableDataSubmission":
michael@0 105 this.onOptIn();
michael@0 106 break;
michael@0 107 case "RequestCurrentPrefs":
michael@0 108 this.updatePrefState();
michael@0 109 break;
michael@0 110 case "RequestCurrentPayload":
michael@0 111 this.refreshPayload();
michael@0 112 break;
michael@0 113 default:
michael@0 114 Cu.reportError("Unexpected remote command received: " + evt.detail.command + ". Ignoring command.");
michael@0 115 break;
michael@0 116 }
michael@0 117 },
michael@0 118
michael@0 119 initRemotePage: function () {
michael@0 120 let iframe = document.getElementById("remote-report").contentDocument;
michael@0 121 iframe.addEventListener("RemoteHealthReportCommand",
michael@0 122 function onCommand(e) {healthReportWrapper.handleRemoteCommand(e);},
michael@0 123 false);
michael@0 124 healthReportWrapper.updatePrefState();
michael@0 125 },
michael@0 126
michael@0 127 // error handling
michael@0 128 ERROR_INIT_FAILED: 1,
michael@0 129 ERROR_PAYLOAD_FAILED: 2,
michael@0 130 ERROR_PREFS_FAILED: 3,
michael@0 131
michael@0 132 reportFailure: function (error) {
michael@0 133 let details = {
michael@0 134 errorType: error,
michael@0 135 }
michael@0 136 healthReportWrapper.injectData("error", details);
michael@0 137 },
michael@0 138
michael@0 139 handleInitFailure: function () {
michael@0 140 healthReportWrapper.reportFailure(healthReportWrapper.ERROR_INIT_FAILED);
michael@0 141 },
michael@0 142
michael@0 143 handlePayloadFailure: function () {
michael@0 144 healthReportWrapper.reportFailure(healthReportWrapper.ERROR_PAYLOAD_FAILED);
michael@0 145 },
michael@0 146 }
michael@0 147
michael@0 148 window.addEventListener("load", function () { healthReportWrapper.init(); });
michael@0 149 window.addEventListener("unload", function () { healthReportWrapper.uninit(); });

mercurial