OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/net/wake_on_wifi_manager.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/containers/scoped_ptr_hash_map.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/sys_info.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 20 #include "chromeos/login/login_state.h" |
| 21 #include "chromeos/network/device_state.h" |
| 22 #include "chromeos/network/network_device_handler.h" |
| 23 #include "chromeos/network/network_handler.h" |
| 24 #include "chromeos/network/network_state_handler.h" |
| 25 #include "chromeos/network/network_type_pattern.h" |
| 26 #include "components/gcm_driver/gcm_connection_observer.h" |
| 27 #include "components/gcm_driver/gcm_driver.h" |
| 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/notification_service.h" |
| 30 #include "content/public/browser/notification_source.h" |
| 31 #include "net/base/ip_endpoint.h" |
| 32 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 33 |
| 34 namespace chromeos { |
| 35 |
| 36 namespace { |
| 37 |
| 38 const char kWakeOnNone[] = "none"; |
| 39 const char kWakeOnPacket[] = "packet"; |
| 40 const char kWakeOnSsid[] = "ssid"; |
| 41 const char kWakeOnPacketAndSsid[] = "packet_and_ssid"; |
| 42 |
| 43 std::string WakeOnWifiFeatureToString( |
| 44 WakeOnWifiManager::WakeOnWifiFeature feature) { |
| 45 switch (feature) { |
| 46 case WakeOnWifiManager::WAKE_ON_NONE: |
| 47 return kWakeOnNone; |
| 48 case WakeOnWifiManager::WAKE_ON_PACKET: |
| 49 return kWakeOnPacket; |
| 50 case WakeOnWifiManager::WAKE_ON_SSID: |
| 51 return kWakeOnSsid; |
| 52 case WakeOnWifiManager::WAKE_ON_PACKET_AND_SSID: |
| 53 return kWakeOnPacketAndSsid; |
| 54 } |
| 55 |
| 56 NOTREACHED() << "Unknown wake on wifi feature: " << feature; |
| 57 return std::string(); |
| 58 } |
| 59 |
| 60 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. |
| 61 WakeOnWifiManager* g_wake_on_wifi_manager = NULL; |
| 62 |
| 63 } // namespace |
| 64 |
| 65 // Simple class that listens for a connection to the GCM server and passes the |
| 66 // connection information down to shill. Each profile gets its own instance of |
| 67 // this class. |
| 68 class WakeOnWifiManager::WakeOnPacketConnectionObserver |
| 69 : public gcm::GCMConnectionObserver { |
| 70 public: |
| 71 explicit WakeOnPacketConnectionObserver(Profile* profile) |
| 72 : profile_(profile), |
| 73 ip_endpoint_(net::IPEndPoint()) { |
| 74 gcm::GCMProfileServiceFactory::GetForProfile(profile_) |
| 75 ->driver() |
| 76 ->AddConnectionObserver(this); |
| 77 } |
| 78 |
| 79 ~WakeOnPacketConnectionObserver() override { |
| 80 if (!(ip_endpoint_ == net::IPEndPoint())) |
| 81 OnDisconnected(); |
| 82 |
| 83 gcm::GCMProfileServiceFactory::GetForProfile(profile_) |
| 84 ->driver() |
| 85 ->RemoveConnectionObserver(this); |
| 86 } |
| 87 |
| 88 // gcm::GCMConnectionObserver overrides. |
| 89 |
| 90 void OnConnected(const net::IPEndPoint& ip_endpoint) override { |
| 91 ip_endpoint_ = ip_endpoint; |
| 92 |
| 93 NetworkHandler::Get() |
| 94 ->network_device_handler() |
| 95 ->AddWifiWakeOnPacketConnection( |
| 96 ip_endpoint_, |
| 97 base::Bind(&base::DoNothing), |
| 98 network_handler::ErrorCallback()); |
| 99 } |
| 100 |
| 101 void OnDisconnected() override { |
| 102 if (ip_endpoint_ == net::IPEndPoint()) { |
| 103 LOG(WARNING) << "Received GCMConnectionObserver::OnDisconnected without " |
| 104 << "a valid IPEndPoint."; |
| 105 return; |
| 106 } |
| 107 |
| 108 NetworkHandler::Get() |
| 109 ->network_device_handler() |
| 110 ->RemoveWifiWakeOnPacketConnection( |
| 111 ip_endpoint_, |
| 112 base::Bind(&base::DoNothing), |
| 113 network_handler::ErrorCallback()); |
| 114 |
| 115 ip_endpoint_ = net::IPEndPoint(); |
| 116 } |
| 117 |
| 118 private: |
| 119 Profile* profile_; |
| 120 net::IPEndPoint ip_endpoint_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(WakeOnPacketConnectionObserver); |
| 123 }; |
| 124 |
| 125 // static |
| 126 WakeOnWifiManager* WakeOnWifiManager::Get() { |
| 127 DCHECK(g_wake_on_wifi_manager); |
| 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 129 return g_wake_on_wifi_manager; |
| 130 } |
| 131 |
| 132 WakeOnWifiManager::WakeOnWifiManager() { |
| 133 // This class must be constructed before any users are logged in, i.e., before |
| 134 // any profiles are created or added to the ProfileManager. Additionally, |
| 135 // IsUserLoggedIn always returns true when we are not running on a Chrome OS |
| 136 // device so this check should only run on real devices. |
| 137 CHECK(!base::SysInfo::IsRunningOnChromeOS() || |
| 138 !LoginState::Get()->IsUserLoggedIn()); |
| 139 DCHECK(!g_wake_on_wifi_manager); |
| 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 141 |
| 142 g_wake_on_wifi_manager = this; |
| 143 |
| 144 registrar_.Add(this, |
| 145 chrome::NOTIFICATION_PROFILE_ADDED, |
| 146 content::NotificationService::AllBrowserContextsAndSources()); |
| 147 registrar_.Add(this, |
| 148 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 149 content::NotificationService::AllBrowserContextsAndSources()); |
| 150 |
| 151 NetworkHandler::Get() |
| 152 ->network_device_handler() |
| 153 ->RemoveAllWifiWakeOnPacketConnections( |
| 154 base::Bind(&base::DoNothing), |
| 155 network_handler::ErrorCallback()); |
| 156 } |
| 157 |
| 158 WakeOnWifiManager::~WakeOnWifiManager() { |
| 159 DCHECK(g_wake_on_wifi_manager); |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 161 g_wake_on_wifi_manager = NULL; |
| 162 } |
| 163 |
| 164 void WakeOnWifiManager::OnPreferenceChanged( |
| 165 WakeOnWifiManager::WakeOnWifiFeature feature) { |
| 166 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 167 const DeviceState* device = |
| 168 NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType( |
| 169 NetworkTypePattern::WiFi()); |
| 170 if (!device) |
| 171 return; |
| 172 |
| 173 std::string feature_string(WakeOnWifiFeatureToString(feature)); |
| 174 DCHECK(!feature_string.empty()); |
| 175 |
| 176 NetworkHandler::Get()->network_device_handler()->SetDeviceProperty( |
| 177 device->path(), |
| 178 shill::kWakeOnWiFiFeaturesEnabledProperty, |
| 179 base::StringValue(feature_string), |
| 180 base::Bind(&base::DoNothing), |
| 181 network_handler::ErrorCallback()); |
| 182 } |
| 183 |
| 184 void WakeOnWifiManager::Observe(int type, |
| 185 const content::NotificationSource& source, |
| 186 const content::NotificationDetails& details) { |
| 187 switch (type) { |
| 188 case chrome::NOTIFICATION_PROFILE_ADDED: { |
| 189 OnProfileAdded(content::Source<Profile>(source).ptr()); |
| 190 break; |
| 191 } |
| 192 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 193 OnProfileDestroyed(content::Source<Profile>(source).ptr()); |
| 194 break; |
| 195 } |
| 196 default: |
| 197 NOTREACHED(); |
| 198 } |
| 199 } |
| 200 |
| 201 void WakeOnWifiManager::OnProfileAdded(Profile* profile) { |
| 202 // add will do nothing if |profile| already exists in |connection_observers_|. |
| 203 connection_observers_.add( |
| 204 profile, |
| 205 make_scoped_ptr( |
| 206 new WakeOnWifiManager::WakeOnPacketConnectionObserver(profile))); |
| 207 } |
| 208 |
| 209 void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) { |
| 210 connection_observers_.erase(profile); |
| 211 } |
| 212 |
| 213 } // namespace chromeos |
OLD | NEW |