• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
6 
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/prefs/proxy_prefs.h"
11 #include "chrome/common/pref_names.h"
12 
DataReductionProxyChromeConfigurator(PrefService * prefs)13 DataReductionProxyChromeConfigurator::DataReductionProxyChromeConfigurator(
14     PrefService* prefs) : prefs_(prefs) {
15   DCHECK(prefs);
16 }
17 
~DataReductionProxyChromeConfigurator()18 DataReductionProxyChromeConfigurator::~DataReductionProxyChromeConfigurator() {
19 }
20 
Enable(bool primary_restricted,bool fallback_restricted,const std::string & primary_origin,const std::string & fallback_origin,const std::string & ssl_origin)21 void DataReductionProxyChromeConfigurator::Enable(
22     bool primary_restricted,
23     bool fallback_restricted,
24     const std::string& primary_origin,
25     const std::string& fallback_origin,
26     const std::string& ssl_origin) {
27   DCHECK(prefs_);
28   DictionaryPrefUpdate update(prefs_, prefs::kProxy);
29   base::DictionaryValue* dict = update.Get();
30 
31   std::vector<std::string> proxies;
32   if (!primary_restricted) {
33     std::string trimmed_primary;
34     base::TrimString(primary_origin, "/", &trimmed_primary);
35     if (!trimmed_primary.empty())
36       proxies.push_back(trimmed_primary);
37   }
38   if (!fallback_restricted) {
39     std::string trimmed_fallback;
40     base::TrimString(fallback_origin, "/", &trimmed_fallback);
41     if (!trimmed_fallback.empty())
42       proxies.push_back(trimmed_fallback);
43   }
44   if (proxies.empty()) {
45     std::string mode;
46     // If already in a disabled mode, do nothing.
47     if (dict->GetString("mode", &mode))
48       if (ProxyModeToString(ProxyPrefs::MODE_SYSTEM) == mode)
49         return;
50     Disable();
51     return;
52   }
53 
54   std::string trimmed_ssl;
55   base::TrimString(ssl_origin, "/", &trimmed_ssl);
56 
57   std::string server = "http=" + JoinString(proxies, ",") + ",direct://;"
58       + (ssl_origin.empty() ? "" : ("https=" + trimmed_ssl + ",direct://;"));
59 
60   dict->SetString("server", server);
61   dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
62   dict->SetString("bypass_list", JoinString(bypass_rules_, ", "));
63 }
64 
Disable()65 void DataReductionProxyChromeConfigurator::Disable() {
66   DCHECK(prefs_);
67   DictionaryPrefUpdate update(prefs_, prefs::kProxy);
68   base::DictionaryValue* dict = update.Get();
69   dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
70   dict->SetString("server", "");
71   dict->SetString("bypass_list", "");
72 }
73 
AddHostPatternToBypass(const std::string & pattern)74 void DataReductionProxyChromeConfigurator::AddHostPatternToBypass(
75     const std::string& pattern) {
76   bypass_rules_.push_back(pattern);
77 }
78 
AddURLPatternToBypass(const std::string & pattern)79 void DataReductionProxyChromeConfigurator::AddURLPatternToBypass(
80     const std::string& pattern) {
81   size_t pos = pattern.find('/');
82   if (pattern.find('/', pos + 1) == pos + 1)
83     pos = pattern.find('/', pos + 2);
84 
85   std::string host_pattern;
86   if (pos != std::string::npos)
87     host_pattern = pattern.substr(0, pos);
88   else
89     host_pattern = pattern;
90 
91   AddHostPatternToBypass(host_pattern);
92 }
93