blob: baa67237d7f819ad7a5e806b9978760309f7f485 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0651b812011-02-24 00:22:502// 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]c2911d72011-10-03 22:16:369#include "base/callback.h"
[email protected]82a37672011-05-03 12:02:4110#include "base/string16.h"
[email protected]0651b812011-02-24 00:22:5011#include "base/threading/non_thread_safe.h"
[email protected]c2911d72011-10-03 22:16:3612#include "net/base/auth.h"
[email protected]05cc4e72011-03-08 21:29:4813#include "net/base/completion_callback.h"
[email protected]0651b812011-02-24 00:22:5014
[email protected]4c76d7c2011-04-15 19:14:1215class GURL;
16
[email protected]0651b812011-02-24 00:22:5017namespace 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.
29class HttpRequestHeaders;
[email protected]ea8141e2011-10-05 13:12:5130class HttpResponseHeaders;
[email protected]0651b812011-02-24 00:22:5031class URLRequest;
32
33class NetworkDelegate : public base::NonThreadSafe {
34 public:
[email protected]c2911d72011-10-03 22:16:3635 // 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]0651b812011-02-24 00:22:5046 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]4875ba12011-03-30 22:31:5150 // checking on parameters. See the corresponding virtuals for explanations of
51 // the methods and their arguments.
52 int NotifyBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4753 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1254 GURL* new_url);
[email protected]636eccd2011-06-28 12:28:0155 int NotifyBeforeSendHeaders(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4756 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1257 HttpRequestHeaders* headers);
[email protected]5796dc942011-07-14 19:26:1058 void NotifySendHeaders(URLRequest* request,
[email protected]783573b2011-05-13 11:05:1559 const HttpRequestHeaders& headers);
[email protected]ea8141e2011-10-05 13:12:5160 int NotifyHeadersReceived(
61 URLRequest* request,
[email protected]084262c2011-12-01 21:12:4762 const CompletionCallback& callback,
[email protected]ea8141e2011-10-05 13:12:5163 HttpResponseHeaders* original_response_headers,
64 scoped_refptr<HttpResponseHeaders>* override_response_headers);
[email protected]31b2e5f2011-04-20 16:58:3265 void NotifyBeforeRedirect(URLRequest* request,
66 const GURL& new_location);
[email protected]0651b812011-02-24 00:22:5067 void NotifyResponseStarted(URLRequest* request);
[email protected]8523ba52011-05-22 19:00:5868 void NotifyRawBytesRead(const URLRequest& request, int bytes_read);
[email protected]9045b8822012-01-13 20:35:3569 void NotifyCompleted(URLRequest* request, bool started);
[email protected]4875ba12011-03-30 22:31:5170 void NotifyURLRequestDestroyed(URLRequest* request);
[email protected]82a37672011-05-03 12:02:4171 void NotifyPACScriptError(int line_number, const string16& error);
[email protected]c2911d72011-10-03 22:16:3672 AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
73 const AuthChallengeInfo& auth_info,
74 const AuthCallback& callback,
75 AuthCredentials* credentials);
[email protected]82a37672011-05-03 12:02:4176
[email protected]0651b812011-02-24 00:22:5077 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]4c76d7c2011-04-15 19:14:1282 // Called before a request is sent. Allows the delegate to rewrite the URL
[email protected]5aa20132011-04-27 23:11:3483 // 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]ea8141e2011-10-05 13:12:5186 // 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]4875ba12011-03-30 22:31:5189 virtual int OnBeforeURLRequest(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4790 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1291 GURL* new_url) = 0;
[email protected]0651b812011-02-24 00:22:5092
[email protected]4875ba12011-03-30 22:31:5193 // Called right before the HTTP headers are sent. Allows the delegate to
[email protected]5aa20132011-04-27 23:11:3494 // read/write |headers| before they get sent out. |callback| and |headers| are
[email protected]9045b8822012-01-13 20:35:3595 // valid only until OnCompleted or OnURLRequestDestroyed is called for this
96 // request.
[email protected]5aa20132011-04-27 23:11:3497 // Returns a net status code.
[email protected]636eccd2011-06-28 12:28:0198 virtual int OnBeforeSendHeaders(URLRequest* request,
[email protected]084262c2011-12-01 21:12:4799 const CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12100 HttpRequestHeaders* headers) = 0;
[email protected]0651b812011-02-24 00:22:50101
[email protected]5796dc942011-07-14 19:26:10102 // Called right before the HTTP request(s) are being sent to the network.
[email protected]9045b8822012-01-13 20:35:35103 // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
104 // called for this request.
[email protected]5796dc942011-07-14 19:26:10105 virtual void OnSendHeaders(URLRequest* request,
[email protected]783573b2011-05-13 11:05:15106 const HttpRequestHeaders& headers) = 0;
[email protected]82b42302011-04-20 16:28:16107
[email protected]ea8141e2011-10-05 13:12:51108 // 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]9045b8822012-01-13 20:35:35117 // |callback|, |original_response_headers|, and |override_response_headers|
118 // are only valid until OnURLRequestDestroyed is called for this request.
[email protected]ea8141e2011-10-05 13:12:51119 virtual int OnHeadersReceived(
120 URLRequest* request,
[email protected]084262c2011-12-01 21:12:47121 const CompletionCallback& callback,
[email protected]ea8141e2011-10-05 13:12:51122 HttpResponseHeaders* original_response_headers,
123 scoped_refptr<HttpResponseHeaders>* override_response_headers) = 0;
124
[email protected]31b2e5f2011-04-20 16:58:32125 // Called right after a redirect response code was received.
[email protected]9045b8822012-01-13 20:35:35126 // |new_location| is only valid until OnURLRequestDestroyed is called for this
127 // request.
[email protected]31b2e5f2011-04-20 16:58:32128 virtual void OnBeforeRedirect(URLRequest* request,
129 const GURL& new_location) = 0;
130
[email protected]0651b812011-02-24 00:22:50131 // This corresponds to URLRequestDelegate::OnResponseStarted.
132 virtual void OnResponseStarted(URLRequest* request) = 0;
133
[email protected]8523ba52011-05-22 19:00:58134 // Called every time we read raw bytes.
135 virtual void OnRawBytesRead(const URLRequest& request, int bytes_read) = 0;
136
[email protected]48944382011-04-23 13:28:16137 // Indicates that the URL request has been completed or failed.
[email protected]9045b8822012-01-13 20:35:35138 // |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]4b50cb52011-03-10 00:29:37141
[email protected]4875ba12011-03-30 22:31:51142 // 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]82a37672011-05-03 12:02:41147 // Corresponds to ProxyResolverJSBindings::OnError.
148 virtual void OnPACScriptError(int line_number, const string16& error) = 0;
[email protected]7efc582d2011-08-03 20:46:35149
[email protected]c2911d72011-10-03 22:16:36150 // 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]0651b812011-02-24 00:22:50172};
173
174} // namespace net
175
176#endif // NET_BASE_NETWORK_DELEGATE_H_