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 #ifndef CONTENT_BROWSER_LOADER_ASYNC_RESOURCE_HANDLER_H_
      6 #define CONTENT_BROWSER_LOADER_ASYNC_RESOURCE_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "content/browser/loader/resource_handler.h"
     12 #include "content/browser/loader/resource_message_delegate.h"
     13 #include "url/gurl.h"
     14 
     15 namespace net {
     16 class URLRequest;
     17 }
     18 
     19 namespace content {
     20 class ResourceBuffer;
     21 class ResourceContext;
     22 class ResourceDispatcherHostImpl;
     23 class ResourceMessageFilter;
     24 class SharedIOBuffer;
     25 
     26 // Used to complete an asynchronous resource request in response to resource
     27 // load events from the resource dispatcher host.
     28 class AsyncResourceHandler : public ResourceHandler,
     29                              public ResourceMessageDelegate {
     30  public:
     31   AsyncResourceHandler(net::URLRequest* request,
     32                        ResourceDispatcherHostImpl* rdh);
     33   virtual ~AsyncResourceHandler();
     34 
     35   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     36 
     37   // ResourceHandler implementation:
     38   virtual bool OnUploadProgress(uint64 position, uint64 size) OVERRIDE;
     39   virtual bool OnRequestRedirected(const GURL& new_url,
     40                                    ResourceResponse* response,
     41                                    bool* defer) OVERRIDE;
     42   virtual bool OnResponseStarted(ResourceResponse* response,
     43                                  bool* defer) OVERRIDE;
     44   virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE;
     45   virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) OVERRIDE;
     46   virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
     47                           int* buf_size,
     48                           int min_size) OVERRIDE;
     49   virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE;
     50   virtual void OnResponseCompleted(const net::URLRequestStatus& status,
     51                                    const std::string& security_info,
     52                                    bool* defer) OVERRIDE;
     53   virtual void OnDataDownloaded(int bytes_downloaded) OVERRIDE;
     54 
     55  private:
     56   // IPC message handlers:
     57   void OnFollowRedirect(int request_id);
     58   void OnDataReceivedACK(int request_id);
     59 
     60   bool EnsureResourceBufferIsInitialized();
     61   void ResumeIfDeferred();
     62   void OnDefer();
     63 
     64   scoped_refptr<ResourceBuffer> buffer_;
     65   ResourceDispatcherHostImpl* rdh_;
     66 
     67   // Number of messages we've sent to the renderer that we haven't gotten an
     68   // ACK for. This allows us to avoid having too many messages in flight.
     69   int pending_data_count_;
     70 
     71   int allocation_size_;
     72 
     73   bool did_defer_;
     74 
     75   bool has_checked_for_sufficient_resources_;
     76   bool sent_received_response_msg_;
     77   bool sent_first_data_msg_;
     78 
     79   int64_t reported_transfer_size_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(AsyncResourceHandler);
     82 };
     83 
     84 }  // namespace content
     85 
     86 #endif  // CONTENT_BROWSER_LOADER_ASYNC_RESOURCE_HANDLER_H_
     87