| 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/proxy_config_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/ptr_util.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/prefs/pref_registry_simple.h" | |
| 24 #include "components/proxy_config/proxy_config_dictionary.h" | |
| 25 #include "dbus/object_path.h" | |
| 26 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 27 | |
| 28 namespace chromeos { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 void NotifyNetworkStateHandler(const std::string& service_path) { | |
| 33 if (NetworkHandler::IsInitialized()) { | |
| 34 NetworkHandler::Get()->network_state_handler()->RequestUpdateForNetwork( | |
| 35 service_path); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 namespace proxy_config { | |
| 42 | |
| 43 std::unique_ptr<ProxyConfigDictionary> GetProxyConfigForNetwork( | |
| 44 const PrefService* profile_prefs, | |
| 45 const PrefService* local_state_prefs, | |
| 46 const NetworkState& network, | |
| 47 ::onc::ONCSource* onc_source) { | |
| 48 const base::DictionaryValue* network_policy = | |
| 49 onc::GetPolicyForNetwork( | |
| 50 profile_prefs, local_state_prefs, network, onc_source); | |
| 51 | |
| 52 if (network_policy) { | |
| 53 const base::DictionaryValue* proxy_policy = NULL; | |
| 54 network_policy->GetDictionaryWithoutPathExpansion( | |
| 55 ::onc::network_config::kProxySettings, &proxy_policy); | |
| 56 if (!proxy_policy) { | |
| 57 // This policy doesn't set a proxy for this network. Nonetheless, this | |
| 58 // disallows changes by the user. | |
| 59 return std::unique_ptr<ProxyConfigDictionary>(); | |
| 60 } | |
| 61 | |
| 62 std::unique_ptr<base::DictionaryValue> proxy_dict = | |
| 63 onc::ConvertOncProxySettingsToProxyConfig(*proxy_policy); | |
| 64 return base::MakeUnique<ProxyConfigDictionary>(proxy_dict.get()); | |
| 65 } | |
| 66 | |
| 67 if (network.profile_path().empty()) | |
| 68 return std::unique_ptr<ProxyConfigDictionary>(); | |
| 69 | |
| 70 const NetworkProfile* profile = NetworkHandler::Get() | |
| 71 ->network_profile_handler()->GetProfileForPath(network.profile_path()); | |
| 72 if (!profile) { | |
| 73 VLOG(1) << "Unknown profile_path '" << network.profile_path() << "'."; | |
| 74 return std::unique_ptr<ProxyConfigDictionary>(); | |
| 75 } | |
| 76 if (!profile_prefs && profile->type() == NetworkProfile::TYPE_USER) { | |
| 77 // This case occurs, for example, if called from the proxy config tracker | |
| 78 // created for the system request context and the signin screen. Both don't | |
| 79 // use profile prefs and shouldn't depend on the user's not shared proxy | |
| 80 // settings. | |
| 81 VLOG(1) | |
| 82 << "Don't use unshared settings for system context or signin screen."; | |
| 83 return std::unique_ptr<ProxyConfigDictionary>(); | |
| 84 } | |
| 85 | |
| 86 // No policy set for this network, read instead the user's (shared or | |
| 87 // unshared) configuration. | |
| 88 // The user's proxy setting is not stored in the Chrome preference yet. We | |
| 89 // still rely on Shill storing it. | |
| 90 const base::DictionaryValue& value = network.proxy_config(); | |
| 91 if (value.empty()) | |
| 92 return std::unique_ptr<ProxyConfigDictionary>(); | |
| 93 return base::MakeUnique<ProxyConfigDictionary>(&value); | |
| 94 } | |
| 95 | |
| 96 void SetProxyConfigForNetwork(const ProxyConfigDictionary& proxy_config, | |
| 97 const NetworkState& network) { | |
| 98 chromeos::ShillServiceClient* shill_service_client = | |
| 99 DBusThreadManager::Get()->GetShillServiceClient(); | |
| 100 | |
| 101 // The user's proxy setting is not stored in the Chrome preference yet. We | |
| 102 // still rely on Shill storing it. | |
| 103 ProxyPrefs::ProxyMode mode; | |
| 104 if (!proxy_config.GetMode(&mode) || mode == ProxyPrefs::MODE_DIRECT) { | |
| 105 // Return empty string for direct mode for portal check to work correctly. | |
| 106 // TODO(pneubeck): Consider removing this legacy code. | |
| 107 shill_service_client->ClearProperty( | |
| 108 dbus::ObjectPath(network.path()), | |
| 109 shill::kProxyConfigProperty, | |
| 110 base::Bind(&NotifyNetworkStateHandler, network.path()), | |
| 111 base::Bind(&network_handler::ShillErrorCallbackFunction, | |
| 112 "SetProxyConfig.ClearProperty Failed", | |
| 113 network.path(), | |
| 114 network_handler::ErrorCallback())); | |
| 115 } else { | |
| 116 std::string proxy_config_str; | |
| 117 base::JSONWriter::Write(proxy_config.GetDictionary(), &proxy_config_str); | |
| 118 shill_service_client->SetProperty( | |
| 119 dbus::ObjectPath(network.path()), | |
| 120 shill::kProxyConfigProperty, | |
| 121 base::StringValue(proxy_config_str), | |
| 122 base::Bind(&NotifyNetworkStateHandler, network.path()), | |
| 123 base::Bind(&network_handler::ShillErrorCallbackFunction, | |
| 124 "SetProxyConfig.SetProperty Failed", | |
| 125 network.path(), | |
| 126 network_handler::ErrorCallback())); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace proxy_config | |
| 131 | |
| 132 } // namespace chromeos | |
| OLD | NEW |