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"
20 struct StringWriteFunc
: public JSONWriteFunc
{
22 explicit StringWriteFunc(nsCString
& aCString
) : mCString(aCString
) {}
23 void Write(const Span
<const char>& aStr
) override
{ mCString
.Append(aStr
); }
27 bool CrashReport::Deliver(nsIPrincipal
* aPrincipal
, bool aIsOOM
) {
28 MOZ_ASSERT(aPrincipal
);
30 nsAutoCString endpoint_url
;
31 ReportingHeader::GetEndpointForReport(u
"default"_ns
, aPrincipal
,
33 if (endpoint_url
.IsEmpty()) {
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
;
49 data
.mEndpointURL
= endpoint_url
;
52 JSONWriter writer
{MakeUnique
<StringWriteFunc
>(body
)};
56 writer
.StringProperty("reason", "oom");
60 data
.mReportBodyJSON
= body
;
62 ReportDeliver::Fetch(data
);
67 } // namespace mozilla