Index: chrome/browser/chromeos/extensions/networking_private_api.cc |
diff --git a/chrome/browser/chromeos/extensions/networking_private_api.cc b/chrome/browser/chromeos/extensions/networking_private_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0fdf6d61fefd6bb8bb3de51c3258c4c8e148dd33 |
--- /dev/null |
+++ b/chrome/browser/chromeos/extensions/networking_private_api.cc |
@@ -0,0 +1,259 @@ |
+// Copyright (c) 2013 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/extensions/networking_private_api.h" |
+ |
+#include "base/bind_helpers.h" |
+#include "chrome/browser/chromeos/extensions/networking_private_api_factory.h" |
+#include "chrome/browser/extensions/extension_function_registry.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/shill_manager_client.h" |
+#include "chromeos/dbus/shill_service_client.h" |
+#include "dbus/object_path.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+ |
+namespace { |
+ |
+// These are keys used by the JavaScript code to access information in the |
+// result. |
+const char kBssid[] = "bssid"; |
+const char kName[] = "name"; |
+const char kNetworkId[] = "networkId"; |
+const char kNetworkTypeAll[] = "all"; |
+const char kStatus[] = "status"; |
+const char kType[] = "type"; |
+ |
+// This creates a new dictionary that only contains the information we're |
+// interested in passing on to JavaScript. |
+base::DictionaryValue* CreateFilteredResult( |
+ const base::DictionaryValue& properties) { |
+ scoped_ptr<base::DictionaryValue> filtered_result(new base::DictionaryValue); |
+ |
+ std::string bssid; |
+ if (properties.GetString(flimflam::kWifiBSsid, &bssid)) |
+ filtered_result->SetString(kBssid, bssid); |
+ std::string name; |
+ if (properties.GetString(flimflam::kNameProperty, &name)) { |
mazda
2013/01/16 22:08:52
nit: braces are not needed
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ filtered_result->SetString(kName, name); |
+ } |
+ std::string type; |
+ if (properties.GetString(flimflam::kTypeProperty, &type)) |
+ filtered_result->SetString(kType, type); |
+ std::string state; |
+ if (properties.GetString(flimflam::kStateProperty, &state)) { |
+ if (state == flimflam::kStateReady || state == flimflam::kStateOnline) { |
+ filtered_result->SetString(kStatus, "connected"); |
+ } else if (state == flimflam::kStateAssociation || |
+ state == flimflam::kStatePortal || |
+ state == flimflam::kStateConfiguration) { |
+ filtered_result->SetString(kStatus, "connecting"); |
+ } else { |
+ filtered_result->SetString(kStatus, "notConnected"); |
+ } |
+ } |
+ |
+ return filtered_result.release(); |
+} |
+ |
+} // namespace |
+ |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingGetPropertiesFunction |
+ |
+NetworkingGetPropertiesFunction::~NetworkingGetPropertiesFunction() {} |
+ |
+bool NetworkingGetPropertiesFunction::RunImpl() { |
+ std::string service_path; |
+ if (!args_->GetString(0, &service_path)) |
+ return false; |
mazda
2013/01/16 22:08:52
nit: indent with two spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ |
+ DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
+ dbus::ObjectPath(service_path), base::Bind( |
+ &NetworkingGetPropertiesFunction::ResultCallback, this)); |
+ return true; |
+} |
+ |
+void NetworkingGetPropertiesFunction::ResultCallback( |
+ DBusMethodCallStatus call_status, |
+ const base::DictionaryValue& result) { |
+ scoped_ptr<base::DictionaryValue> filtered_result( |
+ CreateFilteredResult(result)); |
+ |
+ SetResult(filtered_result.release()); |
+ SendResponse(true); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingGetVisibleNetworksFunction::ScopedDecrementer |
+ |
+NetworkingGetVisibleNetworksFunction::ScopedDecrementer::ScopedDecrementer( |
+ NetworkingGetVisibleNetworksFunction* parent, |
+ scoped_refptr<NetworkingGetVisibleNetworksFunction::ResultList> result_list) |
+ : parent_(parent), result_list_(result_list) { |
+} |
+ |
+NetworkingGetVisibleNetworksFunction::ScopedDecrementer::~ScopedDecrementer() { |
+ result_list_->count--; |
+ if (result_list_->count == 0) { |
+ parent_->SetResult(result_list_->list.release()); |
+ parent_->SendResponse(true); |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingGetVisibleNetworksFunction |
+ |
+NetworkingGetVisibleNetworksFunction::~NetworkingGetVisibleNetworksFunction() {} |
+ |
+bool NetworkingGetVisibleNetworksFunction::RunImpl() { |
+ std::string network_type; |
+ if (!args_->GetString(0, &network_type)) |
+ return false; |
mazda
2013/01/16 22:08:52
nit: indent with two spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ |
+ DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( |
+ base::Bind( |
+ &NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback, |
+ this, |
+ network_type)); |
+ return true; |
+} |
+ |
+// For each of the available services, fire off a request for its properties. |
+void NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback( |
+ const std::string& network_type, |
+ DBusMethodCallStatus call_status, |
+ const base::DictionaryValue& result) { |
+ const base::ListValue* available_services; |
+ if (!result.GetList(flimflam::kServicesProperty, &available_services)) { |
+ error_ = "Error.noServices"; |
+ SendResponse(false); |
+ return; |
+ } |
+ |
+ scoped_refptr<ResultList> results(new ResultList); |
+ results->count = available_services->GetSize(); |
+ for (size_t i = 0; i < available_services->GetSize(); ++i) { |
+ std::string service_path; |
+ available_services->GetString(i, &service_path); |
+ DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
+ dbus::ObjectPath(service_path), |
+ base::Bind( |
+ &NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback, |
+ this, |
+ service_path, |
+ network_type, |
+ results)); |
+ } |
+} |
+ |
+// If this network is of the appropriate type, add it to the results and |
+// decrement the count. If the count hits zero, then send the result. |
+void NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback( |
+ const std::string& service_path, |
+ const std::string& network_type, |
+ scoped_refptr<ResultList> result_list, |
+ DBusMethodCallStatus call_status, |
+ const base::DictionaryValue& result) { |
+ // We must decrement the count each time we get a callback, regardless of the |
+ // outcome. |
+ ScopedDecrementer decrementer(this, result_list); |
+ |
+ if (call_status != DBUS_METHOD_CALL_SUCCESS) |
+ return; |
+ |
+ std::string type; |
+ if (!result.GetString(flimflam::kTypeProperty, &type)) |
+ return; |
+ |
+ if (type == network_type || network_type == kNetworkTypeAll) { |
+ scoped_ptr<base::DictionaryValue> network_properties( |
+ CreateFilteredResult(result)); |
+ network_properties->SetString(kNetworkId, service_path); |
+ result_list->list->Append(network_properties.release()); |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingRequestConnectFunction |
+ |
+NetworkingRequestConnectFunction::~NetworkingRequestConnectFunction() {} |
+ |
+void NetworkingRequestConnectFunction::ConnectSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingRequestConnectFunction::ConnectFailed( |
+ const std::string& errorName, |
+ const std::string& errorMessage) { |
+ error_ = errorName; |
+ SendResponse(false); |
+} |
+ |
+bool NetworkingRequestConnectFunction::RunImpl() { |
+ std::string service_path; |
+ if (!args_->GetString(0, &service_path)) |
+ return false; |
mazda
2013/01/16 22:08:52
nit: indent with two spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ |
+ DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
+ dbus::ObjectPath(service_path), |
mazda
2013/01/16 22:08:52
nit: indent with four spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ base::Bind(&NetworkingRequestConnectFunction::ConnectSuccess, this), |
+ base::Bind(&NetworkingRequestConnectFunction::ConnectFailed, this)); |
+ return true; |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingRequestDisconnectFunction |
+ |
+NetworkingRequestDisconnectFunction::~NetworkingRequestDisconnectFunction() {} |
+ |
+void NetworkingRequestDisconnectFunction::ConnectSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingRequestDisconnectFunction::ConnectFailed( |
+ const std::string& errorName, |
+ const std::string& errorMessage) { |
+ error_ = errorName; |
+ SendResponse(false); |
+} |
+ |
+bool NetworkingRequestDisconnectFunction::RunImpl() { |
+ std::string service_path; |
+ if (!args_->GetString(0, &service_path)) |
+ return false; |
mazda
2013/01/16 22:08:52
nit: indent with two spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ |
+ DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
+ dbus::ObjectPath(service_path), |
mazda
2013/01/16 22:08:52
nit: indent with four spaces
Greg Spencer (Chromium)
2013/01/16 22:29:17
Done.
|
+ base::Bind(&NetworkingRequestDisconnectFunction::ConnectSuccess, this), |
+ base::Bind(&NetworkingRequestDisconnectFunction::ConnectFailed, this)); |
+ return true; |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingPrivateAPI |
+ |
+NetworkingPrivateAPI::NetworkingPrivateAPI(Profile* profile) { |
+ ExtensionFunctionRegistry* registry = |
+ ExtensionFunctionRegistry::GetInstance(); |
+ registry->RegisterFunction<NetworkingGetPropertiesFunction>(); |
+ registry->RegisterFunction<NetworkingGetVisibleNetworksFunction>(); |
+ registry->RegisterFunction<NetworkingRequestConnectFunction>(); |
+ registry->RegisterFunction<NetworkingRequestDisconnectFunction>(); |
+} |
+ |
+NetworkingPrivateAPI::~NetworkingPrivateAPI() { |
+} |
+ |
+void NetworkingPrivateAPI::Shutdown() { |
+} |
+ |
+// static |
+NetworkingPrivateAPI* NetworkingPrivateAPI::Get(Profile* profile) { |
+ return NetworkingPrivateAPIFactory::GetForProfile(profile); |
+} |
+ |
+} // namespace chromeos |