| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ | 6 #define CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "content/browser/loader/layered_resource_handler.h" | 13 #include "content/browser/loader/layered_resource_handler.h" |
| 14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 15 #include "content/public/browser/resource_controller.h" | 15 #include "content/public/browser/resource_controller.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 class URLRequest; | 18 class URLRequest; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 class PluginService; | |
| 23 class ResourceDispatcherHostImpl; | |
| 24 struct WebPluginInfo; | |
| 25 | 22 |
| 26 // ResourceHandler that, if necessary, buffers a response body without passing | 23 // ResourceHandler that, if necessary, buffers a response body without passing |
| 27 // it to the next ResourceHandler until it can perform mime sniffing on it. | 24 // it to the next ResourceHandler until it can perform mime sniffing on it. |
| 28 // Once a response's MIME type is known, initiates special handling of the | |
| 29 // response if needed (starts downloads, sends data to some plugin types via a | |
| 30 // special channel). | |
| 31 // | 25 // |
| 32 // Uses the buffer provided by the original event handler for buffering, and | 26 // Uses the buffer provided by the original event handler for buffering, and |
| 33 // continues to reuses it until it can determine the MIME type | 27 // continues to reuses it until it can determine the MIME type |
| 34 // subsequent reads until it's done buffering. As a result, the buffer | 28 // subsequent reads until it's done buffering. As a result, the buffer |
| 35 // returned by the next ResourceHandler must have a capacity of at least | 29 // returned by the next ResourceHandler must have a capacity of at least |
| 36 // net::kMaxBytesToSniff * 2. | 30 // net::kMaxBytesToSniff * 2. |
| 37 // | 31 // |
| 38 // Before a request is sent, this ResourceHandler will also set an appropriate | 32 // Before a request is sent, this ResourceHandler will also set an appropriate |
| 39 // Accept header on the request based on its ResourceType, if one isn't already | 33 // Accept header on the request based on its ResourceType, if one isn't already |
| 40 // present. | 34 // present. |
| 41 class CONTENT_EXPORT MimeTypeResourceHandler | 35 class CONTENT_EXPORT MimeSniffingResourceHandler |
| 42 : public LayeredResourceHandler, | 36 : public LayeredResourceHandler, |
| 43 public ResourceController { | 37 public ResourceController { |
| 44 public: | 38 public: |
| 45 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. | 39 MimeSniffingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
| 46 MimeTypeResourceHandler(std::unique_ptr<ResourceHandler> next_handler, | 40 net::URLRequest* request); |
| 47 ResourceDispatcherHostImpl* host, | 41 ~MimeSniffingResourceHandler() override; |
| 48 PluginService* plugin_service, | |
| 49 net::URLRequest* request); | |
| 50 ~MimeTypeResourceHandler() override; | |
| 51 | 42 |
| 52 private: | 43 private: |
| 44 friend class MimeSniffingResourceHandlerTest; |
| 45 enum State { |
| 46 // Starting state of the MimeSniffingResourceHandler. In this state, it is |
| 47 // acting as a blind pass-through ResourceHandler until the response is |
| 48 // received. |
| 49 STATE_STARTING, |
| 50 |
| 51 // In this state, the MimeSniffingResourceHandler is buffering the response |
| 52 // data in read_buffer_. |
| 53 STATE_BUFFERING, |
| 54 |
| 55 // In this state, the MimeSniffingResourceHandler is replaying the buffered |
| 56 // OnResponseStarted event to the downstream ResourceHandlers. |
| 57 STATE_REPLAYING_RESPONSE_RECEIVED, |
| 58 |
| 59 // In this state, the MimeSniffingResourceHandler is just a blind |
| 60 // pass-through |
| 61 // ResourceHandler. |
| 62 STATE_STREAMING, |
| 63 }; |
| 64 |
| 53 // ResourceHandler implementation: | 65 // ResourceHandler implementation: |
| 54 void SetController(ResourceController* controller) override; | 66 void SetController(ResourceController* controller) override; |
| 67 bool OnWillStart(const GURL&, bool* defer) override; |
| 55 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | 68 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
| 56 bool OnWillStart(const GURL&, bool* defer) override; | |
| 57 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 69 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 58 int* buf_size, | 70 int* buf_size, |
| 59 int min_size) override; | 71 int min_size) override; |
| 60 bool OnReadCompleted(int bytes_read, bool* defer) override; | 72 bool OnReadCompleted(int bytes_read, bool* defer) override; |
| 61 void OnResponseCompleted(const net::URLRequestStatus& status, | 73 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 62 const std::string& security_info, | 74 const std::string& security_info, |
| 63 bool* defer) override; | 75 bool* defer) override; |
| 64 | 76 |
| 65 // ResourceController implementation: | 77 // ResourceController implementation: |
| 66 void Resume() override; | 78 void Resume() override; |
| 67 void Cancel() override; | 79 void Cancel() override; |
| 68 void CancelAndIgnore() override; | 80 void CancelAndIgnore() override; |
| 69 void CancelWithError(int error_code) override; | 81 void CancelWithError(int error_code) override; |
| 70 | 82 |
| 71 bool ProcessResponse(bool* defer); | 83 // Replays the buffered data to the downstream ResourceHandlers. |
| 84 bool ProcessReplay(bool* defer); |
| 85 bool ReplayResponseReceived(bool* defer); |
| 86 bool ReplayResponseReceivedNewHandler(bool* defer); |
| 87 bool ReplayReadCompleted(bool* defer); |
| 72 | 88 |
| 73 bool ShouldSniffContent(); | 89 bool ShouldSniffContent(); |
| 74 bool DetermineMimeType(); | 90 bool DetermineMimeType(); |
| 75 // Determines whether a plugin will handle the current request, and if so, | |
| 76 // sets up the handler to direct the response to that plugin. Returns false | |
| 77 // if there is an error and the request should be cancelled and true | |
| 78 // otherwise. |defer| is set to true if plugin data is stale and needs to be | |
| 79 // refreshed before the request can be handled (in this case the function | |
| 80 // still returns true). If the request is directed to a plugin, | |
| 81 // |handled_by_plugin| is set to true. | |
| 82 bool SelectPluginHandler(bool* defer, bool* handled_by_plugin); | |
| 83 // Returns false if the request should be cancelled. | |
| 84 bool SelectNextHandler(bool* defer); | |
| 85 bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler, | |
| 86 const std::string& payload_for_old_handler); | |
| 87 | 91 |
| 88 bool ReplayReadCompleted(bool* defer); | |
| 89 void CallReplayReadCompleted(); | |
| 90 | |
| 91 bool MustDownload(); | |
| 92 | |
| 93 // Copies data from |read_buffer_| to |next_handler_|. | |
| 94 bool CopyReadBufferToNextHandler(); | |
| 95 | |
| 96 // Called on the IO thread once the list of plugins has been loaded. | |
| 97 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); | |
| 98 | |
| 99 enum State { | |
| 100 STATE_STARTING, | |
| 101 | |
| 102 // In this state, we are filling read_buffer_ with data for the purpose | |
| 103 // of sniffing the mime type of the response. | |
| 104 STATE_BUFFERING, | |
| 105 | |
| 106 // In this state, we are select an appropriate downstream ResourceHandler | |
| 107 // based on the mime type of the response. We are also potentially waiting | |
| 108 // for plugins to load so that we can determine if a plugin is available to | |
| 109 // handle the mime type. | |
| 110 STATE_PROCESSING, | |
| 111 | |
| 112 // In this state, we are replaying buffered events (OnResponseStarted and | |
| 113 // OnReadCompleted) to the downstream ResourceHandler. | |
| 114 STATE_REPLAYING, | |
| 115 | |
| 116 // In this state, we are just a blind pass-through ResourceHandler. | |
| 117 STATE_STREAMING | |
| 118 }; | |
| 119 State state_; | 92 State state_; |
| 120 | 93 |
| 94 // Used to buffer the reponse received until replay. |
| 121 scoped_refptr<ResourceResponse> response_; | 95 scoped_refptr<ResourceResponse> response_; |
| 122 ResourceDispatcherHostImpl* host_; | |
| 123 #if defined(ENABLE_PLUGINS) | |
| 124 PluginService* plugin_service_; | |
| 125 #endif | |
| 126 scoped_refptr<net::IOBuffer> read_buffer_; | 96 scoped_refptr<net::IOBuffer> read_buffer_; |
| 127 int read_buffer_size_; | 97 int read_buffer_size_; |
| 128 int bytes_read_; | 98 int bytes_read_; |
| 129 | 99 |
| 130 bool must_download_; | 100 DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler); |
| 131 bool must_download_is_set_; | |
| 132 | |
| 133 base::WeakPtrFactory<MimeTypeResourceHandler> weak_ptr_factory_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(MimeTypeResourceHandler); | |
| 136 }; | 101 }; |
| 137 | 102 |
| 138 } // namespace content | 103 } // namespace content |
| 139 | 104 |
| 140 #endif // CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ | 105 #endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
| OLD | NEW |