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

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

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Fix Created 4 years 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.
41 class CONTENT_EXPORT ResourceHandler 42 class CONTENT_EXPORT ResourceHandler
42 : public NON_EXPORTED_BASE(base::NonThreadSafe) { 43 : public NON_EXPORTED_BASE(base::NonThreadSafe) {
43 public: 44 public:
44 virtual ~ResourceHandler(); 45 virtual ~ResourceHandler();
45 46
46 // Sets the controller for this handler. 47 class CONTENT_EXPORT Delegate {
47 virtual void SetController(ResourceController* controller); 48 protected:
49 Delegate();
50 virtual ~Delegate();
48 51
49 // The request was redirected to a new URL. |*defer| has an initial value of 52 private:
50 // false. Set |*defer| to true to defer the redirect. The redirect may be 53 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 54
57 // Response headers and meta data are available. If the handler returns 55 // 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 56 // the
59 // processing of the response. Call ResourceDispatcherHostImpl:: 57 // ResourceController.
60 // ResumeDeferredRequest to continue processing the response. 58 // |error_code| indicates the reason for the cancellation, and
61 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; 59 // |tell_renderer| whether the renderer needs to be informed of the
60 // cancellation.
61 virtual void OutOfBandCancel(int error_code, bool tell_renderer) = 0;
62
63 DISALLOW_COPY_AND_ASSIGN(Delegate);
64 };
65
66 // Sets the ResourceHandler::Delegate, which handles out-of-bounds
67 // cancellation.
68 virtual void SetDelegate(Delegate* delegate);
69
70 // The request was redirected to a new URL. The request will not continue
71 // until one of |controller|'s resume or cancellation methods is invoked, at
72 // which point the request will be resumed synchronously.
Randy Smith (Not in Mondays) 2016/12/31 22:00:25 I'm struggling with the actual behavior and the ap
mmenke 2017/01/06 18:46:58 There isn't a PostTask: If we call Resume() immed
73 virtual void OnRequestRedirected(
74 const net::RedirectInfo& redirect_info,
75 ResourceResponse* response,
76 std::unique_ptr<ResourceController> controller) = 0;
77
78 // Response headers and metadata are available. The request will not continue
79 // until one of |controller|'s resume or cancellation methods is invoked, at
80 // which point the request will be resumed synchronously.
81 virtual void OnResponseStarted(
82 ResourceResponse* response,
83 std::unique_ptr<ResourceController> controller) = 0;
62 84
63 // Called before the net::URLRequest (whose url is |url|) is to be started. 85 // 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 86 // 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 87 // cancellation methods is invoked, at which point the request will be started
66 // starting by setting |*defer = true|. A deferred request will not have 88 // synchronously.
67 // called net::URLRequest::Start(), and will not resume until someone calls 89 virtual void OnWillStart(const GURL& url,
68 // ResourceDispatcherHost::StartDeferredRequest(). 90 std::unique_ptr<ResourceController> controller) = 0;
69 virtual bool OnWillStart(const GURL& url, bool* defer) = 0;
70 91
71 // Data will be read for the response. Upon success, this method places the 92 // 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 93 // 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 94 // out-params. This call will be followed by either OnReadCompleted (on
74 // successful read or EOF) or OnResponseCompleted (on error). If 95 // successful read or EOF) or OnResponseCompleted (on error). If
75 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may 96 // 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 97 // 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. 98 // not -1, it is the minimum size of the returned buffer.
78 // 99 //
79 // If the handler returns false, then the request is cancelled. Otherwise, 100 // If the handler returns false, then the request is cancelled. Otherwise,
80 // once data is available, OnReadCompleted will be called. 101 // once data is available, OnReadCompleted will be called.
102 // TODO(mmenke): Make this method use a ResourceController, and allow it to
103 // succeed asynchronously.
81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 104 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
82 int* buf_size, 105 int* buf_size,
83 int min_size) = 0; 106 int min_size) = 0;
84 107
85 // Data (*bytes_read bytes) was written into the buffer provided by 108 // Data (*bytes_read bytes) was written into the buffer provided by
86 // OnWillRead. A return value of false cancels the request, true continues 109 // OnWillRead. The request will not continue until one of |controller|'s
87 // reading data. Set |*defer| to true to defer reading more response data. 110 // resume or cancellation methods is invoked, at which point the request will
88 // Call controller()->Resume() to continue reading response data. A zero 111 // be resumed synchronously. A zero |bytes_read| signals that no further data
89 // |bytes_read| signals that no further data is available. 112 // is available.
90 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; 113 virtual void OnReadCompleted(
114 int bytes_read,
115 std::unique_ptr<ResourceController> controller) = 0;
91 116
92 // The response is complete. The final response status is given. Set 117 // 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 118 // will not be deleted until controller's Resume() method is invoked. It is
94 // request will be destroyed upon return. 119 // illegal to use its cancellation methods.
95 virtual void OnResponseCompleted(const net::URLRequestStatus& status, 120 virtual void OnResponseCompleted(
96 bool* defer) = 0; 121 const net::URLRequestStatus& status,
122 std::unique_ptr<ResourceController> controller) = 0;
97 123
98 // This notification is synthesized by the RedirectToFileResourceHandler 124 // This notification is synthesized by the RedirectToFileResourceHandler
99 // to indicate progress of 'download_to_file' requests. OnReadCompleted 125 // to indicate progress of 'download_to_file' requests. OnReadCompleted
100 // calls are consumed by the RedirectToFileResourceHandler and replaced 126 // calls are consumed by the RedirectToFileResourceHandler and replaced
101 // with OnDataDownloaded calls. 127 // with OnDataDownloaded calls.
102 virtual void OnDataDownloaded(int bytes_downloaded) = 0; 128 virtual void OnDataDownloaded(int bytes_downloaded) = 0;
103 129
104 protected: 130 protected:
105 ResourceHandler(net::URLRequest* request); 131 explicit ResourceHandler(net::URLRequest* request);
106 132
107 ResourceController* controller() const { return controller_; } 133 // Utility methods for managing a ResourceHandler's controller in the async
134 // completion case. These ensure that the controller is nullptr after being
135 // invoked, which allows for DCHECKing on it and better crashes on calling
136 // into deleting objects.
137
138 void set_controller(std::unique_ptr<ResourceController> controller) {
139 controller_ = std::move(controller);
140 }
141
142 bool has_controller() const { return !!controller_; }
143
144 std::unique_ptr<ResourceController> TakeController();
145
146 // These call the corresponding methods on the previously set
147 // ResourceController, and then destroy the controller.
148 void Resume();
149 void Cancel();
150 void CancelAndIgnore();
151 void CancelWithError(int error_code);
152
153 // Cancels the request when the class does not currently have ownership of the
154 // ResourceController.
155 void OutOfBandCancel(int error_code, bool tell_renderer);
156
108 net::URLRequest* request() const { return request_; } 157 net::URLRequest* request() const { return request_; }
109 158
110 // Convenience functions. 159 // Convenience functions.
111 ResourceRequestInfoImpl* GetRequestInfo() const; 160 ResourceRequestInfoImpl* GetRequestInfo() const;
112 int GetRequestID() const; 161 int GetRequestID() const;
113 ResourceMessageFilter* GetFilter() const; 162 ResourceMessageFilter* GetFilter() const;
114 163
164 Delegate* delegate() { return delegate_; }
165
115 private: 166 private:
116 ResourceController* controller_;
117 net::URLRequest* request_; 167 net::URLRequest* request_;
168 Delegate* delegate_;
169 std::unique_ptr<ResourceController> controller_;
118 170
119 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); 171 DISALLOW_COPY_AND_ASSIGN(ResourceHandler);
120 }; 172 };
121 173
122 } // namespace content 174 } // namespace content
123 175
124 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 176 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
OLDNEW
« no previous file with comments | « content/browser/loader/resource_dispatcher_host_unittest.cc ('k') | content/browser/loader/resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698