automation_provider_observers_chromeos.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "chrome/browser/automation/automation_provider_observers.h"
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
7ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/values.h"
8ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/automation/automation_provider.h"
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "chrome/browser/chromeos/cros/cros_library.h"
10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/chromeos/login/authentication_notification_details.h"
113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "content/common/notification_service.h"
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottusing chromeos::CrosLibrary;
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottusing chromeos::NetworkLibrary;
15ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottNetworkManagerInitObserver::NetworkManagerInitObserver(
17731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick    AutomationProvider* automation)
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    : automation_(automation->AsWeakPtr()) {}
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenNetworkManagerInitObserver::~NetworkManagerInitObserver() {
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool NetworkManagerInitObserver::Init() {
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  if (!CrosLibrary::Get()->EnsureLoaded()) {
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // If cros library fails to load, don't wait for the network
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // library to finish initializing, because it'll wait forever.
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    automation_->OnNetworkLibraryInit();
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return false;
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
32731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this);
33731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  return true;
34731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick}
35731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid NetworkManagerInitObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  if (!obj->wifi_scanning()) {
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    automation_->OnNetworkLibraryInit();
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    delete this;
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
4221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen
4321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian MonsenLoginManagerObserver::LoginManagerObserver(
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    AutomationProvider* automation,
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    IPC::Message* reply_message)
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    : automation_(automation->AsWeakPtr()),
4721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen      reply_message_(reply_message) {
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  registrar_.Add(this, NotificationType::LOGIN_USER_CHANGED,
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                 NotificationService::AllSources());
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochLoginManagerObserver::~LoginManagerObserver() {}
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid LoginManagerObserver::Observe(NotificationType type,
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                   const NotificationSource& source,
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                   const NotificationDetails& details) {
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DCHECK(type == NotificationType::LOGIN_USER_CHANGED);
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (!automation_) {
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    delete this;
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return;
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutomationJSONReply reply(automation_, reply_message_.release());
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Details<AuthenticationNotificationDetails> auth_details(details);
663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  if (auth_details->success())
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    reply.SendSuccess(NULL);
683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  else
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    reply.SendError("Login failure.");
70731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  delete this;
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochScreenLockUnlockObserver::ScreenLockUnlockObserver(
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    AutomationProvider* automation,
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    IPC::Message* reply_message,
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    bool lock_screen)
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    : automation_(automation),
78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      reply_message_(reply_message),
7921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen      lock_screen_(lock_screen) {
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  registrar_.Add(this, NotificationType::SCREEN_LOCK_STATE_CHANGED,
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 NotificationService::AllSources());
82731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick}
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottScreenLockUnlockObserver::~ScreenLockUnlockObserver() {}
85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid ScreenLockUnlockObserver::Observe(NotificationType type,
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                       const NotificationSource& source,
8872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                                       const NotificationDetails& details) {
8972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(type == NotificationType::SCREEN_LOCK_STATE_CHANGED);
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutomationJSONReply reply(automation_, reply_message_);
9172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  bool is_screen_locked = *Details<bool>(details).ptr();
92731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  if (lock_screen_ == is_screen_locked)
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    reply.SendSuccess(NULL);
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  else
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    reply.SendError("Screen lock failure.");
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  delete this;
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottNetworkScanObserver::NetworkScanObserver(AutomationProvider* automation,
100731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick                                         IPC::Message* reply_message)
101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    : automation_(automation), reply_message_(reply_message) {
102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  network_library->AddNetworkManagerObserver(this);
104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottNetworkScanObserver::~NetworkScanObserver() {
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
10878494470aa829a52d6709093dd00e7704053e806Ben Murdoch  network_library->RemoveNetworkManagerObserver(this);
1097d214dfa174224b459660971e5b5cce2e06be02aBen Murdoch}
1107d214dfa174224b459660971e5b5cce2e06be02aBen Murdoch
1117d214dfa174224b459660971e5b5cce2e06be02aBen Murdochvoid NetworkScanObserver::OnNetworkManagerChanged(NetworkLibrary* obj) {
1127d214dfa174224b459660971e5b5cce2e06be02aBen Murdoch  if (obj->wifi_scanning())
1133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    return;
114731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
115731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  AutomationJSONReply(automation_, reply_message_).SendSuccess(NULL);
116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  delete this;
117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
1187d214dfa174224b459660971e5b5cce2e06be02aBen Murdoch
119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottNetworkConnectObserver::NetworkConnectObserver(AutomationProvider* automation,
120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                                               IPC::Message* reply_message)
121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    : automation_(automation), reply_message_(reply_message) {
122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  network_library->AddNetworkManagerObserver(this);
124}
125
126NetworkConnectObserver::~NetworkConnectObserver() {
127  NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
128  network_library->RemoveNetworkManagerObserver(this);
129}
130
131void 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
159ServicePathConnectObserver::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
165const chromeos::WifiNetwork* ServicePathConnectObserver::GetWifiNetwork(
166    NetworkLibrary* network_library) {
167  return network_library->FindWifiNetworkByPath(service_path_);
168}
169
170SSIDConnectObserver::SSIDConnectObserver(
171    AutomationProvider* automation, IPC::Message* reply_message,
172    const std::string& ssid)
173    : NetworkConnectObserver(automation, reply_message), ssid_(ssid) {}
174
175const 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