blob: caf9a7a6803194a918f1b547a803db7393015ac2 [file] [log] [blame]
[email protected]62b4a962012-01-18 22:53:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1d8a3d1f2011-02-19 07:11:522// 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
[email protected]678c0362012-12-05 08:02:4412#ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
13#define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
[email protected]1d8a3d1f2011-02-19 07:11:5214
15#include <string>
16
[email protected]ef5306e2013-10-15 19:38:1817#include "base/memory/ref_counted.h"
[email protected]fb441962013-05-08 05:35:2418#include "base/sequenced_task_runner_helpers.h"
[email protected]d651cbc2012-05-31 21:33:0519#include "base/threading/non_thread_safe.h"
[email protected]62b4a962012-01-18 22:53:1620#include "content/common/content_export.h"
[email protected]1d8a3d1f2011-02-19 07:11:5221
[email protected]2336ffe2011-11-24 01:23:3422class GURL;
23
[email protected]1d8a3d1f2011-02-19 07:11:5224namespace net {
25class IOBuffer;
[email protected]ef5306e2013-10-15 19:38:1826class URLRequest;
[email protected]1d8a3d1f2011-02-19 07:11:5227class URLRequestStatus;
[email protected]cba24642014-08-15 20:49:5928struct RedirectInfo;
[email protected]1d8a3d1f2011-02-19 07:11:5229} // namespace net
30
[email protected]42ac7852012-06-01 06:38:4831namespace content {
[email protected]56f0b082012-06-14 07:12:3232class ResourceController;
[email protected]ef5306e2013-10-15 19:38:1833class ResourceMessageFilter;
34class ResourceRequestInfoImpl;
[email protected]42ac7852012-06-01 06:38:4835struct ResourceResponse;
36
[email protected]d651cbc2012-05-31 21:33:0537// The resource dispatcher host uses this interface to process network events
38// for an URLRequest instance. A ResourceHandler's lifetime is bound to its
39// associated URLRequest.
[email protected]62b4a962012-01-18 22:53:1640class CONTENT_EXPORT ResourceHandler
[email protected]d651cbc2012-05-31 21:33:0541 : public NON_EXPORTED_BASE(base::NonThreadSafe) {
[email protected]1d8a3d1f2011-02-19 07:11:5242 public:
[email protected]d651cbc2012-05-31 21:33:0543 virtual ~ResourceHandler() {}
44
[email protected]56f0b082012-06-14 07:12:3245 // Sets the controller for this handler.
46 virtual void SetController(ResourceController* controller);
47
[email protected]1d8a3d1f2011-02-19 07:11:5248 // The request was redirected to a new URL. |*defer| has an initial value of
49 // false. Set |*defer| to true to defer the redirect. The redirect may be
[email protected]5e5e0c82012-01-25 09:12:2950 // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If
51 // the handler returns false, then the request is cancelled.
[email protected]cba24642014-08-15 20:49:5952 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
[email protected]42ac7852012-06-01 06:38:4853 ResourceResponse* response,
[email protected]1d8a3d1f2011-02-19 07:11:5254 bool* defer) = 0;
55
[email protected]5e5e0c82012-01-25 09:12:2956 // Response headers and meta data are available. If the handler returns
[email protected]22b305442012-05-21 18:02:5957 // false, then the request is cancelled. Set |*defer| to true to defer
58 // processing of the response. Call ResourceDispatcherHostImpl::
59 // ResumeDeferredRequest to continue processing the response.
[email protected]d22e800e2014-05-28 21:55:5760 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0;
[email protected]1d8a3d1f2011-02-19 07:11:5261
[email protected]d22e800e2014-05-28 21:55:5762 // Called before the net::URLRequest (whose url is |url|) is to be started.
63 // If the handler returns false, then the request is cancelled. Otherwise if
64 // the return value is true, the ResourceHandler can delay the request from
65 // starting by setting |*defer = true|. A deferred request will not have
66 // called net::URLRequest::Start(), and will not resume until someone calls
67 // ResourceDispatcherHost::StartDeferredRequest().
68 virtual bool OnWillStart(const GURL& url, bool* defer) = 0;
[email protected]1d8a3d1f2011-02-19 07:11:5269
[email protected]d22e800e2014-05-28 21:55:5770 // Called before the net::URLRequest (whose url is |url|} uses the network for
71 // the first time to load the resource. If the handler returns false, then the
72 // request is cancelled. Otherwise if the return value is true, the
73 // ResourceHandler can delay the request from starting by setting |*defer =
74 // true|. Call controller()->Resume() to continue if deferred.
75 virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) = 0;
[email protected]5584eab2014-01-09 22:16:1576
[email protected]1d8a3d1f2011-02-19 07:11:5277 // Data will be read for the response. Upon success, this method places the
78 // size and address of the buffer where the data is to be written in its
[email protected]698d13f2014-05-13 19:30:5279 // out-params. This call will be followed by either OnReadCompleted (on
80 // successful read or EOF) or OnResponseCompleted (on error). If
81 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may
82 // not be recycled and may potentially outlive the handler. If |min_size| is
83 // not -1, it is the minimum size of the returned buffer.
[email protected]5e5e0c82012-01-25 09:12:2984 //
[email protected]926360f2012-06-29 04:45:0285 // If the handler returns false, then the request is cancelled. Otherwise,
86 // once data is available, OnReadCompleted will be called.
[email protected]d22e800e2014-05-28 21:55:5787 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
[email protected]1d8a3d1f2011-02-19 07:11:5288 int* buf_size,
89 int min_size) = 0;
90
91 // Data (*bytes_read bytes) was written into the buffer provided by
[email protected]5e5e0c82012-01-25 09:12:2992 // OnWillRead. A return value of false cancels the request, true continues
[email protected]22b305442012-05-21 18:02:5993 // reading data. Set |*defer| to true to defer reading more response data.
[email protected]17b674e02014-05-10 04:30:0294 // Call controller()->Resume() to continue reading response data. A zero
95 // |bytes_read| signals that no further data is available.
[email protected]d22e800e2014-05-28 21:55:5796 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0;
[email protected]1d8a3d1f2011-02-19 07:11:5297
[email protected]3780874a2013-11-18 05:49:0398 // The response is complete. The final response status is given. Set
99 // |*defer| to true to defer destruction to a later time. Otherwise, the
100 // request will be destroyed upon return.
[email protected]d22e800e2014-05-28 21:55:57101 virtual void OnResponseCompleted(const net::URLRequestStatus& status,
[email protected]3780874a2013-11-18 05:49:03102 const std::string& security_info,
103 bool* defer) = 0;
[email protected]1d8a3d1f2011-02-19 07:11:52104
[email protected]1d8a3d1f2011-02-19 07:11:52105 // This notification is synthesized by the RedirectToFileResourceHandler
106 // to indicate progress of 'download_to_file' requests. OnReadCompleted
107 // calls are consumed by the RedirectToFileResourceHandler and replaced
108 // with OnDataDownloaded calls.
[email protected]d22e800e2014-05-28 21:55:57109 virtual void OnDataDownloaded(int bytes_downloaded) = 0;
[email protected]56f0b082012-06-14 07:12:32110
111 protected:
[email protected]ef5306e2013-10-15 19:38:18112 ResourceHandler(net::URLRequest* request);
113
114 ResourceController* controller() const { return controller_; }
115 net::URLRequest* request() const { return request_; }
116
117 // Convenience functions.
118 ResourceRequestInfoImpl* GetRequestInfo() const;
119 int GetRequestID() const;
120 ResourceMessageFilter* GetFilter() const;
[email protected]56f0b082012-06-14 07:12:32121
122 private:
123 ResourceController* controller_;
[email protected]ef5306e2013-10-15 19:38:18124 net::URLRequest* request_;
[email protected]1d8a3d1f2011-02-19 07:11:52125};
126
[email protected]42ac7852012-06-01 06:38:48127} // namespace content
128
[email protected]678c0362012-12-05 08:02:44129#endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_