Home | History | Annotate | Download | only in loader
      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(int request_id,
     49                                 uint64 position,
     50                                 uint64 size) = 0;
     51 
     52   // The request was redirected to a new URL.  |*defer| has an initial value of
     53   // false.  Set |*defer| to true to defer the redirect.  The redirect may be
     54   // followed later on via ResourceDispatcherHost::FollowDeferredRedirect.  If
     55   // the handler returns false, then the request is cancelled.
     56   virtual bool OnRequestRedirected(int request_id, const GURL& url,
     57                                    ResourceResponse* response,
     58                                    bool* defer) = 0;
     59 
     60   // Response headers and meta data are available.  If the handler returns
     61   // false, then the request is cancelled.  Set |*defer| to true to defer
     62   // processing of the response.  Call ResourceDispatcherHostImpl::
     63   // ResumeDeferredRequest to continue processing the response.
     64   virtual bool OnResponseStarted(int request_id,
     65                                  ResourceResponse* response,
     66                                  bool* defer) = 0;
     67 
     68   // Called before the net::URLRequest for |request_id| (whose url is |url|) is
     69   // to be started.  If the handler returns false, then the request is
     70   // cancelled.  Otherwise if the return value is true, the ResourceHandler can
     71   // delay the request from starting by setting |*defer = true|.  A deferred
     72   // request will not have called net::URLRequest::Start(), and will not resume
     73   // until someone calls ResourceDispatcherHost::StartDeferredRequest().
     74   virtual bool OnWillStart(int request_id, const GURL& url, bool* defer) = 0;
     75 
     76   // Data will be read for the response.  Upon success, this method places the
     77   // size and address of the buffer where the data is to be written in its
     78   // out-params.  This call will be followed by either OnReadCompleted or
     79   // OnResponseCompleted, at which point the buffer may be recycled.
     80   //
     81   // If the handler returns false, then the request is cancelled.  Otherwise,
     82   // once data is available, OnReadCompleted will be called.
     83   virtual bool OnWillRead(int request_id,
     84                           scoped_refptr<net::IOBuffer>* buf,
     85                           int* buf_size,
     86                           int min_size) = 0;
     87 
     88   // Data (*bytes_read bytes) was written into the buffer provided by
     89   // OnWillRead.  A return value of false cancels the request, true continues
     90   // reading data.  Set |*defer| to true to defer reading more response data.
     91   // Call controller()->Resume() to continue reading response data.
     92   virtual bool OnReadCompleted(int request_id, int bytes_read,
     93                                bool* defer) = 0;
     94 
     95   // The response is complete.  The final response status is given.  Set
     96   // |*defer| to true to defer destruction to a later time.  Otherwise, the
     97   // request will be destroyed upon return.
     98   virtual void OnResponseCompleted(int request_id,
     99                                    const net::URLRequestStatus& status,
    100                                    const std::string& security_info,
    101                                    bool* defer) = 0;
    102 
    103   // This notification is synthesized by the RedirectToFileResourceHandler
    104   // to indicate progress of 'download_to_file' requests. OnReadCompleted
    105   // calls are consumed by the RedirectToFileResourceHandler and replaced
    106   // with OnDataDownloaded calls.
    107   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) = 0;
    108 
    109  protected:
    110   ResourceHandler(net::URLRequest* request);
    111 
    112   ResourceController* controller() const { return controller_; }
    113   net::URLRequest* request() const { return request_; }
    114 
    115   // Convenience functions.
    116   ResourceRequestInfoImpl* GetRequestInfo() const;
    117   int GetRequestID() const;
    118   ResourceMessageFilter* GetFilter() const;
    119 
    120  private:
    121   ResourceController* controller_;
    122   net::URLRequest* request_;
    123 };
    124 
    125 }  // namespace content
    126 
    127 #endif  // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
    128