Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / chromeos / net / proxy_config_handler.cc
blob297c0c0488c3136d6c9034b62ca3022921416f90
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"
7 #include "base/bind.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/common/pref_names.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/shill_service_client.h"
16 #include "chromeos/network/network_handler_callbacks.h"
17 #include "chromeos/network/network_profile.h"
18 #include "chromeos/network/network_profile_handler.h"
19 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "chromeos/network/onc/onc_utils.h"
22 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "components/proxy_config/proxy_config_dictionary.h"
24 #include "dbus/object_path.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
27 namespace chromeos {
29 namespace {
31 void NotifyNetworkStateHandler(const std::string& service_path) {
32 if (NetworkHandler::IsInitialized()) {
33 NetworkHandler::Get()->network_state_handler()->RequestUpdateForNetwork(
34 service_path);
38 } // namespace
40 namespace proxy_config {
42 scoped_ptr<ProxyConfigDictionary> GetProxyConfigForNetwork(
43 const PrefService* profile_prefs,
44 const PrefService* local_state_prefs,
45 const NetworkState& network,
46 ::onc::ONCSource* onc_source) {
47 const base::DictionaryValue* network_policy =
48 onc::GetPolicyForNetwork(
49 profile_prefs, local_state_prefs, network, onc_source);
51 if (network_policy) {
52 const base::DictionaryValue* proxy_policy = NULL;
53 network_policy->GetDictionaryWithoutPathExpansion(
54 ::onc::network_config::kProxySettings, &proxy_policy);
55 if (!proxy_policy) {
56 // This policy doesn't set a proxy for this network. Nonetheless, this
57 // disallows changes by the user.
58 return scoped_ptr<ProxyConfigDictionary>();
61 scoped_ptr<base::DictionaryValue> proxy_dict =
62 onc::ConvertOncProxySettingsToProxyConfig(*proxy_policy);
63 return make_scoped_ptr(new ProxyConfigDictionary(proxy_dict.get()));
66 if (network.profile_path().empty())
67 return scoped_ptr<ProxyConfigDictionary>();
69 const NetworkProfile* profile = NetworkHandler::Get()
70 ->network_profile_handler()->GetProfileForPath(network.profile_path());
71 if (!profile) {
72 VLOG(1) << "Unknown profile_path '" << network.profile_path() << "'.";
73 return scoped_ptr<ProxyConfigDictionary>();
75 if (!profile_prefs && profile->type() == NetworkProfile::TYPE_USER) {
76 // This case occurs, for example, if called from the proxy config tracker
77 // created for the system request context and the signin screen. Both don't
78 // use profile prefs and shouldn't depend on the user's not shared proxy
79 // settings.
80 VLOG(1)
81 << "Don't use unshared settings for system context or signin screen.";
82 return scoped_ptr<ProxyConfigDictionary>();
85 // No policy set for this network, read instead the user's (shared or
86 // unshared) configuration.
87 // The user's proxy setting is not stored in the Chrome preference yet. We
88 // still rely on Shill storing it.
89 const base::DictionaryValue& value = network.proxy_config();
90 if (value.empty())
91 return scoped_ptr<ProxyConfigDictionary>();
92 return make_scoped_ptr(new ProxyConfigDictionary(&value));
95 void SetProxyConfigForNetwork(const ProxyConfigDictionary& proxy_config,
96 const NetworkState& network) {
97 chromeos::ShillServiceClient* shill_service_client =
98 DBusThreadManager::Get()->GetShillServiceClient();
100 // The user's proxy setting is not stored in the Chrome preference yet. We
101 // still rely on Shill storing it.
102 ProxyPrefs::ProxyMode mode;
103 if (!proxy_config.GetMode(&mode) || mode == ProxyPrefs::MODE_DIRECT) {
104 // Return empty string for direct mode for portal check to work correctly.
105 // TODO(pneubeck): Consider removing this legacy code.
106 shill_service_client->ClearProperty(
107 dbus::ObjectPath(network.path()),
108 shill::kProxyConfigProperty,
109 base::Bind(&NotifyNetworkStateHandler, network.path()),
110 base::Bind(&network_handler::ShillErrorCallbackFunction,
111 "SetProxyConfig.ClearProperty Failed",
112 network.path(),
113 network_handler::ErrorCallback()));
114 } else {
115 std::string proxy_config_str;
116 base::JSONWriter::Write(proxy_config.GetDictionary(), &proxy_config_str);
117 shill_service_client->SetProperty(
118 dbus::ObjectPath(network.path()),
119 shill::kProxyConfigProperty,
120 base::StringValue(proxy_config_str),
121 base::Bind(&NotifyNetworkStateHandler, network.path()),
122 base::Bind(&network_handler::ShillErrorCallbackFunction,
123 "SetProxyConfig.SetProperty Failed",
124 network.path(),
125 network_handler::ErrorCallback()));
129 void RegisterPrefs(PrefRegistrySimple* registry) {
130 registry->RegisterListPref(prefs::kDeviceOpenNetworkConfiguration);
133 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
134 registry->RegisterBooleanPref(prefs::kUseSharedProxies, false);
136 registry->RegisterListPref(prefs::kOpenNetworkConfiguration);
139 } // namespace proxy_config
141 } // namespace chromeos