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