1 // Copyright 2014 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.
5 #include "extensions/browser/api/networking_private/networking_private_chromeos.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h"
12 #include "chromeos/login/login_state.h"
13 #include "chromeos/network/managed_network_configuration_handler.h"
14 #include "chromeos/network/network_activation_handler.h"
15 #include "chromeos/network/network_connection_handler.h"
16 #include "chromeos/network/network_device_handler.h"
17 #include "chromeos/network/network_event_log.h"
18 #include "chromeos/network/network_state.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "chromeos/network/network_util.h"
21 #include "chromeos/network/onc/onc_signature.h"
22 #include "chromeos/network/onc/onc_translator.h"
23 #include "chromeos/network/onc/onc_utils.h"
24 #include "chromeos/network/portal_detector/network_portal_detector.h"
25 #include "components/onc/onc_constants.h"
26 #include "content/public/browser/browser_context.h"
27 #include "extensions/browser/api/networking_private/networking_private_api.h"
28 #include "extensions/browser/extensions_browser_client.h"
29 #include "extensions/common/api/networking_private.h"
31 using chromeos::NetworkHandler
;
32 using chromeos::NetworkTypePattern
;
33 using chromeos::ShillManagerClient
;
34 using extensions::NetworkingPrivateDelegate
;
38 chromeos::NetworkStateHandler
* GetStateHandler() {
39 return NetworkHandler::Get()->network_state_handler();
42 chromeos::ManagedNetworkConfigurationHandler
* GetManagedConfigurationHandler() {
43 return NetworkHandler::Get()->managed_network_configuration_handler();
46 bool GetServicePathFromGuid(const std::string
& guid
,
47 std::string
* service_path
,
49 const chromeos::NetworkState
* network
=
50 GetStateHandler()->GetNetworkStateFromGuid(guid
);
52 *error
= extensions::networking_private::kErrorInvalidNetworkGuid
;
55 *service_path
= network
->path();
59 bool GetUserIdHash(content::BrowserContext
* browser_context
,
60 std::string
* user_hash
,
62 std::string context_user_hash
=
63 extensions::ExtensionsBrowserClient::Get()->GetUserIdHashFromContext(
66 // Currently Chrome OS only configures networks for the primary user.
67 // Configuration attempts from other browser contexts should fail.
68 if (context_user_hash
!= chromeos::LoginState::Get()->primary_user_hash()) {
69 // Disallow class requiring a user id hash from a non-primary user context
70 // to avoid complexities with the policy code.
71 LOG(ERROR
) << "networkingPrivate API call from non primary user: "
73 *error
= "Error.NonPrimaryUser";
76 *user_hash
= context_user_hash
;
80 void NetworkHandlerDictionaryCallback(
81 const NetworkingPrivateDelegate::DictionaryCallback
& callback
,
82 const std::string
& service_path
,
83 const base::DictionaryValue
& dictionary
) {
84 scoped_ptr
<base::DictionaryValue
> dictionary_copy(dictionary
.DeepCopy());
85 callback
.Run(dictionary_copy
.Pass());
88 void NetworkHandlerFailureCallback(
89 const NetworkingPrivateDelegate::FailureCallback
& callback
,
90 const std::string
& error_name
,
91 scoped_ptr
<base::DictionaryValue
> error_data
) {
92 callback
.Run(error_name
);
97 ////////////////////////////////////////////////////////////////////////////////
99 namespace extensions
{
101 NetworkingPrivateChromeOS::NetworkingPrivateChromeOS(
102 content::BrowserContext
* browser_context
,
103 scoped_ptr
<VerifyDelegate
> verify_delegate
)
104 : NetworkingPrivateDelegate(verify_delegate
.Pass()),
105 browser_context_(browser_context
) {
108 NetworkingPrivateChromeOS::~NetworkingPrivateChromeOS() {
111 void NetworkingPrivateChromeOS::GetProperties(
112 const std::string
& guid
,
113 const DictionaryCallback
& success_callback
,
114 const FailureCallback
& failure_callback
) {
115 std::string service_path
, error
;
116 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
117 failure_callback
.Run(error
);
121 GetManagedConfigurationHandler()->GetProperties(
123 base::Bind(&NetworkHandlerDictionaryCallback
, success_callback
),
124 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
127 void NetworkingPrivateChromeOS::GetManagedProperties(
128 const std::string
& guid
,
129 const DictionaryCallback
& success_callback
,
130 const FailureCallback
& failure_callback
) {
131 std::string service_path
, error
;
132 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
133 failure_callback
.Run(error
);
137 std::string user_id_hash
;
138 if (!GetUserIdHash(browser_context_
, &user_id_hash
, &error
)) {
139 failure_callback
.Run(error
);
143 GetManagedConfigurationHandler()->GetManagedProperties(
144 user_id_hash
, service_path
,
145 base::Bind(&NetworkHandlerDictionaryCallback
, success_callback
),
146 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
149 void NetworkingPrivateChromeOS::GetState(
150 const std::string
& guid
,
151 const DictionaryCallback
& success_callback
,
152 const FailureCallback
& failure_callback
) {
153 std::string service_path
, error
;
154 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
155 failure_callback
.Run(error
);
159 const chromeos::NetworkState
* network_state
=
160 GetStateHandler()->GetNetworkStateFromServicePath(
161 service_path
, false /* configured_only */);
162 if (!network_state
) {
163 failure_callback
.Run(networking_private::kErrorNetworkUnavailable
);
167 scoped_ptr
<base::DictionaryValue
> network_properties
=
168 chromeos::network_util::TranslateNetworkStateToONC(network_state
);
170 success_callback
.Run(network_properties
.Pass());
173 void NetworkingPrivateChromeOS::SetProperties(
174 const std::string
& guid
,
175 scoped_ptr
<base::DictionaryValue
> properties
,
176 const VoidCallback
& success_callback
,
177 const FailureCallback
& failure_callback
) {
178 std::string service_path
, error
;
179 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
180 failure_callback
.Run(error
);
184 GetManagedConfigurationHandler()->SetProperties(
185 service_path
, *properties
, success_callback
,
186 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
189 void NetworkingPrivateChromeOS::CreateNetwork(
191 scoped_ptr
<base::DictionaryValue
> properties
,
192 const StringCallback
& success_callback
,
193 const FailureCallback
& failure_callback
) {
194 std::string user_id_hash
, error
;
195 // Do not allow configuring a non-shared network from a non-primary user.
196 if (!shared
&& !GetUserIdHash(browser_context_
, &user_id_hash
, &error
)) {
197 failure_callback
.Run(error
);
201 GetManagedConfigurationHandler()->CreateConfiguration(
202 user_id_hash
, *properties
, success_callback
,
203 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
206 void NetworkingPrivateChromeOS::ForgetNetwork(
207 const std::string
& guid
,
208 const VoidCallback
& success_callback
,
209 const FailureCallback
& failure_callback
) {
210 std::string service_path
, error
;
211 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
212 failure_callback
.Run(error
);
216 GetManagedConfigurationHandler()->RemoveConfiguration(
217 service_path
, success_callback
,
218 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
221 void NetworkingPrivateChromeOS::GetNetworks(
222 const std::string
& network_type
,
223 bool configured_only
,
226 const NetworkListCallback
& success_callback
,
227 const FailureCallback
& failure_callback
) {
228 NetworkTypePattern pattern
=
229 chromeos::onc::NetworkTypePatternFromOncType(network_type
);
230 scoped_ptr
<base::ListValue
> network_properties_list
=
231 chromeos::network_util::TranslateNetworkListToONC(
232 pattern
, configured_only
, visible_only
, limit
, false /* debugging */);
233 success_callback
.Run(network_properties_list
.Pass());
236 void NetworkingPrivateChromeOS::StartConnect(
237 const std::string
& guid
,
238 const VoidCallback
& success_callback
,
239 const FailureCallback
& failure_callback
) {
240 std::string service_path
, error
;
241 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
242 failure_callback
.Run(error
);
246 const bool check_error_state
= false;
247 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
248 service_path
, success_callback
,
249 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
),
253 void NetworkingPrivateChromeOS::StartDisconnect(
254 const std::string
& guid
,
255 const VoidCallback
& success_callback
,
256 const FailureCallback
& failure_callback
) {
257 std::string service_path
, error
;
258 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
259 failure_callback
.Run(error
);
263 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
264 service_path
, success_callback
,
265 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
268 void NetworkingPrivateChromeOS::StartActivate(
269 const std::string
& guid
,
270 const std::string
& carrier
,
271 const VoidCallback
& success_callback
,
272 const FailureCallback
& failure_callback
) {
273 std::string service_path
, error
;
274 if (!GetServicePathFromGuid(guid
, &service_path
, &error
)) {
275 failure_callback
.Run(error
);
279 NetworkHandler::Get()->network_activation_handler()->Activate(
280 service_path
, carrier
, success_callback
,
281 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
284 void NetworkingPrivateChromeOS::SetWifiTDLSEnabledState(
285 const std::string
& ip_or_mac_address
,
287 const StringCallback
& success_callback
,
288 const FailureCallback
& failure_callback
) {
289 NetworkHandler::Get()->network_device_handler()->SetWifiTDLSEnabled(
290 ip_or_mac_address
, enabled
, success_callback
,
291 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
294 void NetworkingPrivateChromeOS::GetWifiTDLSStatus(
295 const std::string
& ip_or_mac_address
,
296 const StringCallback
& success_callback
,
297 const FailureCallback
& failure_callback
) {
298 NetworkHandler::Get()->network_device_handler()->GetWifiTDLSStatus(
299 ip_or_mac_address
, success_callback
,
300 base::Bind(&NetworkHandlerFailureCallback
, failure_callback
));
303 void NetworkingPrivateChromeOS::GetCaptivePortalStatus(
304 const std::string
& guid
,
305 const StringCallback
& success_callback
,
306 const FailureCallback
& failure_callback
) {
307 if (!chromeos::NetworkPortalDetector::IsInitialized()) {
308 failure_callback
.Run(networking_private::kErrorNotReady
);
312 chromeos::NetworkPortalDetector::CaptivePortalState state
=
313 chromeos::NetworkPortalDetector::Get()->GetCaptivePortalState(guid
);
314 success_callback
.Run(
315 chromeos::NetworkPortalDetector::CaptivePortalStatusString(state
.status
));
318 scoped_ptr
<base::ListValue
>
319 NetworkingPrivateChromeOS::GetEnabledNetworkTypes() {
320 chromeos::NetworkStateHandler
* state_handler
= GetStateHandler();
322 scoped_ptr
<base::ListValue
> network_list(new base::ListValue
);
324 if (state_handler
->IsTechnologyEnabled(NetworkTypePattern::Ethernet()))
325 network_list
->AppendString(::onc::network_type::kEthernet
);
326 if (state_handler
->IsTechnologyEnabled(NetworkTypePattern::WiFi()))
327 network_list
->AppendString(::onc::network_type::kWiFi
);
328 if (state_handler
->IsTechnologyEnabled(NetworkTypePattern::Wimax()))
329 network_list
->AppendString(::onc::network_type::kWimax
);
330 if (state_handler
->IsTechnologyEnabled(NetworkTypePattern::Cellular()))
331 network_list
->AppendString(::onc::network_type::kCellular
);
333 return network_list
.Pass();
336 bool NetworkingPrivateChromeOS::EnableNetworkType(const std::string
& type
) {
337 NetworkTypePattern pattern
=
338 chromeos::onc::NetworkTypePatternFromOncType(type
);
340 GetStateHandler()->SetTechnologyEnabled(
341 pattern
, true, chromeos::network_handler::ErrorCallback());
346 bool NetworkingPrivateChromeOS::DisableNetworkType(const std::string
& type
) {
347 NetworkTypePattern pattern
=
348 chromeos::onc::NetworkTypePatternFromOncType(type
);
350 GetStateHandler()->SetTechnologyEnabled(
351 pattern
, false, chromeos::network_handler::ErrorCallback());
356 bool NetworkingPrivateChromeOS::RequestScan() {
357 GetStateHandler()->RequestScan();
361 } // namespace extensions