Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(873)

Side by Side Diff: content/browser/loader/mime_sniffing_resource_handler.h

Issue 2005273002: Move MimeTypeResourceHandler before ThrottlingResourceHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_TYPE_RESOURCE_HANDLER_H_ 5 #ifndef CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_MIME_TYPE_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" 15 #include "content/public/browser/resource_controller.h"
16 16
17 namespace net { 17 namespace net {
18 class URLRequest; 18 class URLRequest;
19 } 19 }
20 20
21 namespace content { 21 namespace content {
22 class InterceptingResourceHandler;
22 class PluginService; 23 class PluginService;
23 class ResourceDispatcherHostImpl; 24 class ResourceDispatcherHostImpl;
24 struct WebPluginInfo; 25 struct WebPluginInfo;
25 26
26 // ResourceHandler that, if necessary, buffers a response body without passing 27 // ResourceHandler that, if necessary, buffers a response body without passing
27 // it to the next ResourceHandler until it can perform mime sniffing on it. 28 // it to the next ResourceHandler until it can perform mime sniffing on it.
28 // Once a response's MIME type is known, initiates special handling of the
29 // response if needed (starts downloads, sends data to some plugin types via a
30 // special channel).
31 // 29 //
32 // Uses the buffer provided by the original event handler for buffering, and 30 // Uses the buffer provided by the original event handler for buffering, and
33 // continues to reuses it until it can determine the MIME type 31 // continues to reuses it until it can determine the MIME type
34 // subsequent reads until it's done buffering. As a result, the buffer 32 // 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 33 // returned by the next ResourceHandler must have a capacity of at least
36 // net::kMaxBytesToSniff * 2. 34 // net::kMaxBytesToSniff * 2.
37 // 35 //
38 // Before a request is sent, this ResourceHandler will also set an appropriate 36 // 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 37 // Accept header on the request based on its ResourceType, if one isn't already
40 // present. 38 // present.
41 class CONTENT_EXPORT MimeTypeResourceHandler 39 class CONTENT_EXPORT MimeSniffingResourceHandler
42 : public LayeredResourceHandler, 40 : public LayeredResourceHandler,
43 public ResourceController { 41 public ResourceController {
44 public: 42 public:
45 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. 43 MimeSniffingResourceHandler(std::unique_ptr<ResourceHandler> next_handler,
46 MimeTypeResourceHandler(std::unique_ptr<ResourceHandler> next_handler, 44 ResourceDispatcherHostImpl* host,
47 ResourceDispatcherHostImpl* host, 45 PluginService* plugin_service,
48 PluginService* plugin_service, 46 InterceptingResourceHandler* intercepting_handler,
49 net::URLRequest* request); 47 net::URLRequest* request);
50 ~MimeTypeResourceHandler() override; 48 ~MimeSniffingResourceHandler() override;
51 49
52 private: 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
53 // ResourceHandler implementation: 79 // ResourceHandler implementation:
54 void SetController(ResourceController* controller) override; 80 void SetController(ResourceController* controller) override;
81 bool OnWillStart(const GURL&, bool* defer) override;
55 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; 82 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
56 bool OnWillStart(const GURL&, bool* defer) override;
57 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 83 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
58 int* buf_size, 84 int* buf_size,
59 int min_size) override; 85 int min_size) override;
60 bool OnReadCompleted(int bytes_read, bool* defer) override; 86 bool OnReadCompleted(int bytes_read, bool* defer) override;
61 void OnResponseCompleted(const net::URLRequestStatus& status, 87 void OnResponseCompleted(const net::URLRequestStatus& status,
62 const std::string& security_info, 88 const std::string& security_info,
63 bool* defer) override; 89 bool* defer) override;
64 90
65 // ResourceController implementation: 91 // ResourceController implementation:
66 void Resume() override; 92 void Resume() override;
67 void Cancel() override; 93 void Cancel() override;
68 void CancelAndIgnore() override; 94 void CancelAndIgnore() override;
69 void CancelWithError(int error_code) override; 95 void CancelWithError(int error_code) override;
70 96
71 bool ProcessResponse(bool* defer); 97 // Replays the buffered data to the downstream ResourceHandlers.
98 void ProceedWithReplay();
99 bool ProcessReplay(bool* defer);
100 bool MaybeCheckForInterception(bool* defer);
101 bool GetFinalMimeType(bool* defer);
102 bool ReplayResponseReceived(bool* defer);
103 bool ReplayResponseReceivedNewHandler(bool* defer);
104 bool ReplayReadCompleted(bool* defer);
72 105
73 bool ShouldSniffContent(); 106 bool ShouldSniffContent();
74 bool DetermineMimeType(); 107 bool DetermineMimeType();
75 // Determines whether a plugin will handle the current request, and if so, 108
76 // sets up the handler to direct the response to that plugin. Returns false 109 // Checks whether this request should be intercepted as a stream or a
110 // download.
111 bool CheckForInterception(bool* defer);
112
113 // Determines whether a plugin will handle the current reques. Returns false
77 // if there is an error and the request should be cancelled and true 114 // if there is an error and the request should be cancelled and true
78 // otherwise. |defer| is set to true if plugin data is stale and needs to be 115 // otherwise. |defer| is set to true if plugin data is stale and needs to be
79 // refreshed before the request can be handled (in this case the function 116 // refreshed before the request can be handled (in this case the function
80 // still returns true). If the request is directed to a plugin, 117 // still returns true). If the request is directed to a plugin,
81 // |handled_by_plugin| is set to true. 118 // |handled_by_plugin| is set to true.
82 bool SelectPluginHandler(bool* defer, bool* handled_by_plugin); 119 bool CheckForPluginHandler(bool* defer, bool* handled_by_plugin);
83 // Returns false if the request should be cancelled.
84 bool SelectNextHandler(bool* defer);
85 bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler,
86 const std::string& payload_for_old_handler);
87 120
88 bool ReplayReadCompleted(bool* defer); 121 // Whether this request is allowed to be intercepted as a download or a
89 void CallReplayReadCompleted(); 122 // stream.
123 bool CanBeIntercepted();
124
125 // Whether the response we received is not provisional.
126 bool CheckResponseIsNotProvisional();
90 127
91 bool MustDownload(); 128 bool MustDownload();
92 129
93 // Copies data from |read_buffer_| to |next_handler_|.
94 bool CopyReadBufferToNextHandler();
95
96 // Called on the IO thread once the list of plugins has been loaded. 130 // Called on the IO thread once the list of plugins has been loaded.
97 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); 131 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins);
98 132
99 enum State {
100 STATE_STARTING,
101
102 // In this state, we are filling read_buffer_ with data for the purpose
103 // of sniffing the mime type of the response.
104 STATE_BUFFERING,
105
106 // In this state, we are select an appropriate downstream ResourceHandler
107 // based on the mime type of the response. We are also potentially waiting
108 // for plugins to load so that we can determine if a plugin is available to
109 // handle the mime type.
110 STATE_PROCESSING,
111
112 // In this state, we are replaying buffered events (OnResponseStarted and
113 // OnReadCompleted) to the downstream ResourceHandler.
114 STATE_REPLAYING,
115
116 // In this state, we are just a blind pass-through ResourceHandler.
117 STATE_STREAMING
118 };
119 State state_; 133 State state_;
120 134
121 scoped_refptr<ResourceResponse> response_;
122 ResourceDispatcherHostImpl* host_; 135 ResourceDispatcherHostImpl* host_;
123 #if defined(ENABLE_PLUGINS) 136 #if defined(ENABLE_PLUGINS)
124 PluginService* plugin_service_; 137 PluginService* plugin_service_;
125 #endif 138 #endif
139
140 bool must_download_;
141 bool must_download_is_set_;
142
143 // Used to buffer the reponse received until replay.
144 scoped_refptr<ResourceResponse> response_;
126 scoped_refptr<net::IOBuffer> read_buffer_; 145 scoped_refptr<net::IOBuffer> read_buffer_;
127 int read_buffer_size_; 146 int read_buffer_size_;
128 int bytes_read_; 147 int bytes_read_;
129 148
130 bool must_download_; 149 // The InterceptingResourceHandler that will perform ResourceHandler swap if
131 bool must_download_is_set_; 150 // needed.
151 InterceptingResourceHandler* intercepting_handler_;
132 152
133 base::WeakPtrFactory<MimeTypeResourceHandler> weak_ptr_factory_; 153 base::WeakPtrFactory<MimeSniffingResourceHandler> weak_ptr_factory_;
134 154
135 DISALLOW_COPY_AND_ASSIGN(MimeTypeResourceHandler); 155 DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler);
136 }; 156 };
137 157
138 } // namespace content 158 } // namespace content
139 159
140 #endif // CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ 160 #endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698