proxy_config_handler.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/values.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/prefs/proxy_config_dictionary.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/dbus/shill_service_client.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_handler_callbacks.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chromeos/network/network_state_handler.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "dbus/object_path.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace chromeos {
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void LogError(const std::string& network,
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              const std::string& error_name,
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)              const std::string& error_message) {
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  network_handler::ShillErrorCallbackFunction(
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      network,
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      network_handler::ErrorCallback(),
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "Could not clear or set ProxyConfig",
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      error_message);
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace proxy_config {
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)scoped_ptr<ProxyConfigDictionary> GetProxyConfigForNetwork(
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const NetworkState& network) {
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::DictionaryValue& value = network.proxy_config();
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (value.empty())
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return scoped_ptr<ProxyConfigDictionary>();
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return make_scoped_ptr(new ProxyConfigDictionary(&value));
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void SetProxyConfigForNetwork(const ProxyConfigDictionary& proxy_config,
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              const NetworkState& network) {
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  chromeos::ShillServiceClient* shill_service_client =
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DBusThreadManager::Get()->GetShillServiceClient();
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ProxyPrefs::ProxyMode mode;
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!proxy_config.GetMode(&mode) || mode == ProxyPrefs::MODE_DIRECT) {
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(pneubeck): Consider removing this legacy code.  Return empty string
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // for direct mode for portal check to work correctly.
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    shill_service_client->ClearProperty(
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        dbus::ObjectPath(network.path()),
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        flimflam::kProxyConfigProperty,
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::Bind(&base::DoNothing),
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::Bind(&LogError, network.path()));
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    std::string proxy_config_str;
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::JSONWriter::Write(&proxy_config.GetDictionary(), &proxy_config_str);
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    shill_service_client->SetProperty(
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        dbus::ObjectPath(network.path()),
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        flimflam::kProxyConfigProperty,
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::StringValue(proxy_config_str),
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::Bind(&base::DoNothing),
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::Bind(&LogError, network.path()));
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (NetworkHandler::IsInitialized()) {
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NetworkHandler::Get()->network_state_handler()->
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        RequestUpdateForNetwork(network.path());
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace proxy_config
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace chromeos
80