1// Copyright (c) 2011 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/prefs/command_line_pref_store.h"
6
7#include "base/logging.h"
8#include "base/values.h"
9#include "chrome/browser/prefs/proxy_config_dictionary.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/common/pref_names.h"
12#include "ui/base/ui_base_switches.h"
13
14const CommandLinePrefStore::StringSwitchToPreferenceMapEntry
15    CommandLinePrefStore::string_switch_map_[] = {
16      { switches::kLang, prefs::kApplicationLocale },
17      { switches::kAuthSchemes, prefs::kAuthSchemes },
18      { switches::kAuthServerWhitelist, prefs::kAuthServerWhitelist },
19      { switches::kAuthNegotiateDelegateWhitelist,
20          prefs::kAuthNegotiateDelegateWhitelist },
21      { switches::kGSSAPILibraryName, prefs::kGSSAPILibraryName },
22};
23
24const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
25    CommandLinePrefStore::boolean_switch_map_[] = {
26      { switches::kDisableAuthNegotiateCnameLookup,
27          prefs::kDisableAuthNegotiateCnameLookup, true },
28      { switches::kEnableAuthNegotiatePort, prefs::kEnableAuthNegotiatePort,
29          true },
30      { switches::kDisable3DAPIs, prefs::kDisable3DAPIs, true },
31      { switches::kEnableCloudPrintProxy, prefs::kCloudPrintProxyEnabled,
32          true },
33      { switches::kAllowOutdatedPlugins, prefs::kPluginsAllowOutdated, true },
34      { switches::kNoPings, prefs::kEnableHyperlinkAuditing, false },
35      { switches::kNoReferrers, prefs::kEnableReferrers, false },
36};
37
38CommandLinePrefStore::CommandLinePrefStore(const CommandLine* command_line)
39    : command_line_(command_line) {
40  ApplySimpleSwitches();
41  ApplyProxyMode();
42  ValidateProxySwitches();
43}
44
45CommandLinePrefStore::~CommandLinePrefStore() {}
46
47void CommandLinePrefStore::ApplySimpleSwitches() {
48  // Look for each switch we know about and set its preference accordingly.
49  for (size_t i = 0; i < arraysize(string_switch_map_); ++i) {
50    if (command_line_->HasSwitch(string_switch_map_[i].switch_name)) {
51      Value* value = Value::CreateStringValue(command_line_->
52          GetSwitchValueASCII(string_switch_map_[i].switch_name));
53      SetValue(string_switch_map_[i].preference_path, value);
54    }
55  }
56
57  for (size_t i = 0; i < arraysize(boolean_switch_map_); ++i) {
58    if (command_line_->HasSwitch(boolean_switch_map_[i].switch_name)) {
59      Value* value = Value::CreateBooleanValue(
60          boolean_switch_map_[i].set_value);
61      SetValue(boolean_switch_map_[i].preference_path, value);
62    }
63  }
64}
65
66bool CommandLinePrefStore::ValidateProxySwitches() {
67  if (command_line_->HasSwitch(switches::kNoProxyServer) &&
68      (command_line_->HasSwitch(switches::kProxyAutoDetect) ||
69       command_line_->HasSwitch(switches::kProxyServer) ||
70       command_line_->HasSwitch(switches::kProxyPacUrl) ||
71       command_line_->HasSwitch(switches::kProxyBypassList))) {
72    LOG(WARNING) << "Additional command-line proxy switches specified when --"
73                 << switches::kNoProxyServer << " was also specified.";
74    return false;
75  }
76  return true;
77}
78
79void CommandLinePrefStore::ApplyProxyMode() {
80  if (command_line_->HasSwitch(switches::kNoProxyServer)) {
81    SetValue(prefs::kProxy,
82             ProxyConfigDictionary::CreateDirect());
83  } else if (command_line_->HasSwitch(switches::kProxyPacUrl)) {
84    std::string pac_script_url =
85        command_line_->GetSwitchValueASCII(switches::kProxyPacUrl);
86    SetValue(prefs::kProxy,
87             ProxyConfigDictionary::CreatePacScript(pac_script_url));
88  } else if (command_line_->HasSwitch(switches::kProxyAutoDetect)) {
89    SetValue(prefs::kProxy,
90             ProxyConfigDictionary::CreateAutoDetect());
91  } else if (command_line_->HasSwitch(switches::kProxyServer)) {
92    std::string proxy_server =
93        command_line_->GetSwitchValueASCII(switches::kProxyServer);
94    std::string bypass_list =
95        command_line_->GetSwitchValueASCII(switches::kProxyBypassList);
96    SetValue(prefs::kProxy,
97             ProxyConfigDictionary::CreateFixedServers(proxy_server,
98                                                       bypass_list));
99  }
100}
101