• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 NET_BASE_NETWORK_DELEGATE_H_
6 #define NET_BASE_NETWORK_DELEGATE_H_
7 #pragma once
8 
9 #include "base/threading/non_thread_safe.h"
10 #include "net/base/completion_callback.h"
11 
12 class GURL;
13 
14 namespace net {
15 
16 // NOTE: Layering violations!
17 // We decided to accept these violations (depending
18 // on other net/ submodules from net/base/), because otherwise NetworkDelegate
19 // would have to be broken up into too many smaller interfaces targeted to each
20 // submodule. Also, since the lower levels in net/ may callback into higher
21 // levels, we may encounter dangerous casting issues.
22 //
23 // NOTE: It is not okay to add any compile-time dependencies on symbols outside
24 // of net/base here, because we have a net_base library. Forward declarations
25 // are ok.
26 class HttpRequestHeaders;
27 class URLRequest;
28 class URLRequestJob;
29 
30 class NetworkDelegate : public base::NonThreadSafe {
31  public:
~NetworkDelegate()32   virtual ~NetworkDelegate() {}
33 
34   // Notification interface called by the network stack. Note that these
35   // functions mostly forward to the private virtuals. They also add some sanity
36   // checking on parameters. See the corresponding virtuals for explanations of
37   // the methods and their arguments.
38   int NotifyBeforeURLRequest(URLRequest* request,
39                              CompletionCallback* callback,
40                              GURL* new_url);
41   int NotifyBeforeSendHeaders(uint64 request_id,
42                               CompletionCallback* callback,
43                               HttpRequestHeaders* headers);
44   void NotifyResponseStarted(URLRequest* request);
45   void NotifyReadCompleted(URLRequest* request, int bytes_read);
46   void NotifyURLRequestDestroyed(URLRequest* request);
47 
48   // Returns a URLRequestJob that will be used to handle the request if
49   // non-null.
50   // TODO(koz): Currently this is called inside registered ProtocolFactories,
51   // so that we can perform Delegate-dependent request handling from the static
52   // factories, but ultimately it should be called directly from
53   // URLRequestJobManager::CreateJob() as a general override mechanism.
54   URLRequestJob* MaybeCreateURLRequestJob(URLRequest* request);
55 
56  private:
57   // This is the interface for subclasses of NetworkDelegate to implement. This
58   // member functions will be called by the respective public notification
59   // member function, which will perform basic sanity checking.
60 
61   // Called before a request is sent. Allows the delegate to rewrite the URL
62   // being fetched by modifying |new_url|. The callback can be called at any
63   // time, but will have no effect if the request has already been cancelled or
64   // deleted. Returns a net status code, generally either OK to continue with
65   // the request or ERR_IO_PENDING if the result is not ready yet.
66   virtual int OnBeforeURLRequest(URLRequest* request,
67                                  CompletionCallback* callback,
68                                  GURL* new_url) = 0;
69 
70   // Called right before the HTTP headers are sent. Allows the delegate to
71   // read/write |headers| before they get sent out. The callback can be called
72   // at any time, but will have no effect if the transaction handling this
73   // request has been cancelled. Returns a net status code.
74   virtual int OnBeforeSendHeaders(uint64 request_id,
75                                   CompletionCallback* callback,
76                                   HttpRequestHeaders* headers) = 0;
77 
78   // This corresponds to URLRequestDelegate::OnResponseStarted.
79   virtual void OnResponseStarted(URLRequest* request) = 0;
80 
81   // This corresponds to URLRequestDelegate::OnReadCompleted.
82   virtual void OnReadCompleted(URLRequest* request, int bytes_read) = 0;
83 
84   // Called when an URLRequest is being destroyed. Note that the request is
85   // being deleted, so it's not safe to call any methods that may result in
86   // a virtual method call.
87   virtual void OnURLRequestDestroyed(URLRequest* request) = 0;
88 
89   // Called before a request is sent and before a URLRequestJob is created to
90   // handle the request.
91   virtual URLRequestJob* OnMaybeCreateURLRequestJob(URLRequest* request) = 0;
92 
93 };
94 
95 }  // namespace net
96 
97 #endif  // NET_BASE_NETWORK_DELEGATE_H_
98