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