tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestReportGenerator.cpp (2198B)


      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/TestReportGenerator.h"
      8 
      9 #include "mozilla/dom/ReportingBinding.h"
     10 #include "mozilla/dom/ReportingUtils.h"
     11 #include "mozilla/dom/TestReportBody.h"
     12 
     13 namespace mozilla::dom {
     14 
     15 /* static */
     16 already_AddRefed<Promise> TestReportGenerator::GenerateReport(
     17    const GlobalObject& aGlobal, const GenerateTestReportParameters& aParams,
     18    ErrorResult& aRv) {
     19  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
     20  MOZ_ASSERT(global);
     21 
     22  RefPtr<Promise> promise = Promise::Create(global, aRv);
     23  if (NS_WARN_IF(aRv.Failed())) {
     24    return nullptr;
     25  }
     26  MOZ_ASSERT(promise);
     27 
     28  nsString messageBody = aParams.mMessage;
     29  if (messageBody.IsEmpty()) {
     30    promise->MaybeRejectWithNotSupportedError(
     31        "Report must have a message string");
     32    return promise.forget();
     33  }
     34 
     35  nsString reportGroup = aParams.mGroup;
     36 
     37  nsPIDOMWindowInner* window = global->GetAsInnerWindow();
     38  if (NS_WARN_IF(!window)) {
     39    promise->MaybeRejectWithNotSupportedError(
     40        "Failed to convert global object to window");
     41    return promise.forget();
     42  }
     43 
     44  RefPtr<TestReportBody> reportBody = new TestReportBody(global, messageBody);
     45 
     46  nsCOMPtr<nsIURI> docURI = window->GetDocumentURI();
     47  nsAutoCString spec;
     48  if (!docURI || NS_FAILED(docURI->GetSpec(spec))) {
     49    promise->MaybeRejectWithNotSupportedError(
     50        "Failed to retrieve active document's URI from window");
     51    return promise.forget();
     52  }
     53 
     54  NS_ConvertUTF8toUTF16 docURIString(spec);
     55 
     56  ReportingUtils::Report(global, nsGkAtoms::test, reportGroup, docURIString,
     57                         reportBody);
     58 
     59  AutoJSAPI jsapi;
     60  if (!jsapi.Init(global)) {
     61    promise->MaybeRejectWithNotSupportedError(
     62        "Failed to initialize JS context");
     63    return promise.forget();
     64  }
     65 
     66  promise->MaybeResolveWithUndefined();
     67  return promise.forget();
     68 }
     69 
     70 }  // namespace mozilla::dom