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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// Copyright (c) 2012 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 <set>
#include <utility>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_resource_throttle.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/test/test_browser_thread.h"
#include "ipc/ipc_message.h"
#include "net/base/request_priority.h"
#include "net/test/url_request/url_request_mock_http_job.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using content::ResourceType;
namespace prerender {
namespace {
class TestPrerenderContents : public PrerenderContents {
public:
TestPrerenderContents(PrerenderManager* prerender_manager,
int child_id, int route_id)
: PrerenderContents(prerender_manager, static_cast<Profile*>(NULL),
GURL(), content::Referrer(), ORIGIN_NONE),
child_id_(child_id),
route_id_(route_id) {
PrerenderResourceThrottle::OverridePrerenderContentsForTesting(this);
}
~TestPrerenderContents() override {
if (final_status() == FINAL_STATUS_MAX)
SetFinalStatus(FINAL_STATUS_USED);
PrerenderResourceThrottle::OverridePrerenderContentsForTesting(NULL);
}
bool GetChildId(int* child_id) const override {
*child_id = child_id_;
return true;
}
bool GetRouteId(int* route_id) const override {
*route_id = route_id_;
return true;
}
void Start() {
prerendering_has_started_ = true;
NotifyPrerenderStart();
}
void Cancel() {
Destroy(FINAL_STATUS_CANCELLED);
}
void Use() {
PrepareForUse();
}
private:
int child_id_;
int route_id_;
};
class TestPrerenderManager : public PrerenderManager {
public:
TestPrerenderManager() : PrerenderManager(nullptr) {
mutable_config().rate_limit_enabled = false;
}
// We never allocate our PrerenderContents in PrerenderManager, so we don't
// ever want the default pending delete behaviour.
void MoveEntryToPendingDelete(PrerenderContents* entry,
FinalStatus final_status) override {}
};
class DeferredRedirectDelegate : public net::URLRequest::Delegate,
public content::ResourceController {
public:
DeferredRedirectDelegate()
: throttle_(NULL),
was_deferred_(false),
cancel_called_(false),
resume_called_(false) {
}
void SetThrottle(PrerenderResourceThrottle* throttle) {
throttle_ = throttle;
throttle_->set_controller_for_testing(this);
}
void Run() {
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
}
bool was_deferred() const { return was_deferred_; }
bool cancel_called() const { return cancel_called_; }
bool resume_called() const { return resume_called_; }
// net::URLRequest::Delegate implementation:
void OnReceivedRedirect(net::URLRequest* request,
const net::RedirectInfo& redirect_info,
bool* defer_redirect) override {
// Defer the redirect either way.
*defer_redirect = true;
// Find out what the throttle would have done.
throttle_->WillRedirectRequest(redirect_info, &was_deferred_);
run_loop_->Quit();
}
void OnResponseStarted(net::URLRequest* request) override {}
void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
// content::ResourceController implementation:
void Cancel() override {
EXPECT_FALSE(cancel_called_);
EXPECT_FALSE(resume_called_);
cancel_called_ = true;
run_loop_->Quit();
}
void CancelAndIgnore() override { Cancel(); }
void CancelWithError(int error_code) override { Cancel(); }
void Resume() override {
EXPECT_TRUE(was_deferred_);
EXPECT_FALSE(cancel_called_);
EXPECT_FALSE(resume_called_);
resume_called_ = true;
run_loop_->Quit();
}
private:
scoped_ptr<base::RunLoop> run_loop_;
PrerenderResourceThrottle* throttle_;
bool was_deferred_;
bool cancel_called_;
bool resume_called_;
DISALLOW_COPY_AND_ASSIGN(DeferredRedirectDelegate);
};
} // namespace
class PrerenderResourceThrottleTest : public testing::Test {
public:
static const int kDefaultChildId = 0;
static const int kDefaultRouteId = 100;
PrerenderResourceThrottleTest() :
ui_thread_(BrowserThread::UI, &message_loop_),
io_thread_(BrowserThread::IO, &message_loop_),
test_contents_(&prerender_manager_, kDefaultChildId, kDefaultRouteId) {
chrome_browser_net::SetUrlRequestMocksEnabled(true);
}
~PrerenderResourceThrottleTest() override {
chrome_browser_net::SetUrlRequestMocksEnabled(false);
// Cleanup work so the file IO tasks from URLRequestMockHTTPJob
// are gone.
content::BrowserThread::GetBlockingPool()->FlushForTesting();
RunEvents();
}
TestPrerenderManager* prerender_manager() {
return &prerender_manager_;
}
TestPrerenderContents* test_contents() {
return &test_contents_;
}
// Runs any tasks queued on either thread.
void RunEvents() {
message_loop_.RunUntilIdle();
}
private:
base::MessageLoopForIO message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread io_thread_;
TestPrerenderManager prerender_manager_;
TestPrerenderContents test_contents_;
};
// Checks that deferred redirects are throttled and resumed correctly.
TEST_F(PrerenderResourceThrottleTest, RedirectResume) {
const base::FilePath::CharType kRedirectPath[] =
FILE_PATH_LITERAL("prerender/image-deferred.png");
test_contents()->Start();
RunEvents();
// Fake a request.
net::TestURLRequestContext url_request_context;
DeferredRedirectDelegate delegate;
scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
net::DEFAULT_PRIORITY,
&delegate));
content::ResourceRequestInfo::AllocateForTesting(
request.get(),
content::RESOURCE_TYPE_IMAGE,
NULL,
kDefaultChildId,
kDefaultRouteId,
MSG_ROUTING_NONE,
false, // is_main_frame
false, // parent_is_main_frame
true, // allow_download
true); // is_async
// Install a prerender throttle.
PrerenderResourceThrottle throttle(request.get());
delegate.SetThrottle(&throttle);
// Start the request and wait for a redirect.
request->Start();
delegate.Run();
EXPECT_TRUE(delegate.was_deferred());
// This calls WillRedirectRequestOnUI().
RunEvents();
// Display the prerendered RenderView and wait for the throttle to
// notice.
test_contents()->Use();
delegate.Run();
EXPECT_TRUE(delegate.resume_called());
EXPECT_FALSE(delegate.cancel_called());
}
// Checks that redirects in main frame loads are not deferred.
TEST_F(PrerenderResourceThrottleTest, RedirectMainFrame) {
const base::FilePath::CharType kRedirectPath[] =
FILE_PATH_LITERAL("prerender/image-deferred.png");
test_contents()->Start();
RunEvents();
// Fake a request.
net::TestURLRequestContext url_request_context;
DeferredRedirectDelegate delegate;
scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
net::DEFAULT_PRIORITY,
&delegate));
content::ResourceRequestInfo::AllocateForTesting(
request.get(),
content::RESOURCE_TYPE_MAIN_FRAME,
NULL,
kDefaultChildId,
kDefaultRouteId,
MSG_ROUTING_NONE,
true, // is_main_frame
false, // parent_is_main_frame
true, // allow_download
true); // is_async
// Install a prerender throttle.
PrerenderResourceThrottle throttle(request.get());
delegate.SetThrottle(&throttle);
// Start the request and wait for a redirect. This time, it should
// not be deferred.
request->Start();
delegate.Run();
// This calls WillRedirectRequestOnUI().
RunEvents();
// Cleanup work so the prerender is gone.
test_contents()->Cancel();
RunEvents();
}
// Checks that attempting to defer a synchronous request aborts the
// prerender.
TEST_F(PrerenderResourceThrottleTest, RedirectSyncXHR) {
const base::FilePath::CharType kRedirectPath[] =
FILE_PATH_LITERAL("prerender/image-deferred.png");
test_contents()->Start();
RunEvents();
// Fake a request.
net::TestURLRequestContext url_request_context;
DeferredRedirectDelegate delegate;
scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
net::DEFAULT_PRIORITY,
&delegate));
content::ResourceRequestInfo::AllocateForTesting(
request.get(),
content::RESOURCE_TYPE_XHR,
NULL,
kDefaultChildId,
kDefaultRouteId,
MSG_ROUTING_NONE,
false, // is_main_frame
false, // parent_is_main_frame
true, // allow_download
false); // is_async
// Install a prerender throttle.
PrerenderResourceThrottle throttle(request.get());
delegate.SetThrottle(&throttle);
// Start the request and wait for a redirect.
request->Start();
delegate.Run();
// This calls WillRedirectRequestOnUI().
RunEvents();
// We should have cancelled the prerender.
EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT,
test_contents()->final_status());
// Cleanup work so the prerender is gone.
test_contents()->Cancel();
RunEvents();
}
} // namespace prerender
|