OLD | NEW |
(Empty) | |
| 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 "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" |
| 9 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
| 10 |
| 11 using base::FieldTrialList; |
| 12 |
| 13 namespace { |
| 14 const char kEnabled[] = "Enabled"; |
| 15 } |
| 16 |
| 17 namespace data_reduction_proxy { |
| 18 |
| 19 // static |
| 20 bool DataReductionProxyParams::IsIncludedInFieldTrial() { |
| 21 return base::FieldTrialList::FindFullName( |
| 22 "DataCompressionProxyRollout") == kEnabled; |
| 23 } |
| 24 |
| 25 // static |
| 26 bool DataReductionProxyParams::IsIncludedInAlternativeFieldTrial() { |
| 27 return base::FieldTrialList::FindFullName( |
| 28 "DataCompressionProxyAlternativeConfiguration") == kEnabled; |
| 29 } |
| 30 |
| 31 // static |
| 32 bool DataReductionProxyParams::IsIncludedInPromoFieldTrial() { |
| 33 return FieldTrialList::FindFullName( |
| 34 "DataCompressionProxyPromoVisibility") == kEnabled; |
| 35 } |
| 36 |
| 37 // static |
| 38 bool DataReductionProxyParams::IsIncludedInPreconnectHintingFieldTrial() { |
| 39 return IsIncludedInFieldTrial() && |
| 40 FieldTrialList::FindFullName( |
| 41 "DataCompressionProxyPreconnectHints") == kEnabled; |
| 42 } |
| 43 |
| 44 // static |
| 45 bool DataReductionProxyParams::IsKeySetOnCommandLine() { |
| 46 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 47 return command_line.HasSwitch( |
| 48 data_reduction_proxy::switches::kEnableDataReductionProxy); |
| 49 } |
| 50 |
| 51 DataReductionProxyParams::DataReductionProxyParams(int flags) |
| 52 : allowed_((flags & kAllowed) == kAllowed), |
| 53 fallback_allowed_((flags & kFallbackAllowed) == kFallbackAllowed), |
| 54 alt_allowed_((flags & kAlternativeAllowed) == kAlternativeAllowed), |
| 55 promo_allowed_((flags & kPromoAllowed) == kPromoAllowed) { |
| 56 bool result = Init(allowed_, fallback_allowed_, alt_allowed_); |
| 57 DCHECK(result); |
| 58 } |
| 59 |
| 60 DataReductionProxyParams::~DataReductionProxyParams() { |
| 61 } |
| 62 |
| 63 DataReductionProxyParams::DataReductionProxyList |
| 64 DataReductionProxyParams::GetAllowedProxies() const { |
| 65 DataReductionProxyList list; |
| 66 if (allowed_) |
| 67 list.push_back(origin_); |
| 68 if (allowed_ && fallback_allowed_) |
| 69 list.push_back(fallback_origin_); |
| 70 if (alt_allowed_) { |
| 71 list.push_back(alt_origin_); |
| 72 list.push_back(ssl_origin_); |
| 73 } |
| 74 if (alt_allowed_ && fallback_allowed_) |
| 75 list.push_back(alt_fallback_origin_); |
| 76 return list; |
| 77 } |
| 78 |
| 79 DataReductionProxyParams::DataReductionProxyParams(int flags, |
| 80 bool should_call_init) |
| 81 : allowed_((flags & kAllowed) == kAllowed), |
| 82 fallback_allowed_((flags & kFallbackAllowed) == kFallbackAllowed), |
| 83 alt_allowed_((flags & kAlternativeAllowed) == kAlternativeAllowed), |
| 84 promo_allowed_((flags & kPromoAllowed) == kPromoAllowed) { |
| 85 if (should_call_init) { |
| 86 bool result = Init(allowed_, fallback_allowed_, alt_allowed_); |
| 87 DCHECK(result); |
| 88 } |
| 89 } |
| 90 |
| 91 bool DataReductionProxyParams::Init( |
| 92 bool allowed, bool fallback_allowed, bool alt_allowed) { |
| 93 InitWithoutChecks(); |
| 94 // Verify that all necessary params are set. |
| 95 if (allowed) { |
| 96 if (!origin_.is_valid()) { |
| 97 DVLOG(1) << "Invalid data reduction proxy origin: " << origin_.spec(); |
| 98 return false; |
| 99 } |
| 100 } |
| 101 |
| 102 if (allowed && fallback_allowed) { |
| 103 if (!fallback_origin_.is_valid()) { |
| 104 DVLOG(1) << "Invalid data reduction proxy fallback origin: " |
| 105 << fallback_origin_.spec(); |
| 106 return false; |
| 107 } |
| 108 } |
| 109 |
| 110 if (alt_allowed) { |
| 111 if (!allowed) { |
| 112 DVLOG(1) << "Alternative data reduction proxy configuration cannot " |
| 113 << "be allowed if the regular configuration is not allowed"; |
| 114 return false; |
| 115 } |
| 116 if (!alt_origin_.is_valid()) { |
| 117 DVLOG(1) << "Invalid alternative origin:" << alt_origin_.spec(); |
| 118 return false; |
| 119 } |
| 120 if (!ssl_origin_.is_valid()) { |
| 121 DVLOG(1) << "Invalid ssl origin: " << ssl_origin_.spec(); |
| 122 return false; |
| 123 } |
| 124 } |
| 125 |
| 126 if (alt_allowed && fallback_allowed) { |
| 127 if (!alt_fallback_origin_.is_valid()) { |
| 128 DVLOG(1) << "Invalid alternative fallback origin:" |
| 129 << alt_fallback_origin_.spec(); |
| 130 return false; |
| 131 } |
| 132 } |
| 133 |
| 134 if (allowed && !probe_url_.is_valid()) { |
| 135 DVLOG(1) << "Invalid probe url: <null>"; |
| 136 return false; |
| 137 } |
| 138 |
| 139 if (allowed || alt_allowed) { |
| 140 if (key_.empty()) { |
| 141 DVLOG(1) << "Invalid key: <empty>"; |
| 142 return false; |
| 143 } |
| 144 } |
| 145 |
| 146 if (fallback_allowed_ && !allowed_) { |
| 147 DVLOG(1) << "The data reduction proxy fallback cannot be allowed if " |
| 148 << "the data reduction proxy is not allowed"; |
| 149 return false; |
| 150 } |
| 151 if (promo_allowed_ && !allowed_) { |
| 152 DVLOG(1) << "The data reduction proxy promo cannot be allowed if the " |
| 153 << "data reduction proxy is not allowed"; |
| 154 return false; |
| 155 } |
| 156 return true; |
| 157 |
| 158 } |
| 159 |
| 160 |
| 161 void DataReductionProxyParams::InitWithoutChecks() { |
| 162 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 163 std::string origin = |
| 164 command_line.GetSwitchValueASCII(switches::kDataReductionProxyDev); |
| 165 if (origin.empty()) |
| 166 origin = command_line.GetSwitchValueASCII(switches::kDataReductionProxy); |
| 167 std::string fallback_origin = |
| 168 command_line.GetSwitchValueASCII(switches::kDataReductionProxyFallback); |
| 169 std::string ssl_origin = |
| 170 command_line.GetSwitchValueASCII(switches::kDataReductionSSLProxy); |
| 171 std::string alt_origin = |
| 172 command_line.GetSwitchValueASCII(switches::kDataReductionProxyAlt); |
| 173 std::string alt_fallback_origin = command_line.GetSwitchValueASCII( |
| 174 switches::kDataReductionProxyAltFallback); |
| 175 key_ = command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); |
| 176 |
| 177 bool configured_on_command_line = |
| 178 !(origin.empty() && fallback_origin.empty() && ssl_origin.empty() && |
| 179 alt_origin.empty() && alt_fallback_origin.empty()); |
| 180 |
| 181 |
| 182 // Configuring the proxy on the command line overrides the values of |
| 183 // |allowed_| and |alt_allowed_|. |
| 184 if (configured_on_command_line) |
| 185 allowed_ = true; |
| 186 if (!(ssl_origin.empty() && |
| 187 alt_origin.empty() && |
| 188 alt_fallback_origin.empty())) |
| 189 alt_allowed_ = true; |
| 190 |
| 191 // Only use default key if non of the proxies are configured on the command |
| 192 // line. |
| 193 if (key_.empty() && !configured_on_command_line) |
| 194 key_ = GetDefaultKey(); |
| 195 |
| 196 std::string probe_url = command_line.GetSwitchValueASCII( |
| 197 switches::kDataReductionProxyProbeURL); |
| 198 |
| 199 // Set from preprocessor constants those params that are not specified on the |
| 200 // command line. |
| 201 if (origin.empty()) |
| 202 origin = GetDefaultDevOrigin(); |
| 203 if (origin.empty()) |
| 204 origin = GetDefaultOrigin(); |
| 205 if (fallback_origin.empty()) |
| 206 fallback_origin = GetDefaultFallbackOrigin(); |
| 207 if (ssl_origin.empty()) |
| 208 ssl_origin = GetDefaultSSLOrigin(); |
| 209 if (alt_origin.empty()) |
| 210 alt_origin = GetDefaultAltOrigin(); |
| 211 if (alt_fallback_origin.empty()) |
| 212 alt_fallback_origin = GetDefaultAltFallbackOrigin(); |
| 213 if (probe_url.empty()) |
| 214 probe_url = GetDefaultProbeURL(); |
| 215 |
| 216 origin_ = GURL(origin); |
| 217 fallback_origin_ = GURL(fallback_origin); |
| 218 ssl_origin_ = GURL(ssl_origin); |
| 219 alt_origin_ = GURL(alt_origin); |
| 220 alt_fallback_origin_ = GURL(alt_fallback_origin); |
| 221 probe_url_ = GURL(probe_url); |
| 222 |
| 223 } |
| 224 |
| 225 std::string DataReductionProxyParams::GetDefaultKey() const { |
| 226 #if defined(SPDY_PROXY_AUTH_VALUE) |
| 227 return SPDY_PROXY_AUTH_VALUE; |
| 228 #endif |
| 229 return std::string(); |
| 230 } |
| 231 |
| 232 std::string DataReductionProxyParams::GetDefaultDevOrigin() const { |
| 233 #if defined(DATA_REDUCTION_DEV_HOST) |
| 234 if (FieldTrialList::FindFullName("DataCompressionProxyDevRollout") == |
| 235 kEnabled) { |
| 236 return DATA_REDUCTION_DEV_HOST; |
| 237 } |
| 238 #endif |
| 239 return std::string(); |
| 240 } |
| 241 |
| 242 std::string DataReductionProxyParams::GetDefaultOrigin() const { |
| 243 #if defined(SPDY_PROXY_AUTH_ORIGIN) |
| 244 return SPDY_PROXY_AUTH_ORIGIN; |
| 245 #endif |
| 246 return std::string(); |
| 247 } |
| 248 |
| 249 std::string DataReductionProxyParams::GetDefaultFallbackOrigin() const { |
| 250 #if defined(DATA_REDUCTION_FALLBACK_HOST) |
| 251 return DATA_REDUCTION_FALLBACK_HOST; |
| 252 #endif |
| 253 return std::string(); |
| 254 } |
| 255 |
| 256 std::string DataReductionProxyParams::GetDefaultSSLOrigin() const { |
| 257 #if defined(DATA_REDUCTION_PROXY_SSL_ORIGIN) |
| 258 return DATA_REDUCTION_PROXY_SSL_ORIGIN; |
| 259 #endif |
| 260 return std::string(); |
| 261 } |
| 262 |
| 263 std::string DataReductionProxyParams::GetDefaultAltOrigin() const { |
| 264 #if defined(DATA_REDUCTION_PROXY_ALT_ORIGIN) |
| 265 return DATA_REDUCTION_PROXY_ALT_ORIGIN; |
| 266 #endif |
| 267 return std::string(); |
| 268 } |
| 269 |
| 270 std::string DataReductionProxyParams::GetDefaultAltFallbackOrigin() const { |
| 271 #if defined(DATA_REDUCTION_PROXY_ALT_FALLBACK_ORIGIN) |
| 272 return DATA_REDUCTION_PROXY_ALT_FALLBACK_ORIGIN; |
| 273 #endif |
| 274 return std::string(); |
| 275 } |
| 276 |
| 277 std::string DataReductionProxyParams::GetDefaultProbeURL() const { |
| 278 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) |
| 279 return DATA_REDUCTION_PROXY_PROBE_URL; |
| 280 #endif |
| 281 return std::string(); |
| 282 } |
| 283 |
| 284 } // namespace data_reduction_proxy |
OLD | NEW |