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

Side by Side Diff: chrome/browser/chromeos/net/wake_on_wifi_manager.cc

Issue 1891633002: Remove unnecessary calls to AddWakeOnPaketConnection and RemoveWakeOnPacketConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/net/wake_on_wifi_manager.h" 5 #include "chrome/browser/chromeos/net/wake_on_wifi_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/logging.h" 11 #include "base/logging.h"
13 #include "base/macros.h" 12 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
15 #include "base/sys_info.h" 14 #include "base/sys_info.h"
16 #include "base/values.h" 15 #include "base/values.h"
17 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
20 #include "chromeos/chromeos_switches.h" 19 #include "chromeos/chromeos_switches.h"
21 #include "chromeos/login/login_state.h" 20 #include "chromeos/login/login_state.h"
22 #include "chromeos/network/device_state.h" 21 #include "chromeos/network/device_state.h"
23 #include "chromeos/network/network_device_handler.h"
24 #include "chromeos/network/network_handler.h" 22 #include "chromeos/network/network_handler.h"
25 #include "chromeos/network/network_state_handler.h" 23 #include "chromeos/network/network_state_handler.h"
26 #include "chromeos/network/network_type_pattern.h" 24 #include "chromeos/network/network_type_pattern.h"
27 #include "components/gcm_driver/gcm_connection_observer.h" 25 #include "components/gcm_driver/gcm_connection_observer.h"
28 #include "components/gcm_driver/gcm_driver.h" 26 #include "components/gcm_driver/gcm_driver.h"
29 #include "components/gcm_driver/gcm_profile_service.h" 27 #include "components/gcm_driver/gcm_profile_service.h"
30 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/notification_source.h" 30 #include "content/public/browser/notification_source.h"
33 #include "net/base/ip_endpoint.h" 31 #include "net/base/ip_endpoint.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 64
67 bool IsWakeOnPacketEnabled(WakeOnWifiManager::WakeOnWifiFeature feature) { 65 bool IsWakeOnPacketEnabled(WakeOnWifiManager::WakeOnWifiFeature feature) {
68 return feature & WakeOnWifiManager::WAKE_ON_WIFI_PACKET; 66 return feature & WakeOnWifiManager::WAKE_ON_WIFI_PACKET;
69 } 67 }
70 68
71 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. 69 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos.
72 WakeOnWifiManager* g_wake_on_wifi_manager = NULL; 70 WakeOnWifiManager* g_wake_on_wifi_manager = NULL;
73 71
74 } // namespace 72 } // namespace
75 73
76 // Simple class that listens for a connection to the GCM server and passes the 74 WakeOnWifiManager::ConnectionObserver::ConnectionObserver(
77 // connection information down to shill. Each profile gets its own instance of 75 Profile* profile,
78 // this class. 76 bool wifi_properties_received,
79 class WakeOnWifiManager::WakeOnPacketConnectionObserver 77 WakeOnWifiManager::WakeOnWifiFeature feature,
80 : public gcm::GCMConnectionObserver { 78 NetworkDeviceHandler* network_device_handler)
81 public: 79 : profile_(profile),
82 WakeOnPacketConnectionObserver(Profile* profile, 80 ip_endpoint_(net::IPEndPoint()),
83 bool wifi_properties_received) 81 wifi_properties_received_(wifi_properties_received),
84 : profile_(profile), 82 feature_(feature),
85 ip_endpoint_(net::IPEndPoint()), 83 network_device_handler_(network_device_handler) {
86 wifi_properties_received_(wifi_properties_received) { 84 gcm::GCMProfileServiceFactory::GetForProfile(profile_)
87 gcm::GCMProfileServiceFactory::GetForProfile(profile_) 85 ->driver()
88 ->driver() 86 ->AddConnectionObserver(this);
89 ->AddConnectionObserver(this); 87 }
88
89 WakeOnWifiManager::ConnectionObserver::~ConnectionObserver() {
90 if (!(ip_endpoint_ == net::IPEndPoint()))
91 OnDisconnected();
92
93 gcm::GCMProfileServiceFactory::GetForProfile(profile_)
94 ->driver()
95 ->RemoveConnectionObserver(this);
96 }
97
98 void WakeOnWifiManager::ConnectionObserver::HandleWifiDevicePropertiesReady() {
99 wifi_properties_received_ = true;
100
101 // IPEndPoint doesn't implement operator!=
102 if (ip_endpoint_ == net::IPEndPoint())
103 return;
104
105 AddWakeOnPacketConnection();
106 }
107
108 void WakeOnWifiManager::ConnectionObserver::OnConnected(
109 const net::IPEndPoint& ip_endpoint) {
110 ip_endpoint_ = ip_endpoint;
111
112 if (wifi_properties_received_)
113 AddWakeOnPacketConnection();
114 }
115
116 void WakeOnWifiManager::ConnectionObserver::OnDisconnected() {
117 if (ip_endpoint_ == net::IPEndPoint()) {
118 VLOG(1) << "Received GCMConnectionObserver::OnDisconnected without a "
119 << "valid IPEndPoint.";
120 return;
90 } 121 }
91 122
92 ~WakeOnPacketConnectionObserver() override { 123 if (wifi_properties_received_)
93 if (!(ip_endpoint_ == net::IPEndPoint())) 124 RemoveWakeOnPacketConnection();
94 OnDisconnected();
95 125
96 gcm::GCMProfileServiceFactory::GetForProfile(profile_) 126 ip_endpoint_ = net::IPEndPoint();
97 ->driver() 127 }
98 ->RemoveConnectionObserver(this);
99 }
100 128
101 void HandleWifiDevicePropertiesReady() { 129 void WakeOnWifiManager::ConnectionObserver::AddWakeOnPacketConnection() {
102 wifi_properties_received_ = true; 130 if (!IsWakeOnPacketEnabled(WakeOnWifiManager::ConnectionObserver::feature_))
131 return;
132 network_device_handler_
133 ->AddWifiWakeOnPacketConnection(ip_endpoint_,
134 base::Bind(&base::DoNothing),
135 network_handler::ErrorCallback());
136 }
103 137
104 // IPEndPoint doesn't implement operator!= 138 void WakeOnWifiManager::ConnectionObserver::RemoveWakeOnPacketConnection() {
105 if (ip_endpoint_ == net::IPEndPoint()) 139 if (!IsWakeOnPacketEnabled(WakeOnWifiManager::ConnectionObserver::feature_))
106 return; 140 return;
107 141 network_device_handler_
108 AddWakeOnPacketConnection(); 142 ->RemoveWifiWakeOnPacketConnection(ip_endpoint_,
109 } 143 base::Bind(&base::DoNothing),
110 144 network_handler::ErrorCallback());
111 // gcm::GCMConnectionObserver overrides. 145 }
112
113 void OnConnected(const net::IPEndPoint& ip_endpoint) override {
114 ip_endpoint_ = ip_endpoint;
115
116 if (wifi_properties_received_)
117 AddWakeOnPacketConnection();
118 }
119
120 void OnDisconnected() override {
121 if (ip_endpoint_ == net::IPEndPoint()) {
122 VLOG(1) << "Received GCMConnectionObserver::OnDisconnected without a "
123 << "valid IPEndPoint.";
124 return;
125 }
126
127 if (wifi_properties_received_)
128 RemoveWakeOnPacketConnection();
129
130 ip_endpoint_ = net::IPEndPoint();
131 }
132
133 private:
134 void AddWakeOnPacketConnection() {
135 NetworkHandler::Get()
136 ->network_device_handler()
137 ->AddWifiWakeOnPacketConnection(ip_endpoint_,
138 base::Bind(&base::DoNothing),
139 network_handler::ErrorCallback());
140 }
141
142 void RemoveWakeOnPacketConnection() {
143 NetworkHandler::Get()
144 ->network_device_handler()
145 ->RemoveWifiWakeOnPacketConnection(ip_endpoint_,
146 base::Bind(&base::DoNothing),
147 network_handler::ErrorCallback());
148 }
149
150 Profile* profile_;
151 net::IPEndPoint ip_endpoint_;
152 bool wifi_properties_received_;
153
154 DISALLOW_COPY_AND_ASSIGN(WakeOnPacketConnectionObserver);
155 };
156 146
157 // static 147 // static
158 WakeOnWifiManager* WakeOnWifiManager::Get() { 148 WakeOnWifiManager* WakeOnWifiManager::Get() {
159 DCHECK(g_wake_on_wifi_manager); 149 DCHECK(g_wake_on_wifi_manager);
160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
161 return g_wake_on_wifi_manager; 151 return g_wake_on_wifi_manager;
162 } 152 }
163 153
164 WakeOnWifiManager::WakeOnWifiManager() 154 WakeOnWifiManager::WakeOnWifiManager()
165 : current_feature_(WakeOnWifiManager::INVALID), 155 : current_feature_(WakeOnWifiManager::INVALID),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 194 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
205 if (current_feature_ == NOT_SUPPORTED) 195 if (current_feature_ == NOT_SUPPORTED)
206 return; 196 return;
207 if (!switches::WakeOnWifiEnabled()) 197 if (!switches::WakeOnWifiEnabled())
208 feature = WAKE_ON_WIFI_NONE; 198 feature = WAKE_ON_WIFI_NONE;
209 if (feature == current_feature_) 199 if (feature == current_feature_)
210 return; 200 return;
211 201
212 current_feature_ = feature; 202 current_feature_ = feature;
213 203
204 // Update value of member variable feature for all connection observers.
205 for (const auto& kv_pair : connection_observers_) {
206 kv_pair.second->set_feature(current_feature_);
207 }
208
214 if (wifi_properties_received_) 209 if (wifi_properties_received_)
215 HandleWakeOnWifiFeatureUpdated(); 210 HandleWakeOnWifiFeatureUpdated();
216 } 211 }
217 212
218 bool WakeOnWifiManager::WakeOnWifiSupported() { 213 bool WakeOnWifiManager::WakeOnWifiSupported() {
219 return current_feature_ != NOT_SUPPORTED && current_feature_ != INVALID; 214 return current_feature_ != NOT_SUPPORTED && current_feature_ != INVALID;
220 } 215 }
221 216
222 void WakeOnWifiManager::Observe(int type, 217 void WakeOnWifiManager::Observe(int type,
223 const content::NotificationSource& source, 218 const content::NotificationSource& source,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return; 313 return;
319 314
320 wifi_properties_received_ = true; 315 wifi_properties_received_ = true;
321 316
322 NetworkHandler::Get() 317 NetworkHandler::Get()
323 ->network_device_handler() 318 ->network_device_handler()
324 ->RemoveAllWifiWakeOnPacketConnections(base::Bind(&base::DoNothing), 319 ->RemoveAllWifiWakeOnPacketConnections(base::Bind(&base::DoNothing),
325 network_handler::ErrorCallback()); 320 network_handler::ErrorCallback());
326 321
327 for (const auto& kv_pair : connection_observers_) { 322 for (const auto& kv_pair : connection_observers_) {
328 WakeOnPacketConnectionObserver* observer = kv_pair.second; 323 kv_pair.second->HandleWifiDevicePropertiesReady();
329 observer->HandleWifiDevicePropertiesReady();
330 } 324 }
331 } 325 }
332 326
333 void WakeOnWifiManager::OnProfileAdded(Profile* profile) { 327 void WakeOnWifiManager::OnProfileAdded(Profile* profile) {
334 // add will do nothing if |profile| already exists in |connection_observers_|. 328 auto result = connection_observers_.find(profile);
335 auto result = connection_observers_.add(
336 profile,
337 base::WrapUnique(new WakeOnWifiManager::WakeOnPacketConnectionObserver(
338 profile, wifi_properties_received_)));
339 329
340 if (result.second) { 330 // Only add the profile if it is not already present.
341 // This is a profile we haven't seen before. 331 if (result != connection_observers_.end())
342 gcm::GCMProfileServiceFactory::GetForProfile(profile) 332 return;
343 ->driver() 333
344 ->WakeFromSuspendForHeartbeat( 334 connection_observers_[profile] =
345 IsWakeOnPacketEnabled(current_feature_)); 335 base::WrapUnique(new WakeOnWifiManager::ConnectionObserver(
346 } 336 profile, wifi_properties_received_, current_feature_,
337 NetworkHandler::Get()->network_device_handler()));
338
339 gcm::GCMProfileServiceFactory::GetForProfile(profile)
340 ->driver()
341 ->WakeFromSuspendForHeartbeat(
342 IsWakeOnPacketEnabled(current_feature_));
347 } 343 }
348 344
349 void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) { 345 void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) {
350 connection_observers_.erase(profile); 346 connection_observers_.erase(profile);
351 } 347 }
352 348
353 } // namespace chromeos 349 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698