- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / spdyproxy / data_reduction_proxy_settings.h
1 // Copyright 2013 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 #ifndef CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_
6 #define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/url_request/url_fetcher_delegate.h"
17
18 class PrefService;
19
20 namespace net {
21 class AuthChallengeInfo;
22 class HostPortPair;
23 class HttpAuthCache;
24 class HttpNetworkSession;
25 class URLFetcher;
26 }
27
28 namespace spdyproxy {
29
30 // The number of days of bandwidth usage statistics that are tracked.
31 const unsigned int kNumDaysInHistory = 60;
32
33 // The number of days of bandwidth usage statistics that are presented.
34 const unsigned int kNumDaysInHistorySummary = 30;
35
36 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory,
37                DataReductionProxySettings_summary_too_long);
38
39 }  // namespace spdyproxy
40
41 // Central point for configuring the data reduction proxy.
42 // This object lives on the UI thread and all of its methods are expected to
43 // be called from there.
44 // TODO(marq): Convert this to be a BrowserContextKeyedService with an
45 // associated factory class, and refactor the Java call sites accordingly.
46 class DataReductionProxySettings
47     : public net::URLFetcherDelegate,
48       public net::NetworkChangeNotifier::IPAddressObserver {
49  public:
50   typedef std::vector<long long> ContentLengthList;
51   // TODO(marq): Consider instead using a std::pair instead of a vector.
52   typedef std::vector<GURL> DataReductionProxyList;
53
54   DataReductionProxySettings();
55   virtual ~DataReductionProxySettings();
56
57   void InitDataReductionProxySettings();
58
59   // If proxy authentication is compiled in, pre-cache authentication
60   // keys for all configured proxies in |session|.
61   static void InitDataReductionProxySession(net::HttpNetworkSession* session);
62
63   // Add a host pattern to bypass. This should follow the same syntax used
64   // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix
65   // pattern, an IP literal, a CIDR block, or the magic string '<local>'.
66   // Bypass settings persist for the life of this object and are applied
67   // each time the proxy is enabled, but are not updated while it is enabled.
68   void AddHostPatternToBypass(const std::string& pattern);
69
70   // Add a URL pattern to bypass the proxy. The base implementation strips
71   // everything in |pattern| after the first single slash and then treats it
72   // as a hostname pattern. Subclasses may implement other semantics.
73   virtual void AddURLPatternToBypass(const std::string& pattern);
74
75   // Returns true if the data reduction proxy is allowed to be used on this
76   // instance of Chrome. This could return false, for example, if this instance
77   // is not part of the field trial, or if the proxy name is not configured
78   // via gyp.
79   static bool IsDataReductionProxyAllowed();
80
81   // Returns true if a screen promoting the data reduction proxy is allowed to
82   // be shown. Logic that decides when to show the promo should check its
83   // availability. This would return false if not part of a separate field
84   // trial that governs the use of the promotion.
85   static bool IsDataReductionProxyPromoAllowed();
86
87   // Returns the URL of the data reduction proxy.
88   static std::string GetDataReductionProxyOrigin();
89
90   // Returns the URL of the fallback data reduction proxy.
91   static std::string GetDataReductionProxyFallback();
92
93   // Returns a vector of GURLs for all configured proxies.
94   static DataReductionProxyList GetDataReductionProxies();
95
96   // Returns true if |auth_info| represents an authentication challenge from
97   // a compatible, configured proxy.
98   bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info);
99
100   // Returns a UTF16 string suitable for use as an authentication token in
101   // response to the challenge represented by |auth_info|. If the token can't
102   // be correctly generated for |auth_info|, returns an empty UTF16 string.
103   base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info);
104
105   // Returns true if the proxy is enabled.
106   bool IsDataReductionProxyEnabled();
107
108   // Returns true if the proxy is managed by an adminstrator's policy.
109   bool IsDataReductionProxyManaged();
110
111   // Enables or disables the data reduction proxy. If a probe URL is available,
112   // and a probe request fails at some point, the proxy won't be used until a
113   // probe succeeds.
114   void SetDataReductionProxyEnabled(bool enabled);
115
116   // Returns the time in microseconds that the last update was made to the
117   // daily original and received content lengths.
118   int64 GetDataReductionLastUpdateTime();
119
120   // Returns a vector containing the total size of all HTTP content that was
121   // received over the last |kNumDaysInHistory| before any compression by the
122   // data reduction proxy. Each element in the vector contains one day of data.
123   ContentLengthList GetDailyOriginalContentLengths();
124
125   // Returns an vector containing the aggregate received HTTP content in the
126   // last |kNumDaysInHistory| days.
127   ContentLengthList GetDailyReceivedContentLengths();
128
129   // net::URLFetcherDelegate:
130   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
131
132  protected:
133   void InitPrefMembers();
134
135   virtual net::URLFetcher* GetURLFetcher();
136
137   // Virtualized for unit test support.
138   virtual PrefService* GetOriginalProfilePrefs();
139   virtual PrefService* GetLocalStatePrefs();
140
141   void GetContentLengths(unsigned int days,
142                          int64* original_content_length,
143                          int64* received_content_length,
144                          int64* last_update_time);
145   ContentLengthList GetDailyContentLengths(const char* pref_name);
146
147   // Sets the proxy configs, enabling or disabling the proxy according to
148   // the value of |enabled|. |at_startup| is true when this method is called
149   // from InitDataReductionProxySettings.
150   virtual void SetProxyConfigs(bool enabled, bool at_startup);
151
152   // Metrics methods. Subclasses should override if they wish to provide
153   // alternate methods.
154   virtual void RecordDataReductionInit();
155
156   virtual void AddDefaultProxyBypassRules();
157
158   // Writes a warning to the log that is used in backend processing of
159   // customer feedback. Virtual so tests can mock it for verification.
160   virtual void LogProxyState(bool enabled, bool at_startup);
161
162   bool HasTurnedOn() { return has_turned_on_; }
163   bool HasTurnedOff() { return has_turned_off_; }
164   // Note that these flags may only be toggled to true, never back to false.
165   void SetHasTurnedOn() { has_turned_on_ = true; }
166   void SetHasTurnedOff() { has_turned_off_ = true; }
167
168   // Accessor for unit tests.
169   std::vector<std::string> BypassRules() { return bypass_rules_;}
170
171  private:
172   friend class DataReductionProxySettingsTestBase;
173   friend class DataReductionProxySettingsTest;
174   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
175                            TestAuthenticationInit);
176   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
177                            TestAuthHashGeneration);
178   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
179                            TestAuthHashGenerationWithOriginSetViaSwitch);
180   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
181                            TestResetDataReductionStatistics);
182   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
183                            TestIsProxyEnabledOrManaged);
184   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
185                            TestContentLengths);
186   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
187                            TestGetDailyContentLengths);
188   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
189                            TestMaybeActivateDataReductionProxy);
190   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
191                            TestOnIPAddressChanged);
192   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
193                            TestOnProxyEnabledPrefChange);
194   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
195                            TestInitDataReductionProxyOn);
196   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
197                            TestInitDataReductionProxyOff);
198   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
199                            TestBypassList);
200
201   // NetworkChangeNotifier::IPAddressObserver:
202   virtual void OnIPAddressChanged() OVERRIDE;
203
204   // Underlying implementation of InitDataReductionProxySession(), factored
205   // out to be testable without creating a full HttpNetworkSession.
206   static void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache);
207
208   void OnProxyEnabledPrefChange();
209
210   void ResetDataReductionStatistics();
211
212   void MaybeActivateDataReductionProxy(bool at_startup);
213
214   // Requests the proxy probe URL, if one is set.  If unable to do so, disables
215   // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
216   // failure.
217   void ProbeWhetherDataReductionProxyIsAvailable();
218   std::string GetProxyCheckURL();
219
220   // Returns a UTF16 string that's the hash of the configured authentication
221   // key and |salt|. Returns an empty UTF16 string if no key is configured or
222   // the data reduction proxy feature isn't available.
223   static base::string16 AuthHashForSalt(int64 salt);
224
225   std::vector<std::string> bypass_rules_;
226
227   // Indicate whether a user has turned on the data reduction proxy previously
228   // in this session.
229   bool has_turned_on_;
230
231   // Indicate whether a user has turned off the data reduction proxy previously
232   // in this session.
233   bool has_turned_off_;
234
235   bool disabled_by_carrier_;
236   bool enabled_by_user_;
237
238   scoped_ptr<net::URLFetcher> fetcher_;
239   BooleanPrefMember spdy_proxy_auth_enabled_;
240
241   DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings);
242 };
243
244 #endif  // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_