| OLD | NEW |
| 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" | |
| 11 #include "base/containers/scoped_ptr_hash_map.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | 10 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 15 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 16 #include "base/values.h" | 13 #include "base/values.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/chromeos/net/wake_on_wifi_connection_observer.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 20 #include "chromeos/chromeos_switches.h" | 18 #include "chromeos/chromeos_switches.h" |
| 21 #include "chromeos/login/login_state.h" | 19 #include "chromeos/login/login_state.h" |
| 22 #include "chromeos/network/device_state.h" | 20 #include "chromeos/network/device_state.h" |
| 23 #include "chromeos/network/network_device_handler.h" | 21 #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" | |
| 28 #include "components/gcm_driver/gcm_driver.h" | 25 #include "components/gcm_driver/gcm_driver.h" |
| 29 #include "components/gcm_driver/gcm_profile_service.h" | 26 #include "components/gcm_driver/gcm_profile_service.h" |
| 30 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 32 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
| 33 #include "net/base/ip_endpoint.h" | |
| 34 #include "third_party/cros_system_api/dbus/service_constants.h" | 30 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 35 | 31 |
| 36 namespace chromeos { | 32 namespace chromeos { |
| 37 | 33 |
| 38 namespace { | 34 namespace { |
| 39 | 35 |
| 40 const char kWakeOnWifiNone[] = "none"; | 36 const char kWakeOnWifiNone[] = "none"; |
| 41 const char kWakeOnWifiPacket[] = "packet"; | 37 const char kWakeOnWifiPacket[] = "packet"; |
| 42 const char kWakeOnWifiDarkConnect[] = "darkconnect"; | 38 const char kWakeOnWifiDarkConnect[] = "darkconnect"; |
| 43 const char kWakeOnWifiPacketAndDarkConnect[] = "packet_and_darkconnect"; | 39 const char kWakeOnWifiPacketAndDarkConnect[] = "packet_and_darkconnect"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 57 return std::string(); | 53 return std::string(); |
| 58 case WakeOnWifiManager::NOT_SUPPORTED: | 54 case WakeOnWifiManager::NOT_SUPPORTED: |
| 59 NOTREACHED(); | 55 NOTREACHED(); |
| 60 return std::string(); | 56 return std::string(); |
| 61 } | 57 } |
| 62 | 58 |
| 63 NOTREACHED() << "Unknown wake on wifi feature: " << feature; | 59 NOTREACHED() << "Unknown wake on wifi feature: " << feature; |
| 64 return std::string(); | 60 return std::string(); |
| 65 } | 61 } |
| 66 | 62 |
| 67 bool IsWakeOnPacketEnabled(WakeOnWifiManager::WakeOnWifiFeature feature) { | |
| 68 return feature & WakeOnWifiManager::WAKE_ON_WIFI_PACKET; | |
| 69 } | |
| 70 | |
| 71 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. | 63 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. |
| 72 WakeOnWifiManager* g_wake_on_wifi_manager = NULL; | 64 WakeOnWifiManager* g_wake_on_wifi_manager = NULL; |
| 73 | 65 |
| 74 } // namespace | 66 } // namespace |
| 75 | 67 |
| 76 // Simple class that listens for a connection to the GCM server and passes the | |
| 77 // connection information down to shill. Each profile gets its own instance of | |
| 78 // this class. | |
| 79 class WakeOnWifiManager::WakeOnPacketConnectionObserver | |
| 80 : public gcm::GCMConnectionObserver { | |
| 81 public: | |
| 82 WakeOnPacketConnectionObserver(Profile* profile, | |
| 83 bool wifi_properties_received) | |
| 84 : profile_(profile), | |
| 85 ip_endpoint_(net::IPEndPoint()), | |
| 86 wifi_properties_received_(wifi_properties_received) { | |
| 87 gcm::GCMProfileServiceFactory::GetForProfile(profile_) | |
| 88 ->driver() | |
| 89 ->AddConnectionObserver(this); | |
| 90 } | |
| 91 | |
| 92 ~WakeOnPacketConnectionObserver() override { | |
| 93 if (!(ip_endpoint_ == net::IPEndPoint())) | |
| 94 OnDisconnected(); | |
| 95 | |
| 96 gcm::GCMProfileServiceFactory::GetForProfile(profile_) | |
| 97 ->driver() | |
| 98 ->RemoveConnectionObserver(this); | |
| 99 } | |
| 100 | |
| 101 void HandleWifiDevicePropertiesReady() { | |
| 102 wifi_properties_received_ = true; | |
| 103 | |
| 104 // IPEndPoint doesn't implement operator!= | |
| 105 if (ip_endpoint_ == net::IPEndPoint()) | |
| 106 return; | |
| 107 | |
| 108 AddWakeOnPacketConnection(); | |
| 109 } | |
| 110 | |
| 111 // gcm::GCMConnectionObserver overrides. | |
| 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 | |
| 157 // static | 68 // static |
| 158 WakeOnWifiManager* WakeOnWifiManager::Get() { | 69 WakeOnWifiManager* WakeOnWifiManager::Get() { |
| 159 DCHECK(g_wake_on_wifi_manager); | 70 DCHECK(g_wake_on_wifi_manager); |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 161 return g_wake_on_wifi_manager; | 72 return g_wake_on_wifi_manager; |
| 162 } | 73 } |
| 163 | 74 |
| 75 // static |
| 76 bool WakeOnWifiManager::IsWakeOnPacketEnabled(WakeOnWifiFeature feature) { |
| 77 return feature & WakeOnWifiManager::WAKE_ON_WIFI_PACKET; |
| 78 } |
| 79 |
| 164 WakeOnWifiManager::WakeOnWifiManager() | 80 WakeOnWifiManager::WakeOnWifiManager() |
| 165 : current_feature_(WakeOnWifiManager::INVALID), | 81 : current_feature_(WakeOnWifiManager::INVALID), |
| 166 wifi_properties_received_(false), | 82 wifi_properties_received_(false), |
| 167 extension_event_observer_(new ExtensionEventObserver()), | 83 extension_event_observer_(new ExtensionEventObserver()), |
| 168 weak_ptr_factory_(this) { | 84 weak_ptr_factory_(this) { |
| 169 // This class must be constructed before any users are logged in, i.e., before | 85 // This class must be constructed before any users are logged in, i.e., before |
| 170 // any profiles are created or added to the ProfileManager. Additionally, | 86 // any profiles are created or added to the ProfileManager. Additionally, |
| 171 // IsUserLoggedIn always returns true when we are not running on a Chrome OS | 87 // IsUserLoggedIn always returns true when we are not running on a Chrome OS |
| 172 // device so this check should only run on real devices. | 88 // device so this check should only run on real devices. |
| 173 CHECK(!base::SysInfo::IsRunningOnChromeOS() || | 89 CHECK(!base::SysInfo::IsRunningOnChromeOS() || |
| (...skipping 30 matching lines...) Expand all Loading... |
| 204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 205 if (current_feature_ == NOT_SUPPORTED) | 121 if (current_feature_ == NOT_SUPPORTED) |
| 206 return; | 122 return; |
| 207 if (!switches::WakeOnWifiEnabled()) | 123 if (!switches::WakeOnWifiEnabled()) |
| 208 feature = WAKE_ON_WIFI_NONE; | 124 feature = WAKE_ON_WIFI_NONE; |
| 209 if (feature == current_feature_) | 125 if (feature == current_feature_) |
| 210 return; | 126 return; |
| 211 | 127 |
| 212 current_feature_ = feature; | 128 current_feature_ = feature; |
| 213 | 129 |
| 130 // Update value of member variable feature for all connection observers. |
| 131 for (const auto& kv_pair : connection_observers_) { |
| 132 kv_pair.second->set_feature(current_feature_); |
| 133 } |
| 134 |
| 214 if (wifi_properties_received_) | 135 if (wifi_properties_received_) |
| 215 HandleWakeOnWifiFeatureUpdated(); | 136 HandleWakeOnWifiFeatureUpdated(); |
| 216 } | 137 } |
| 217 | 138 |
| 218 bool WakeOnWifiManager::WakeOnWifiSupported() { | 139 bool WakeOnWifiManager::WakeOnWifiSupported() { |
| 219 return current_feature_ != NOT_SUPPORTED && current_feature_ != INVALID; | 140 return current_feature_ != NOT_SUPPORTED && current_feature_ != INVALID; |
| 220 } | 141 } |
| 221 | 142 |
| 222 void WakeOnWifiManager::Observe(int type, | 143 void WakeOnWifiManager::Observe(int type, |
| 223 const content::NotificationSource& source, | 144 const content::NotificationSource& source, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 return; | 239 return; |
| 319 | 240 |
| 320 wifi_properties_received_ = true; | 241 wifi_properties_received_ = true; |
| 321 | 242 |
| 322 NetworkHandler::Get() | 243 NetworkHandler::Get() |
| 323 ->network_device_handler() | 244 ->network_device_handler() |
| 324 ->RemoveAllWifiWakeOnPacketConnections(base::Bind(&base::DoNothing), | 245 ->RemoveAllWifiWakeOnPacketConnections(base::Bind(&base::DoNothing), |
| 325 network_handler::ErrorCallback()); | 246 network_handler::ErrorCallback()); |
| 326 | 247 |
| 327 for (const auto& kv_pair : connection_observers_) { | 248 for (const auto& kv_pair : connection_observers_) { |
| 328 WakeOnPacketConnectionObserver* observer = kv_pair.second; | 249 kv_pair.second->HandleWifiDevicePropertiesReady(); |
| 329 observer->HandleWifiDevicePropertiesReady(); | |
| 330 } | 250 } |
| 331 } | 251 } |
| 332 | 252 |
| 333 void WakeOnWifiManager::OnProfileAdded(Profile* profile) { | 253 void WakeOnWifiManager::OnProfileAdded(Profile* profile) { |
| 334 // add will do nothing if |profile| already exists in |connection_observers_|. | 254 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 | 255 |
| 340 if (result.second) { | 256 // Only add the profile if it is not already present. |
| 341 // This is a profile we haven't seen before. | 257 if (result != connection_observers_.end()) |
| 342 gcm::GCMProfileServiceFactory::GetForProfile(profile) | 258 return; |
| 343 ->driver() | 259 |
| 344 ->WakeFromSuspendForHeartbeat( | 260 connection_observers_[profile] = |
| 345 IsWakeOnPacketEnabled(current_feature_)); | 261 base::WrapUnique(new WakeOnWifiConnectionObserver( |
| 346 } | 262 profile, wifi_properties_received_, current_feature_, |
| 263 NetworkHandler::Get()->network_device_handler())); |
| 264 |
| 265 gcm::GCMProfileServiceFactory::GetForProfile(profile) |
| 266 ->driver() |
| 267 ->WakeFromSuspendForHeartbeat( |
| 268 IsWakeOnPacketEnabled(current_feature_)); |
| 347 } | 269 } |
| 348 | 270 |
| 349 void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) { | 271 void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) { |
| 350 connection_observers_.erase(profile); | 272 connection_observers_.erase(profile); |
| 351 } | 273 } |
| 352 | 274 |
| 353 } // namespace chromeos | 275 } // namespace chromeos |
| OLD | NEW |