OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/chromeos/net/managed_network_configuration_handler.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/guid.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/values.h" |
| 16 #include "chromeos/dbus/dbus_method_call_status.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/shill_manager_client.h" |
| 19 #include "chromeos/dbus/shill_service_client.h" |
| 20 #include "chromeos/network/network_configuration_handler.h" |
| 21 #include "chromeos/network/network_event_log.h" |
| 22 #include "chromeos/network/network_state.h" |
| 23 #include "chromeos/network/network_state_handler.h" |
| 24 #include "chromeos/network/onc/onc_constants.h" |
| 25 #include "chromeos/network/onc/onc_signature.h" |
| 26 #include "chromeos/network/onc/onc_translator.h" |
| 27 #include "dbus/object_path.h" |
| 28 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 29 |
| 30 namespace chromeos { |
| 31 |
| 32 namespace { |
| 33 |
| 34 ManagedNetworkConfigurationHandler* g_configuration_handler_instance = NULL; |
| 35 |
| 36 const char kLogModule[] = "ManagedNetworkConfigurationHandler"; |
| 37 |
| 38 // These are error strings used for error callbacks. None of these error |
| 39 // messages are user-facing: they should only appear in logs. |
| 40 const char kServicePath[] = "servicePath"; |
| 41 const char kSetOnUnconfiguredNetworkMessage[] = |
| 42 "Unable to modify properties of an unconfigured network."; |
| 43 const char kSetOnUnconfiguredNetwork[] = "Error.SetCalledOnUnconfiguredNetwork"; |
| 44 const char kUnknownServicePathMessage[] = "Service path is unknown."; |
| 45 const char kUnknownServicePath[] = "Error.UnknownServicePath"; |
| 46 |
| 47 void RunErrorCallback(const std::string& service_path, |
| 48 const std::string& error_name, |
| 49 const std::string& error_message, |
| 50 const network_handler::ErrorCallback& error_callback) { |
| 51 network_event_log::AddEntry(kLogModule, error_name, error_message); |
| 52 error_callback.Run( |
| 53 error_name, |
| 54 make_scoped_ptr( |
| 55 network_handler::CreateErrorData(service_path, |
| 56 error_name, |
| 57 error_message))); |
| 58 } |
| 59 |
| 60 void TranslatePropertiesAndRunCallback( |
| 61 const network_handler::DictionaryResultCallback& callback, |
| 62 const std::string& service_path, |
| 63 const base::DictionaryValue& shill_properties) { |
| 64 scoped_ptr<base::DictionaryValue> onc_network( |
| 65 onc::TranslateShillServiceToONCPart( |
| 66 shill_properties, |
| 67 &onc::kNetworkConfigurationSignature)); |
| 68 callback.Run(service_path, *onc_network); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 // static |
| 74 void ManagedNetworkConfigurationHandler::Initialize() { |
| 75 CHECK(!g_configuration_handler_instance); |
| 76 g_configuration_handler_instance = new ManagedNetworkConfigurationHandler; |
| 77 } |
| 78 |
| 79 // static |
| 80 bool ManagedNetworkConfigurationHandler::IsInitialized() { |
| 81 return g_configuration_handler_instance; |
| 82 } |
| 83 |
| 84 // static |
| 85 void ManagedNetworkConfigurationHandler::Shutdown() { |
| 86 CHECK(g_configuration_handler_instance); |
| 87 delete g_configuration_handler_instance; |
| 88 g_configuration_handler_instance = NULL; |
| 89 } |
| 90 |
| 91 // static |
| 92 ManagedNetworkConfigurationHandler* ManagedNetworkConfigurationHandler::Get() { |
| 93 CHECK(g_configuration_handler_instance); |
| 94 return g_configuration_handler_instance; |
| 95 } |
| 96 |
| 97 void ManagedNetworkConfigurationHandler::GetProperties( |
| 98 const std::string& service_path, |
| 99 const network_handler::DictionaryResultCallback& callback, |
| 100 const network_handler::ErrorCallback& error_callback) const { |
| 101 // TODO(pneubeck): Merge with policies. |
| 102 NetworkConfigurationHandler::Get()->GetProperties( |
| 103 service_path, |
| 104 base::Bind(&TranslatePropertiesAndRunCallback, callback), |
| 105 error_callback); |
| 106 } |
| 107 |
| 108 void ManagedNetworkConfigurationHandler::SetProperties( |
| 109 const std::string& service_path, |
| 110 const base::DictionaryValue& properties, |
| 111 const base::Closure& callback, |
| 112 const network_handler::ErrorCallback& error_callback) const { |
| 113 const NetworkState* state = |
| 114 NetworkStateHandler::Get()->GetNetworkState(service_path); |
| 115 |
| 116 if (!state) { |
| 117 RunErrorCallback(service_path, |
| 118 kUnknownServicePath, |
| 119 kUnknownServicePathMessage, |
| 120 error_callback); |
| 121 } |
| 122 std::string guid = state->guid(); |
| 123 if (guid.empty()) { |
| 124 RunErrorCallback(service_path, |
| 125 kSetOnUnconfiguredNetwork, |
| 126 kSetOnUnconfiguredNetworkMessage, |
| 127 error_callback); |
| 128 } |
| 129 |
| 130 // TODO(pneubeck): Enforce policies. |
| 131 |
| 132 scoped_ptr<base::DictionaryValue> shill_dictionary( |
| 133 onc::TranslateONCObjectToShill( |
| 134 &onc::kNetworkConfigurationSignature, |
| 135 properties)); |
| 136 |
| 137 NetworkConfigurationHandler::Get()->SetProperties(service_path, |
| 138 *shill_dictionary, |
| 139 callback, |
| 140 error_callback); |
| 141 } |
| 142 |
| 143 void ManagedNetworkConfigurationHandler::Connect( |
| 144 const std::string& service_path, |
| 145 const base::Closure& callback, |
| 146 const network_handler::ErrorCallback& error_callback) const { |
| 147 // TODO(pneubeck): Update the user profile with tracked/followed settings of |
| 148 // the shared profile. |
| 149 NetworkConfigurationHandler::Get()->Connect(service_path, |
| 150 callback, |
| 151 error_callback); |
| 152 } |
| 153 |
| 154 void ManagedNetworkConfigurationHandler::Disconnect( |
| 155 const std::string& service_path, |
| 156 const base::Closure& callback, |
| 157 const network_handler::ErrorCallback& error_callback) const { |
| 158 NetworkConfigurationHandler::Get()->Disconnect(service_path, |
| 159 callback, |
| 160 error_callback); |
| 161 } |
| 162 |
| 163 void ManagedNetworkConfigurationHandler::CreateConfiguration( |
| 164 const base::DictionaryValue& properties, |
| 165 const network_handler::StringResultCallback& callback, |
| 166 const network_handler::ErrorCallback& error_callback) const { |
| 167 scoped_ptr<base::DictionaryValue> modified_properties( |
| 168 properties.DeepCopy()); |
| 169 |
| 170 // If there isn't already a GUID attached to these properties, then |
| 171 // generate one and add it. |
| 172 std::string guid; |
| 173 if (!properties.GetString(onc::network_config::kGUID, &guid)) { |
| 174 guid = base::GenerateGUID(); |
| 175 modified_properties->SetStringWithoutPathExpansion( |
| 176 onc::network_config::kGUID, guid); |
| 177 } else { |
| 178 NOTREACHED(); // TODO(pneubeck): Return an error using error_callback. |
| 179 } |
| 180 |
| 181 // TODO(pneubeck): Enforce policies. |
| 182 |
| 183 scoped_ptr<base::DictionaryValue> shill_dictionary( |
| 184 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature, |
| 185 properties)); |
| 186 |
| 187 NetworkConfigurationHandler::Get()->CreateConfiguration(*shill_dictionary, |
| 188 callback, |
| 189 error_callback); |
| 190 } |
| 191 |
| 192 void ManagedNetworkConfigurationHandler::RemoveConfiguration( |
| 193 const std::string& service_path, |
| 194 const base::Closure& callback, |
| 195 const network_handler::ErrorCallback& error_callback) const { |
| 196 NetworkConfigurationHandler::Get()->RemoveConfiguration(service_path, |
| 197 callback, |
| 198 error_callback); |
| 199 } |
| 200 |
| 201 ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler() { |
| 202 } |
| 203 |
| 204 ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() { |
| 205 } |
| 206 |
| 207 } // namespace chromeos |
OLD | NEW |