summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/nsHttpActivityDistributor.cpp
blob: b432bb5ea85a659d3af4080237947207a83a40b5 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* 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 {

using ObserverHolder = nsMainThreadPtrHolder<nsIHttpActivityObserver>;
using ObserverHandle = nsMainThreadPtrHandle<nsIHttpActivityObserver>;

NS_IMPL_ISUPPORTS(nsHttpActivityDistributor, nsIHttpActivityDistributor,
                  nsIHttpActivityObserver)

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::ObserveConnectionActivity(
    const nsACString& aHost, int32_t aPort, bool aSSL, bool aHasECH,
    bool aIsHttp3, uint32_t aActivityType, uint32_t aActivitySubtype,
    PRTime aTimestamp, const nsACString& aExtraStringData) {
  MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());

  for (size_t i = 0; i < mObservers.Length(); i++) {
    Unused << mObservers[i]->ObserveConnectionActivity(
        aHost, aPort, aSSL, aHasECH, aIsHttp3, aActivityType, aActivitySubtype,
        aTimestamp, 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(
          static_cast<nsIChannel*>(channel), aActivityType, aActivitySubtype,
          aTimestamp, aExtraSizeData, extraStringData);
    } else if (args.type() == HttpActivityArgs::THttpConnectionActivity) {
      const HttpConnectionActivity& activity =
          args.get_HttpConnectionActivity();
      Unused << self->ObserveConnectionActivity(
          activity.host(), activity.port(), activity.ssl(), activity.hasECH(),
          activity.isHttp3(), aActivityType, aActivitySubtype, aTimestamp,
          activity.connInfoKey());
    }
  };

  if (!NS_IsMainThread()) {
    return NS_DispatchToMainThread(NS_NewRunnableFunction(
        "net::nsHttpActivityDistributor::ObserveActivityWithArgs", task));
  }

  task();
  return NS_OK;
}

bool nsHttpActivityDistributor::Activated() { return mActivated; }

bool nsHttpActivityDistributor::ObserveProxyResponseEnabled() {
  return mObserveProxyResponse;
}

bool nsHttpActivityDistributor::ObserveConnectionEnabled() {
  return mObserveConnection;
}

NS_IMETHODIMP
nsHttpActivityDistributor::GetIsActive(bool* isActive) {
  NS_ENSURE_ARG_POINTER(isActive);
  if (XRE_IsSocketProcess()) {
    *isActive = mActivated;
    return NS_OK;
  }

  MutexAutoLock lock(mLock);
  *isActive = mActivated = !!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());
  if (!NS_IsMainThread()) {
    // We only support calling this from the main thread.
    return NS_ERROR_FAILURE;
  }

  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 (wasEmpty) {
    mActivated = true;
    if (nsIOService::UseSocketProcess()) {
      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());
  if (!NS_IsMainThread()) {
    // We only support calling this from the main thread.
    return NS_ERROR_FAILURE;
  }

  ObserverHandle observer(
      new ObserverHolder("nsIHttpActivityObserver", aObserver));

  {
    MutexAutoLock lock(mLock);
    if (!mObservers.RemoveElement(observer)) {
      return NS_ERROR_FAILURE;
    }
    mActivated = mObservers.IsEmpty();
  }

  if (nsIOService::UseSocketProcess() && !mActivated) {
    auto task = []() {
      SocketProcessParent* parent = SocketProcessParent::GetSingleton();
      if (parent && parent->CanSend()) {
        Unused << parent->SendOnHttpActivityDistributorActivated(false);
      }
    };
    gIOService->CallOrWaitForSocketProcess(task);
  }
  return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::GetObserveProxyResponse(
    bool* aObserveProxyResponse) {
  NS_ENSURE_ARG_POINTER(aObserveProxyResponse);
  bool result = mObserveProxyResponse;
  *aObserveProxyResponse = result;
  return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::SetObserveProxyResponse(bool aObserveProxyResponse) {
  if (!NS_IsMainThread()) {
    // We only support calling this from the main thread.
    return NS_ERROR_FAILURE;
  }

  mObserveProxyResponse = aObserveProxyResponse;
  if (nsIOService::UseSocketProcess()) {
    auto task = [aObserveProxyResponse]() {
      SocketProcessParent* parent = SocketProcessParent::GetSingleton();
      if (parent && parent->CanSend()) {
        Unused << parent->SendOnHttpActivityDistributorObserveProxyResponse(
            aObserveProxyResponse);
      }
    };
    gIOService->CallOrWaitForSocketProcess(task);
  }
  return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::GetObserveConnection(bool* aObserveConnection) {
  NS_ENSURE_ARG_POINTER(aObserveConnection);

  *aObserveConnection = mObserveConnection;
  return NS_OK;
}

NS_IMETHODIMP
nsHttpActivityDistributor::SetObserveConnection(bool aObserveConnection) {
  if (!NS_IsMainThread()) {
    // We only support calling this from the main thread.
    return NS_ERROR_FAILURE;
  }

  mObserveConnection = aObserveConnection;
  if (nsIOService::UseSocketProcess()) {
    auto task = [aObserveConnection]() {
      SocketProcessParent* parent = SocketProcessParent::GetSingleton();
      if (parent && parent->CanSend()) {
        Unused << parent->SendOnHttpActivityDistributorObserveConnection(
            aObserveConnection);
      }
    };
    gIOService->CallOrWaitForSocketProcess(task);
  }
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla