Index: content/browser/loader/mime_sniffing_resource_handler.h |
diff --git a/content/browser/loader/mime_type_resource_handler.h b/content/browser/loader/mime_sniffing_resource_handler.h |
similarity index 52% |
rename from content/browser/loader/mime_type_resource_handler.h |
rename to content/browser/loader/mime_sniffing_resource_handler.h |
index f5581a6a8072522363e1f61e1030129b3ec9d4f2..f0dd8ee1c658204a710630f30d5a81119a94b2ca 100644 |
--- a/content/browser/loader/mime_type_resource_handler.h |
+++ b/content/browser/loader/mime_sniffing_resource_handler.h |
@@ -2,8 +2,8 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#ifndef CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ |
-#define CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ |
+#ifndef CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
+#define CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
#include <string> |
#include <vector> |
@@ -19,15 +19,13 @@ class URLRequest; |
} |
namespace content { |
+class InterceptingResourceHandler; |
class PluginService; |
class ResourceDispatcherHostImpl; |
struct WebPluginInfo; |
// ResourceHandler that, if necessary, buffers a response body without passing |
// it to the next ResourceHandler until it can perform mime sniffing on it. |
-// Once a response's MIME type is known, initiates special handling of the |
-// response if needed (starts downloads, sends data to some plugin types via a |
-// special channel). |
// |
// Uses the buffer provided by the original event handler for buffering, and |
// continues to reuses it until it can determine the MIME type |
@@ -38,22 +36,50 @@ struct WebPluginInfo; |
// Before a request is sent, this ResourceHandler will also set an appropriate |
// Accept header on the request based on its ResourceType, if one isn't already |
// present. |
-class CONTENT_EXPORT MimeTypeResourceHandler |
+class CONTENT_EXPORT MimeSniffingResourceHandler |
: public LayeredResourceHandler, |
public ResourceController { |
public: |
- // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. |
- MimeTypeResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
- ResourceDispatcherHostImpl* host, |
- PluginService* plugin_service, |
- net::URLRequest* request); |
- ~MimeTypeResourceHandler() override; |
+ MimeSniffingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
+ ResourceDispatcherHostImpl* host, |
+ PluginService* plugin_service, |
+ InterceptingResourceHandler* intercepting_handler, |
+ net::URLRequest* request); |
+ ~MimeSniffingResourceHandler() override; |
private: |
+ friend class MimeSniffingResourceHandlerTest; |
+ enum State { |
+ // Starting state of the MimeSniffingResourceHandler. In this state, it is |
+ // acting as a blind pass-through ResourceHandler until the response is |
+ // received. |
+ STATE_STARTING, |
+ |
+ // In this state, the MimeSniffingResourceHandler is buffering the response |
+ // data in read_buffer_, waiting to sniff the mime type and make a choice |
+ // about request interception. |
+ STATE_BUFFERING, |
+ |
+ // In this state, the MimeSniffingResourceHandler has identified the mime |
+ // type and made a decision on whether the request should be intercepted or |
+ // not. It is nows attempting to replay the response to downstream |
+ // handlers. |
+ STATE_INTERCEPTION_CHECK_DONE, |
+ |
+ // In this state, the MimeSniffingResourceHandler is replaying the buffered |
+ // OnResponseStarted event to the downstream ResourceHandlers. |
+ STATE_REPLAYING_RESPONSE_RECEIVED, |
+ |
+ // In this state, the MimeSniffingResourceHandler is just a blind |
+ // pass-through |
+ // ResourceHandler. |
+ STATE_STREAMING, |
+ }; |
+ |
// ResourceHandler implementation: |
void SetController(ResourceController* controller) override; |
- bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
bool OnWillStart(const GURL&, bool* defer) override; |
+ bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
int* buf_size, |
int min_size) override; |
@@ -68,73 +94,67 @@ class CONTENT_EXPORT MimeTypeResourceHandler |
void CancelAndIgnore() override; |
void CancelWithError(int error_code) override; |
- bool ProcessResponse(bool* defer); |
+ // Replays the buffered data to the downstream ResourceHandlers. |
+ void ProceedWithReplay(); |
+ bool ProcessReplay(bool* defer); |
+ bool MaybeCheckForInterception(bool* defer); |
+ bool GetFinalMimeType(bool* defer); |
+ bool ReplayResponseReceived(bool* defer); |
+ bool ReplayResponseReceivedNewHandler(bool* defer); |
+ bool ReplayReadCompleted(bool* defer); |
bool ShouldSniffContent(); |
bool DetermineMimeType(); |
- // Determines whether a plugin will handle the current request, and if so, |
- // sets up the handler to direct the response to that plugin. Returns false |
+ |
+ // Checks whether this request should be intercepted as a stream or a |
+ // download. |
+ bool CheckForInterception(bool* defer); |
+ |
+ // Determines whether a plugin will handle the current reques. Returns false |
// if there is an error and the request should be cancelled and true |
// otherwise. |defer| is set to true if plugin data is stale and needs to be |
// refreshed before the request can be handled (in this case the function |
// still returns true). If the request is directed to a plugin, |
// |handled_by_plugin| is set to true. |
- bool SelectPluginHandler(bool* defer, bool* handled_by_plugin); |
- // Returns false if the request should be cancelled. |
- bool SelectNextHandler(bool* defer); |
- bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler, |
- const std::string& payload_for_old_handler); |
+ bool CheckForPluginHandler(bool* defer, bool* handled_by_plugin); |
- bool ReplayReadCompleted(bool* defer); |
- void CallReplayReadCompleted(); |
+ // Whether this request is allowed to be intercepted as a download or a |
+ // stream. |
+ bool CanBeIntercepted(); |
- bool MustDownload(); |
+ // Whether the response we received is not provisional. |
+ bool CheckResponseIsNotProvisional(); |
- // Copies data from |read_buffer_| to |next_handler_|. |
- bool CopyReadBufferToNextHandler(); |
+ bool MustDownload(); |
// Called on the IO thread once the list of plugins has been loaded. |
void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); |
- enum State { |
- STATE_STARTING, |
- |
- // In this state, we are filling read_buffer_ with data for the purpose |
- // of sniffing the mime type of the response. |
- STATE_BUFFERING, |
- |
- // In this state, we are select an appropriate downstream ResourceHandler |
- // based on the mime type of the response. We are also potentially waiting |
- // for plugins to load so that we can determine if a plugin is available to |
- // handle the mime type. |
- STATE_PROCESSING, |
- |
- // In this state, we are replaying buffered events (OnResponseStarted and |
- // OnReadCompleted) to the downstream ResourceHandler. |
- STATE_REPLAYING, |
- |
- // In this state, we are just a blind pass-through ResourceHandler. |
- STATE_STREAMING |
- }; |
State state_; |
- scoped_refptr<ResourceResponse> response_; |
ResourceDispatcherHostImpl* host_; |
#if defined(ENABLE_PLUGINS) |
PluginService* plugin_service_; |
#endif |
+ |
+ bool must_download_; |
+ bool must_download_is_set_; |
+ |
+ // Used to buffer the reponse received until replay. |
+ scoped_refptr<ResourceResponse> response_; |
scoped_refptr<net::IOBuffer> read_buffer_; |
int read_buffer_size_; |
int bytes_read_; |
- bool must_download_; |
- bool must_download_is_set_; |
+ // The InterceptingResourceHandler that will perform ResourceHandler swap if |
+ // needed. |
+ InterceptingResourceHandler* intercepting_handler_; |
- base::WeakPtrFactory<MimeTypeResourceHandler> weak_ptr_factory_; |
+ base::WeakPtrFactory<MimeSniffingResourceHandler> weak_ptr_factory_; |
- DISALLOW_COPY_AND_ASSIGN(MimeTypeResourceHandler); |
+ DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler); |
}; |
} // namespace content |
-#endif // CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ |
+#endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |