1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/net/wake_on_wifi_manager.h"
#include <string>
#include "base/bind.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/sys_info.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/services/gcm/gcm_profile_service.h"
#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/login/login_state.h"
#include "chromeos/network/device_state.h"
#include "chromeos/network/network_device_handler.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_type_pattern.h"
#include "components/gcm_driver/gcm_connection_observer.h"
#include "components/gcm_driver/gcm_driver.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "net/base/ip_endpoint.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace {
const char kWakeOnNone[] = "none";
const char kWakeOnPacket[] = "packet";
const char kWakeOnSsid[] = "ssid";
const char kWakeOnPacketAndSsid[] = "packet_and_ssid";
std::string WakeOnWifiFeatureToString(
WakeOnWifiManager::WakeOnWifiFeature feature) {
switch (feature) {
case WakeOnWifiManager::WAKE_ON_NONE:
return kWakeOnNone;
case WakeOnWifiManager::WAKE_ON_PACKET:
return kWakeOnPacket;
case WakeOnWifiManager::WAKE_ON_SSID:
return kWakeOnSsid;
case WakeOnWifiManager::WAKE_ON_PACKET_AND_SSID:
return kWakeOnPacketAndSsid;
case WakeOnWifiManager::INVALID:
return std::string();
case WakeOnWifiManager::NOT_SUPPORTED:
NOTREACHED();
return std::string();
}
NOTREACHED() << "Unknown wake on wifi feature: " << feature;
return std::string();
}
bool IsWakeOnPacketEnabled(WakeOnWifiManager::WakeOnWifiFeature feature) {
return feature & WakeOnWifiManager::WAKE_ON_PACKET;
}
// Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos.
WakeOnWifiManager* g_wake_on_wifi_manager = NULL;
} // namespace
// Simple class that listens for a connection to the GCM server and passes the
// connection information down to shill. Each profile gets its own instance of
// this class.
class WakeOnWifiManager::WakeOnPacketConnectionObserver
: public gcm::GCMConnectionObserver {
public:
explicit WakeOnPacketConnectionObserver(Profile* profile)
: profile_(profile),
ip_endpoint_(net::IPEndPoint()) {
gcm::GCMProfileServiceFactory::GetForProfile(profile_)
->driver()
->AddConnectionObserver(this);
}
~WakeOnPacketConnectionObserver() override {
if (!(ip_endpoint_ == net::IPEndPoint()))
OnDisconnected();
gcm::GCMProfileServiceFactory::GetForProfile(profile_)
->driver()
->RemoveConnectionObserver(this);
}
// gcm::GCMConnectionObserver overrides.
void OnConnected(const net::IPEndPoint& ip_endpoint) override {
ip_endpoint_ = ip_endpoint;
NetworkHandler::Get()
->network_device_handler()
->AddWifiWakeOnPacketConnection(
ip_endpoint_,
base::Bind(&base::DoNothing),
network_handler::ErrorCallback());
}
void OnDisconnected() override {
if (ip_endpoint_ == net::IPEndPoint()) {
LOG(WARNING) << "Received GCMConnectionObserver::OnDisconnected without "
<< "a valid IPEndPoint.";
return;
}
NetworkHandler::Get()
->network_device_handler()
->RemoveWifiWakeOnPacketConnection(
ip_endpoint_,
base::Bind(&base::DoNothing),
network_handler::ErrorCallback());
ip_endpoint_ = net::IPEndPoint();
}
private:
Profile* profile_;
net::IPEndPoint ip_endpoint_;
DISALLOW_COPY_AND_ASSIGN(WakeOnPacketConnectionObserver);
};
// static
WakeOnWifiManager* WakeOnWifiManager::Get() {
DCHECK(g_wake_on_wifi_manager);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return g_wake_on_wifi_manager;
}
WakeOnWifiManager::WakeOnWifiManager()
: current_feature_(WakeOnWifiManager::INVALID),
weak_ptr_factory_(this) {
// This class must be constructed before any users are logged in, i.e., before
// any profiles are created or added to the ProfileManager. Additionally,
// IsUserLoggedIn always returns true when we are not running on a Chrome OS
// device so this check should only run on real devices.
CHECK(!base::SysInfo::IsRunningOnChromeOS() ||
!LoginState::Get()->IsUserLoggedIn());
DCHECK(!g_wake_on_wifi_manager);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
g_wake_on_wifi_manager = this;
registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_ADDED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllBrowserContextsAndSources());
NetworkHandler::Get()
->network_device_handler()
->RemoveAllWifiWakeOnPacketConnections(
base::Bind(&base::DoNothing),
network_handler::ErrorCallback());
if (!switches::WakeOnWifiEnabled())
return;
const DeviceState* device =
NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
NetworkTypePattern::WiFi());
if (!device)
return;
// Get device properties to check whether wake-on-wifi is supported.
NetworkHandler::Get()->network_device_handler()->GetDeviceProperties(
device->path(),
base::Bind(&WakeOnWifiManager::GetDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()),
network_handler::ErrorCallback());
}
WakeOnWifiManager::~WakeOnWifiManager() {
DCHECK(g_wake_on_wifi_manager);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
g_wake_on_wifi_manager = NULL;
}
void WakeOnWifiManager::OnPreferenceChanged(
WakeOnWifiManager::WakeOnWifiFeature feature) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (current_feature_ == NOT_SUPPORTED)
return;
if (!switches::WakeOnWifiEnabled())
feature = WAKE_ON_NONE;
if (feature == current_feature_)
return;
current_feature_ = feature;
const DeviceState* device =
NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
NetworkTypePattern::WiFi());
if (!device)
return;
std::string feature_string(WakeOnWifiFeatureToString(feature));
DCHECK(!feature_string.empty());
NetworkHandler::Get()->network_device_handler()->SetDeviceProperty(
device->path(),
shill::kWakeOnWiFiFeaturesEnabledProperty,
base::StringValue(feature_string),
base::Bind(&base::DoNothing),
network_handler::ErrorCallback());
bool wake_from_suspend = IsWakeOnPacketEnabled(current_feature_);
for (const auto& kv_pair : connection_observers_) {
Profile* profile = kv_pair.first;
gcm::GCMProfileServiceFactory::GetForProfile(profile)
->driver()
->WakeFromSuspendForHeartbeat(wake_from_suspend);
}
}
bool WakeOnWifiManager::WakeOnWifiSupported() {
return current_feature_ != NOT_SUPPORTED && current_feature_ != INVALID;
}
void WakeOnWifiManager::GetDevicePropertiesCallback(
const std::string& device_path,
const base::DictionaryValue& properties) {
if (!properties.HasKey(shill::kWakeOnWiFiFeaturesEnabledProperty))
return;
std::string enabled;
if (properties.GetString(shill::kWakeOnWiFiFeaturesEnabledProperty,
&enabled) &&
enabled == shill::kWakeOnWiFiFeaturesEnabledNotSupported) {
current_feature_ = NOT_SUPPORTED;
}
}
void WakeOnWifiManager::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PROFILE_ADDED: {
OnProfileAdded(content::Source<Profile>(source).ptr());
break;
}
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
OnProfileDestroyed(content::Source<Profile>(source).ptr());
break;
}
default:
NOTREACHED();
}
}
void WakeOnWifiManager::OnProfileAdded(Profile* profile) {
// add will do nothing if |profile| already exists in |connection_observers_|.
auto result = connection_observers_.add(
profile,
make_scoped_ptr(
new WakeOnWifiManager::WakeOnPacketConnectionObserver(profile)));
if (result.second) {
// This is a profile we haven't seen before.
gcm::GCMProfileServiceFactory::GetForProfile(profile)
->driver()
->WakeFromSuspendForHeartbeat(
IsWakeOnPacketEnabled(current_feature_));
}
}
void WakeOnWifiManager::OnProfileDestroyed(Profile* profile) {
connection_observers_.erase(profile);
}
} // namespace chromeos
|