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