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 scoped_refptr<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 void OnResponseCompleted(int request_id, 46 const net::URLRequestStatus& status, 47 const std::string& security_info, 48 bool* defer) OVERRIDE; 49 50 // ResourceController implementation: 51 virtual void Resume() OVERRIDE; 52 virtual void Cancel() OVERRIDE; 53 virtual void CancelAndIgnore() OVERRIDE; 54 virtual void CancelWithError(int error_code) OVERRIDE; 55 56 bool ProcessResponse(bool* defer); 57 58 bool ShouldSniffContent(); 59 bool DetermineMimeType(); 60 bool SelectNextHandler(bool* defer); 61 bool UseAlternateNextHandler(scoped_ptr<ResourceHandler> handler); 62 63 bool ReplayReadCompleted(bool* defer); 64 void CallReplayReadCompleted(); 65 66 bool MustDownload(); 67 bool HasSupportingPlugin(bool* is_stale); 68 69 // Copies data from |read_buffer_| to |next_handler_|. 70 bool CopyReadBufferToNextHandler(int request_id); 71 72 // Called on the IO thread once the list of plugins has been loaded. 73 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); 74 75 enum State { 76 STATE_STARTING, 77 78 // In this state, we are filling read_buffer_ with data for the purpose 79 // of sniffing the mime type of the response. 80 STATE_BUFFERING, 81 82 // In this state, we are select an appropriate downstream ResourceHandler 83 // based on the mime type of the response. We are also potentially waiting 84 // for plugins to load so that we can determine if a plugin is available to 85 // handle the mime type. 86 STATE_PROCESSING, 87 88 // In this state, we are replaying buffered events (OnResponseStarted and 89 // OnReadCompleted) to the downstream ResourceHandler. 90 STATE_REPLAYING, 91 92 // In this state, we are just a blind pass-through ResourceHandler. 93 STATE_STREAMING 94 }; 95 State state_; 96 97 scoped_refptr<ResourceResponse> response_; 98 ResourceDispatcherHostImpl* host_; 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