Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: third_party/WebKit/Source/core/page/NetworkStateNotifier.cpp

Issue 2087293003: [DevTools] Network.emulateNetworkConditions now affects NetworkStateNotifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: proper initialized checks Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 24 matching lines...) Expand all
35 #include "wtf/Threading.h" 35 #include "wtf/Threading.h"
36 36
37 namespace blink { 37 namespace blink {
38 38
39 NetworkStateNotifier& networkStateNotifier() 39 NetworkStateNotifier& networkStateNotifier()
40 { 40 {
41 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier, new NetworkStateNotifier); 41 DEFINE_THREAD_SAFE_STATIC_LOCAL(NetworkStateNotifier, networkStateNotifier, new NetworkStateNotifier);
42 return networkStateNotifier; 42 return networkStateNotifier;
43 } 43 }
44 44
45 void NetworkStateNotifier::setOnLine(bool onLine) 45 void NetworkStateNotifier::setOnLine(bool onLineValue)
46 { 46 {
47 ASSERT(isMainThread()); 47 DCHECK(isMainThread());
48 48
49 { 49 {
50 MutexLocker locker(m_mutex); 50 MutexLocker locker(m_mutex);
51 if (m_isOnLine == onLine) 51 m_onLineInitialized = true;
52
53 if (m_isOnLine == onLineValue)
52 return; 54 return;
55 m_isOnLine = onLineValue;
53 56
54 m_isOnLine = onLine; 57 if (m_hasOverride)
58 return;
55 } 59 }
56 60
57 Page::networkStateChanged(onLine); 61 Page::networkStateChanged(onLine());
58 } 62 }
59 63
60 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa ndwidthMbps) 64 void NetworkStateNotifier::setWebConnection(WebConnectionType type, double maxBa ndwidthMbps)
61 { 65 {
62 ASSERT(isMainThread()); 66 DCHECK(isMainThread());
63 if (m_testUpdatesOnly)
64 return;
65 67
66 setWebConnectionImpl(type, maxBandwidthMbps); 68 {
69 MutexLocker locker(m_mutex);
70 m_connectionInitialized = true;
71
72 if (m_type == type && m_maxBandwidthMbps == maxBandwidthMbps)
73 return;
74 m_type = type;
75 m_maxBandwidthMbps = maxBandwidthMbps;
76
77 if (m_hasOverride)
78 return;
79 }
80
81 notifyObservers();
67 } 82 }
68 83
69 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution Context* context) 84 void NetworkStateNotifier::addObserver(NetworkStateObserver* observer, Execution Context* context)
70 { 85 {
71 ASSERT(context->isContextThread()); 86 ASSERT(context->isContextThread());
72 ASSERT(observer); 87 ASSERT(observer);
73 88
74 MutexLocker locker(m_mutex); 89 MutexLocker locker(m_mutex);
75 ObserverListMap::AddResult result = m_observers.add(context, nullptr); 90 ObserverListMap::AddResult result = m_observers.add(context, nullptr);
76 if (result.isNewEntry) 91 if (result.isNewEntry)
(...skipping 16 matching lines...) Expand all
93 size_t index = observers.find(observer); 108 size_t index = observers.find(observer);
94 if (index != kNotFound) { 109 if (index != kNotFound) {
95 observers[index] = 0; 110 observers[index] = 0;
96 observerList->zeroedObservers.append(index); 111 observerList->zeroedObservers.append(index);
97 } 112 }
98 113
99 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty()) 114 if (!observerList->iterating && !observerList->zeroedObservers.isEmpty())
100 collectZeroedObservers(observerList, context); 115 collectZeroedObservers(observerList, context);
101 } 116 }
102 117
103 void NetworkStateNotifier::setTestUpdatesOnly(bool updatesOnly) 118 void NetworkStateNotifier::setOverride(bool onLineValue, WebConnectionType type, double maxBandwidthMbps)
119 {
120 DCHECK(isMainThread());
121
122 {
123 MutexLocker locker(m_mutex);
124 m_hasOverride = true;
125 m_overrideOnLine = onLineValue;
126 m_overrideType = type;
127 m_overrideMaxBandwidthMbps = maxBandwidthMbps;
128 }
129
130 notifyObservers();
131 Page::networkStateChanged(onLine());
jkarlin 2016/06/24 14:47:50 setOnline and setWebConnection check if anything h
dgozman 2016/06/24 15:13:44 I think this would be undesirable. Consider two ca
jkarlin 2016/06/24 19:20:16 The clients of NetworkStateNotifier (e.g., the onl
dgozman 2016/06/27 23:03:49 Alright, you convinced me. Can't go against the sp
132 }
133
134 void NetworkStateNotifier::clearOverride()
135 {
136 DCHECK(isMainThread());
137
138 {
139 MutexLocker locker(m_mutex);
140 m_hasOverride = false;
141 }
142
143 notifyObservers();
144 Page::networkStateChanged(onLine());
jkarlin 2016/06/24 14:47:50 setOnline and setWebConnection check if anything h
145 }
146
147 void NetworkStateNotifier::notifyObservers()
104 { 148 {
105 ASSERT(isMainThread()); 149 ASSERT(isMainThread());
106 MutexLocker locker(m_mutex); 150 WebConnectionType type = connectionType();
107 151 double maxBandwidthMbps = maxBandwidth();
108 // Reset state to default when entering or leaving test mode.
109 if (updatesOnly != m_testUpdatesOnly) {
110 m_isOnLine = true;
111 m_type = WebConnectionTypeOther;
112 m_maxBandwidthMbps = std::numeric_limits<double>::infinity();
113 }
114
115 m_testUpdatesOnly = updatesOnly;
116 }
117
118 void NetworkStateNotifier::setWebConnectionForTest(WebConnectionType type, doubl e maxBandwidthMbps)
119 {
120 ASSERT(isMainThread());
121 ASSERT(m_testUpdatesOnly);
122 setWebConnectionImpl(type, maxBandwidthMbps);
123 }
124
125 void NetworkStateNotifier::setWebConnectionImpl(WebConnectionType type, double m axBandwidthMbps)
126 {
127 ASSERT(isMainThread());
128 152
129 MutexLocker locker(m_mutex); 153 MutexLocker locker(m_mutex);
130 m_initialized = true;
131
132 if (m_type == type && m_maxBandwidthMbps == maxBandwidthMbps)
133 return;
134 m_type = type;
135 m_maxBandwidthMbps = maxBandwidthMbps;
136
137 for (const auto& entry : m_observers) { 154 for (const auto& entry : m_observers) {
138 ExecutionContext* context = entry.key; 155 ExecutionContext* context = entry.key;
139 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo tifier::notifyObserversOfConnectionChangeOnContext, crossThreadUnretained(this), type, maxBandwidthMbps)); 156 context->postTask(BLINK_FROM_HERE, createCrossThreadTask(&NetworkStateNo tifier::notifyObserversOfConnectionChangeOnContext, crossThreadUnretained(this), type, maxBandwidthMbps));
140 } 157 }
141 } 158 }
142 159
143 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect ionType type, double maxBandwidthMbps, ExecutionContext* context) 160 void NetworkStateNotifier::notifyObserversOfConnectionChangeOnContext(WebConnect ionType type, double maxBandwidthMbps, ExecutionContext* context)
144 { 161 {
145 ObserverList* observerList = lockAndFindObserverList(context); 162 ObserverList* observerList = lockAndFindObserverList(context);
146 163
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 200
184 list->zeroedObservers.clear(); 201 list->zeroedObservers.clear();
185 202
186 if (list->observers.isEmpty()) { 203 if (list->observers.isEmpty()) {
187 MutexLocker locker(m_mutex); 204 MutexLocker locker(m_mutex);
188 m_observers.remove(context); // deletes list 205 m_observers.remove(context); // deletes list
189 } 206 }
190 } 207 }
191 208
192 } // namespace blink 209 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698