OLD | NEW |
(Empty) | |
| 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 |
| 16 NetworkManagerInitObserver::NetworkManagerInitObserver( |
| 17 AutomationProvider* automation) |
| 18 : automation_(automation->AsWeakPtr()) {} |
| 19 |
| 20 NetworkManagerInitObserver::~NetworkManagerInitObserver() { |
| 21 CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); |
| 22 } |
| 23 |
| 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 |
| 36 void NetworkManagerInitObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { |
| 37 if (!obj->wifi_scanning()) { |
| 38 automation_->OnNetworkLibraryInit(); |
| 39 delete this; |
| 40 } |
| 41 } |
| 42 |
| 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 |
| 52 LoginManagerObserver::~LoginManagerObserver() {} |
| 53 |
| 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 |
| 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 |
| 84 ScreenLockUnlockObserver::~ScreenLockUnlockObserver() {} |
| 85 |
| 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 |
| 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 |
| 106 NetworkScanObserver::~NetworkScanObserver() { |
| 107 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 108 network_library->RemoveNetworkManagerObserver(this); |
| 109 } |
| 110 |
| 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 |
| 119 NetworkConnectObserver::NetworkConnectObserver(AutomationProvider* automation, |
| 120 IPC::Message* reply_message, |
| 121 const std::string& service_path) |
| 122 : automation_(automation), |
| 123 reply_message_(reply_message), |
| 124 service_path_(service_path) { |
| 125 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 126 network_library->AddNetworkManagerObserver(this); |
| 127 } |
| 128 |
| 129 NetworkConnectObserver::~NetworkConnectObserver() { |
| 130 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 131 network_library->RemoveNetworkManagerObserver(this); |
| 132 } |
| 133 |
| 134 void NetworkConnectObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { |
| 135 AutomationJSONReply reply(automation_, reply_message_); |
| 136 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); |
| 137 chromeos::WifiNetwork* wifi = |
| 138 network_library->FindWifiNetworkByPath(service_path_); |
| 139 if (!wifi) |
| 140 return; |
| 141 if (wifi->failed()) { |
| 142 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); |
| 143 return_value->SetString("error_code", wifi->GetErrorString()); |
| 144 reply.SendSuccess(return_value.get()); |
| 145 delete this; |
| 146 } |
| 147 if (wifi->connected()) { |
| 148 reply.SendSuccess(NULL); |
| 149 delete this; |
| 150 } |
| 151 } |
| 152 |
OLD | NEW |