1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/net/proxy_config_handler.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/bind.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/json/json_writer.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h"
10ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/prefs/pref_registry_simple.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/values.h"
12ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/browser/chromeos/net/onc_utils.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/prefs/proxy_config_dictionary.h"
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/common/pref_names.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/shill_service_client.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_handler_callbacks.h"
18ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chromeos/network/network_profile.h"
19ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chromeos/network/network_profile_handler.h"
20f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chromeos/network/network_state.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state_handler.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "dbus/object_path.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace chromeos {
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
28ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochnamespace {
29ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void NotifyNetworkStateHandler(const std::string& service_path) {
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (NetworkHandler::IsInitialized()) {
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    NetworkHandler::Get()->network_state_handler()->RequestUpdateForNetwork(
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        service_path);
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
37ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}  // namespace
38ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace proxy_config {
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)scoped_ptr<ProxyConfigDictionary> GetProxyConfigForNetwork(
42ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const PrefService* profile_prefs,
43ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const PrefService* local_state_prefs,
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const NetworkState& network,
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ::onc::ONCSource* onc_source) {
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const base::DictionaryValue* network_policy =
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      onc::GetPolicyForNetwork(
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          profile_prefs, local_state_prefs, network, onc_source);
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (network_policy) {
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const base::DictionaryValue* proxy_policy = NULL;
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    network_policy->GetDictionaryWithoutPathExpansion(
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        ::onc::network_config::kProxySettings, &proxy_policy);
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!proxy_policy) {
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // This policy doesn't set a proxy for this network. Nonetheless, this
564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // disallows changes by the user.
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return scoped_ptr<ProxyConfigDictionary>();
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    scoped_ptr<base::DictionaryValue> proxy_dict =
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        onc::ConvertOncProxySettingsToProxyConfig(*proxy_policy);
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return make_scoped_ptr(new ProxyConfigDictionary(proxy_dict.get()));
63ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
64ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
65ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (network.profile_path().empty())
66ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return scoped_ptr<ProxyConfigDictionary>();
67ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
68ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  const NetworkProfile* profile = NetworkHandler::Get()
69ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      ->network_profile_handler()->GetProfileForPath(network.profile_path());
70ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!profile) {
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VLOG(1) << "Unknown profile_path '" << network.profile_path() << "'.";
72ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return scoped_ptr<ProxyConfigDictionary>();
73ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
74ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!profile_prefs && profile->type() == NetworkProfile::TYPE_USER) {
75ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // This case occurs, for example, if called from the proxy config tracker
76ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // created for the system request context and the signin screen. Both don't
77ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // use profile prefs and shouldn't depend on the user's not shared proxy
78ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // settings.
79ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    VLOG(1)
80ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        << "Don't use unshared settings for system context or signin screen.";
81ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return scoped_ptr<ProxyConfigDictionary>();
82ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
83ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
84ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // No policy set for this network, read instead the user's (shared or
85ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // unshared) configuration.
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The user's proxy setting is not stored in the Chrome preference yet. We
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // still rely on Shill storing it.
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::DictionaryValue& value = network.proxy_config();
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (value.empty())
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return scoped_ptr<ProxyConfigDictionary>();
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return make_scoped_ptr(new ProxyConfigDictionary(&value));
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void SetProxyConfigForNetwork(const ProxyConfigDictionary& proxy_config,
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                              const NetworkState& network) {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chromeos::ShillServiceClient* shill_service_client =
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DBusThreadManager::Get()->GetShillServiceClient();
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The user's proxy setting is not stored in the Chrome preference yet. We
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // still rely on Shill storing it.
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyPrefs::ProxyMode mode;
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!proxy_config.GetMode(&mode) || mode == ProxyPrefs::MODE_DIRECT) {
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Return empty string for direct mode for portal check to work correctly.
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // TODO(pneubeck): Consider removing this legacy code.
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    shill_service_client->ClearProperty(
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        dbus::ObjectPath(network.path()),
10768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        shill::kProxyConfigProperty,
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        base::Bind(&NotifyNetworkStateHandler, network.path()),
1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        base::Bind(&network_handler::ShillErrorCallbackFunction,
1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   "SetProxyConfig.ClearProperty Failed",
111ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                   network.path(),
112ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                   network_handler::ErrorCallback()));
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    std::string proxy_config_str;
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::JSONWriter::Write(&proxy_config.GetDictionary(), &proxy_config_str);
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    shill_service_client->SetProperty(
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        dbus::ObjectPath(network.path()),
11868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        shill::kProxyConfigProperty,
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::StringValue(proxy_config_str),
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        base::Bind(&NotifyNetworkStateHandler, network.path()),
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        base::Bind(&network_handler::ShillErrorCallbackFunction,
1227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   "SetProxyConfig.SetProperty Failed",
123ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                   network.path(),
124ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                   network_handler::ErrorCallback()));
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
128ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid RegisterPrefs(PrefRegistrySimple* registry) {
129ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->RegisterListPref(prefs::kDeviceOpenNetworkConfiguration);
130ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
131ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
132ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  registry->RegisterBooleanPref(
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      prefs::kUseSharedProxies,
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      false,
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
137ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
138ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registry->RegisterListPref(prefs::kOpenNetworkConfiguration,
139ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace proxy_config
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace chromeos
145