e4a3d4166439091c52b66716a8c044d69df7a354
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_auth_request_handler.h
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 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_
7
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string16.h"
11 #include "base/time/time.h"
12 #include "url/gurl.h"
13
14 namespace base {
15 class SingleThreadTaskRunner;
16 }
17
18 namespace net {
19 class HostPortPair;
20 class HttpRequestHeaders;
21 class HttpResponseHeaders;
22 class ProxyServer;
23 class URLRequest;
24 }
25
26 namespace data_reduction_proxy {
27
28 #if defined(OS_ANDROID)
29 extern const char kAndroidWebViewProtocolVersion[];
30 #endif
31
32 extern const char kClientAndroidWebview[];
33 extern const char kClientChromeAndroid[];
34 extern const char kClientChromeIOS[];
35
36 class DataReductionProxyParams;
37
38 class DataReductionProxyAuthRequestHandler {
39  public:
40   static bool IsKeySetOnCommandLine();
41
42   // Constructs a DataReductionProxyAuthRequestHandler object with the given
43   // client type, params, and network task runner.
44   DataReductionProxyAuthRequestHandler(
45       const std::string& client,
46       DataReductionProxyParams* params,
47       scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
48
49   virtual ~DataReductionProxyAuthRequestHandler();
50
51   // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
52   // proxy authentication credentials. Only adds this header if the provided
53   // |proxy_server| is a data reduction proxy and not the data reduction proxy's
54   // CONNECT server. Must be called on the IO thread.
55   void MaybeAddRequestHeader(net::URLRequest* request,
56                              const net::ProxyServer& proxy_server,
57                              net::HttpRequestHeaders* request_headers);
58
59   // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
60   // proxy authentication credentials. Only adds this header if the provided
61   // |proxy_server| is the data reduction proxy's CONNECT server. Must be called
62   // on the IO thread.
63   void MaybeAddProxyTunnelRequestHandler(
64       const net::HostPortPair& proxy_server,
65       net::HttpRequestHeaders* request_headers);
66
67   // Sets a new authentication key. This must be called for platforms that do
68   // not have a default key defined. See the constructor implementation for
69   // those platforms. Must be called on the UI thread.
70   void SetKeyOnUI(const std::string& key);
71
72  protected:
73   void Init();
74   void InitAuthenticationOnUI(const std::string& key);
75
76   void AddAuthorizationHeader(net::HttpRequestHeaders* headers);
77
78   // Returns a UTF16 string that's the hash of the configured authentication
79   // |key| and |salt|. Returns an empty UTF16 string if no key is configured or
80   // the data reduction proxy feature isn't available.
81   static base::string16 AuthHashForSalt(int64 salt,
82                                         const std::string& key);
83   // Visible for testing.
84   virtual base::Time Now() const;
85   virtual void RandBytes(void* output, size_t length);
86
87   // Visible for testing.
88   virtual std::string GetDefaultKey() const;
89
90   // Visible for testing.
91   DataReductionProxyAuthRequestHandler(
92       const std::string& client,
93       const std::string& version,
94       DataReductionProxyParams* params,
95       scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
96
97  private:
98   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
99                            Authorization);
100   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
101                            AuthorizationBogusVersion);
102   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
103                            AuthHashForSalt);
104
105   // Returns the version of Chromium that is being used.
106   std::string ChromiumVersion() const;
107
108   // Returns the build and patch numbers of |version|. If |version| isn't of the
109   // form xx.xx.xx.xx build and patch are not modified.
110   void GetChromiumBuildAndPatch(const std::string& version,
111                                 std::string* build,
112                                 std::string* patch) const;
113
114   // Stores the supplied key and sets up credentials suitable for authenticating
115   // with the data reduction proxy.
116   void InitAuthentication(const std::string& key);
117
118   // Generates a session ID and credentials suitable for authenticating with
119   // the data reduction proxy.
120   void ComputeCredentials(const base::Time& now,
121                           std::string* session,
122                           std::string* credentials);
123
124   // Adds authentication headers only if |expects_ssl| is true and
125   // |proxy_server| is a data reduction proxy used for ssl tunneling via
126   // HTTP CONNECT, or |expect_ssl| is false and |proxy_server| is a data
127   // reduction proxy for HTTP traffic.
128   void MaybeAddRequestHeaderImpl(const net::HostPortPair& proxy_server,
129                                  bool expect_ssl,
130                                  net::HttpRequestHeaders* request_headers);
131
132   // Authentication state.
133   std::string key_;
134
135   // Lives on the IO thread.
136   std::string session_;
137   std::string credentials_;
138
139   // Name of the client and version of the data reduction proxy protocol to use.
140   // Both live on the IO thread.
141   std::string client_;
142   std::string build_number_;
143   std::string patch_number_;
144
145   // The last time the session was updated. Used to ensure that a session is
146   // never used for more than twenty-four hours.
147   base::Time last_update_time_;
148
149   DataReductionProxyParams* data_reduction_proxy_params_;
150
151   scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
152
153   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyAuthRequestHandler);
154 };
155
156 }  // namespace data_reduction_proxy
157 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_