Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "content/browser/loader/buffered_resource_handler.h" | 5 #include "content/browser/loader/buffered_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 11 #include "content/public/browser/resource_controller.h" | 11 #include "content/public/browser/resource_controller.h" |
| 12 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 12 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "content/public/common/resource_response.h" | 14 #include "content/public/common/resource_response.h" |
| 15 #include "content/public/common/webplugininfo.h" | |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 17 #include "content/test/fake_plugin_service.h" | 18 #include "content/test/fake_plugin_service.h" |
| 18 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 net::URLRequest* request, | 93 net::URLRequest* request, |
| 93 bool is_content_initiated, | 94 bool is_content_initiated, |
| 94 bool must_download, | 95 bool must_download, |
| 95 uint32 id, | 96 uint32 id, |
| 96 scoped_ptr<DownloadSaveInfo> save_info, | 97 scoped_ptr<DownloadSaveInfo> save_info, |
| 97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { | 98 const DownloadUrlParameters::OnStartedCallback& started_cb) override { |
| 98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 99 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | 102 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( |
| 103 const base::FilePath& plugin_path, | |
| 102 net::URLRequest* request, | 104 net::URLRequest* request, |
| 103 ResourceResponse* response, | 105 ResourceResponse* response, |
| 104 std::string* payload) override { | 106 std::string* payload) override { |
| 105 if (stream_has_handler_) { | 107 if (stream_has_handler_) { |
| 106 intercepted_as_stream_ = true; | 108 intercepted_as_stream_ = true; |
| 107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 109 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 108 } else { | 110 } else { |
| 109 return scoped_ptr<ResourceHandler>(); | 111 return scoped_ptr<ResourceHandler>(); |
| 110 } | 112 } |
| 111 } | 113 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 146 |
| 145 void CancelWithError(int error_code) override { | 147 void CancelWithError(int error_code) override { |
| 146 NOTREACHED(); | 148 NOTREACHED(); |
| 147 } | 149 } |
| 148 | 150 |
| 149 void Resume() override { | 151 void Resume() override { |
| 150 NOTREACHED(); | 152 NOTREACHED(); |
| 151 } | 153 } |
| 152 }; | 154 }; |
| 153 | 155 |
| 156 class TestFakePluginService : public FakePluginService { | |
| 157 public: | |
| 158 TestFakePluginService(bool fake_plugin_available) | |
|
raymes
2015/03/24 03:01:49
nit: plugin_available is fine
jochen (gone - plz use gerrit)
2015/03/24 09:20:54
explicit
Deepak
2015/03/24 10:33:46
Done.
Deepak
2015/03/24 10:33:46
Done.
| |
| 159 : fake_plugin_available_(fake_plugin_available) {} | |
| 160 bool GetPluginInfo(int render_process_id, | |
| 161 int render_frame_id, | |
| 162 ResourceContext* context, | |
| 163 const GURL& url, | |
| 164 const GURL& page_url, | |
| 165 const std::string& mime_type, | |
| 166 bool allow_wildcard, | |
| 167 bool* is_stale, | |
| 168 WebPluginInfo* info, | |
| 169 std::string* actual_mime_type) override; | |
| 170 | |
| 171 private: | |
| 172 bool fake_plugin_available_; | |
| 173 }; | |
|
jochen (gone - plz use gerrit)
2015/03/24 09:20:54
DISALLOW_COPY_AND_ASSIGN()
Deepak
2015/03/24 10:33:46
Done.
| |
| 174 | |
| 175 bool TestFakePluginService::GetPluginInfo(int render_process_id, | |
| 176 int render_frame_id, | |
| 177 ResourceContext* context, | |
| 178 const GURL& url, | |
| 179 const GURL& page_url, | |
| 180 const std::string& mime_type, | |
| 181 bool allow_wildcard, | |
| 182 bool* is_stale, | |
| 183 WebPluginInfo* info, | |
| 184 std::string* actual_mime_type) { | |
| 185 *is_stale = false; | |
| 186 if (!fake_plugin_available_) | |
| 187 return false; | |
| 188 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN; | |
| 189 info->path = base::FilePath( | |
| 190 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/")); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 154 class BufferedResourceHandlerTest : public testing::Test { | 194 class BufferedResourceHandlerTest : public testing::Test { |
| 155 public: | 195 public: |
| 156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} | 196 BufferedResourceHandlerTest() |
| 197 : stream_has_handler_(false), plugin_available_(false) {} | |
| 157 | 198 |
| 158 void set_stream_has_handler(bool stream_has_handler) { | 199 void set_stream_has_handler(bool stream_has_handler) { |
| 159 stream_has_handler_ = stream_has_handler; | 200 stream_has_handler_ = stream_has_handler; |
| 160 } | 201 } |
| 161 | 202 |
| 203 void set_plugin_available(bool plugin_available) { | |
| 204 plugin_available_ = plugin_available; | |
| 205 } | |
| 206 | |
| 162 bool TestStreamIsIntercepted(bool allow_download, | 207 bool TestStreamIsIntercepted(bool allow_download, |
| 163 bool must_download, | 208 bool must_download, |
| 164 ResourceType request_resource_type); | 209 ResourceType request_resource_type); |
| 165 | 210 |
| 166 private: | 211 private: |
| 167 // Whether the URL request should be intercepted as a stream. | 212 // Whether the URL request should be intercepted as a stream. |
| 168 bool stream_has_handler_; | 213 bool stream_has_handler_; |
| 214 bool plugin_available_; | |
| 169 | 215 |
| 170 TestBrowserThreadBundle thread_bundle_; | 216 TestBrowserThreadBundle thread_bundle_; |
| 171 }; | 217 }; |
| 172 | 218 |
| 173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | 219 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( |
| 174 bool allow_download, | 220 bool allow_download, |
| 175 bool must_download, | 221 bool must_download, |
| 176 ResourceType request_resource_type) { | 222 ResourceType request_resource_type) { |
| 177 net::URLRequestContext context; | 223 net::URLRequestContext context; |
| 178 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 224 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); | 225 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); |
| 180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 226 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 181 ResourceRequestInfo::AllocateForTesting( | 227 ResourceRequestInfo::AllocateForTesting( |
| 182 request.get(), | 228 request.get(), |
| 183 request_resource_type, | 229 request_resource_type, |
| 184 nullptr, // context | 230 nullptr, // context |
| 185 0, // render_process_id | 231 0, // render_process_id |
| 186 0, // render_view_id | 232 0, // render_view_id |
| 187 0, // render_frame_id | 233 0, // render_frame_id |
| 188 is_main_frame, // is_main_frame | 234 is_main_frame, // is_main_frame |
| 189 false, // parent_is_main_frame | 235 false, // parent_is_main_frame |
| 190 allow_download, // allow_download | 236 allow_download, // allow_download |
| 191 true); // is_async | 237 true); // is_async |
| 192 | 238 |
| 193 TestResourceDispatcherHost host(stream_has_handler_); | 239 TestResourceDispatcherHost host(stream_has_handler_); |
| 194 TestResourceDispatcherHostDelegate host_delegate(must_download); | 240 TestResourceDispatcherHostDelegate host_delegate(must_download); |
| 195 host.SetDelegate(&host_delegate); | 241 host.SetDelegate(&host_delegate); |
| 196 | 242 |
| 197 FakePluginService plugin_service; | 243 TestFakePluginService plugin_service(plugin_available_); |
| 198 scoped_ptr<ResourceHandler> buffered_handler( | 244 scoped_ptr<ResourceHandler> buffered_handler( |
| 199 new BufferedResourceHandler( | 245 new BufferedResourceHandler( |
| 200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | 246 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), |
| 201 &host, | 247 &host, |
| 202 &plugin_service, | 248 &plugin_service, |
| 203 request.get())); | 249 request.get())); |
| 204 TestResourceController resource_controller; | 250 TestResourceController resource_controller; |
| 205 buffered_handler->SetController(&resource_controller); | 251 buffered_handler->SetController(&resource_controller); |
| 206 | 252 |
| 207 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 253 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 220 // circumstances. | 266 // circumstances. |
| 221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | 267 TEST_F(BufferedResourceHandlerTest, StreamHandling) { |
| 222 bool allow_download; | 268 bool allow_download; |
| 223 bool must_download; | 269 bool must_download; |
| 224 ResourceType resource_type; | 270 ResourceType resource_type; |
| 225 | 271 |
| 226 // Ensure the stream is handled by MaybeInterceptAsStream in the | 272 // Ensure the stream is handled by MaybeInterceptAsStream in the |
| 227 // ResourceDispatcherHost. | 273 // ResourceDispatcherHost. |
| 228 set_stream_has_handler(true); | 274 set_stream_has_handler(true); |
| 229 | 275 |
| 276 set_plugin_available(true); | |
| 230 // Main frame request with no download allowed. Stream shouldn't be | 277 // Main frame request with no download allowed. Stream shouldn't be |
| 231 // intercepted. | 278 // intercepted. |
| 232 allow_download = false; | 279 allow_download = false; |
| 233 must_download = false; | 280 must_download = false; |
| 234 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 281 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 235 EXPECT_FALSE( | 282 EXPECT_FALSE( |
| 236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 283 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 237 | 284 |
| 238 // Main frame request with download allowed. Stream should be intercepted. | 285 // Main frame request with download allowed. Stream should be intercepted. |
| 239 allow_download = true; | 286 allow_download = true; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 must_download = false; | 319 must_download = false; |
| 273 resource_type = RESOURCE_TYPE_OBJECT; | 320 resource_type = RESOURCE_TYPE_OBJECT; |
| 274 EXPECT_FALSE( | 321 EXPECT_FALSE( |
| 275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 322 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 276 | 323 |
| 277 allow_download = true; | 324 allow_download = true; |
| 278 must_download = false; | 325 must_download = false; |
| 279 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 326 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 280 EXPECT_FALSE( | 327 EXPECT_FALSE( |
| 281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 328 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 329 | |
| 330 // Test the cases where the stream handled by MaybeInterceptAsStream | |
| 331 // with plugin not available. | |
|
raymes
2015/03/24 03:01:49
nit: add to the comment: "This is the case when in
Deepak
2015/03/24 10:33:46
Done.
| |
| 332 set_stream_has_handler(true); | |
| 333 allow_download = false; | |
| 334 must_download = false; | |
| 335 resource_type = RESOURCE_TYPE_OBJECT; | |
| 336 set_plugin_available(false); | |
| 337 EXPECT_TRUE( | |
| 338 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 282 } | 339 } |
| 283 | 340 |
| 284 } // namespace | 341 } // namespace |
| 285 | 342 |
| 286 } // namespace content | 343 } // namespace content |
| OLD | NEW |