Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / reporting / CrashReport.cpp
blobd19ad42a6473f25fea2500cab58c486385041922
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/CrashReport.h"
9 #include "mozilla/dom/Navigator.h"
10 #include "mozilla/dom/ReportingHeader.h"
11 #include "mozilla/dom/ReportDeliver.h"
12 #include "mozilla/JSONWriter.h"
13 #include "nsIPrincipal.h"
14 #include "nsIURIMutator.h"
15 #include "nsString.h"
17 namespace mozilla {
18 namespace dom {
20 struct StringWriteFunc : public JSONWriteFunc {
21 nsCString& mCString;
22 explicit StringWriteFunc(nsCString& aCString) : mCString(aCString) {}
23 void Write(const Span<const char>& aStr) override { mCString.Append(aStr); }
26 /* static */
27 bool CrashReport::Deliver(nsIPrincipal* aPrincipal, bool aIsOOM) {
28 MOZ_ASSERT(aPrincipal);
30 nsAutoCString endpoint_url;
31 ReportingHeader::GetEndpointForReport(u"default"_ns, aPrincipal,
32 endpoint_url);
33 if (endpoint_url.IsEmpty()) {
34 return false;
37 nsCString safe_origin_spec;
38 aPrincipal->GetExposableSpec(safe_origin_spec);
40 ReportDeliver::ReportData data;
41 data.mType = u"crash"_ns;
42 data.mGroupName = u"default"_ns;
43 CopyUTF8toUTF16(safe_origin_spec, data.mURL);
44 data.mCreationTime = TimeStamp::Now();
46 Navigator::GetUserAgent(nullptr, aPrincipal, false, data.mUserAgent);
47 data.mPrincipal = aPrincipal;
48 data.mFailures = 0;
49 data.mEndpointURL = endpoint_url;
51 nsCString body;
52 JSONWriter writer{MakeUnique<StringWriteFunc>(body)};
54 writer.Start();
55 if (aIsOOM) {
56 writer.StringProperty("reason", "oom");
58 writer.End();
60 data.mReportBodyJSON = body;
62 ReportDeliver::Fetch(data);
63 return true;
66 } // namespace dom
67 } // namespace mozilla