tor-browser

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

GeckoViewExternalAppService.cpp (3332B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
      5 
      6 #include "GeckoViewExternalAppService.h"
      7 
      8 #include "mozilla/dom/BrowsingContext.h"
      9 #include "mozilla/dom/CanonicalBrowsingContext.h"
     10 #include "mozilla/dom/WindowGlobalParent.h"
     11 #include "nsIChannel.h"
     12 
     13 #include "mozilla/widget/EventDispatcher.h"
     14 #include "mozilla/widget/nsWindow.h"
     15 #include "GeckoViewStreamListener.h"
     16 
     17 #include "JavaBuiltins.h"
     18 
     19 class StreamListener final : public mozilla::GeckoViewStreamListener {
     20 public:
     21  explicit StreamListener(nsWindow* aWindow)
     22      : GeckoViewStreamListener(), mWindow(aWindow) {
     23    MOZ_ASSERT(aWindow);
     24  }
     25 
     26  void SendWebResponse(mozilla::java::WebResponse::Param aResponse) {
     27    mWindow->PassExternalResponse(aResponse);
     28  }
     29 
     30  void CompleteWithError(nsresult aStatus, nsIChannel* aChannel) {
     31    // Currently we don't do anything about errors here
     32  }
     33 
     34  virtual ~StreamListener() {}
     35 
     36 private:
     37  RefPtr<nsWindow> mWindow;
     38 };
     39 
     40 mozilla::StaticRefPtr<GeckoViewExternalAppService>
     41    GeckoViewExternalAppService::sService;
     42 
     43 /* static */
     44 already_AddRefed<GeckoViewExternalAppService>
     45 GeckoViewExternalAppService::GetSingleton() {
     46  if (!sService) {
     47    sService = new GeckoViewExternalAppService();
     48  }
     49  RefPtr<GeckoViewExternalAppService> service = sService;
     50  return service.forget();
     51 }
     52 
     53 GeckoViewExternalAppService::GeckoViewExternalAppService() {}
     54 
     55 NS_IMPL_ISUPPORTS(GeckoViewExternalAppService, nsIExternalHelperAppService);
     56 
     57 NS_IMETHODIMP GeckoViewExternalAppService::DoContent(
     58    const nsACString& aMimeContentType, nsIChannel* aChannel,
     59    nsIInterfaceRequestor* aContentContext, bool aForceSave,
     60    nsIInterfaceRequestor* aWindowContext,
     61    nsIStreamListener** aStreamListener) {
     62  return NS_ERROR_FAILURE;
     63 }
     64 
     65 NS_IMETHODIMP GeckoViewExternalAppService::CreateListener(
     66    const nsACString& aMimeContentType, nsIChannel* aChannel,
     67    mozilla::dom::BrowsingContext* aContentContext, bool aForceSave,
     68    nsIInterfaceRequestor* aWindowContext,
     69    nsIStreamListener** aStreamListener) {
     70  using namespace mozilla;
     71  using namespace mozilla::dom;
     72  MOZ_ASSERT(XRE_IsParentProcess());
     73  NS_ENSURE_ARG_POINTER(aChannel);
     74 
     75  nsCOMPtr<nsIWidget> widget =
     76      aContentContext->Canonical()->GetParentProcessWidgetContaining();
     77  if (!widget) {
     78    return NS_ERROR_ABORT;
     79  }
     80 
     81  RefPtr<nsWindow> window = nsWindow::From(widget);
     82  if (NS_WARN_IF(!window) || NS_WARN_IF(window->Destroyed())) {
     83    return NS_ERROR_ABORT;
     84  }
     85 
     86  RefPtr<StreamListener> listener = new StreamListener(window);
     87 
     88  nsresult rv;
     89  rv = aChannel->SetNotificationCallbacks(listener);
     90  NS_ENSURE_SUCCESS(rv, rv);
     91 
     92  listener.forget(aStreamListener);
     93  return NS_OK;
     94 }
     95 
     96 NS_IMETHODIMP GeckoViewExternalAppService::ApplyDecodingForExtension(
     97    const nsACString& aExtension, const nsACString& aEncodingType,
     98    bool* aApplyDecoding) {
     99  // This currently doesn't matter, because we never read the stream.
    100  *aApplyDecoding = true;
    101  return NS_OK;
    102 }
    103 
    104 NS_IMETHODIMP GeckoViewExternalAppService::GetPreferredDownloadsDirectory(
    105    nsIFile** _retval) {
    106  MOZ_ASSERT(false);
    107  return nsresult::NS_ERROR_NOT_IMPLEMENTED;
    108 }