[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 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 NET_BASE_NETWORK_DELEGATE_H_ |
| 6 | #define NET_BASE_NETWORK_DELEGATE_H_ |
| 7 | #pragma once |
| 8 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 10 | #include "base/string16.h" |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 11 | #include "base/threading/non_thread_safe.h" |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 12 | #include "net/base/auth.h" |
[email protected] | 05cc4e7 | 2011-03-08 21:29:48 | [diff] [blame] | 13 | #include "net/base/completion_callback.h" |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 14 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 15 | class GURL; |
| 16 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 17 | namespace net { |
| 18 | |
| 19 | // NOTE: Layering violations! |
| 20 | // We decided to accept these violations (depending |
| 21 | // on other net/ submodules from net/base/), because otherwise NetworkDelegate |
| 22 | // would have to be broken up into too many smaller interfaces targeted to each |
| 23 | // submodule. Also, since the lower levels in net/ may callback into higher |
| 24 | // levels, we may encounter dangerous casting issues. |
| 25 | // |
| 26 | // NOTE: It is not okay to add any compile-time dependencies on symbols outside |
| 27 | // of net/base here, because we have a net_base library. Forward declarations |
| 28 | // are ok. |
| 29 | class HttpRequestHeaders; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 30 | class HttpResponseHeaders; |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 31 | class URLRequest; |
| 32 | |
| 33 | class NetworkDelegate : public base::NonThreadSafe { |
| 34 | public: |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 35 | // AuthRequiredResponse indicates how a NetworkDelegate handles an |
| 36 | // OnAuthRequired call. It's placed in this file to prevent url_request.h |
| 37 | // from having to include network_delegate.h. |
| 38 | enum AuthRequiredResponse { |
| 39 | AUTH_REQUIRED_RESPONSE_NO_ACTION, |
| 40 | AUTH_REQUIRED_RESPONSE_SET_AUTH, |
| 41 | AUTH_REQUIRED_RESPONSE_CANCEL_AUTH, |
| 42 | AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 43 | }; |
| 44 | typedef base::Callback<void(AuthRequiredResponse)> AuthCallback; |
| 45 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 46 | virtual ~NetworkDelegate() {} |
| 47 | |
| 48 | // Notification interface called by the network stack. Note that these |
| 49 | // functions mostly forward to the private virtuals. They also add some sanity |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 50 | // checking on parameters. See the corresponding virtuals for explanations of |
| 51 | // the methods and their arguments. |
| 52 | int NotifyBeforeURLRequest(URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 53 | const CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 54 | GURL* new_url); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 55 | int NotifyBeforeSendHeaders(URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 56 | const CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 57 | HttpRequestHeaders* headers); |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 58 | void NotifySendHeaders(URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 59 | const HttpRequestHeaders& headers); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 60 | int NotifyHeadersReceived( |
| 61 | URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 62 | const CompletionCallback& callback, |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 63 | HttpResponseHeaders* original_response_headers, |
| 64 | scoped_refptr<HttpResponseHeaders>* override_response_headers); |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 65 | void NotifyBeforeRedirect(URLRequest* request, |
| 66 | const GURL& new_location); |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 67 | void NotifyResponseStarted(URLRequest* request); |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 68 | void NotifyRawBytesRead(const URLRequest& request, int bytes_read); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 69 | void NotifyCompleted(URLRequest* request, bool started); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 70 | void NotifyURLRequestDestroyed(URLRequest* request); |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 71 | void NotifyPACScriptError(int line_number, const string16& error); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 72 | AuthRequiredResponse NotifyAuthRequired(URLRequest* request, |
| 73 | const AuthChallengeInfo& auth_info, |
| 74 | const AuthCallback& callback, |
| 75 | AuthCredentials* credentials); |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 76 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 77 | private: |
| 78 | // This is the interface for subclasses of NetworkDelegate to implement. This |
| 79 | // member functions will be called by the respective public notification |
| 80 | // member function, which will perform basic sanity checking. |
| 81 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 82 | // Called before a request is sent. Allows the delegate to rewrite the URL |
[email protected] | 5aa2013 | 2011-04-27 23:11:34 | [diff] [blame] | 83 | // being fetched by modifying |new_url|. |callback| and |new_url| are valid |
| 84 | // only until OnURLRequestDestroyed is called for this request. Returns a net |
| 85 | // status code, generally either OK to continue with the request or |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 86 | // ERR_IO_PENDING if the result is not ready yet. A status code other than OK |
| 87 | // and ERR_IO_PENDING will cancel the request and report the status code as |
| 88 | // the reason. |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 89 | virtual int OnBeforeURLRequest(URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 90 | const CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 91 | GURL* new_url) = 0; |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 92 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 93 | // Called right before the HTTP headers are sent. Allows the delegate to |
[email protected] | 5aa2013 | 2011-04-27 23:11:34 | [diff] [blame] | 94 | // read/write |headers| before they get sent out. |callback| and |headers| are |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 95 | // valid only until OnCompleted or OnURLRequestDestroyed is called for this |
| 96 | // request. |
[email protected] | 5aa2013 | 2011-04-27 23:11:34 | [diff] [blame] | 97 | // Returns a net status code. |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 98 | virtual int OnBeforeSendHeaders(URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 99 | const CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 100 | HttpRequestHeaders* headers) = 0; |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 101 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 102 | // Called right before the HTTP request(s) are being sent to the network. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 103 | // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is |
| 104 | // called for this request. |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 105 | virtual void OnSendHeaders(URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 106 | const HttpRequestHeaders& headers) = 0; |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 107 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 108 | // Called for HTTP requests when the headers have been received. Returns a net |
| 109 | // status code, generally either OK to continue with the request or |
| 110 | // ERR_IO_PENDING if the result is not ready yet. A status code other than OK |
| 111 | // and ERR_IO_PENDING will cancel the request and report the status code as |
| 112 | // the reason. |
| 113 | // |original_response_headers| contains the headers as received over the |
| 114 | // network, these must not be modified. |override_response_headers| can be set |
| 115 | // to new values, that should be considered as overriding |
| 116 | // |original_response_headers|. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 117 | // |callback|, |original_response_headers|, and |override_response_headers| |
| 118 | // are only valid until OnURLRequestDestroyed is called for this request. |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 119 | virtual int OnHeadersReceived( |
| 120 | URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 121 | const CompletionCallback& callback, |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 122 | HttpResponseHeaders* original_response_headers, |
| 123 | scoped_refptr<HttpResponseHeaders>* override_response_headers) = 0; |
| 124 | |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 125 | // Called right after a redirect response code was received. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 126 | // |new_location| is only valid until OnURLRequestDestroyed is called for this |
| 127 | // request. |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 128 | virtual void OnBeforeRedirect(URLRequest* request, |
| 129 | const GURL& new_location) = 0; |
| 130 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 131 | // This corresponds to URLRequestDelegate::OnResponseStarted. |
| 132 | virtual void OnResponseStarted(URLRequest* request) = 0; |
| 133 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 134 | // Called every time we read raw bytes. |
| 135 | virtual void OnRawBytesRead(const URLRequest& request, int bytes_read) = 0; |
| 136 | |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 137 | // Indicates that the URL request has been completed or failed. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame^] | 138 | // |started| indicates whether the request has been started. If false, |
| 139 | // some information like the socket address is not available. |
| 140 | virtual void OnCompleted(URLRequest* request, bool started) = 0; |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 141 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 142 | // Called when an URLRequest is being destroyed. Note that the request is |
| 143 | // being deleted, so it's not safe to call any methods that may result in |
| 144 | // a virtual method call. |
| 145 | virtual void OnURLRequestDestroyed(URLRequest* request) = 0; |
| 146 | |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 147 | // Corresponds to ProxyResolverJSBindings::OnError. |
| 148 | virtual void OnPACScriptError(int line_number, const string16& error) = 0; |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 149 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 150 | // Called when a request receives an authentication challenge |
| 151 | // specified by |auth_info|, and is unable to respond using cached |
| 152 | // credentials. |callback| and |credentials| must be non-NULL, and must |
| 153 | // be valid until OnURLRequestDestroyed is called for |request|. |
| 154 | // |
| 155 | // The following return values are allowed: |
| 156 | // - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but |
| 157 | // no action is being taken on it. |
| 158 | // - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with |
| 159 | // a username and password, which should be used in a response to |
| 160 | // |auth_info|. |
| 161 | // - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge |
| 162 | // should not be attempted. |
| 163 | // - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided |
| 164 | // asynchronously. |callback| will be invoked when the decision is made, |
| 165 | // and one of the other AuthRequiredResponse values will be passed in with |
| 166 | // the same semantics as described above. |
| 167 | virtual AuthRequiredResponse OnAuthRequired( |
| 168 | URLRequest* request, |
| 169 | const AuthChallengeInfo& auth_info, |
| 170 | const AuthCallback& callback, |
| 171 | AuthCredentials* credentials) = 0; |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace net |
| 175 | |
| 176 | #endif // NET_BASE_NETWORK_DELEGATE_H_ |