tor-browser

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

InputChannelThrottleQueueParent.cpp (3801B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
      2 /* vim:set ts=4 sw=4 sts=4 et cin: */
      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 "InputChannelThrottleQueueParent.h"
      8 #include "mozilla/net/SocketProcessParent.h"
      9 #include "nsIOService.h"
     10 
     11 namespace mozilla {
     12 namespace net {
     13 
     14 NS_IMPL_ADDREF(InputChannelThrottleQueueParent)
     15 NS_INTERFACE_MAP_BEGIN(InputChannelThrottleQueueParent)
     16  NS_INTERFACE_MAP_ENTRY(nsIInputChannelThrottleQueue)
     17  NS_INTERFACE_MAP_ENTRY(nsISupports)
     18  NS_INTERFACE_MAP_ENTRY_CONCRETE(InputChannelThrottleQueueParent)
     19 NS_INTERFACE_MAP_END
     20 
     21 NS_IMETHODIMP_(MozExternalRefCountType)
     22 InputChannelThrottleQueueParent::Release(void) {
     23  MOZ_ASSERT(NS_IsMainThread());
     24  MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
     25 
     26  if (!nsAutoRefCnt::isThreadSafe) {
     27    NS_ASSERT_OWNINGTHREAD(InputChannelThrottleQueueParent);
     28  }
     29 
     30  nsrefcnt count = --mRefCnt;
     31  NS_LOG_RELEASE(this, count, "InputChannelThrottleQueueParent");
     32 
     33  if (count == 0) {
     34    if (!nsAutoRefCnt::isThreadSafe) {
     35      NS_ASSERT_OWNINGTHREAD(InputChannelThrottleQueueParent);
     36    }
     37 
     38    mRefCnt = 1; /* stabilize */
     39    delete (this);
     40    return 0;
     41  }
     42 
     43  // When ref count goes down to 1 (held internally by IPDL), it means that
     44  // we are done with this ThrottleQueue. We should send a delete message
     45  // to delete the InputChannelThrottleQueueChild in socket process.
     46  if (count == 1 && CanSend()) {
     47    (void)Send__delete__(this);
     48    return 1;
     49  }
     50  return count;
     51 }
     52 
     53 mozilla::ipc::IPCResult InputChannelThrottleQueueParent::RecvRecordRead(
     54    const uint32_t& aBytesRead) {
     55  mBytesProcessed += aBytesRead;
     56  return IPC_OK();
     57 }
     58 
     59 NS_IMETHODIMP
     60 InputChannelThrottleQueueParent::RecordRead(uint32_t aBytesRead) {
     61  return NS_ERROR_NOT_IMPLEMENTED;
     62 }
     63 
     64 NS_IMETHODIMP
     65 InputChannelThrottleQueueParent::Available(uint32_t aRemaining,
     66                                           uint32_t* aAvailable) {
     67  return NS_ERROR_NOT_IMPLEMENTED;
     68 }
     69 
     70 NS_IMETHODIMP
     71 InputChannelThrottleQueueParent::Init(uint32_t aMeanBytesPerSecond,
     72                                      uint32_t aMaxBytesPerSecond) {
     73  // Can be called on any thread.
     74  if (aMeanBytesPerSecond == 0 || aMaxBytesPerSecond == 0 ||
     75      aMaxBytesPerSecond < aMeanBytesPerSecond) {
     76    return NS_ERROR_ILLEGAL_VALUE;
     77  }
     78 
     79  mMeanBytesPerSecond = aMeanBytesPerSecond;
     80  mMaxBytesPerSecond = aMaxBytesPerSecond;
     81 
     82  RefPtr<InputChannelThrottleQueueParent> self = this;
     83  gIOService->CallOrWaitForSocketProcess(
     84      [self, meanBytesPerSecond(mMeanBytesPerSecond),
     85       maxBytesPerSecond(mMaxBytesPerSecond)] {
     86        RefPtr<SocketProcessParent> socketParent =
     87            SocketProcessParent::GetSingleton();
     88        (void)socketParent->SendPInputChannelThrottleQueueConstructor(
     89            self, meanBytesPerSecond, maxBytesPerSecond);
     90      });
     91 
     92  return NS_OK;
     93 }
     94 
     95 NS_IMETHODIMP
     96 InputChannelThrottleQueueParent::BytesProcessed(uint64_t* aResult) {
     97  *aResult = mBytesProcessed;
     98  return NS_OK;
     99 }
    100 
    101 NS_IMETHODIMP
    102 InputChannelThrottleQueueParent::WrapStream(nsIInputStream* aInputStream,
    103                                            nsIAsyncInputStream** aResult) {
    104  return NS_ERROR_NOT_IMPLEMENTED;
    105 }
    106 
    107 NS_IMETHODIMP
    108 InputChannelThrottleQueueParent::GetMeanBytesPerSecond(
    109    uint32_t* aMeanBytesPerSecond) {
    110  NS_ENSURE_ARG(aMeanBytesPerSecond);
    111 
    112  *aMeanBytesPerSecond = mMeanBytesPerSecond;
    113  return NS_OK;
    114 }
    115 
    116 NS_IMETHODIMP
    117 InputChannelThrottleQueueParent::GetMaxBytesPerSecond(
    118    uint32_t* aMaxBytesPerSecond) {
    119  NS_ENSURE_ARG(aMaxBytesPerSecond);
    120 
    121  *aMaxBytesPerSecond = mMaxBytesPerSecond;
    122  return NS_OK;
    123 }
    124 
    125 }  // namespace net
    126 }  // namespace mozilla