Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/loader/buffered_resource_handler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 12 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
| 13 #include "content/public/browser/resource_request_info.h" | |
| 14 #include "content/public/common/resource_response.h" | |
| 15 #include "net/url_request/url_request_context.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 class BufferedResourceHandlerTest : public testing::Test { | |
|
mmenke
2014/11/19 16:44:59
This can now go below the other classes, which it
raymes
2014/11/19 23:34:33
Done.
| |
| 22 public: | |
| 23 BufferedResourceHandlerTest() | |
| 24 : stream_has_handler_(false) {} | |
| 25 | |
| 26 void set_stream_has_handler(bool stream_has_handler) { | |
| 27 stream_has_handler_ = stream_has_handler; | |
| 28 } | |
| 29 | |
| 30 bool TestStreamIsIntercepted(bool allow_download, | |
| 31 bool must_download, | |
| 32 ResourceType request_resource_type); | |
| 33 | |
| 34 private: | |
| 35 // Whether the URL request should be intercepted as a stream. | |
| 36 bool stream_has_handler_; | |
| 37 | |
| 38 base::MessageLoop message_loop_; | |
| 39 }; | |
| 40 | |
| 41 class TestResourceHandler : public ResourceHandler { | |
|
mmenke
2014/11/19 16:44:59
This class and the next one need to go in an anony
raymes
2014/11/19 23:34:34
Done.
| |
| 42 public: | |
| 43 TestResourceHandler() : ResourceHandler(nullptr) {} | |
| 44 | |
| 45 void SetController(ResourceController* controller) override {} | |
| 46 | |
| 47 bool OnUploadProgress(uint64 position, uint64 size) override { | |
| 48 NOTREACHED(); | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 53 ResourceResponse* response, | |
| 54 bool* defer) override { | |
| 55 NOTREACHED(); | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | |
| 60 NOTREACHED(); | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 bool OnWillStart(const GURL& url, bool* defer) override { | |
| 65 NOTREACHED(); | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | |
| 70 NOTREACHED(); | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 75 int* buf_size, | |
| 76 int min_size) override { | |
| 77 NOTREACHED(); | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool OnReadCompleted(int bytes_read, bool* defer) override { | |
| 82 NOTREACHED(); | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 void OnResponseCompleted(const net::URLRequestStatus& status, | |
| 87 const std::string& security_info, | |
| 88 bool* defer) override { | |
| 89 NOTREACHED(); | |
| 90 } | |
| 91 | |
| 92 void OnDataDownloaded(int bytes_downloaded) override { | |
| 93 NOTREACHED(); | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | |
| 98 }; | |
| 99 | |
| 100 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | |
| 101 public: | |
| 102 TestResourceDispatcherHost(bool stream_has_handler) | |
|
mmenke
2014/11/19 16:44:59
explicit
raymes
2014/11/19 23:34:34
Done.
| |
| 103 : stream_has_handler_(stream_has_handler), | |
| 104 intercepted_as_stream_(false) {} | |
| 105 | |
| 106 bool intercepted_as_stream() { return intercepted_as_stream_; } | |
|
mmenke
2014/11/19 16:44:59
nit const
raymes
2014/11/19 23:34:34
Done.
| |
| 107 | |
| 108 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( | |
| 109 net::URLRequest* request, | |
| 110 bool is_content_initiated, | |
| 111 bool must_download, | |
| 112 uint32 id, | |
| 113 scoped_ptr<DownloadSaveInfo> save_info, | |
| 114 const DownloadUrlParameters::OnStartedCallback& started_cb) override { | |
| 115 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | |
| 116 } | |
| 117 | |
| 118 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | |
| 119 net::URLRequest* request, | |
| 120 ResourceResponse* response, | |
| 121 std::string* payload) override { | |
| 122 if (stream_has_handler_) { | |
| 123 intercepted_as_stream_ = true; | |
| 124 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | |
| 125 } else { | |
| 126 return scoped_ptr<ResourceHandler>(); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 // Whether the URL request should be intercepted as a stream. | |
| 132 bool stream_has_handler_; | |
| 133 | |
| 134 // Whether the URL request has been intercepted as a stream. | |
| 135 bool intercepted_as_stream_; | |
| 136 }; | |
| 137 | |
| 138 class TestResourceDispatcherHostDelegate | |
| 139 : public ResourceDispatcherHostDelegate { | |
| 140 public: | |
| 141 TestResourceDispatcherHostDelegate(bool must_download) | |
| 142 : must_download_(must_download) { | |
| 143 } | |
| 144 | |
| 145 bool ShouldForceDownloadResource(const GURL& url, | |
| 146 const std::string& mime_type) override { | |
| 147 return must_download_; | |
| 148 } | |
| 149 | |
| 150 private: | |
| 151 const bool must_download_; | |
| 152 }; | |
| 153 | |
| 154 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | |
| 155 bool allow_download, | |
| 156 bool must_download, | |
| 157 ResourceType request_resource_type) { | |
| 158 net::URLRequestContext context; | |
| 159 scoped_ptr<net::URLRequest> request(context.CreateRequest( | |
| 160 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); | |
| 161 ResourceRequestInfo::AllocateForTesting( | |
| 162 request.get(), request_resource_type, nullptr, 0, 0, 0, false, false, | |
| 163 allow_download, true); | |
| 164 | |
| 165 TestResourceDispatcherHost host(stream_has_handler_); | |
| 166 TestResourceDispatcherHostDelegate host_delegate(must_download); | |
| 167 host.SetDelegate(&host_delegate); | |
| 168 | |
| 169 scoped_ptr<ResourceHandler> buffered_handler( | |
| 170 new BufferedResourceHandler( | |
| 171 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | |
| 172 &host, request.get())); | |
| 173 | |
| 174 scoped_refptr<ResourceResponse> response(new ResourceResponse); | |
| 175 // The MIME type isn't important but it shouldn't be empty. | |
| 176 response->head.mime_type = "application/pdf"; | |
| 177 | |
| 178 bool defer = false; | |
| 179 buffered_handler->OnResponseStarted(response.get(), &defer); | |
| 180 | |
| 181 return host.intercepted_as_stream(); | |
| 182 } | |
| 183 | |
| 184 // Test that stream requests are correctly intercepted under the right | |
| 185 // circumstances. | |
| 186 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | |
| 187 bool allow_download; | |
| 188 bool must_download; | |
| 189 ResourceType resource_type; | |
| 190 | |
| 191 // Ensure the stream is handled by MaybeInterceptAsStream in the | |
| 192 // ResourceDispatcherHost. | |
| 193 set_stream_has_handler(true); | |
| 194 | |
| 195 // Main frame request with no download allowed. Stream shouldn't be | |
| 196 // intercepted. | |
| 197 allow_download = false; | |
| 198 must_download = false; | |
| 199 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
| 200 EXPECT_FALSE( | |
| 201 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 202 | |
| 203 // Main frame request with download allowed. Stream should be intercepted. | |
| 204 allow_download = true; | |
| 205 must_download = false; | |
| 206 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
| 207 EXPECT_TRUE( | |
| 208 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 209 | |
| 210 // Main frame request with download forced. Stream shouldn't be intercepted. | |
| 211 allow_download = true; | |
| 212 must_download = true; | |
| 213 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
| 214 EXPECT_FALSE( | |
| 215 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 216 | |
| 217 // Sub-resource request with download not allowed. Stream shouldn't be | |
| 218 // intercepted. | |
| 219 allow_download = false; | |
| 220 must_download = false; | |
| 221 resource_type = RESOURCE_TYPE_SUB_RESOURCE; | |
| 222 EXPECT_FALSE( | |
| 223 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 224 | |
| 225 // Object request with download not allowed. Stream should be intercepted. | |
| 226 allow_download = false; | |
| 227 must_download = false; | |
| 228 resource_type = RESOURCE_TYPE_OBJECT; | |
| 229 EXPECT_TRUE( | |
| 230 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 231 | |
| 232 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | |
| 233 // in the ResourceDispatcherHost. | |
| 234 set_stream_has_handler(false); | |
| 235 | |
| 236 allow_download = false; | |
| 237 must_download = false; | |
| 238 resource_type = RESOURCE_TYPE_OBJECT; | |
| 239 EXPECT_FALSE( | |
| 240 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 241 | |
| 242 allow_download = true; | |
| 243 must_download = false; | |
| 244 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
| 245 EXPECT_FALSE( | |
| 246 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 247 } | |
| 248 | |
| 249 } // namespace content | |
| OLD | NEW |