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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
// Copyright (c) 2011 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 "chrome/browser/prerender/prerender_tracker.h"
#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "content/browser/browser_thread.h"
#include "content/browser/resource_context.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/common/resource_messages.h"
#include "net/base/load_flags.h"
namespace prerender {
namespace {
void CancelDeferredRequestOnIOThread(
ResourceDispatcherHost* resource_dispatcher_host,
int child_id, int request_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
resource_dispatcher_host->CancelRequest(child_id, request_id, false);
}
void StartDeferredRequestOnIOThread(
ResourceDispatcherHost* resource_dispatcher_host,
int child_id, int request_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
resource_dispatcher_host->StartDeferredRequest(child_id, request_id);
}
bool ShouldCancelRequest(
const base::Callback<PrerenderManager*(void)>& prerender_manager_getter,
int child_id,
int route_id) {
// Check if the RenderViewHost associated with (child_id, route_id) no
// longer exists, or has already been swapped out for a prerendered page.
// If that happens, then we do not want to issue the request which originated
// from it. Otherwise, keep it going.
// The RenderViewHost may exist for a couple of different reasons: such as
// being an XHR from the originating page, being included as an iframe,
// being requested as a favicon or stylesheet, and many other corner cases.
RenderViewHost* render_view_host =
RenderViewHost::FromID(child_id, route_id);
if (!render_view_host)
return true;
PrerenderManager* prerender_manager = prerender_manager_getter.Run();
return (prerender_manager &&
prerender_manager->IsOldRenderViewHost(render_view_host));
}
void HandleDelayedRequestOnUIThread(
const base::Callback<PrerenderManager*(void)>& prerender_manager_getter,
int child_id,
int route_id,
int request_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ResourceDispatcherHost* resource_dispatcher_host =
g_browser_process->resource_dispatcher_host();
CHECK(resource_dispatcher_host);
if (ShouldCancelRequest(prerender_manager_getter, child_id, route_id)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&CancelDeferredRequestOnIOThread,
resource_dispatcher_host, child_id, request_id));
} else {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&StartDeferredRequestOnIOThread,
resource_dispatcher_host, child_id, request_id));
}
}
void DestroyPrerenderForRenderViewOnUI(
const base::WeakPtr<PrerenderManager>& prerender_manager_weak_ptr,
int render_process_id,
int render_view_id,
FinalStatus final_status) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
PrerenderManager* prerender_manager = prerender_manager_weak_ptr.get();
if (!prerender_manager)
return;
prerender_manager->DestroyPrerenderForRenderView(
render_process_id, render_view_id, final_status);
}
void AddURL(const GURL& url, URLCounter* counter) {
DCHECK(counter);
counter->AddURL(url);
}
void RemoveURLs(const std::vector<GURL>& urls, URLCounter* counter) {
DCHECK(counter);
counter->RemoveURLs(urls);
}
} // namespace
URLCounter::URLCounter() {
// URLCounter is currently constructed on the UI thread but
// accessed on the IO thread.
DetachFromThread();
}
URLCounter::~URLCounter() {
// URLCounter is currently destructed on the UI thread but
// accessed on the IO thread.
DetachFromThread();
}
bool URLCounter::MatchesURL(const GURL& url) const {
DCHECK(CalledOnValidThread());
URLCountMap::const_iterator it = url_count_map_.find(url);
if (it == url_count_map_.end())
return false;
DCHECK(it->second > 0);
return true;
}
void URLCounter::AddURL(const GURL& url) {
DCHECK(CalledOnValidThread());
URLCountMap::iterator it = url_count_map_.find(url);
if (it == url_count_map_.end())
url_count_map_[url] = 1;
else
it->second++;
}
void URLCounter::RemoveURLs(const std::vector<GURL>& urls) {
DCHECK(CalledOnValidThread());
for (std::vector<GURL>::const_iterator it = urls.begin();
it != urls.end();
++it) {
URLCountMap::iterator map_entry = url_count_map_.find(*it);
DCHECK(url_count_map_.end() != map_entry);
DCHECK(map_entry->second > 0);
--map_entry->second;
if (map_entry->second == 0)
url_count_map_.erase(map_entry);
}
}
struct RenderViewInfo {
explicit RenderViewInfo(PrerenderManager* prerender_manager)
: final_status(FINAL_STATUS_MAX),
prerender_manager(prerender_manager->AsWeakPtr()) {
}
~RenderViewInfo() {}
FinalStatus final_status;
base::WeakPtr<PrerenderManager> prerender_manager;
};
PrerenderTracker::PrerenderTracker() {
}
PrerenderTracker::~PrerenderTracker() {
}
bool PrerenderTracker::TryUse(int child_id, int route_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return SetFinalStatus(child_id, route_id, FINAL_STATUS_USED, NULL);
}
bool PrerenderTracker::TryCancel(
int child_id,
int route_id,
FinalStatus final_status) {
DCHECK_NE(FINAL_STATUS_USED, final_status);
DCHECK(final_status >= 0 && final_status < FINAL_STATUS_MAX);
FinalStatus actual_final_status;
SetFinalStatus(child_id, route_id, final_status, &actual_final_status);
return actual_final_status != FINAL_STATUS_USED &&
actual_final_status != FINAL_STATUS_MAX;
}
bool PrerenderTracker::TryCancelOnIOThread(
int child_id,
int route_id,
FinalStatus final_status) {
DCHECK_NE(FINAL_STATUS_USED, final_status);
DCHECK(final_status >= 0 && final_status < FINAL_STATUS_MAX);
if (!IsPrerenderingOnIOThread(child_id, route_id))
return false;
return TryCancel(child_id, route_id, final_status);
}
bool PrerenderTracker::PotentiallyDelayRequestOnIOThread(
const GURL& gurl,
const base::Callback<PrerenderManager*(void)>& prerender_manager_getter,
int process_id,
int route_id,
int request_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!url_counter_.MatchesURL(gurl))
return false;
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
NewRunnableFunction(&HandleDelayedRequestOnUIThread,
prerender_manager_getter,
process_id,
route_id,
request_id));
return true;
}
void PrerenderTracker::AddPrerenderURLOnUIThread(const GURL& url) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableFunction(&AddURL, url, &url_counter_));
}
void PrerenderTracker::RemovePrerenderURLsOnUIThread(
const std::vector<GURL>& urls) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableFunction(&RemoveURLs, urls, &url_counter_));
}
bool PrerenderTracker::GetFinalStatus(int child_id, int route_id,
FinalStatus* final_status) const {
ChildRouteIdPair child_route_id_pair(child_id, route_id);
base::AutoLock lock(final_status_map_lock_);
FinalStatusMap::const_iterator final_status_it =
final_status_map_.find(child_route_id_pair);
if (final_status_map_.end() == final_status_map_.find(child_route_id_pair))
return false;
*final_status = final_status_it->second.final_status;
return true;
}
void PrerenderTracker::OnPrerenderingStarted(
int child_id, int route_id, PrerenderManager* prerender_manager) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_GE(child_id, 0);
DCHECK_GE(route_id, 0);
ChildRouteIdPair child_route_id_pair(child_id, route_id);
// The RenderView should not already be prerendering.
DCHECK(final_status_map_.end() ==
final_status_map_.find(child_route_id_pair));
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&AddPrerenderOnIOThreadTask, child_route_id_pair));
base::AutoLock lock(final_status_map_lock_);
final_status_map_.insert(
std::make_pair(child_route_id_pair, RenderViewInfo(prerender_manager)));
}
void PrerenderTracker::OnPrerenderingFinished(int child_id, int route_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_GE(child_id, 0);
DCHECK_GE(route_id, 0);
ChildRouteIdPair child_route_id_pair(child_id, route_id);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&RemovePrerenderOnIOThreadTask, child_route_id_pair));
base::AutoLock lock(final_status_map_lock_);
size_t num_erased = final_status_map_.erase(child_route_id_pair);
DCHECK_EQ(1u, num_erased);
}
bool PrerenderTracker::SetFinalStatus(int child_id, int route_id,
FinalStatus desired_final_status,
FinalStatus* actual_final_status) {
DCHECK(desired_final_status >= FINAL_STATUS_USED &&
desired_final_status < FINAL_STATUS_MAX);
ChildRouteIdPair child_route_id_pair(child_id, route_id);
base::AutoLock lock(final_status_map_lock_);
FinalStatusMap::iterator final_status_it =
final_status_map_.find(child_route_id_pair);
if (final_status_it == final_status_map_.end()) {
// The RenderView has already been either used or destroyed.
if (actual_final_status)
*actual_final_status = FINAL_STATUS_MAX;
return false;
}
if (final_status_it->second.final_status == FINAL_STATUS_MAX) {
final_status_it->second.final_status = desired_final_status;
if (desired_final_status != FINAL_STATUS_USED) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableFunction(&DestroyPrerenderForRenderViewOnUI,
final_status_it->second.prerender_manager,
child_id,
route_id,
desired_final_status));
}
if (actual_final_status)
*actual_final_status = desired_final_status;
return true;
}
if (actual_final_status)
*actual_final_status = final_status_it->second.final_status;
return false;
}
bool PrerenderTracker::IsPrerenderingOnIOThread(int child_id,
int route_id) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ChildRouteIdPair child_route_id_pair(child_id, route_id);
return possibly_prerendering_io_thread_set_.end() !=
possibly_prerendering_io_thread_set_.find(child_route_id_pair);
}
void PrerenderTracker::AddPrerenderOnIOThread(
const ChildRouteIdPair& child_route_id_pair) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(!IsPrerenderingOnIOThread(child_route_id_pair.first,
child_route_id_pair.second));
possibly_prerendering_io_thread_set_.insert(child_route_id_pair);
}
void PrerenderTracker::RemovePrerenderOnIOThread(
const ChildRouteIdPair& child_route_id_pair) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(IsPrerenderingOnIOThread(child_route_id_pair.first,
child_route_id_pair.second));
possibly_prerendering_io_thread_set_.erase(child_route_id_pair);
}
// static
PrerenderTracker* PrerenderTracker::GetDefault() {
return g_browser_process->prerender_tracker();
}
// static
void PrerenderTracker::AddPrerenderOnIOThreadTask(
const ChildRouteIdPair& child_route_id_pair) {
GetDefault()->AddPrerenderOnIOThread(child_route_id_pair);
}
// static
void PrerenderTracker::RemovePrerenderOnIOThreadTask(
const ChildRouteIdPair& child_route_id_pair) {
GetDefault()->RemovePrerenderOnIOThread(child_route_id_pair);
}
} // namespace prerender
|