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 3 // found in the LICENSE file. 4 5 // This is the browser side of the resource dispatcher, it receives requests 6 // from the RenderProcessHosts, and dispatches them to URLRequests. It then 7 // fowards the messages from the URLRequests back to the correct process for 8 // handling. 9 // 10 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading 11 12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 13 #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 14 15 #include <string> 16 17 #include "base/memory/ref_counted.h" 18 #include "base/sequenced_task_runner_helpers.h" 19 #include "base/threading/non_thread_safe.h" 20 #include "content/common/content_export.h" 21 22 class GURL; 23 24 namespace net { 25 class IOBuffer; 26 class URLRequest; 27 class URLRequestStatus; 28 } // namespace net 29 30 namespace content { 31 class ResourceController; 32 class ResourceMessageFilter; 33 class ResourceRequestInfoImpl; 34 struct ResourceResponse; 35 36 // The resource dispatcher host uses this interface to process network events 37 // for an URLRequest instance. A ResourceHandler's lifetime is bound to its 38 // associated URLRequest. 39 class CONTENT_EXPORT ResourceHandler 40 : public NON_EXPORTED_BASE(base::NonThreadSafe) { 41 public: 42 virtual ~ResourceHandler() {} 43 44 // Sets the controller for this handler. 45 virtual void SetController(ResourceController* controller); 46 47 // Called as upload progress is made. The return value is ignored. 48 virtual bool OnUploadProgress(uint64 position, uint64 size) = 0; 49 50 // The request was redirected to a new URL. |*defer| has an initial value of 51 // false. Set |*defer| to true to defer the redirect. The redirect may be 52 // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If 53 // the handler returns false, then the request is cancelled. 54 virtual bool OnRequestRedirected(const GURL& url, 55 ResourceResponse* response, 56 bool* defer) = 0; 57 58 // Response headers and meta data are available. If the handler returns 59 // false, then the request is cancelled. Set |*defer| to true to defer 60 // processing of the response. Call ResourceDispatcherHostImpl:: 61 // ResumeDeferredRequest to continue processing the response. 62 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; 63 64 // Called before the net::URLRequest (whose url is |url|) is to be started. 65 // If the handler returns false, then the request is cancelled. Otherwise if 66 // the return value is true, the ResourceHandler can delay the request from 67 // starting by setting |*defer = true|. A deferred request will not have 68 // called net::URLRequest::Start(), and will not resume until someone calls 69 // ResourceDispatcherHost::StartDeferredRequest(). 70 virtual bool OnWillStart(const GURL& url, bool* defer) = 0; 71 72 // Called before the net::URLRequest (whose url is |url|} uses the network for 73 // the first time to load the resource. If the handler returns false, then the 74 // request is cancelled. Otherwise if the return value is true, the 75 // ResourceHandler can delay the request from starting by setting |*defer = 76 // true|. Call controller()->Resume() to continue if deferred. 77 virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) = 0; 78 79 // Data will be read for the response. Upon success, this method places the 80 // size and address of the buffer where the data is to be written in its 81 // out-params. This call will be followed by either OnReadCompleted (on 82 // successful read or EOF) or OnResponseCompleted (on error). If 83 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may 84 // not be recycled and may potentially outlive the handler. If |min_size| is 85 // not -1, it is the minimum size of the returned buffer. 86 // 87 // If the handler returns false, then the request is cancelled. Otherwise, 88 // once data is available, OnReadCompleted will be called. 89 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 90 int* buf_size, 91 int min_size) = 0; 92 93 // Data (*bytes_read bytes) was written into the buffer provided by 94 // OnWillRead. A return value of false cancels the request, true continues 95 // reading data. Set |*defer| to true to defer reading more response data. 96 // Call controller()->Resume() to continue reading response data. A zero 97 // |bytes_read| signals that no further data is available. 98 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; 99 100 // The response is complete. The final response status is given. Set 101 // |*defer| to true to defer destruction to a later time. Otherwise, the 102 // request will be destroyed upon return. 103 virtual void OnResponseCompleted(const net::URLRequestStatus& status, 104 const std::string& security_info, 105 bool* defer) = 0; 106 107 // This notification is synthesized by the RedirectToFileResourceHandler 108 // to indicate progress of 'download_to_file' requests. OnReadCompleted 109 // calls are consumed by the RedirectToFileResourceHandler and replaced 110 // with OnDataDownloaded calls. 111 virtual void OnDataDownloaded(int bytes_downloaded) = 0; 112 113 protected: 114 ResourceHandler(net::URLRequest* request); 115 116 ResourceController* controller() const { return controller_; } 117 net::URLRequest* request() const { return request_; } 118 119 // Convenience functions. 120 ResourceRequestInfoImpl* GetRequestInfo() const; 121 int GetRequestID() const; 122 ResourceMessageFilter* GetFilter() const; 123 124 private: 125 ResourceController* controller_; 126 net::URLRequest* request_; 127 }; 128 129 } // namespace content 130 131 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ 132