Line data Source code
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 "nsIDOMClassInfo.h"
11 : #include "Constants.h"
12 : #include "mozilla/Telemetry.h"
13 : #include "WorkerPrivate.h"
14 :
15 : /**
16 : * We have to use macros here because our leak analysis tool things we are
17 : * leaking strings when we have |static const nsString|. Sad :(
18 : */
19 : #define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
20 :
21 : namespace mozilla {
22 : namespace dom {
23 :
24 : using namespace workers;
25 :
26 : namespace network {
27 :
28 0 : NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
29 : nsINetworkProperties)
30 :
31 : // Don't use |Connection| alone, since that confuses nsTraceRefcnt since
32 : // we're not the only class with that name.
33 0 : NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper)
34 0 : NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper)
35 :
36 0 : Connection::Connection(nsPIDOMWindowInner* aWindow)
37 : : DOMEventTargetHelper(aWindow)
38 : , mType(static_cast<ConnectionType>(kDefaultType))
39 : , mIsWifi(kDefaultIsWifi)
40 : , mDHCPGateway(kDefaultDHCPGateway)
41 0 : , mBeenShutDown(false)
42 : {
43 0 : Telemetry::Accumulate(Telemetry::NETWORK_CONNECTION_COUNT, 1);
44 0 : }
45 :
46 0 : Connection::~Connection()
47 : {
48 0 : NS_ASSERT_OWNINGTHREAD(Connection);
49 0 : MOZ_ASSERT(mBeenShutDown);
50 0 : }
51 :
52 : void
53 0 : Connection::Shutdown()
54 : {
55 0 : NS_ASSERT_OWNINGTHREAD(Connection);
56 :
57 0 : if (mBeenShutDown) {
58 0 : return;
59 : }
60 :
61 0 : mBeenShutDown = true;
62 0 : ShutdownInternal();
63 : }
64 :
65 : NS_IMETHODIMP
66 0 : Connection::GetIsWifi(bool* aIsWifi)
67 : {
68 0 : NS_ENSURE_ARG_POINTER(aIsWifi);
69 0 : NS_ASSERT_OWNINGTHREAD(Connection);
70 :
71 0 : *aIsWifi = mIsWifi;
72 0 : return NS_OK;
73 : }
74 :
75 : NS_IMETHODIMP
76 0 : Connection::GetDhcpGateway(uint32_t* aGW)
77 : {
78 0 : NS_ENSURE_ARG_POINTER(aGW);
79 0 : NS_ASSERT_OWNINGTHREAD(Connection);
80 :
81 0 : *aGW = mDHCPGateway;
82 0 : return NS_OK;
83 : }
84 :
85 : JSObject*
86 0 : Connection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
87 : {
88 0 : return NetworkInformationBinding::Wrap(aCx, this, aGivenProto);
89 : }
90 :
91 : void
92 0 : Connection::Update(ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway,
93 : bool aNotify)
94 : {
95 0 : NS_ASSERT_OWNINGTHREAD(Connection);
96 :
97 0 : ConnectionType previousType = mType;
98 :
99 0 : mType = aType;
100 0 : mIsWifi = aIsWifi;
101 0 : mDHCPGateway = aDHCPGateway;
102 :
103 0 : if (aNotify && previousType != aType &&
104 0 : !nsContentUtils::ShouldResistFingerprinting()) {
105 0 : DispatchTrustedEvent(CHANGE_EVENT_NAME);
106 : }
107 0 : }
108 :
109 : /* static */ bool
110 2 : Connection::IsEnabled(JSContext* aCx, JSObject* aObj)
111 : {
112 2 : if (NS_IsMainThread()) {
113 0 : return Preferences::GetBool("dom.netinfo.enabled");
114 : }
115 :
116 2 : WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
117 2 : MOZ_ASSERT(workerPrivate);
118 2 : return workerPrivate->NetworkInformationEnabled();
119 : }
120 :
121 : /* static */ Connection*
122 0 : Connection::CreateForWindow(nsPIDOMWindowInner* aWindow)
123 : {
124 0 : MOZ_ASSERT(aWindow);
125 0 : return new ConnectionMainThread(aWindow);
126 : }
127 :
128 : /* static */ already_AddRefed<Connection>
129 0 : Connection::CreateForWorker(workers::WorkerPrivate* aWorkerPrivate,
130 : ErrorResult& aRv)
131 : {
132 0 : MOZ_ASSERT(aWorkerPrivate);
133 0 : aWorkerPrivate->AssertIsOnWorkerThread();
134 0 : return ConnectionWorker::Create(aWorkerPrivate, aRv);
135 : }
136 :
137 : } // namespace network
138 : } // namespace dom
139 : } // namespace mozilla
|