tor-browser

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

SimpleChannel.cpp (3129B)


      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 "SimpleChannel.h"
      7 
      8 #include "nsBaseChannel.h"
      9 #include "nsIChannel.h"
     10 #include "nsIChildChannel.h"
     11 #include "nsICancelable.h"
     12 #include "nsIInputStream.h"
     13 #include "nsIRequest.h"
     14 #include "nsISupportsImpl.h"
     15 #include "nsNetUtil.h"
     16 
     17 #include "mozilla/Try.h"
     18 #include "mozilla/dom/ContentChild.h"
     19 #include "mozilla/net/NeckoChild.h"
     20 
     21 namespace mozilla {
     22 namespace net {
     23 
     24 NS_IMPL_ISUPPORTS_INHERITED(SimpleChannel, nsBaseChannel, nsIChildChannel)
     25 
     26 SimpleChannel::SimpleChannel(UniquePtr<SimpleChannelCallbacks>&& aCallbacks)
     27    : mCallbacks(std::move(aCallbacks)) {
     28  EnableSynthesizedProgressEvents(true);
     29 }
     30 
     31 nsresult SimpleChannel::OpenContentStream(bool async,
     32                                          nsIInputStream** streamOut,
     33                                          nsIChannel** channel) {
     34  NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
     35 
     36  nsCOMPtr<nsIInputStream> stream =
     37      MOZ_TRY(mCallbacks->OpenContentStream(async, this));
     38  MOZ_ASSERT(stream);
     39 
     40  mCallbacks = nullptr;
     41 
     42  *channel = nullptr;
     43  stream.forget(streamOut);
     44  return NS_OK;
     45 }
     46 
     47 nsresult SimpleChannel::BeginAsyncRead(nsIStreamListener* listener,
     48                                       nsIRequest** request,
     49                                       nsICancelable** cancelableRequest) {
     50  NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
     51 
     52  RequestOrReason res = mCallbacks->StartAsyncRead(listener, this);
     53 
     54  if (res.isErr()) {
     55    return res.propagateErr();
     56  }
     57 
     58  mCallbacks = nullptr;
     59 
     60  RequestOrCancelable value = res.unwrap();
     61 
     62  if (value.is<NotNullRequest>()) {
     63    nsCOMPtr<nsIRequest> req = value.as<NotNullRequest>().get();
     64    req.forget(request);
     65  } else if (value.is<NotNullCancelable>()) {
     66    nsCOMPtr<nsICancelable> cancelable = value.as<NotNullCancelable>().get();
     67    cancelable.forget(cancelableRequest);
     68  } else {
     69    MOZ_ASSERT_UNREACHABLE(
     70        "StartAsyncRead didn't return a NotNull nsIRequest or nsICancelable.");
     71    return NS_ERROR_UNEXPECTED;
     72  }
     73 
     74  return NS_OK;
     75 }
     76 
     77 NS_IMETHODIMP
     78 SimpleChannel::ConnectParent(uint32_t aId) {
     79  if (!IsNeckoChild()) {
     80    return NS_ERROR_NOT_IMPLEMENTED;
     81  }
     82 
     83  mozilla::dom::ContentChild* cc =
     84      static_cast<mozilla::dom::ContentChild*>(gNeckoChild->Manager());
     85  if (cc->IsShuttingDown()) {
     86    return NS_ERROR_FAILURE;
     87  }
     88 
     89  gNeckoChild->SendConnectBaseChannel(aId);
     90  return NS_OK;
     91 }
     92 
     93 NS_IMETHODIMP
     94 SimpleChannel::CompleteRedirectSetup(nsIStreamListener* aListener) {
     95  return AsyncOpen(aListener);
     96 }
     97 
     98 already_AddRefed<nsIChannel> NS_NewSimpleChannelInternal(
     99    nsIURI* aURI, nsILoadInfo* aLoadInfo,
    100    UniquePtr<SimpleChannelCallbacks>&& aCallbacks) {
    101  RefPtr<SimpleChannel> chan = new SimpleChannel(std::move(aCallbacks));
    102 
    103  chan->SetURI(aURI);
    104 
    105  MOZ_ALWAYS_SUCCEEDS(chan->SetLoadInfo(aLoadInfo));
    106 
    107  return chan.forget();
    108 }
    109 
    110 }  // namespace net
    111 }  // namespace mozilla