Connection.cpp (2666B)
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 9 #include "ConnectionMainThread.h" 10 #include "ConnectionWorker.h" 11 #include "Constants.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. 24 NS_IMPL_ISUPPORTS_INHERITED0(dom::network::Connection, DOMEventTargetHelper) 25 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 35 Connection::~Connection() { 36 NS_ASSERT_OWNINGTHREAD(Connection); 37 MOZ_ASSERT(mBeenShutDown); 38 } 39 40 void Connection::Shutdown() { 41 NS_ASSERT_OWNINGTHREAD(Connection); 42 43 if (mBeenShutDown) { 44 return; 45 } 46 47 mBeenShutDown = true; 48 ShutdownInternal(); 49 } 50 51 JSObject* Connection::WrapObject(JSContext* aCx, 52 JS::Handle<JSObject*> aGivenProto) { 53 return NetworkInformation_Binding::Wrap(aCx, this, aGivenProto); 54 } 55 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 && !mShouldResistFingerprinting) { 67 DispatchTrustedEvent(CHANGE_EVENT_NAME); 68 } 69 } 70 71 /* static */ 72 Connection* Connection::CreateForWindow(nsPIDOMWindowInner* aWindow, 73 bool aShouldResistFingerprinting) { 74 MOZ_ASSERT(aWindow); 75 return new ConnectionMainThread(aWindow, aShouldResistFingerprinting); 76 } 77 78 /* static */ 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