• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/automation/automation_provider_observers.h"
6 
7 #include "base/values.h"
8 #include "chrome/browser/automation/automation_provider.h"
9 #include "chrome/browser/chromeos/cros/cros_library.h"
10 #include "chrome/browser/chromeos/login/authentication_notification_details.h"
11 #include "content/common/notification_service.h"
12 
13 using chromeos::CrosLibrary;
14 using chromeos::NetworkLibrary;
15 
NetworkManagerInitObserver(AutomationProvider * automation)16 NetworkManagerInitObserver::NetworkManagerInitObserver(
17     AutomationProvider* automation)
18     : automation_(automation->AsWeakPtr()) {}
19 
~NetworkManagerInitObserver()20 NetworkManagerInitObserver::~NetworkManagerInitObserver() {
21     CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
22 }
23 
Init()24 bool NetworkManagerInitObserver::Init() {
25   if (!CrosLibrary::Get()->EnsureLoaded()) {
26     // If cros library fails to load, don't wait for the network
27     // library to finish initializing, because it'll wait forever.
28     automation_->OnNetworkLibraryInit();
29     return false;
30   }
31 
32   CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this);
33   return true;
34 }
35 
OnNetworkManagerChanged(NetworkLibrary * obj)36 void NetworkManagerInitObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
37   if (!obj->wifi_scanning()) {
38     automation_->OnNetworkLibraryInit();
39     delete this;
40   }
41 }
42 
LoginManagerObserver(AutomationProvider * automation,IPC::Message * reply_message)43 LoginManagerObserver::LoginManagerObserver(
44     AutomationProvider* automation,
45     IPC::Message* reply_message)
46     : automation_(automation->AsWeakPtr()),
47       reply_message_(reply_message) {
48   registrar_.Add(this, NotificationType::LOGIN_USER_CHANGED,
49                  NotificationService::AllSources());
50 }
51 
~LoginManagerObserver()52 LoginManagerObserver::~LoginManagerObserver() {}
53 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)54 void LoginManagerObserver::Observe(NotificationType type,
55                                    const NotificationSource& source,
56                                    const NotificationDetails& details) {
57   DCHECK(type == NotificationType::LOGIN_USER_CHANGED);
58 
59   if (!automation_) {
60     delete this;
61     return;
62   }
63 
64   AutomationJSONReply reply(automation_, reply_message_.release());
65   Details<AuthenticationNotificationDetails> auth_details(details);
66   if (auth_details->success())
67     reply.SendSuccess(NULL);
68   else
69     reply.SendError("Login failure.");
70   delete this;
71 }
72 
ScreenLockUnlockObserver(AutomationProvider * automation,IPC::Message * reply_message,bool lock_screen)73 ScreenLockUnlockObserver::ScreenLockUnlockObserver(
74     AutomationProvider* automation,
75     IPC::Message* reply_message,
76     bool lock_screen)
77     : automation_(automation),
78       reply_message_(reply_message),
79       lock_screen_(lock_screen) {
80   registrar_.Add(this, NotificationType::SCREEN_LOCK_STATE_CHANGED,
81                  NotificationService::AllSources());
82 }
83 
~ScreenLockUnlockObserver()84 ScreenLockUnlockObserver::~ScreenLockUnlockObserver() {}
85 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)86 void ScreenLockUnlockObserver::Observe(NotificationType type,
87                                        const NotificationSource& source,
88                                        const NotificationDetails& details) {
89   DCHECK(type == NotificationType::SCREEN_LOCK_STATE_CHANGED);
90   AutomationJSONReply reply(automation_, reply_message_);
91   bool is_screen_locked = *Details<bool>(details).ptr();
92   if (lock_screen_ == is_screen_locked)
93     reply.SendSuccess(NULL);
94   else
95     reply.SendError("Screen lock failure.");
96   delete this;
97 }
98 
NetworkScanObserver(AutomationProvider * automation,IPC::Message * reply_message)99 NetworkScanObserver::NetworkScanObserver(AutomationProvider* automation,
100                                          IPC::Message* reply_message)
101     : automation_(automation), reply_message_(reply_message) {
102   NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
103   network_library->AddNetworkManagerObserver(this);
104 }
105 
~NetworkScanObserver()106 NetworkScanObserver::~NetworkScanObserver() {
107   NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
108   network_library->RemoveNetworkManagerObserver(this);
109 }
110 
OnNetworkManagerChanged(NetworkLibrary * obj)111 void NetworkScanObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
112   if (obj->wifi_scanning())
113     return;
114 
115   AutomationJSONReply(automation_, reply_message_).SendSuccess(NULL);
116   delete this;
117 }
118 
NetworkConnectObserver(AutomationProvider * automation,IPC::Message * reply_message)119 NetworkConnectObserver::NetworkConnectObserver(AutomationProvider* automation,
120                                                IPC::Message* reply_message)
121     : automation_(automation), reply_message_(reply_message) {
122   NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
123   network_library->AddNetworkManagerObserver(this);
124 }
125 
~NetworkConnectObserver()126 NetworkConnectObserver::~NetworkConnectObserver() {
127   NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
128   network_library->RemoveNetworkManagerObserver(this);
129 }
130 
OnNetworkManagerChanged(NetworkLibrary * obj)131 void NetworkConnectObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
132   const chromeos::WifiNetwork* wifi = GetWifiNetwork(obj);
133   if (!wifi) {
134     // The network was not found, and we assume it no longer exists.
135     // This could be because the SSID is invalid, or the network went away.
136     scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
137     return_value->SetString("error_string", "Network not found.");
138     AutomationJSONReply(automation_, reply_message_)
139         .SendSuccess(return_value.get());
140     delete this;
141     return;
142   }
143 
144   if (wifi->failed()) {
145     scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
146     return_value->SetString("error_string", wifi->GetErrorString());
147     AutomationJSONReply(automation_, reply_message_)
148         .SendSuccess(return_value.get());
149     delete this;
150   } else if (wifi->connected()) {
151     AutomationJSONReply(automation_, reply_message_).SendSuccess(NULL);
152     delete this;
153   }
154 
155   // The network is in the NetworkLibrary's list, but there's no failure or
156   // success condition, so just continue waiting for more network events.
157 }
158 
ServicePathConnectObserver(AutomationProvider * automation,IPC::Message * reply_message,const std::string & service_path)159 ServicePathConnectObserver::ServicePathConnectObserver(
160     AutomationProvider* automation, IPC::Message* reply_message,
161     const std::string& service_path)
162     : NetworkConnectObserver(automation, reply_message),
163     service_path_(service_path) {}
164 
GetWifiNetwork(NetworkLibrary * network_library)165 const chromeos::WifiNetwork* ServicePathConnectObserver::GetWifiNetwork(
166     NetworkLibrary* network_library) {
167   return network_library->FindWifiNetworkByPath(service_path_);
168 }
169 
SSIDConnectObserver(AutomationProvider * automation,IPC::Message * reply_message,const std::string & ssid)170 SSIDConnectObserver::SSIDConnectObserver(
171     AutomationProvider* automation, IPC::Message* reply_message,
172     const std::string& ssid)
173     : NetworkConnectObserver(automation, reply_message), ssid_(ssid) {}
174 
GetWifiNetwork(NetworkLibrary * network_library)175 const chromeos::WifiNetwork* SSIDConnectObserver::GetWifiNetwork(
176     NetworkLibrary* network_library) {
177   const chromeos::WifiNetworkVector& wifi_networks =
178       network_library->wifi_networks();
179   for (chromeos::WifiNetworkVector::const_iterator iter = wifi_networks.begin();
180        iter != wifi_networks.end(); ++iter) {
181     const chromeos::WifiNetwork* wifi = *iter;
182     if (wifi->name() == ssid_)
183       return wifi;
184   }
185   return NULL;
186 }
187