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_BUFFERED_RESOURCE_HANDLER_H_
      6 #define CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/weak_ptr.h"
     12 #include "content/browser/loader/layered_resource_handler.h"
     13 #include "content/public/browser/resource_controller.h"
     14 
     15 namespace net {
     16 class URLRequest;
     17 }
     18 
     19 namespace content {
     20 class ResourceDispatcherHostImpl;
     21 struct WebPluginInfo;
     22 
     23 // Used to buffer a request until enough data has been received.
     24 class BufferedResourceHandler
     25     : public LayeredResourceHandler,
     26       public ResourceController {
     27  public:
     28   BufferedResourceHandler(scoped_ptr<ResourceHandler> next_handler,
     29                           ResourceDispatcherHostImpl* host,
     30                           net::URLRequest* request);
     31   virtual ~BufferedResourceHandler();
     32 
     33  private:
     34   // ResourceHandler implementation:
     35   virtual void SetController(ResourceController* controller) OVERRIDE;
     36   virtual bool OnResponseStarted(int request_id,
     37                                  ResourceResponse* response,
     38                                  bool* defer) OVERRIDE;
     39   virtual bool OnWillRead(int request_id,
     40                           net::IOBuffer** buf,
     41                           int* buf_size,
     42                           int min_size) OVERRIDE;
     43   virtual bool OnReadCompleted(int request_id, int bytes_read,
     44                                bool* defer) OVERRIDE;
     45   virtual bool OnResponseCompleted(int request_id,
     46                                    const net::URLRequestStatus& status,
     47                                    const std::string& security_info) OVERRIDE;
     48 
     49   // ResourceController implementation:
     50   virtual void Resume() OVERRIDE;
     51   virtual void Cancel() OVERRIDE;
     52   virtual void CancelAndIgnore() OVERRIDE;
     53   virtual void CancelWithError(int error_code) OVERRIDE;
     54 
     55   bool ProcessResponse(bool* defer);
     56 
     57   bool ShouldSniffContent();
     58   bool DetermineMimeType();
     59   bool SelectNextHandler(bool* defer);
     60   bool UseAlternateNextHandler(scoped_ptr<ResourceHandler> handler);
     61 
     62   bool ReplayReadCompleted(bool* defer);
     63   void CallReplayReadCompleted();
     64 
     65   bool MustDownload();
     66   bool HasSupportingPlugin(bool* is_stale);
     67 
     68   // Copies data from |read_buffer_| to |next_handler_|.
     69   bool CopyReadBufferToNextHandler(int request_id);
     70 
     71   // Called on the IO thread once the list of plugins has been loaded.
     72   void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins);
     73 
     74   enum State {
     75     STATE_STARTING,
     76 
     77     // In this state, we are filling read_buffer_ with data for the purpose
     78     // of sniffing the mime type of the response.
     79     STATE_BUFFERING,
     80 
     81     // In this state, we are select an appropriate downstream ResourceHandler
     82     // based on the mime type of the response.  We are also potentially waiting
     83     // for plugins to load so that we can determine if a plugin is available to
     84     // handle the mime type.
     85     STATE_PROCESSING,
     86 
     87     // In this state, we are replaying buffered events (OnResponseStarted and
     88     // OnReadCompleted) to the downstream ResourceHandler.
     89     STATE_REPLAYING,
     90 
     91     // In this state, we are just a blind pass-through ResourceHandler.
     92     STATE_STREAMING
     93   };
     94   State state_;
     95 
     96   scoped_refptr<ResourceResponse> response_;
     97   ResourceDispatcherHostImpl* host_;
     98   net::URLRequest* request_;
     99   scoped_refptr<net::IOBuffer> read_buffer_;
    100   int read_buffer_size_;
    101   int bytes_read_;
    102 
    103   bool must_download_;
    104   bool must_download_is_set_;
    105 
    106   base::WeakPtrFactory<BufferedResourceHandler> weak_ptr_factory_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(BufferedResourceHandler);
    109 };
    110 
    111 }  // namespace content
    112 
    113 #endif  // CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_
    114