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 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
10 ChromeUtils.defineESModuleGetters(lazy, {
11 Log: "resource://gre/modules/Log.sys.mjs",
15 const PREF_CLIENT_ID = "asanreporter.clientid";
16 const PREF_API_URL = "asanreporter.apiurl";
17 const PREF_AUTH_TOKEN = "asanreporter.authtoken";
18 const PREF_LOG_LEVEL = "asanreporter.loglevel";
20 // Reporter product map
21 const REPORTER_PRODUCT = {
22 firefox: "mozilla-central-asan-nightly",
23 thunderbird: "comm-central-asan-daily",
26 const LOGGER_NAME = "asanreporter";
30 export const AsanReporter = {
32 if (this.initialized) {
35 this.initialized = true;
38 logger = lazy.Log.repository.getLogger(LOGGER_NAME);
40 new lazy.Log.ConsoleAppender(new lazy.Log.BasicFormatter())
43 new lazy.Log.DumpAppender(new lazy.Log.BasicFormatter())
45 logger.level = Services.prefs.getIntPref(
50 logger.info("Starting up...");
52 // Install a handler to observe tab crashes, so we can report those right
53 // after they happen instead of relying on the user to restart the browser.
54 Services.obs.addObserver(this, "ipc:content-shutdown");
59 observe(aSubject, aTopic) {
60 if (aTopic == "ipc:content-shutdown") {
61 aSubject.QueryInterface(Ci.nsIPropertyBag2);
62 if (!aSubject.get("abnormal")) {
70 async function processDirectory() {
71 const asanDumpDir = PathUtils.join(PathUtils.profileDir, "asan");
72 const children = await IOUtils.getChildren(asanDumpDir);
74 const results = children.filter(function (path) {
75 const name = PathUtils.filename(path);
76 return name.startsWith("ff_asan_log.") && !name.includes("submitted");
79 logger.info(`Processing ${results.length} reports...`);
80 for (const result of results) {
82 await submitReport(result);
83 logger.info(`Successfully submitted ${result.path}`);
85 logger.error(`Failed to submit ${result.path}. Reason: ${e}`);
89 logger.info("Done processing reports.");
92 async function submitReport(reportFile) {
93 logger.info("Processing " + reportFile);
94 const data = await IOUtils.read(reportFile);
95 await submitToServer(data);
96 // Mark as submitted only if we successfully submitted it to the server.
97 await IOUtils.move(reportFile, `${reportFile}.submitted`);
100 function submitToServer(data) {
101 return new Promise(function (resolve, reject) {
102 logger.debug("Setting up XHR request");
103 let client = Services.prefs.getStringPref(PREF_CLIENT_ID);
104 let api_url = Services.prefs.getStringPref(PREF_API_URL);
105 let auth_token = Services.prefs.getStringPref(PREF_AUTH_TOKEN, null);
107 let decoder = new TextDecoder();
114 Services.appinfo.version,
115 Services.appinfo.appBuildID,
116 AppConstants.SOURCE_REVISION_URL || "unknown",
119 // Concatenate all relevant information as our server only
120 // has one field available for version information.
121 let product_version = versionArr.join("-");
122 let os = AppConstants.platform;
123 let reporter_product = REPORTER_PRODUCT[AppConstants.MOZ_APP_NAME];
128 rawCrashData: decoder.decode(data),
129 // Hardcode platform as there is no other reasonable platform for ASan
131 product: reporter_product,
135 tool: "asan-nightly-program",
138 var xhr = new XMLHttpRequest({ mozAnon: !auth_token });
139 xhr.open("POST", api_url, true);
140 xhr.setRequestHeader("Content-Type", "application/json");
142 // For internal testing purposes, an auth_token can be specified
144 xhr.setRequestHeader("Authorization", "Token " + auth_token);
146 // Prevent privacy leaks
147 xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS;
150 xhr.onreadystatechange = function () {
151 if (xhr.readyState == 4) {
152 if (xhr.status == "201") {
153 logger.debug("XHR: OK");
157 "XHR: Status: " + xhr.status + " Response: " + xhr.responseText
164 xhr.send(JSON.stringify(reportObj));