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