Index: content/browser/loader/intercepting_resource_handler.h |
diff --git a/content/browser/loader/mime_type_resource_handler.h b/content/browser/loader/intercepting_resource_handler.h |
similarity index 41% |
copy from content/browser/loader/mime_type_resource_handler.h |
copy to content/browser/loader/intercepting_resource_handler.h |
index f5581a6a8072522363e1f61e1030129b3ec9d4f2..e357e9e3adfdd80c6912f5a96b5142cf7a1b74e3 100644 |
--- a/content/browser/loader/mime_type_resource_handler.h |
+++ b/content/browser/loader/intercepting_resource_handler.h |
@@ -1,9 +1,9 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
// 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_INTERCEPTING_RESOURCE_HANDLER_H_ |
+#define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
#include <string> |
#include <vector> |
@@ -12,7 +12,6 @@ |
#include "base/memory/weak_ptr.h" |
#include "content/browser/loader/layered_resource_handler.h" |
#include "content/common/content_export.h" |
-#include "content/public/browser/resource_controller.h" |
namespace net { |
class URLRequest; |
@@ -23,55 +22,55 @@ 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 |
-// subsequent reads until it's done buffering. As a result, the buffer |
-// returned by the next ResourceHandler must have a capacity of at least |
-// net::kMaxBytesToSniff * 2. |
-// |
-// 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 |
- : public LayeredResourceHandler, |
- public ResourceController { |
+// ResourceHandler that initiates special handling of the response if needed, |
+// based on the response's MIME type (starts downloads, sends data to some |
+// plugin types via a special channel). |
+class CONTENT_EXPORT InterceptingResourceHandler |
+ : public LayeredResourceHandler { |
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; |
+ InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
+ ResourceDispatcherHostImpl* host, |
+ PluginService* plugin_service, |
+ net::URLRequest* request); |
+ ~InterceptingResourceHandler() override; |
private: |
+ enum class State { |
+ // In this state, the InterceptingResourceHandler is waiting for the mime |
+ // type of the response to be identified, to check if the next handler |
+ // should be replaced with an appropriate one. |
+ STARTING, |
+ |
+ // In this state, the InterceptingResourceHandler is waiting for the plugin |
+ // info to be ready in order to possibly intercept the resource has a |
+ // stream. |
+ WAITING_FOR_PLUGINS, |
+ |
+ // In this state, the InterceptingResourceHandler is waiting to copy the |
+ // read buffer to the next handler if it was replaced. This is needed |
+ // because MimeTypeResourceHandler may call OnWillRead before calling |
+ // OnResponseStarted, that is before the InterceptingResourceHandler |
+ // replaces the original ResourceHandler of the request. Therefore, the |
+ // data read at that time should be copied to the new ResourceHandler. |
+ WAITING_FOR_BUFFER_COPY, |
+ |
+ // In this state, the InterceptingResourceHandler has replaced its next |
+ // ResourceHandler if needed, and has ensured the buffered read data was |
+ // properly transmitted to the new ResourceHandler. The |
+ // InterceptingResourceHandler now acts as a pass-through ResourceHandler. |
+ DONE, |
+ }; |
+ |
// ResourceHandler implementation: |
- void SetController(ResourceController* controller) override; |
bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
- bool OnWillStart(const GURL&, bool* defer) override; |
bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
int* buf_size, |
int min_size) override; |
bool OnReadCompleted(int bytes_read, bool* defer) override; |
- void OnResponseCompleted(const net::URLRequestStatus& status, |
- const std::string& security_info, |
- bool* defer) override; |
- |
- // ResourceController implementation: |
- void Resume() override; |
- void Cancel() override; |
- void CancelAndIgnore() override; |
- void CancelWithError(int error_code) override; |
- bool ProcessResponse(bool* defer); |
+ bool MakeHandlerChoice(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 |
// if there is an error and the request should be cancelled and true |
@@ -85,56 +84,35 @@ class CONTENT_EXPORT MimeTypeResourceHandler |
bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler, |
const std::string& payload_for_old_handler); |
- bool ReplayReadCompleted(bool* defer); |
- void CallReplayReadCompleted(); |
- |
bool MustDownload(); |
- // Copies data from |read_buffer_| to |next_handler_|. |
- bool CopyReadBufferToNextHandler(); |
- |
// 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, |
+ bool CopyReadBufferToNextHandler(); |
- // 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 |
- scoped_refptr<net::IOBuffer> read_buffer_; |
- int read_buffer_size_; |
- int bytes_read_; |
+ |
+ bool switched_handler_; |
bool must_download_; |
bool must_download_is_set_; |
- base::WeakPtrFactory<MimeTypeResourceHandler> weak_ptr_factory_; |
+ // Used to copy data to the new next ResourceHandler. |
+ scoped_refptr<ResourceResponse> response_; |
+ scoped_refptr<net::IOBuffer> read_buffer_; |
+ int bytes_read_; |
+ |
+ base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; |
- DISALLOW_COPY_AND_ASSIGN(MimeTypeResourceHandler); |
+ DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); |
}; |
} // namespace content |
-#endif // CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ |
+#endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |