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::kNetworkWithStateSignature)); | |
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(&onc::kNetworkConfigurationSignature, | |
134 properties)); | |
135 | |
136 NetworkConfigurationHandler::Get()->SetProperties(service_path, | |
137 *shill_dictionary, | |
138 callback, | |
139 error_callback); | |
140 } | |
141 | |
142 void ManagedNetworkConfigurationHandler::Connect( | |
143 const std::string& service_path, | |
144 const base::Closure& callback, | |
145 const network_handler::ErrorCallback& error_callback) const { | |
146 // TODO(pneubeck): Update the user profile with tracked/followed settings of | |
147 // the shared profile. | |
148 NetworkConfigurationHandler::Get()->Connect(service_path, | |
149 callback, | |
150 error_callback); | |
151 } | |
152 | |
153 void ManagedNetworkConfigurationHandler::Disconnect( | |
154 const std::string& service_path, | |
155 const base::Closure& callback, | |
156 const network_handler::ErrorCallback& error_callback) const { | |
157 NetworkConfigurationHandler::Get()->Disconnect(service_path, | |
158 callback, | |
159 error_callback); | |
160 } | |
161 | |
162 void ManagedNetworkConfigurationHandler::CreateConfiguration( | |
163 const base::DictionaryValue& properties, | |
164 const network_handler::StringResultCallback& callback, | |
165 const network_handler::ErrorCallback& error_callback) const { | |
166 scoped_ptr<base::DictionaryValue> modified_properties( | |
167 properties.DeepCopy()); | |
168 | |
169 // If there isn't already a GUID attached to these properties, then | |
170 // generate one and add it. | |
171 std::string guid; | |
172 if (!properties.GetString(onc::network_config::kGUID, &guid)) { | |
173 guid = base::GenerateGUID(); | |
174 modified_properties->SetStringWithoutPathExpansion( | |
175 onc::network_config::kGUID, guid); | |
176 } else { | |
177 NOTREACHED(); // TODO(pneubeck): Return an error using error_callback. | |
178 } | |
179 | |
180 // TODO(pneubeck): Enforce policies. | |
181 | |
182 scoped_ptr<base::DictionaryValue> shill_dictionary( | |
183 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature, | |
184 properties)); | |
185 | |
186 NetworkConfigurationHandler::Get()->CreateConfiguration(*shill_dictionary, | |
187 callback, | |
188 error_callback); | |
189 } | |
190 | |
191 void ManagedNetworkConfigurationHandler::RemoveConfiguration( | |
192 const std::string& service_path, | |
193 const base::Closure& callback, | |
194 const network_handler::ErrorCallback& error_callback) const { | |
195 NetworkConfigurationHandler::Get()->RemoveConfiguration(service_path, | |
196 callback, | |
197 error_callback); | |
198 } | |
199 | |
200 ManagedNetworkConfigurationHandler::ManagedNetworkConfigurationHandler() { | |
201 } | |
202 | |
203 ManagedNetworkConfigurationHandler::~ManagedNetworkConfigurationHandler() { | |
204 } | |
205 | |
206 } // namespace chromeos | |
OLD | NEW |