Index: chromeos/network/managed_network_configuration_handler.cc |
diff --git a/chromeos/network/managed_network_configuration_handler.cc b/chromeos/network/managed_network_configuration_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..49e4e09d38713b7e573ed9de7fd43b5acb5b24ec |
--- /dev/null |
+++ b/chromeos/network/managed_network_configuration_handler.cc |
@@ -0,0 +1,286 @@ |
+// Copyright (c) 2012 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 "chromeos/network/managed_network_configuration_handler.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/guid.h" |
+#include "base/logging.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
+#include "chromeos/dbus/dbus_method_call_status.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/network_configuration_handler.h" |
+#include "chromeos/network/network_state.h" |
+#include "chromeos/network/network_state_handler.h" |
+#include "chromeos/network/onc/onc_signature.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 { |
+ |
+ManagedNetworkConfigurationHandler* g_configuration_handler_instance = NULL; |
+ |
+const char kGuidTag[] = "guid"; |
+const char kUnableToFindGuid[] = "Error.UnableToFindGuid"; |
+const char kUnableToFindGuidMessage[] = |
+ "Unable to match GUID to a service path."; |
+const char kServicePathTag[] = "servicePath"; |
+const char kUnableToFindServicePath[] = "Error.UnableToFindServicePath"; |
+const char kUnableToFindServicePathMessage[] = |
+ "Unable to match service path to a GUID."; |
+const char kErrorName[] = "errorName"; |
+const char kErrorMessage[] = "errorMessage"; |
+ |
+void RunErrorCallback(const std::string& guid, |
+ const std::string& service_path, |
+ const network_handler::ErrorCallback& error_callback, |
+ const std::string& error_name, |
+ const std::string& error_message) { |
+ // Couldn't find the GUID, so fire off the error callback. |
+ scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue); |
+ error_data->SetString(kErrorName, error_name); |
+ error_data->SetString(kErrorMessage, error_message); |
+ if (!guid.empty()) |
+ error_data->SetString(kGuidTag, guid); |
+ if (!service_path.empty()) |
+ error_data->SetString(kServicePathTag, service_path); |
+ error_callback.Run(kUnableToFindGuid, error_data.Pass()); |
+} |
+ |
+std::string FindServicePathForGuid( |
+ const std::string& guid, |
+ const network_handler::ErrorCallback& error_callback) { |
+ NetworkStateHandler::NetworkStateList networks; |
+ NetworkStateHandler::Get()->GetNetworkList(&networks); |
+ |
+ for (NetworkStateHandler::NetworkStateList::iterator iter = networks.begin(); |
+ iter != networks.end(); ++iter) { |
+ if ((*iter)->guid() == guid) |
+ return (*iter)->path(); |
+ } |
+ RunErrorCallback(guid, |
+ "", |
+ error_callback, |
+ kUnableToFindGuid, |
+ kUnableToFindGuidMessage); |
+ return std::string(); |
+} |
+ |
+std::string FindGuidForServicePath( |
+ const std::string& service_path, |
+ const network_handler::ErrorCallback& error_callback) { |
+ NetworkStateHandler::NetworkStateList networks; |
+ NetworkStateHandler::Get()->GetNetworkList(&networks); |
+ |
+ for (NetworkStateHandler::NetworkStateList::iterator iter = networks.begin(); |
+ iter != networks.end(); ++iter) { |
+ if ((*iter)->path() == service_path) |
+ return (*iter)->guid(); |
+ } |
+ RunErrorCallback("", |
+ service_path, |
+ error_callback, |
+ kUnableToFindServicePath, |
+ kUnableToFindServicePathMessage); |
+ return std::string(); |
+} |
+ |
+void ErrorCallback(const std::string& guid, |
+ const network_handler::ErrorCallback& error_callback, |
+ const std::string& error_name, |
+ scoped_ptr<base::DictionaryValue> error_data) { |
+ error_data->SetString(kGuidTag, guid); |
+ error_callback.Run(error_name, error_data.Pass()); |
+} |
+ |
+// This is here so that we can pass back the GUID instead of the service path. |
+void ConfigurationCallback( |
+ const network_handler::StringResultCallback& callback, |
+ scoped_ptr<base::DictionaryValue> properties, |
+ const std::string& service_path) { |
+ std::string guid; |
+ if (!properties->GetString(flimflam::kGuidProperty, &guid)) { |
+ NOTREACHED() << "Missing GUID in new configuration."; |
+ return; |
+ } |
+ callback.Run(guid); |
+} |
+ |
+void TranslatePropertiesCallback( |
+ const network_handler::DictionaryResultCallback& callback, |
+ const network_handler::ErrorCallback& error_callback, |
+ const std::string& service_path, |
+ const base::DictionaryValue& result) { |
+ std::string guid = FindGuidForServicePath(service_path, error_callback); |
+ if (guid.empty()) |
+ return; |
+ |
+ scoped_ptr<base::DictionaryValue> onc_result( |
+ onc::TranslateShillServiceToONCPart( |
+ result, |
+ &onc::kNetworkConfigurationSignature)); |
+ callback.Run(guid, *onc_result); |
+} |
+ |
+} // namespace |
+ |
+// static |
+void ManagedNetworkConfigurationHandler::Initialize() { |
+ CHECK(!g_configuration_handler_instance); |
+ g_configuration_handler_instance = new ManagedNetworkConfigurationHandler; |
+} |
+ |
+// static |
+void ManagedNetworkConfigurationHandler::Shutdown() { |
+ CHECK(g_configuration_handler_instance); |
+ delete g_configuration_handler_instance; |
+ g_configuration_handler_instance = NULL; |
+} |
+ |
+// static |
+ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() { |
+ CHECK(g_configuration_handler_instance); |
+ return g_configuration_handler_instance; |
+} |
+ |
+void ManagedNetworkConfigurationHandler::GetProperties( |
+ const std::string& guid, |
+ const network_handler::DictionaryResultCallback& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ NetworkConfigurationHandler::Get()->GetProperties( |
+ service_path, |
+ base::Bind(&TranslatePropertiesCallback, callback, error_callback), |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::SetProperties( |
+ const std::string& guid, |
+ const base::DictionaryValue& properties, |
+ const base::Closure& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ // Convert the given ONC properties into a Shill dictionary. |
+ scoped_ptr<base::DictionaryValue> shill_result( |
+ onc::TranslateONCObjectToShill( |
+ properties, |
+ &onc::kNetworkConfigurationSignature)); |
+ |
+ NetworkConfigurationHandler::Get()->SetProperties( |
+ service_path, |
+ *shill_result, |
+ callback, |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::ClearProperties( |
+ const std::string& guid, |
+ const std::vector<std::string>& names, |
+ const base::Closure& callback, |
+ const network_handler::ErrorCallback& error_callback) { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ std::vector<std::string> shill_names = onc::TranslateONCPropertyNamesToShill( |
+ names, &onc::kNetworkConfigurationSignature); |
+ |
+ NetworkConfigurationHandler::Get()->ClearProperties( |
+ service_path, |
+ shill_names, |
+ callback, |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::Connect( |
+ const std::string& guid, |
+ const base::Closure& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ NetworkConfigurationHandler::Get()->Connect( |
+ service_path, |
+ callback, |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::Disconnect( |
+ const std::string& guid, |
+ const base::Closure& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ NetworkConfigurationHandler::Get()->Disconnect( |
+ service_path, |
+ callback, |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::CreateConfiguration( |
+ const base::DictionaryValue& properties, |
+ const network_handler::StringResultCallback& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ // Convert this to a Shill dictionary. |
+ scoped_ptr<base::DictionaryValue> shill_properties( |
+ onc::TranslateONCObjectToShill( |
+ properties, |
+ &onc::kNetworkConfigurationSignature)); |
+ |
+ // If there isn't already a GUID attached to these properties, then |
+ // generate one and add it. |
+ std::string existing_guid; |
+ if (!properties.GetString(flimflam::kGuidProperty, &existing_guid)) { |
+ existing_guid = base::GenerateGUID(); |
+ shill_properties->SetString(flimflam::kGuidProperty, existing_guid); |
+ } |
+ |
+ NetworkConfigurationHandler::Get()->CreateConfiguration( |
+ *shill_properties, |
+ base::Bind(&ConfigurationCallback, |
+ callback, |
+ base::Passed(&shill_properties)), |
+ base::Bind(&ErrorCallback, existing_guid, error_callback)); |
+} |
+ |
+void ManagedNetworkConfigurationHandler::RemoveConfiguration( |
+ const std::string& guid, |
+ const base::Closure& callback, |
+ const network_handler::ErrorCallback& error_callback) const { |
+ std::string service_path = FindServicePathForGuid(guid, error_callback); |
+ if (service_path.empty()) |
+ return; |
+ |
+ NetworkConfigurationHandler::Get()->RemoveConfiguration( |
+ service_path, |
+ callback, |
+ base::Bind(&ErrorCallback, guid, error_callback)); |
+} |
+ |
+ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler() { |
+} |
+ |
+ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() { |
+} |
+ |
+} // namespace chromeos |