Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / network / Connection.cpp
blob217a937cf6c547f18016b05184d6dd122ce885ec
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/. */
7 #include "Connection.h"
8 #include "ConnectionMainThread.h"
9 #include "ConnectionWorker.h"
10 #include "Constants.h"
11 #include "mozilla/Telemetry.h"
12 #include "mozilla/dom/WorkerPrivate.h"
14 /**
15 * We have to use macros here because our leak analysis tool things we are
16 * leaking strings when we have |static const nsString|. Sad :(
18 #define CHANGE_EVENT_NAME u"typechange"_ns
20 namespace mozilla::dom::network {
22 // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
23 // we're not the only class with that name.
24 NS_IMPL_ISUPPORTS_INHERITED0(dom::network::Connection, DOMEventTargetHelper)
26 Connection::Connection(nsPIDOMWindowInner* aWindow,
27 bool aShouldResistFingerprinting)
28 : DOMEventTargetHelper(aWindow),
29 mShouldResistFingerprinting(aShouldResistFingerprinting),
30 mType(static_cast<ConnectionType>(kDefaultType)),
31 mIsWifi(kDefaultIsWifi),
32 mDHCPGateway(kDefaultDHCPGateway),
33 mBeenShutDown(false) {
34 Telemetry::Accumulate(Telemetry::NETWORK_CONNECTION_COUNT, 1);
37 Connection::~Connection() {
38 NS_ASSERT_OWNINGTHREAD(Connection);
39 MOZ_ASSERT(mBeenShutDown);
42 void Connection::Shutdown() {
43 NS_ASSERT_OWNINGTHREAD(Connection);
45 if (mBeenShutDown) {
46 return;
49 mBeenShutDown = true;
50 ShutdownInternal();
53 JSObject* Connection::WrapObject(JSContext* aCx,
54 JS::Handle<JSObject*> aGivenProto) {
55 return NetworkInformation_Binding::Wrap(aCx, this, aGivenProto);
58 void Connection::Update(ConnectionType aType, bool aIsWifi,
59 uint32_t aDHCPGateway, bool aNotify) {
60 NS_ASSERT_OWNINGTHREAD(Connection);
62 ConnectionType previousType = mType;
64 mType = aType;
65 mIsWifi = aIsWifi;
66 mDHCPGateway = aDHCPGateway;
68 if (aNotify && previousType != aType && !mShouldResistFingerprinting) {
69 DispatchTrustedEvent(CHANGE_EVENT_NAME);
73 /* static */
74 Connection* Connection::CreateForWindow(nsPIDOMWindowInner* aWindow,
75 bool aShouldResistFingerprinting) {
76 MOZ_ASSERT(aWindow);
77 return new ConnectionMainThread(aWindow, aShouldResistFingerprinting);
80 /* static */
81 already_AddRefed<Connection> Connection::CreateForWorker(
82 WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
83 MOZ_ASSERT(aWorkerPrivate);
84 aWorkerPrivate->AssertIsOnWorkerThread();
85 return ConnectionWorker::Create(aWorkerPrivate, aRv);
88 } // namespace mozilla::dom::network