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..d5ea1925f0f783038a0f9cd81e7c0d6aa19c46e3 |
--- /dev/null |
+++ b/chrome/browser/chromeos/extensions/networking_private_api.cc |
@@ -0,0 +1,289 @@ |
+// 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 "chromeos/network/onc/onc_constants.h" |
+#include "chromeos/network/onc/onc_signature.h" |
+#include "chromeos/network/onc/onc_translation_tables.h" |
+#include "chromeos/network/onc/onc_translator.h" |
+#include "dbus/object_path.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+ |
+namespace { |
+ |
+// This creates a new ONC 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> onc_properties( |
+ onc::TranslateShillServiceToONCPart( |
+ properties, |
+ &onc::kNetworkConfigurationSignature)); |
+ |
+ // Now we filter it so we only include properties that we care about for this |
+ // interface. |
+ const char* desired_fields[] = { |
pneubeck (no reviews)
2013/01/18 13:22:36
may add static
but could even be
static const ch
Greg Spencer (Chromium)
2013/01/18 19:53:56
Done.
|
+ onc::network_type::kWiFi, |
+ onc::kName, |
+ onc::kGUID, |
+ onc::kType, |
+ onc::kStatus, |
+ }; |
+ |
+ scoped_ptr<base::DictionaryValue> filtered_result(new base::DictionaryValue); |
+ for (size_t i = 0; i < arraysize(desired_fields); ++i) { |
+ base::Value* value; |
+ if (onc_properties->Get(desired_fields[i], &value)) |
+ filtered_result->Set(desired_fields[i], value->DeepCopy()); |
+ } |
+ return filtered_result.release(); |
+} |
+ |
+} // namespace |
+ |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingGetPropertiesFunction |
+ |
+NetworkingGetPropertiesFunction::~NetworkingGetPropertiesFunction() { |
+} |
+ |
+bool NetworkingGetPropertiesFunction::RunImpl() { |
+ std::string service_path; |
+ if (!args_->GetString(0, &service_path)) |
+ return false; |
+ |
+ 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::ResultList |
+ |
+NetworkingGetVisibleNetworksFunction::ResultList::ResultList(int count) |
+ : list_(new base::ListValue), count_(count) { |
+} |
+ |
+NetworkingGetVisibleNetworksFunction::ResultList::~ResultList() { |
+} |
+ |
+void NetworkingGetVisibleNetworksFunction::ResultList::Append( |
+ base::Value* value) { |
+ list_->Append(value); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// 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_->release()); |
+ parent_->SendResponse(true); |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingGetVisibleNetworksFunction |
+ |
+NetworkingGetVisibleNetworksFunction::~NetworkingGetVisibleNetworksFunction() { |
+} |
+ |
+bool NetworkingGetVisibleNetworksFunction::RunImpl() { |
+ std::string network_type; |
+ if (!args_->GetString(0, &network_type)) |
+ return false; |
+ |
+ 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(available_services->GetSize())); |
+ for (size_t i = 0; i < available_services->GetSize(); ++i) { |
pneubeck (no reviews)
2013/01/18 13:22:36
As you don't need the index, I'd prefer using List
Greg Spencer (Chromium)
2013/01/18 19:53:56
Done.
|
+ 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); |
pneubeck (no reviews)
2013/01/18 13:22:36
The only usage now and in the future seems to be t
Greg Spencer (Chromium)
2013/01/18 19:53:56
Yes, I suppose that's simpler. When I wrote the d
|
+ |
+ if (call_status != DBUS_METHOD_CALL_SUCCESS) |
+ return; |
+ |
+ std::string shill_type; |
+ if (!result.GetString(flimflam::kTypeProperty, &shill_type)) |
+ return; |
+ |
+ std::string onc_type; |
+ if (!TranslateStringToONC(onc::kNetworkTypeTable, shill_type, &onc_type)) |
+ return; |
+ |
+ if (onc_type == network_type || |
+ network_type == onc::network_type::kAllTypes) { |
+ // TODO(gspencer): For now the "guid" we send back is going to look |
+ // remarkably like the service path. Once this code starts using the |
+ // NetworkStateHandler instead of Shill directly, we should remove this code |
+ // so that we're sending back the actual GUID. The JavaScript shouldn't |
+ // care: this ID is opaque to it, and it shouldn't store it anywhere. |
+ scoped_ptr<base::DictionaryValue> filtered_result( |
+ CreateFilteredResult(result)); |
+ filtered_result->SetString(onc::kGUID, service_path); |
+ result_list->Append(filtered_result.release()); |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingRequestConnectFunction |
+ |
+NetworkingRequestConnectFunction::~NetworkingRequestConnectFunction() { |
+} |
+ |
+void NetworkingRequestConnectFunction::ConnectRequestSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingRequestConnectFunction::ConnectRequestFailed( |
+ const std::string& errorName, |
+ const std::string& errorMessage) { |
+ error_ = errorName; |
+ SendResponse(false); |
+} |
+ |
+bool NetworkingRequestConnectFunction::RunImpl() { |
+ std::string guid; |
+ if (!args_->GetString(0, &guid)) |
+ return false; |
+ |
+ // TODO(gspencer): For now, the "guid" we receive from the JavaScript is going |
+ // to be the service path. Fix this so it actually looks up the service path |
+ // from the GUID once we're using the NetworkStateHandler. |
+ std::string service_path = guid; |
+ |
+ DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
+ dbus::ObjectPath(service_path), |
+ base::Bind(&NetworkingRequestConnectFunction::ConnectRequestSuccess, |
+ this), |
+ base::Bind(&NetworkingRequestConnectFunction::ConnectRequestFailed, |
+ this)); |
+ return true; |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// NetworkingRequestDisconnectFunction |
+ |
+NetworkingRequestDisconnectFunction::~NetworkingRequestDisconnectFunction() { |
+} |
+ |
+void NetworkingRequestDisconnectFunction::DisconnectRequestSuccess() { |
+ SendResponse(true); |
+} |
+ |
+void NetworkingRequestDisconnectFunction::DisconnectRequestFailed( |
+ 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; |
+ |
+ DBusThreadManager::Get()->GetShillServiceClient()->Connect( |
+ dbus::ObjectPath(service_path), |
+ base::Bind(&NetworkingRequestDisconnectFunction::DisconnectRequestSuccess, |
+ this), |
+ base::Bind(&NetworkingRequestDisconnectFunction::DisconnectRequestFailed, |
+ 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 |