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

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

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

Powered by Google App Engine
This is Rietveld 408576698