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/dom/WorkerPrivate.h"
14 * We have to use macros here because our leak analysis tool things we are
15 * leaking strings when we have |static const nsString|. Sad :(
17 #define CHANGE_EVENT_NAME u"typechange"_ns
19 namespace mozilla::dom::network
{
21 // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
22 // we're not the only class with that name.
23 NS_IMPL_ISUPPORTS_INHERITED0(dom::network::Connection
, DOMEventTargetHelper
)
25 Connection::Connection(nsPIDOMWindowInner
* aWindow
,
26 bool aShouldResistFingerprinting
)
27 : DOMEventTargetHelper(aWindow
),
28 mShouldResistFingerprinting(aShouldResistFingerprinting
),
29 mType(static_cast<ConnectionType
>(kDefaultType
)),
30 mIsWifi(kDefaultIsWifi
),
31 mDHCPGateway(kDefaultDHCPGateway
),
32 mBeenShutDown(false) {}
34 Connection::~Connection() {
35 NS_ASSERT_OWNINGTHREAD(Connection
);
36 MOZ_ASSERT(mBeenShutDown
);
39 void Connection::Shutdown() {
40 NS_ASSERT_OWNINGTHREAD(Connection
);
50 JSObject
* Connection::WrapObject(JSContext
* aCx
,
51 JS::Handle
<JSObject
*> aGivenProto
) {
52 return NetworkInformation_Binding::Wrap(aCx
, this, aGivenProto
);
55 void Connection::Update(ConnectionType aType
, bool aIsWifi
,
56 uint32_t aDHCPGateway
, bool aNotify
) {
57 NS_ASSERT_OWNINGTHREAD(Connection
);
59 ConnectionType previousType
= mType
;
63 mDHCPGateway
= aDHCPGateway
;
65 if (aNotify
&& previousType
!= aType
&& !mShouldResistFingerprinting
) {
66 DispatchTrustedEvent(CHANGE_EVENT_NAME
);
71 Connection
* Connection::CreateForWindow(nsPIDOMWindowInner
* aWindow
,
72 bool aShouldResistFingerprinting
) {
74 return new ConnectionMainThread(aWindow
, aShouldResistFingerprinting
);
78 already_AddRefed
<Connection
> Connection::CreateForWorker(
79 WorkerPrivate
* aWorkerPrivate
, ErrorResult
& aRv
) {
80 MOZ_ASSERT(aWorkerPrivate
);
81 aWorkerPrivate
->AssertIsOnWorkerThread();
82 return ConnectionWorker::Create(aWorkerPrivate
, aRv
);
85 } // namespace mozilla::dom::network