1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 this.EXPORTED_SYMBOLS = ["AsanReporter"];
8 const { XPCOMUtils } = ChromeUtils.import(
9 "resource://gre/modules/XPCOMUtils.jsm"
12 XPCOMUtils.defineLazyModuleGetters(this, {
13 AppConstants: "resource://gre/modules/AppConstants.jsm",
14 Log: "resource://gre/modules/Log.jsm",
15 OS: "resource://gre/modules/osfile.jsm",
16 Services: "resource://gre/modules/Services.jsm",
19 XPCOMUtils.defineLazyGlobalGetters(this, ["TextDecoder", "XMLHttpRequest"]);
22 const PREF_CLIENT_ID = "asanreporter.clientid";
23 const PREF_API_URL = "asanreporter.apiurl";
24 const PREF_AUTH_TOKEN = "asanreporter.authtoken";
25 const PREF_LOG_LEVEL = "asanreporter.loglevel";
27 const LOGGER_NAME = "asanreporter";
31 XPCOMUtils.defineLazyGetter(this, "asanDumpDir", () => {
32 let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
33 return OS.Path.join(profileDir.path, "asan");
38 if (this.initialized) {
41 this.initialized = true;
44 logger = Log.repository.getLogger(LOGGER_NAME);
45 logger.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
46 logger.addAppender(new Log.DumpAppender(new Log.BasicFormatter()));
47 logger.level = Services.prefs.getIntPref(PREF_LOG_LEVEL, Log.Level.Info);
49 logger.info("Starting up...");
51 // Install a handler to observe tab crashes, so we can report those right
52 // after they happen instead of relying on the user to restart the browser.
53 Services.obs.addObserver(this, "ipc:content-shutdown");
58 observe(aSubject, aTopic, aData) {
59 if (aTopic == "ipc:content-shutdown") {
60 aSubject.QueryInterface(Ci.nsIPropertyBag2);
61 if (!aSubject.get("abnormal")) {
69 function processDirectory() {
70 let iterator = new OS.File.DirectoryIterator(asanDumpDir);
73 // Scan the directory for any ASan logs that we haven't
74 // submitted yet. Store the filenames in an array so we
75 // can close the iterator early.
79 entry.name.indexOf("ff_asan_log.") == 0 &&
80 !entry.name.includes("submitted")
88 logger.info("Processing " + results.length + " reports...");
90 // Sequentially submit all reports that we found. Note that doing this
91 // with Promise.all would not result in a sequential ordering and would
92 // cause multiple requests to be sent to the server at once.
93 let requests = Promise.resolve();
94 results.forEach(result => {
95 requests = requests.then(
96 // We return a promise here that already handles any submit failures
97 // so our chain is not interrupted if one of the reports couldn't
98 // be submitted for some reason.
100 submitReport(result.path).then(
102 logger.info("Successfully submitted " + result.path);
106 "Failed to submit " + result.path + ". Reason: " + e
113 requests.then(() => logger.info("Done processing reports."));
117 logger.error("Error while iterating over report files: " + e);
122 function submitReport(reportFile) {
123 logger.info("Processing " + reportFile);
124 return OS.File.read(reportFile)
125 .then(submitToServer)
127 // Mark as submitted only if we successfully submitted it to the server.
128 return OS.File.move(reportFile, reportFile + ".submitted");
132 function submitToServer(data) {
133 return new Promise(function(resolve, reject) {
134 logger.debug("Setting up XHR request");
135 let client = Services.prefs.getStringPref(PREF_CLIENT_ID);
136 let api_url = Services.prefs.getStringPref(PREF_API_URL);
137 let auth_token = Services.prefs.getStringPref(PREF_AUTH_TOKEN, null);
139 let decoder = new TextDecoder();
146 Services.appinfo.version,
147 Services.appinfo.appBuildID,
148 AppConstants.SOURCE_REVISION_URL || "unknown",
151 // Concatenate all relevant information as our server only
152 // has one field available for version information.
153 let product_version = versionArr.join("-");
154 let os = AppConstants.platform;
159 rawCrashData: decoder.decode(data),
160 // Hardcode platform as there is no other reasonable platform for ASan
162 product: "mozilla-central-asan-nightly",
166 tool: "asan-nightly-program",
169 var xhr = new XMLHttpRequest();
170 xhr.open("POST", api_url, true);
171 xhr.setRequestHeader("Content-Type", "application/json");
173 // For internal testing purposes, an auth_token can be specified
175 xhr.setRequestHeader("Authorization", "Token " + auth_token);
177 // Prevent privacy leaks
178 xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS;
181 xhr.onreadystatechange = function() {
182 if (xhr.readyState == 4) {
183 if (xhr.status == "201") {
184 logger.debug("XHR: OK");
188 "XHR: Status: " + xhr.status + " Response: " + xhr.responseText
195 xhr.send(JSON.stringify(reportObj));