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 "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"
13 
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 :(
17  */
18 #define CHANGE_EVENT_NAME u"typechange"_ns
19 
20 namespace mozilla::dom::network {
21 
22 // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
23 // we're not the only class with that name.
NS_IMPL_ISUPPORTS_INHERITED0(dom::network::Connection,DOMEventTargetHelper)24 NS_IMPL_ISUPPORTS_INHERITED0(dom::network::Connection, DOMEventTargetHelper)
25 
26 Connection::Connection(nsPIDOMWindowInner* aWindow)
27     : DOMEventTargetHelper(aWindow),
28       mType(static_cast<ConnectionType>(kDefaultType)),
29       mIsWifi(kDefaultIsWifi),
30       mDHCPGateway(kDefaultDHCPGateway),
31       mBeenShutDown(false) {
32   Telemetry::Accumulate(Telemetry::NETWORK_CONNECTION_COUNT, 1);
33 }
34 
~Connection()35 Connection::~Connection() {
36   NS_ASSERT_OWNINGTHREAD(Connection);
37   MOZ_ASSERT(mBeenShutDown);
38 }
39 
Shutdown()40 void Connection::Shutdown() {
41   NS_ASSERT_OWNINGTHREAD(Connection);
42 
43   if (mBeenShutDown) {
44     return;
45   }
46 
47   mBeenShutDown = true;
48   ShutdownInternal();
49 }
50 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)51 JSObject* Connection::WrapObject(JSContext* aCx,
52                                  JS::Handle<JSObject*> aGivenProto) {
53   return NetworkInformation_Binding::Wrap(aCx, this, aGivenProto);
54 }
55 
Update(ConnectionType aType,bool aIsWifi,uint32_t aDHCPGateway,bool aNotify)56 void Connection::Update(ConnectionType aType, bool aIsWifi,
57                         uint32_t aDHCPGateway, bool aNotify) {
58   NS_ASSERT_OWNINGTHREAD(Connection);
59 
60   ConnectionType previousType = mType;
61 
62   mType = aType;
63   mIsWifi = aIsWifi;
64   mDHCPGateway = aDHCPGateway;
65 
66   if (aNotify && previousType != aType &&
67       !nsContentUtils::ShouldResistFingerprinting()) {
68     DispatchTrustedEvent(CHANGE_EVENT_NAME);
69   }
70 }
71 
72 /* static */
CreateForWindow(nsPIDOMWindowInner * aWindow)73 Connection* Connection::CreateForWindow(nsPIDOMWindowInner* aWindow) {
74   MOZ_ASSERT(aWindow);
75   return new ConnectionMainThread(aWindow);
76 }
77 
78 /* static */
CreateForWorker(WorkerPrivate * aWorkerPrivate,ErrorResult & aRv)79 already_AddRefed<Connection> Connection::CreateForWorker(
80     WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
81   MOZ_ASSERT(aWorkerPrivate);
82   aWorkerPrivate->AssertIsOnWorkerThread();
83   return ConnectionWorker::Create(aWorkerPrivate, aRv);
84 }
85 
86 }  // namespace mozilla::dom::network
87