summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/BackgroundChannelRegistrar.cpp
blob: 6beb912deecb0e4f27c32e5fca6f778b14425726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "BackgroundChannelRegistrar.h"

#include "HttpBackgroundChannelParent.h"
#include "HttpChannelParent.h"
#include "nsXULAppAPI.h"
#include "mozilla/StaticPtr.h"

namespace {
mozilla::StaticRefPtr<mozilla::net::BackgroundChannelRegistrar> gSingleton;
}

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS(BackgroundChannelRegistrar, nsIBackgroundChannelRegistrar)

BackgroundChannelRegistrar::BackgroundChannelRegistrar() {
  // BackgroundChannelRegistrar is a main-thread-only object.
  // All the operations should be run on main thread.
  // It should be used on chrome process only.
  MOZ_ASSERT(XRE_IsParentProcess());
  MOZ_ASSERT(NS_IsMainThread());
}

BackgroundChannelRegistrar::~BackgroundChannelRegistrar() {
  MOZ_ASSERT(NS_IsMainThread());
}

// static
already_AddRefed<nsIBackgroundChannelRegistrar>
BackgroundChannelRegistrar::GetOrCreate() {
  if (!gSingleton) {
    gSingleton = new BackgroundChannelRegistrar();
  }
  return do_AddRef(gSingleton);
}

// static
void BackgroundChannelRegistrar::Shutdown() {
  MOZ_ASSERT(NS_IsMainThread());
  gSingleton = nullptr;
}

void BackgroundChannelRegistrar::NotifyChannelLinked(
    HttpChannelParent* aChannelParent, HttpBackgroundChannelParent* aBgParent) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aChannelParent);
  MOZ_ASSERT(aBgParent);

  aBgParent->LinkToChannel(aChannelParent);
  aChannelParent->OnBackgroundParentReady(aBgParent);
}

// nsIBackgroundChannelRegistrar
void BackgroundChannelRegistrar::DeleteChannel(uint64_t aKey) {
  MOZ_ASSERT(NS_IsMainThread());

  mChannels.Remove(aKey);
  mBgChannels.Remove(aKey);
}

void BackgroundChannelRegistrar::LinkHttpChannel(uint64_t aKey,
                                                 HttpChannelParent* aChannel) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aChannel);

  RefPtr<HttpBackgroundChannelParent> bgParent;
  bool found = mBgChannels.Remove(aKey, getter_AddRefs(bgParent));

  if (!found) {
    mChannels.InsertOrUpdate(aKey, RefPtr{aChannel});
    return;
  }

  MOZ_ASSERT(bgParent);
  NotifyChannelLinked(aChannel, bgParent);
}

void BackgroundChannelRegistrar::LinkBackgroundChannel(
    uint64_t aKey, HttpBackgroundChannelParent* aBgChannel) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aBgChannel);

  RefPtr<HttpChannelParent> parent;
  bool found = mChannels.Remove(aKey, getter_AddRefs(parent));

  if (!found) {
    mBgChannels.InsertOrUpdate(aKey, RefPtr{aBgChannel});
    return;
  }

  MOZ_ASSERT(parent);
  NotifyChannelLinked(parent, aBgChannel);
}

}  // namespace net
}  // namespace mozilla