Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/network/Connection.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#define CHANGE_EVENT_NAME NS_LITERAL_STRING("typechange")
19
20
namespace mozilla {
21
namespace dom {
22
23
namespace network {
24
25
NS_IMPL_QUERY_INTERFACE_INHERITED(Connection, DOMEventTargetHelper,
26
                                  nsINetworkProperties)
27
28
// Don't use |Connection| alone, since that confuses nsTraceRefcnt since
29
// we're not the only class with that name.
30
NS_IMPL_ADDREF_INHERITED(dom::network::Connection, DOMEventTargetHelper)
31
NS_IMPL_RELEASE_INHERITED(dom::network::Connection, DOMEventTargetHelper)
32
33
Connection::Connection(nsPIDOMWindowInner* aWindow)
34
  : DOMEventTargetHelper(aWindow)
35
  , mType(static_cast<ConnectionType>(kDefaultType))
36
  , mIsWifi(kDefaultIsWifi)
37
  , mDHCPGateway(kDefaultDHCPGateway)
38
  , mBeenShutDown(false)
39
0
{
40
0
  Telemetry::Accumulate(Telemetry::NETWORK_CONNECTION_COUNT, 1);
41
0
}
42
43
Connection::~Connection()
44
0
{
45
0
  NS_ASSERT_OWNINGTHREAD(Connection);
46
0
  MOZ_ASSERT(mBeenShutDown);
47
0
}
48
49
void
50
Connection::Shutdown()
51
0
{
52
0
  NS_ASSERT_OWNINGTHREAD(Connection);
53
0
54
0
  if (mBeenShutDown) {
55
0
    return;
56
0
  }
57
0
58
0
  mBeenShutDown = true;
59
0
  ShutdownInternal();
60
0
}
61
62
NS_IMETHODIMP
63
Connection::GetIsWifi(bool* aIsWifi)
64
0
{
65
0
  NS_ENSURE_ARG_POINTER(aIsWifi);
66
0
  NS_ASSERT_OWNINGTHREAD(Connection);
67
0
68
0
  *aIsWifi = mIsWifi;
69
0
  return NS_OK;
70
0
}
71
72
NS_IMETHODIMP
73
Connection::GetDhcpGateway(uint32_t* aGW)
74
0
{
75
0
  NS_ENSURE_ARG_POINTER(aGW);
76
0
  NS_ASSERT_OWNINGTHREAD(Connection);
77
0
78
0
  *aGW = mDHCPGateway;
79
0
  return NS_OK;
80
0
}
81
82
JSObject*
83
Connection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
84
0
{
85
0
  return NetworkInformation_Binding::Wrap(aCx, this, aGivenProto);
86
0
}
87
88
void
89
Connection::Update(ConnectionType aType, bool aIsWifi, uint32_t aDHCPGateway,
90
                   bool aNotify)
91
0
{
92
0
  NS_ASSERT_OWNINGTHREAD(Connection);
93
0
94
0
  ConnectionType previousType = mType;
95
0
96
0
  mType = aType;
97
0
  mIsWifi = aIsWifi;
98
0
  mDHCPGateway = aDHCPGateway;
99
0
100
0
  if (aNotify && previousType != aType &&
101
0
      !nsContentUtils::ShouldResistFingerprinting()) {
102
0
    DispatchTrustedEvent(CHANGE_EVENT_NAME);
103
0
  }
104
0
}
105
106
/* static */ Connection*
107
Connection::CreateForWindow(nsPIDOMWindowInner* aWindow)
108
0
{
109
0
  MOZ_ASSERT(aWindow);
110
0
  return new ConnectionMainThread(aWindow);
111
0
}
112
113
/* static */ already_AddRefed<Connection>
114
Connection::CreateForWorker(WorkerPrivate* aWorkerPrivate,
115
                            ErrorResult& aRv)
116
0
{
117
0
  MOZ_ASSERT(aWorkerPrivate);
118
0
  aWorkerPrivate->AssertIsOnWorkerThread();
119
0
  return ConnectionWorker::Create(aWorkerPrivate, aRv);
120
0
}
121
122
} // namespace network
123
} // namespace dom
124
} // namespace mozilla