OLD | NEW |
(Empty) | |
| 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_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "content/browser/loader/layered_resource_handler.h" |
| 14 #include "content/common/content_export.h" |
| 15 #include "content/public/browser/resource_controller.h" |
| 16 |
| 17 namespace net { |
| 18 class URLRequest; |
| 19 } |
| 20 |
| 21 namespace content { |
| 22 class InterceptingResourceHandler; |
| 23 class PluginService; |
| 24 class ResourceDispatcherHostImpl; |
| 25 struct WebPluginInfo; |
| 26 |
| 27 // ResourceHandler that, if necessary, buffers a response body without passing |
| 28 // it to the next ResourceHandler until it can perform mime sniffing on it. |
| 29 // |
| 30 // Uses the buffer provided by the original event handler for buffering, and |
| 31 // continues to reuses it until it can determine the MIME type |
| 32 // subsequent reads until it's done buffering. As a result, the buffer |
| 33 // returned by the next ResourceHandler must have a capacity of at least |
| 34 // net::kMaxBytesToSniff * 2. |
| 35 // |
| 36 // Before a request is sent, this ResourceHandler will also set an appropriate |
| 37 // Accept header on the request based on its ResourceType, if one isn't already |
| 38 // present. |
| 39 class CONTENT_EXPORT MimeSniffingResourceHandler |
| 40 : public LayeredResourceHandler, |
| 41 public ResourceController { |
| 42 public: |
| 43 MimeSniffingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
| 44 ResourceDispatcherHostImpl* host, |
| 45 PluginService* plugin_service, |
| 46 InterceptingResourceHandler* intercepting_handler, |
| 47 net::URLRequest* request); |
| 48 ~MimeSniffingResourceHandler() override; |
| 49 |
| 50 private: |
| 51 friend class MimeSniffingResourceHandlerTest; |
| 52 enum State { |
| 53 // Starting state of the MimeSniffingResourceHandler. In this state, it is |
| 54 // acting as a blind pass-through ResourceHandler until the response is |
| 55 // received. |
| 56 STATE_STARTING, |
| 57 |
| 58 // In this state, the MimeSniffingResourceHandler is buffering the response |
| 59 // data in read_buffer_, waiting to sniff the mime type and make a choice |
| 60 // about request interception. |
| 61 STATE_BUFFERING, |
| 62 |
| 63 // In this state, the MimeSniffingResourceHandler has identified the mime |
| 64 // type and made a decision on whether the request should be intercepted or |
| 65 // not. It is nows attempting to replay the response to downstream |
| 66 // handlers. |
| 67 STATE_INTERCEPTION_CHECK_DONE, |
| 68 |
| 69 // In this state, the MimeSniffingResourceHandler is replaying the buffered |
| 70 // OnResponseStarted event to the downstream ResourceHandlers. |
| 71 STATE_REPLAYING_RESPONSE_RECEIVED, |
| 72 |
| 73 // In this state, the MimeSniffingResourceHandler is just a blind |
| 74 // pass-through |
| 75 // ResourceHandler. |
| 76 STATE_STREAMING, |
| 77 }; |
| 78 |
| 79 // ResourceHandler implementation: |
| 80 void SetController(ResourceController* controller) override; |
| 81 bool OnWillStart(const GURL&, bool* defer) override; |
| 82 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
| 83 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 84 int* buf_size, |
| 85 int min_size) override; |
| 86 bool OnReadCompleted(int bytes_read, bool* defer) override; |
| 87 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 88 const std::string& security_info, |
| 89 bool* defer) override; |
| 90 |
| 91 // ResourceController implementation: |
| 92 void Resume() override; |
| 93 void Cancel() override; |
| 94 void CancelAndIgnore() override; |
| 95 void CancelWithError(int error_code) override; |
| 96 |
| 97 // -------------------------------------------------------------------------- |
| 98 // The following methods replay the buffered data to the downstream |
| 99 // ResourceHandlers. They return false if the request should be cancelled, |
| 100 // true otherwise. Each of them will set |defer| to true if the request will |
| 101 // proceed to the next stage asynchronously. |
| 102 |
| 103 // Used to advance through the states of the state machine. |
| 104 void AdvanceState(); |
| 105 bool ProcessState(bool* defer); |
| 106 |
| 107 // Intercepts the request as a stream/download if needed. |
| 108 bool MaybeIntercept(bool* defer); |
| 109 |
| 110 // Replays OnResponseStarted on the downstream handlers. |
| 111 bool ReplayResponseReceived(bool* defer); |
| 112 |
| 113 // Replays OnReadCompleted on the downstreams handlers. |
| 114 bool ReplayReadCompleted(bool* defer); |
| 115 |
| 116 // -------------------------------------------------------------------------- |
| 117 |
| 118 // Whether the response body should be sniffed in order to determine the MIME |
| 119 // type of the response. |
| 120 bool ShouldSniffContent(); |
| 121 |
| 122 // Checks whether this request should be intercepted as a stream or a |
| 123 // download. If this is the case, sets up the new ResourceHandler that will be |
| 124 // used for interception. Returns false if teh request should be cancelled, |
| 125 // true otherwise. |defer| is set to true if the interception check needs to |
| 126 // finish asynchronously. |
| 127 bool MaybeStartInterception(bool* defer); |
| 128 |
| 129 // Determines whether a plugin will handle the current request. Returns false |
| 130 // if there is an error and the request should be cancelled and true |
| 131 // otherwise. |defer| is set to true if plugin data is stale and needs to be |
| 132 // refreshed before the request can be handled (in this case the function |
| 133 // still returns true). If the request is directed to a plugin, |
| 134 // |handled_by_plugin| is set to true. |
| 135 bool CheckForPluginHandler(bool* defer, bool* handled_by_plugin); |
| 136 |
| 137 // Whether this request is allowed to be intercepted as a download or a |
| 138 // stream. |
| 139 bool CanBeIntercepted(); |
| 140 |
| 141 // Whether the response we received is not provisional. |
| 142 bool CheckResponseIsNotProvisional(); |
| 143 |
| 144 bool MustDownload(); |
| 145 |
| 146 // Called on the IO thread once the list of plugins has been loaded. |
| 147 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); |
| 148 |
| 149 State state_; |
| 150 |
| 151 ResourceDispatcherHostImpl* host_; |
| 152 #if defined(ENABLE_PLUGINS) |
| 153 PluginService* plugin_service_; |
| 154 #endif |
| 155 |
| 156 bool must_download_; |
| 157 bool must_download_is_set_; |
| 158 |
| 159 // Used to buffer the reponse received until replay. |
| 160 scoped_refptr<ResourceResponse> response_; |
| 161 scoped_refptr<net::IOBuffer> read_buffer_; |
| 162 int read_buffer_size_; |
| 163 int bytes_read_; |
| 164 |
| 165 // The InterceptingResourceHandler that will perform ResourceHandler swap if |
| 166 // needed. |
| 167 InterceptingResourceHandler* intercepting_handler_; |
| 168 |
| 169 base::WeakPtrFactory<MimeSniffingResourceHandler> weak_ptr_factory_; |
| 170 |
| 171 DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler); |
| 172 }; |
| 173 |
| 174 } // namespace content |
| 175 |
| 176 #endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
OLD | NEW |