Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / data_reduction_proxy / browser / data_reduction_proxy_auth_request_handler.h
blob992b8b66e5e5d04435c02800d18ec686866f2c43
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.
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_
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"
14 namespace base {
15 class SingleThreadTaskRunner;
18 namespace net {
19 class HttpRequestHeaders;
20 class HttpResponseHeaders;
21 class ProxyServer;
22 class URLRequest;
25 namespace data_reduction_proxy {
27 #if defined(OS_ANDROID)
28 extern const char kAndroidWebViewProtocolVersion[];
29 #endif
31 extern const char kClientAndroidWebview[];
32 extern const char kClientChromeAndroid[];
33 extern const char kClientChromeIOS[];
35 class DataReductionProxyParams;
37 class DataReductionProxyAuthRequestHandler {
38 public:
39 static bool IsKeySetOnCommandLine();
41 DataReductionProxyAuthRequestHandler(
42 const std::string& client,
43 const std::string& version,
44 DataReductionProxyParams* params,
45 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
47 virtual ~DataReductionProxyAuthRequestHandler();
49 // Adds a 'Chrome-Proxy' header to |request_headers| with the data reduction
50 // proxy authentication credentials. Only adds this header if the provided
51 // |proxy_server| is a data reduction proxy. Must be called on the IO thread.
52 void MaybeAddRequestHeader(net::URLRequest* request,
53 const net::ProxyServer& proxy_server,
54 net::HttpRequestHeaders* request_headers);
56 // Sets a new authentication key. This must be called for platforms that do
57 // not have a default key defined. See the constructor implementation for
58 // those platforms. Must be called on the UI thread.
59 void SetKeyOnUI(const std::string& key);
61 protected:
62 void Init();
63 void InitAuthenticationOnUI(const std::string& key);
65 void AddAuthorizationHeader(net::HttpRequestHeaders* headers);
67 // Returns a UTF16 string that's the hash of the configured authentication
68 // |key| and |salt|. Returns an empty UTF16 string if no key is configured or
69 // the data reduction proxy feature isn't available.
70 static base::string16 AuthHashForSalt(int64 salt,
71 const std::string& key);
72 // Visible for testing.
73 virtual base::Time Now() const;
74 virtual void RandBytes(void* output, size_t length);
76 // Visible for testing.
77 virtual std::string GetDefaultKey() const;
79 private:
80 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
81 Authorization);
82 FRIEND_TEST_ALL_PREFIXES(DataReductionProxyAuthRequestHandlerTest,
83 AuthHashForSalt);
85 // Stores the supplied key and sets up credentials suitable for authenticating
86 // with the data reduction proxy.
87 void InitAuthentication(const std::string& key);
89 // Generates a session ID and credentials suitable for authenticating with
90 // the data reduction proxy.
91 void ComputeCredentials(const base::Time& now,
92 std::string* session,
93 std::string* credentials);
95 // Authentication state.
96 std::string key_;
98 // Lives on the IO thread.
99 std::string session_;
100 std::string credentials_;
102 // Name of the client and version of the data reduction proxy protocol to use.
103 // Both live on the IO thread.
104 std::string client_;
105 std::string version_;
107 // The last time the session was updated. Used to ensure that a session is
108 // never used for more than twenty-four hours.
109 base::Time last_update_time_;
111 DataReductionProxyParams* data_reduction_proxy_params_;
113 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
115 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyAuthRequestHandler);
118 } // namespace data_reduction_proxy
119 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_AUTH_REQUEST_HANDLER_H_