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