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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* 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/. */
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "mozilla/net/SocketProcessChild.h"
#include "mozilla/net/SocketProcessParent.h"
#include "nsHttpActivityDistributor.h"
#include "nsHttpHandler.h"
#include "nsCOMPtr.h"
#include "nsIOService.h"
#include "nsNetUtil.h"
#include "nsQueryObject.h"
#include "nsThreadUtils.h"
#include "NullHttpChannel.h"
namespace mozilla {
namespace net {
typedef nsMainThreadPtrHolder<nsIHttpActivityObserver> ObserverHolder;
typedef nsMainThreadPtrHandle<nsIHttpActivityObserver> ObserverHandle;
NS_IMPL_ISUPPORTS(nsHttpActivityDistributor, nsIHttpActivityDistributor,
nsIHttpActivityObserver)
nsHttpActivityDistributor::nsHttpActivityDistributor()
: mLock("nsHttpActivityDistributor.mLock"), mActivated(false) {}
NS_IMETHODIMP
nsHttpActivityDistributor::ObserveActivity(nsISupports* aHttpChannel,
uint32_t aActivityType,
uint32_t aActivitySubtype,
PRTime aTimestamp,
uint64_t aExtraSizeData,
const nsACString& aExtraStringData) {
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
for (size_t i = 0; i < mObservers.Length(); i++) {
Unused << mObservers[i]->ObserveActivity(aHttpChannel, aActivityType,
aActivitySubtype, aTimestamp,
aExtraSizeData, aExtraStringData);
}
return NS_OK;
}
NS_IMETHODIMP
nsHttpActivityDistributor::ObserveActivityWithArgs(
const HttpActivityArgs& aArgs, uint32_t aActivityType,
uint32_t aActivitySubtype, PRTime aTimestamp, uint64_t aExtraSizeData,
const nsACString& aExtraStringData) {
HttpActivityArgs args(aArgs);
nsCString extraStringData(aExtraStringData);
if (XRE_IsSocketProcess()) {
auto task = [args{std::move(args)}, aActivityType, aActivitySubtype,
aTimestamp, aExtraSizeData,
extraStringData{std::move(extraStringData)}]() {
SocketProcessChild::GetSingleton()->SendObserveHttpActivity(
args, aActivityType, aActivitySubtype, aTimestamp, aExtraSizeData,
extraStringData);
};
if (!NS_IsMainThread()) {
return NS_DispatchToMainThread(NS_NewRunnableFunction(
"net::nsHttpActivityDistributor::ObserveActivityWithArgs", task));
}
task();
return NS_OK;
}
MOZ_ASSERT(XRE_IsParentProcess());
RefPtr<nsHttpActivityDistributor> self = this;
auto task = [args{std::move(args)}, aActivityType, aActivitySubtype,
aTimestamp, aExtraSizeData,
extraStringData{std::move(extraStringData)},
self{std::move(self)}]() {
if (args.type() == HttpActivityArgs::Tuint64_t) {
nsWeakPtr weakPtr = gHttpHandler->GetWeakHttpChannel(args.get_uint64_t());
if (nsCOMPtr<nsIHttpChannel> channel = do_QueryReferent(weakPtr)) {
Unused << self->ObserveActivity(channel, aActivityType,
aActivitySubtype, aTimestamp,
aExtraSizeData, extraStringData);
}
} else if (args.type() == HttpActivityArgs::THttpActivity) {
nsCOMPtr<nsIURI> uri;
nsAutoCString portStr(""_ns);
int32_t port = args.get_HttpActivity().port();
bool endToEndSSL = args.get_HttpActivity().endToEndSSL();
if (port != -1 &&
((endToEndSSL && port != 443) || (!endToEndSSL && port != 80))) {
portStr.AppendInt(port);
}
nsresult rv = NS_NewURI(getter_AddRefs(uri),
(endToEndSSL ? "https://"_ns : "http://"_ns) +
args.get_HttpActivity().host() + portStr);
if (NS_FAILED(rv)) {
return;
}
RefPtr<NullHttpChannel> channel = new NullHttpChannel();
rv = channel->Init(uri, 0, nullptr, 0, nullptr);
MOZ_ASSERT(NS_SUCCEEDED(rv));
Unused << self->ObserveActivity(
nsCOMPtr<nsISupports>(do_QueryObject(channel)), aActivityType,
aActivitySubtype, aTimestamp, aExtraSizeData, extraStringData);
}
};
if (!NS_IsMainThread()) {
return NS_DispatchToMainThread(NS_NewRunnableFunction(
"net::nsHttpActivityDistributor::ObserveActivityWithArgs", task));
}
task();
return NS_OK;
}
NS_IMETHODIMP
nsHttpActivityDistributor::GetIsActive(bool* isActive) {
NS_ENSURE_ARG_POINTER(isActive);
MutexAutoLock lock(mLock);
if (XRE_IsSocketProcess()) {
*isActive = mActivated;
return NS_OK;
}
*isActive = !!mObservers.Length();
return NS_OK;
}
NS_IMETHODIMP nsHttpActivityDistributor::SetIsActive(bool aActived) {
MOZ_RELEASE_ASSERT(XRE_IsSocketProcess());
mActivated = aActived;
return NS_OK;
}
NS_IMETHODIMP
nsHttpActivityDistributor::AddObserver(nsIHttpActivityObserver* aObserver) {
MOZ_ASSERT(XRE_IsParentProcess());
ObserverHandle observer(
new ObserverHolder("nsIHttpActivityObserver", aObserver));
bool wasEmpty = false;
{
MutexAutoLock lock(mLock);
wasEmpty = mObservers.IsEmpty();
// XXX(Bug 1631371) Check if this should use a fallible operation as it
// pretended earlier.
mObservers.AppendElement(observer);
}
if (nsIOService::UseSocketProcess() && wasEmpty) {
auto task = []() {
SocketProcessParent* parent = SocketProcessParent::GetSingleton();
if (parent && parent->CanSend()) {
Unused << parent->SendOnHttpActivityDistributorActivated(true);
}
};
gIOService->CallOrWaitForSocketProcess(task);
}
return NS_OK;
}
NS_IMETHODIMP
nsHttpActivityDistributor::RemoveObserver(nsIHttpActivityObserver* aObserver) {
MOZ_ASSERT(XRE_IsParentProcess());
ObserverHandle observer(
new ObserverHolder("nsIHttpActivityObserver", aObserver));
bool isEmpty = false;
{
MutexAutoLock lock(mLock);
if (!mObservers.RemoveElement(observer)) return NS_ERROR_FAILURE;
isEmpty = mObservers.IsEmpty();
}
if (nsIOService::UseSocketProcess() && isEmpty) {
auto task = []() {
SocketProcessParent* parent = SocketProcessParent::GetSingleton();
if (parent && parent->CanSend()) {
Unused << parent->SendOnHttpActivityDistributorActivated(false);
}
};
gIOService->CallOrWaitForSocketProcess(task);
}
return NS_OK;
}
} // namespace net
} // namespace mozilla
|