summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/content/crashes.js
blob: f1d3f39d9b75999d1a03df36afd2efdd57fd71d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { classes: Cc, utils: Cu, interfaces: Ci } = Components;

var reportURL;

Cu.import("resource://gre/modules/CrashReports.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/osfile.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "CrashSubmit",
  "resource://gre/modules/CrashSubmit.jsm");

const buildID = Services.appinfo.appBuildID;

function submitPendingReport(event) {
  let link = event.target;
  let id = link.firstChild.textContent;
  link.className = "submitting";
  CrashSubmit.submit(id, { noThrottle: true }).then(
    (remoteCrashID) => {
      link.className = "";
      // Reset the link to point at our new crash report. This way, if the
      // user clicks "Back", the link will be correct.
      link.firstChild.textContent = remoteCrashID;
      link.setAttribute("id", remoteCrashID);
      link.removeEventListener("click", submitPendingReport, true);

      if (reportURL) {
        link.setAttribute("href", reportURL + remoteCrashID);
        // redirect the user to their brand new crash report
        window.location.href = reportURL + remoteCrashID;
      }
    },
    () => {
      // XXX: do something more useful here
      link.className = "";

      // Dispatch an event, useful for testing
      let event = document.createEvent("Events");
      event.initEvent("CrashSubmitFailed", true, false);
      document.dispatchEvent(event);
    });
  event.preventDefault();
  return false;
}

function populateReportList() {

  Services.telemetry.getHistogramById("ABOUTCRASHES_OPENED_COUNT").add(1);

  var prefService = Cc["@mozilla.org/preferences-service;1"].
                    getService(Ci.nsIPrefBranch);

  try {
    reportURL = prefService.getCharPref("breakpad.reportURL");
    // Ignore any non http/https urls
    if (!/^https?:/i.test(reportURL))
      reportURL = null;
  }
  catch (e) { }
  if (!reportURL) {
    document.getElementById("clear-reports").style.display = "none";
    document.getElementById("reportList").style.display = "none";
    document.getElementById("noConfig").style.display = "block";
    return;
  }
  let reports = CrashReports.getReports();

  if (reports.length == 0) {
    document.getElementById("clear-reports").style.display = "none";
    document.getElementById("reportList").style.display = "none";
    document.getElementById("noReports").style.display = "block";
    return;
  }

  const locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
                 .getService(Ci.nsIXULChromeRegistry)
                 .getSelectedLocale("global", true);
  var dateFormatter = new Intl.DateTimeFormat(locale, { year: '2-digit',
                                                        month: 'numeric',
                                                        day: 'numeric' });
  var timeFormatter = new Intl.DateTimeFormat(locale, { hour: 'numeric',
                                                        minute: 'numeric' });
  var ios = Cc["@mozilla.org/network/io-service;1"].
            getService(Ci.nsIIOService);
  var reportURI = ios.newURI(reportURL, null, null);
  // resolving this URI relative to /report/index
  var aboutThrottling = ios.newURI("../../about/throttling", null, reportURI);

  for (var i = 0; i < reports.length; i++) {
    var row = document.createElement("tr");
    var cell = document.createElement("td");
    row.appendChild(cell);
    var link = document.createElement("a");
    if (reports[i].pending) {
      link.setAttribute("href", aboutThrottling.spec);
      link.addEventListener("click", submitPendingReport, true);
    }
    else {
      link.setAttribute("href", reportURL + reports[i].id);
    }
    link.setAttribute("id", reports[i].id);
    link.classList.add("crashReport");
    link.appendChild(document.createTextNode(reports[i].id));
    cell.appendChild(link);

    var date = new Date(reports[i].date);
    cell = document.createElement("td");
    cell.appendChild(document.createTextNode(dateFormatter.format(date)));
    row.appendChild(cell);
    cell = document.createElement("td");
    cell.appendChild(document.createTextNode(timeFormatter.format(date)));
    row.appendChild(cell);
    if (reports[i].pending) {
      document.getElementById("unsubmitted").appendChild(row);
    } else {
      document.getElementById("submitted").appendChild(row);
    }
  }
}

var clearReports = Task.async(function*() {
  let bundle = Services.strings.createBundle("chrome://global/locale/crashes.properties");

  if (!Services.
         prompt.confirm(window,
                        bundle.GetStringFromName("deleteconfirm.title"),
                        bundle.GetStringFromName("deleteconfirm.description"))) {
    return;
  }

  let cleanupFolder = Task.async(function*(path, filter) {
    let iterator = new OS.File.DirectoryIterator(path);
    try {
      yield iterator.forEach(Task.async(function*(aEntry) {
        if (!filter || (yield filter(aEntry))) {
          yield OS.File.remove(aEntry.path);
        }
      }));
    } catch (e) {
      if (!(e instanceof OS.File.Error) || !e.becauseNoSuchFile) {
        throw e;
      }
    } finally {
      iterator.close();
    }
  });

  yield cleanupFolder(CrashReports.submittedDir.path, function*(aEntry) {
    return aEntry.name.startsWith("bp-") && aEntry.name.endsWith(".txt");
  });

  let oneYearAgo = Date.now() - 31586000000;
  yield cleanupFolder(CrashReports.reportsDir.path, function*(aEntry) {
    if (!aEntry.name.startsWith("InstallTime") ||
        aEntry.name == "InstallTime" + buildID) {
      return false;
    }

    let date = aEntry.winLastWriteDate;
    if (!date) {
      let stat = yield OS.File.stat(aEntry.path);
      date = stat.lastModificationDate;
    }

    return (date < oneYearAgo);
  });

  yield cleanupFolder(CrashReports.pendingDir.path);

  document.getElementById("clear-reports").style.display = "none";
  document.getElementById("reportList").style.display = "none";
  document.getElementById("noReports").style.display = "block";
});