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.
5 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/values.h"
12 #include "chrome/browser/chromeos/net/onc_utils.h"
13 #include "chrome/browser/prefs/proxy_config_dictionary.h"
14 #include "chrome/common/pref_names.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_service_client.h"
17 #include "chromeos/network/network_handler_callbacks.h"
18 #include "chromeos/network/network_profile.h"
19 #include "chromeos/network/network_profile_handler.h"
20 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_state_handler.h"
22 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "dbus/object_path.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
30 void NotifyNetworkStateHandler(const std::string
& service_path
) {
31 if (NetworkHandler::IsInitialized()) {
32 NetworkHandler::Get()->network_state_handler()->RequestUpdateForNetwork(
39 namespace proxy_config
{
41 scoped_ptr
<ProxyConfigDictionary
> GetProxyConfigForNetwork(
42 const PrefService
* profile_prefs
,
43 const PrefService
* local_state_prefs
,
44 const NetworkState
& network
,
45 ::onc::ONCSource
* onc_source
) {
46 const base::DictionaryValue
* network_policy
=
47 onc::GetPolicyForNetwork(
48 profile_prefs
, local_state_prefs
, network
, onc_source
);
51 const base::DictionaryValue
* proxy_policy
= NULL
;
52 network_policy
->GetDictionaryWithoutPathExpansion(
53 ::onc::network_config::kProxySettings
, &proxy_policy
);
55 // This policy doesn't set a proxy for this network. Nonetheless, this
56 // disallows changes by the user.
57 return scoped_ptr
<ProxyConfigDictionary
>();
60 scoped_ptr
<base::DictionaryValue
> proxy_dict
=
61 onc::ConvertOncProxySettingsToProxyConfig(*proxy_policy
);
62 return make_scoped_ptr(new ProxyConfigDictionary(proxy_dict
.get()));
65 if (network
.profile_path().empty())
66 return scoped_ptr
<ProxyConfigDictionary
>();
68 const NetworkProfile
* profile
= NetworkHandler::Get()
69 ->network_profile_handler()->GetProfileForPath(network
.profile_path());
71 VLOG(1) << "Unknown profile_path '" << network
.profile_path() << "'.";
72 return scoped_ptr
<ProxyConfigDictionary
>();
74 if (!profile_prefs
&& profile
->type() == NetworkProfile::TYPE_USER
) {
75 // This case occurs, for example, if called from the proxy config tracker
76 // created for the system request context and the signin screen. Both don't
77 // use profile prefs and shouldn't depend on the user's not shared proxy
80 << "Don't use unshared settings for system context or signin screen.";
81 return scoped_ptr
<ProxyConfigDictionary
>();
84 // No policy set for this network, read instead the user's (shared or
85 // unshared) configuration.
86 // The user's proxy setting is not stored in the Chrome preference yet. We
87 // still rely on Shill storing it.
88 const base::DictionaryValue
& value
= network
.proxy_config();
90 return scoped_ptr
<ProxyConfigDictionary
>();
91 return make_scoped_ptr(new ProxyConfigDictionary(&value
));
94 void SetProxyConfigForNetwork(const ProxyConfigDictionary
& proxy_config
,
95 const NetworkState
& network
) {
96 chromeos::ShillServiceClient
* shill_service_client
=
97 DBusThreadManager::Get()->GetShillServiceClient();
99 // The user's proxy setting is not stored in the Chrome preference yet. We
100 // still rely on Shill storing it.
101 ProxyPrefs::ProxyMode mode
;
102 if (!proxy_config
.GetMode(&mode
) || mode
== ProxyPrefs::MODE_DIRECT
) {
103 // Return empty string for direct mode for portal check to work correctly.
104 // TODO(pneubeck): Consider removing this legacy code.
105 shill_service_client
->ClearProperty(
106 dbus::ObjectPath(network
.path()),
107 shill::kProxyConfigProperty
,
108 base::Bind(&NotifyNetworkStateHandler
, network
.path()),
109 base::Bind(&network_handler::ShillErrorCallbackFunction
,
110 "SetProxyConfig.ClearProperty Failed",
112 network_handler::ErrorCallback()));
114 std::string proxy_config_str
;
115 base::JSONWriter::Write(&proxy_config
.GetDictionary(), &proxy_config_str
);
116 shill_service_client
->SetProperty(
117 dbus::ObjectPath(network
.path()),
118 shill::kProxyConfigProperty
,
119 base::StringValue(proxy_config_str
),
120 base::Bind(&NotifyNetworkStateHandler
, network
.path()),
121 base::Bind(&network_handler::ShillErrorCallbackFunction
,
122 "SetProxyConfig.SetProperty Failed",
124 network_handler::ErrorCallback()));
128 void RegisterPrefs(PrefRegistrySimple
* registry
) {
129 registry
->RegisterListPref(prefs::kDeviceOpenNetworkConfiguration
);
132 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
133 registry
->RegisterBooleanPref(
134 prefs::kUseSharedProxies
,
136 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
138 registry
->RegisterListPref(prefs::kOpenNetworkConfiguration
,
139 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
142 } // namespace proxy_config
144 } // namespace chromeos