Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: content/browser/loader/resource_handler.h

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Silly merge Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is the browser side of the resource dispatcher, it receives requests 5 // This is the browser side of the resource dispatcher, it receives requests
6 // from the RenderProcessHosts, and dispatches them to URLRequests. It then 6 // from the RenderProcessHosts, and dispatches them to URLRequests. It then
7 // fowards the messages from the URLRequests back to the correct process for 7 // fowards the messages from the URLRequests back to the correct process for
8 // handling. 8 // handling.
9 // 9 //
10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc e-loading 10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc e-loading
11 11
12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
13 #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 13 #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
14 14
15 #include <memory>
15 #include <string> 16 #include <string>
16 17
17 #include "base/compiler_specific.h" 18 #include "base/compiler_specific.h"
18 #include "base/macros.h" 19 #include "base/macros.h"
19 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
20 #include "base/threading/non_thread_safe.h" 21 #include "base/threading/non_thread_safe.h"
22 #include "content/browser/loader/resource_controller.h"
21 #include "content/common/content_export.h" 23 #include "content/common/content_export.h"
22 24
23 class GURL; 25 class GURL;
24 26
25 namespace net { 27 namespace net {
26 class IOBuffer; 28 class IOBuffer;
27 class URLRequest; 29 class URLRequest;
28 class URLRequestStatus; 30 class URLRequestStatus;
29 struct RedirectInfo; 31 struct RedirectInfo;
30 } // namespace net 32 } // namespace net
31 33
32 namespace content { 34 namespace content {
33 class ResourceController;
34 class ResourceMessageFilter; 35 class ResourceMessageFilter;
35 class ResourceRequestInfoImpl; 36 class ResourceRequestInfoImpl;
36 struct ResourceResponse; 37 struct ResourceResponse;
37 38
38 // The resource dispatcher host uses this interface to process network events 39 // The resource dispatcher host uses this interface to process network events
39 // for an URLRequest instance. A ResourceHandler's lifetime is bound to its 40 // for an URLRequest instance. A ResourceHandler's lifetime is bound to its
40 // associated URLRequest. 41 // associated URLRequest.
42 //
43 // No ResourceHandler method will ever be called synchronously when it calls
44 // into the ResourceController passed in to it, either to resume or cancel the
45 // request.
41 class CONTENT_EXPORT ResourceHandler 46 class CONTENT_EXPORT ResourceHandler
42 : public NON_EXPORTED_BASE(base::NonThreadSafe) { 47 : public NON_EXPORTED_BASE(base::NonThreadSafe) {
43 public: 48 public:
44 virtual ~ResourceHandler(); 49 virtual ~ResourceHandler();
45 50
46 // Sets the controller for this handler. 51 class CONTENT_EXPORT Delegate {
Charlie Harrison 2017/01/25 20:23:00 A class comment describing the delegate would be n
mmenke 2017/01/25 22:07:59 Done. Just added a comment.
47 virtual void SetController(ResourceController* controller); 52 protected:
53 Delegate();
54 virtual ~Delegate();
48 55
49 // The request was redirected to a new URL. |*defer| has an initial value of 56 private:
50 // false. Set |*defer| to true to defer the redirect. The redirect may be 57 friend class ResourceHandler;
51 // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If
52 // the handler returns false, then the request is cancelled.
53 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
54 ResourceResponse* response,
55 bool* defer) = 0;
56 58
57 // Response headers and meta data are available. If the handler returns 59 // Cancels the request when the class does not currently have ownership of
58 // false, then the request is cancelled. Set |*defer| to true to defer 60 // the ResourceController.
59 // processing of the response. Call ResourceDispatcherHostImpl:: 61 // |error_code| indicates the reason for the cancellation, and
60 // ResumeDeferredRequest to continue processing the response. 62 // |tell_renderer| whether the renderer needs to be informed of the
61 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; 63 // cancellation.
64 virtual void OutOfBandCancel(int error_code, bool tell_renderer) = 0;
65
66 DISALLOW_COPY_AND_ASSIGN(Delegate);
67 };
68
69 // Sets the ResourceHandler::Delegate, which handles out-of-bounds
70 // cancellation.
71 virtual void SetDelegate(Delegate* delegate);
72
73 // The request was redirected to a new URL. The request will not continue
74 // until one of |controller|'s resume or cancellation methods is invoked.
75 // |response| may be destroyed as soon as the method returns, so if the
76 // ResourceHandler wants to continue to use it, it must maintain a reference
77 // to it.
78 virtual void OnRequestRedirected(
79 const net::RedirectInfo& redirect_info,
80 ResourceResponse* response,
81 std::unique_ptr<ResourceController> controller) = 0;
82
83 // Response headers and metadata are available. The request will not continue
84 // until one of |controller|'s resume or cancellation methods is invoked.
85 // |response| may be destroyed as soon as the method returns, so if the
86 // ResourceHandler wants to continue to use it, it must maintain a reference
87 // to it.
88 virtual void OnResponseStarted(
89 ResourceResponse* response,
90 std::unique_ptr<ResourceController> controller) = 0;
62 91
63 // Called before the net::URLRequest (whose url is |url|) is to be started. 92 // Called before the net::URLRequest (whose url is |url|) is to be started.
64 // If the handler returns false, then the request is cancelled. Otherwise if 93 // The request will not continue until one of |controller|'s resume or
65 // the return value is true, the ResourceHandler can delay the request from 94 // cancellation methods is invoked.
66 // starting by setting |*defer = true|. A deferred request will not have 95 virtual void OnWillStart(const GURL& url,
67 // called net::URLRequest::Start(), and will not resume until someone calls 96 std::unique_ptr<ResourceController> controller) = 0;
68 // ResourceDispatcherHost::StartDeferredRequest().
69 virtual bool OnWillStart(const GURL& url, bool* defer) = 0;
70 97
71 // Data will be read for the response. Upon success, this method places the 98 // Data will be read for the response. Upon success, this method places the
72 // size and address of the buffer where the data is to be written in its 99 // size and address of the buffer where the data is to be written in its
73 // out-params. This call will be followed by either OnReadCompleted (on 100 // out-params. This call will be followed by either OnReadCompleted (on
74 // successful read or EOF) or OnResponseCompleted (on error). If 101 // successful read or EOF) or OnResponseCompleted (on error). If
75 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may 102 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may
76 // not be recycled and may potentially outlive the handler. If |min_size| is 103 // not be recycled and may potentially outlive the handler. If |min_size| is
77 // not -1, it is the minimum size of the returned buffer. 104 // not -1, it is the minimum size of the returned buffer.
78 // 105 //
79 // If the handler returns false, then the request is cancelled. Otherwise, 106 // If the handler returns false, then the request is cancelled. Otherwise,
80 // once data is available, OnReadCompleted will be called. 107 // once data is available, OnReadCompleted will be called.
108 // TODO(mmenke): Make this method use a ResourceController, and allow it to
109 // succeed asynchronously.
81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 110 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
82 int* buf_size, 111 int* buf_size,
83 int min_size) = 0; 112 int min_size) = 0;
84 113
85 // Data (*bytes_read bytes) was written into the buffer provided by 114 // Data (*bytes_read bytes) was written into the buffer provided by
86 // OnWillRead. A return value of false cancels the request, true continues 115 // OnWillRead. The request will not continue until one of |controller|'s
87 // reading data. Set |*defer| to true to defer reading more response data. 116 // resume or cancellation methods is invoked. A zero |bytes_read| signals
88 // Call controller()->Resume() to continue reading response data. A zero 117 // that no further data will be received.
89 // |bytes_read| signals that no further data is available. 118 virtual void OnReadCompleted(
90 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; 119 int bytes_read,
120 std::unique_ptr<ResourceController> controller) = 0;
91 121
92 // The response is complete. The final response status is given. Set 122 // The response is complete. The final response status is given. The request
93 // |*defer| to true to defer destruction to a later time. Otherwise, the 123 // will not be deleted until controller's Resume() method is invoked. It is
94 // request will be destroyed upon return. 124 // illegal to use its cancellation methods.
95 virtual void OnResponseCompleted(const net::URLRequestStatus& status, 125 virtual void OnResponseCompleted(
96 bool* defer) = 0; 126 const net::URLRequestStatus& status,
127 std::unique_ptr<ResourceController> controller) = 0;
97 128
98 // This notification is synthesized by the RedirectToFileResourceHandler 129 // This notification is synthesized by the RedirectToFileResourceHandler
99 // to indicate progress of 'download_to_file' requests. OnReadCompleted 130 // to indicate progress of 'download_to_file' requests. OnReadCompleted
100 // calls are consumed by the RedirectToFileResourceHandler and replaced 131 // calls are consumed by the RedirectToFileResourceHandler and replaced
101 // with OnDataDownloaded calls. 132 // with OnDataDownloaded calls.
102 virtual void OnDataDownloaded(int bytes_downloaded) = 0; 133 virtual void OnDataDownloaded(int bytes_downloaded) = 0;
103 134
104 protected: 135 protected:
105 ResourceHandler(net::URLRequest* request); 136 explicit ResourceHandler(net::URLRequest* request);
106 137
107 ResourceController* controller() const { return controller_; } 138 // Convenience methods for managing a ResourceHandler's controller in the
139 // async completion case. These ensure that the controller is nullptr after
140 // being invoked, which allows for DCHECKing on it and better crashes on
141 // calling into deleted objects.
142
143 // Passes ownership of |controller| to |this|. Nothing is done with the
144 // |controller| automatically.
145 void HoldController(std::unique_ptr<ResourceController> controller);
146
147 // Returns ownership of the ResourceController previously passed in to
148 // HoldController.
149 std::unique_ptr<ResourceController> ReleaseController();
150
151 bool has_controller() const { return !!controller_; }
152
153 // These call the corresponding methods on the ResourceController previously
154 // oassed to HoldController and then destroy it.
Charlie Harrison 2017/01/25 20:23:00 s/oassed/passed
mmenke 2017/01/25 22:07:59 Done.
155 void Resume();
156 void Cancel();
157 void CancelAndIgnore();
158 void CancelWithError(int error_code);
159
160 // Cancels the request when the class does not currently have ownership of the
161 // ResourceController.
162 void OutOfBandCancel(int error_code, bool tell_renderer);
163
108 net::URLRequest* request() const { return request_; } 164 net::URLRequest* request() const { return request_; }
109 165
110 // Convenience functions. 166 // Convenience functions.
111 ResourceRequestInfoImpl* GetRequestInfo() const; 167 ResourceRequestInfoImpl* GetRequestInfo() const;
112 int GetRequestID() const; 168 int GetRequestID() const;
113 ResourceMessageFilter* GetFilter() const; 169 ResourceMessageFilter* GetFilter() const;
114 170
171 Delegate* delegate() { return delegate_; }
172
115 private: 173 private:
116 ResourceController* controller_;
117 net::URLRequest* request_; 174 net::URLRequest* request_;
175 Delegate* delegate_;
176 std::unique_ptr<ResourceController> controller_;
118 177
119 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); 178 DISALLOW_COPY_AND_ASSIGN(ResourceHandler);
120 }; 179 };
121 180
122 } // namespace content 181 } // namespace content
123 182
124 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 183 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698