tor-browser

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

BackgroundChannelRegistrar.cpp (2847B)


      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 "BackgroundChannelRegistrar.h"
      8 
      9 #include "mozilla/ClearOnShutdown.h"
     10 #include "mozilla/StaticPtr.h"
     11 #include "HttpBackgroundChannelParent.h"
     12 #include "HttpChannelParent.h"
     13 #include "nsXULAppAPI.h"
     14 
     15 namespace {
     16 mozilla::StaticRefPtr<mozilla::net::BackgroundChannelRegistrar> gSingleton;
     17 }
     18 
     19 namespace mozilla {
     20 namespace net {
     21 
     22 NS_IMPL_ISUPPORTS(BackgroundChannelRegistrar, nsIBackgroundChannelRegistrar)
     23 
     24 BackgroundChannelRegistrar::BackgroundChannelRegistrar() {
     25  // BackgroundChannelRegistrar is a main-thread-only object.
     26  // All the operations should be run on main thread.
     27  // It should be used on chrome process only.
     28  MOZ_ASSERT(XRE_IsParentProcess());
     29  MOZ_ASSERT(NS_IsMainThread());
     30 }
     31 
     32 BackgroundChannelRegistrar::~BackgroundChannelRegistrar() {
     33  MOZ_ASSERT(NS_IsMainThread());
     34 }
     35 
     36 // static
     37 already_AddRefed<nsIBackgroundChannelRegistrar>
     38 BackgroundChannelRegistrar::GetOrCreate() {
     39  if (!gSingleton) {
     40    gSingleton = new BackgroundChannelRegistrar();
     41    ClearOnShutdown(&gSingleton);
     42  }
     43  return do_AddRef(gSingleton);
     44 }
     45 
     46 void BackgroundChannelRegistrar::NotifyChannelLinked(
     47    HttpChannelParent* aChannelParent, HttpBackgroundChannelParent* aBgParent) {
     48  MOZ_ASSERT(NS_IsMainThread());
     49  MOZ_ASSERT(aChannelParent);
     50  MOZ_ASSERT(aBgParent);
     51 
     52  aBgParent->LinkToChannel(aChannelParent);
     53  aChannelParent->OnBackgroundParentReady(aBgParent);
     54 }
     55 
     56 // nsIBackgroundChannelRegistrar
     57 void BackgroundChannelRegistrar::DeleteChannel(uint64_t aKey) {
     58  MOZ_ASSERT(NS_IsMainThread());
     59 
     60  mChannels.Remove(aKey);
     61  mBgChannels.Remove(aKey);
     62 }
     63 
     64 void BackgroundChannelRegistrar::LinkHttpChannel(uint64_t aKey,
     65                                                 HttpChannelParent* aChannel) {
     66  MOZ_ASSERT(NS_IsMainThread());
     67  MOZ_ASSERT(aChannel);
     68 
     69  RefPtr<HttpBackgroundChannelParent> bgParent;
     70  bool found = mBgChannels.Remove(aKey, getter_AddRefs(bgParent));
     71 
     72  if (!found) {
     73    mChannels.InsertOrUpdate(aKey, RefPtr{aChannel});
     74    return;
     75  }
     76 
     77  MOZ_ASSERT(bgParent);
     78  NotifyChannelLinked(aChannel, bgParent);
     79 }
     80 
     81 void BackgroundChannelRegistrar::LinkBackgroundChannel(
     82    uint64_t aKey, HttpBackgroundChannelParent* aBgChannel) {
     83  MOZ_ASSERT(NS_IsMainThread());
     84  MOZ_ASSERT(aBgChannel);
     85 
     86  RefPtr<HttpChannelParent> parent;
     87  bool found = mChannels.Remove(aKey, getter_AddRefs(parent));
     88 
     89  if (!found) {
     90    mBgChannels.InsertOrUpdate(aKey, RefPtr{aBgChannel});
     91    return;
     92  }
     93 
     94  MOZ_ASSERT(parent);
     95  NotifyChannelLinked(parent, aBgChannel);
     96 }
     97 
     98 }  // namespace net
     99 }  // namespace mozilla