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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
// 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_contents.h"
#include <algorithm>
#include <utility>
#include "base/process_util.h"
#include "base/task.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/background_contents_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prerender/prerender_final_status.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_render_widget_host_view.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_preferences_util.h"
#include "chrome/browser/ui/login/login_prompt.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/icon_messages.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/view_types.h"
#include "content/browser/browsing_instance.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/renderer_host/resource_request_details.h"
#include "content/browser/site_instance.h"
#include "content/browser/tab_contents/tab_contents_view.h"
#include "content/common/notification_service.h"
#include "content/common/view_messages.h"
#include "ui/gfx/rect.h"
#if defined(OS_MACOSX)
#include "chrome/browser/mach_broker_mac.h"
#endif
namespace prerender {
namespace {
// Compares URLs ignoring any ref for the purposes of matching URLs when
// prerendering.
struct PrerenderUrlPredicate {
explicit PrerenderUrlPredicate(const GURL& url)
: url_(url) {
}
bool operator()(const GURL& url) const {
return url.scheme() == url_.scheme() &&
url.host() == url_.host() &&
url.port() == url_.port() &&
url.path() == url_.path() &&
url.query() == url_.query();
}
GURL url_;
};
void AddChildRoutePair(ResourceDispatcherHost* resource_dispatcher_host,
int child_id, int route_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
resource_dispatcher_host->AddPrerenderChildRoutePair(child_id, route_id);
}
void RemoveChildRoutePair(ResourceDispatcherHost* resource_dispatcher_host,
int child_id, int route_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
resource_dispatcher_host->RemovePrerenderChildRoutePair(child_id, route_id);
}
} // end namespace
class PrerenderContentsFactoryImpl : public PrerenderContents::Factory {
public:
virtual PrerenderContents* CreatePrerenderContents(
PrerenderManager* prerender_manager, Profile* profile, const GURL& url,
const GURL& referrer) OVERRIDE {
return new PrerenderContents(prerender_manager, profile, url, referrer);
}
};
PrerenderContents::PrerenderContents(PrerenderManager* prerender_manager,
Profile* profile,
const GURL& url,
const GURL& referrer)
: prerender_manager_(prerender_manager),
render_view_host_(NULL),
prerender_url_(url),
referrer_(referrer),
profile_(profile),
page_id_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(tab_contents_observer_registrar_(this)),
has_stopped_loading_(false),
final_status_(FINAL_STATUS_MAX),
prerendering_has_started_(false) {
DCHECK(prerender_manager != NULL);
}
bool PrerenderContents::Init() {
if (!AddAliasURL(prerender_url_))
return false;
return true;
}
// static
PrerenderContents::Factory* PrerenderContents::CreateFactory() {
return new PrerenderContentsFactoryImpl();
}
void PrerenderContents::StartPrerenderingOld(
const RenderViewHost* source_render_view_host) {
DCHECK(profile_ != NULL);
DCHECK(!prerendering_has_started_);
DCHECK(source_render_view_host != NULL);
DCHECK(source_render_view_host->view() != NULL);
prerendering_has_started_ = true;
SiteInstance* site_instance = SiteInstance::CreateSiteInstance(profile_);
render_view_host_ = new RenderViewHost(site_instance, this, MSG_ROUTING_NONE,
NULL);
// Register as an observer of the RenderViewHost so we get messages.
render_view_host_observer_.reset(
new PrerenderRenderViewHostObserver(this, render_view_host_mutable()));
// Create the RenderView, so it can receive messages.
render_view_host_->CreateRenderView(string16());
OnRenderViewHostCreated(render_view_host_);
// Give the RVH a PrerenderRenderWidgetHostView, both so its size can be set
// and so that the prerender can be cancelled under certain circumstances.
PrerenderRenderWidgetHostView* view =
new PrerenderRenderWidgetHostView(render_view_host_, this);
view->Init(source_render_view_host->view());
// Register this with the ResourceDispatcherHost as a prerender
// RenderViewHost. This must be done before the Navigate message to catch all
// resource requests, but as it is on the same thread as the Navigate message
// (IO) there is no race condition.
int process_id = render_view_host_->process()->id();
int view_id = render_view_host_->routing_id();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&AddChildRoutePair,
g_browser_process->resource_dispatcher_host(),
process_id, view_id));
// Close ourselves when the application is shutting down.
notification_registrar_.Add(this, NotificationType::APP_TERMINATING,
NotificationService::AllSources());
// Register for our parent profile to shutdown, so we can shut ourselves down
// as well (should only be called for OTR profiles, as we should receive
// APP_TERMINATING before non-OTR profiles are destroyed).
// TODO(tburkard): figure out if this is needed.
notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
Source<Profile>(profile_));
// Register to cancel if Authentication is required.
notification_registrar_.Add(this, NotificationType::AUTH_NEEDED,
NotificationService::AllSources());
notification_registrar_.Add(this, NotificationType::AUTH_CANCELLED,
NotificationService::AllSources());
// Register all responses to see if we should cancel.
notification_registrar_.Add(this, NotificationType::DOWNLOAD_INITIATED,
NotificationService::AllSources());
// Register for redirect notifications sourced from |this|.
notification_registrar_.Add(
this, NotificationType::RESOURCE_RECEIVED_REDIRECT,
Source<RenderViewHostDelegate>(GetRenderViewHostDelegate()));
DCHECK(load_start_time_.is_null());
load_start_time_ = base::TimeTicks::Now();
render_view_host_->Send(
new ViewMsg_SetIsPrerendering(render_view_host_->routing_id(), true));
ViewMsg_Navigate_Params params;
params.page_id = -1;
params.pending_history_list_offset = -1;
params.current_history_list_offset = -1;
params.current_history_list_length = 0;
params.url = prerender_url_;
params.transition = PageTransition::LINK;
params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params.referrer = referrer_;
params.request_time = base::Time::Now();
render_view_host_->Navigate(params);
}
void PrerenderContents::StartPrerendering(
const RenderViewHost* source_render_view_host) {
if (!UseTabContents()) {
VLOG(1) << "Starting prerendering with LEGACY code";
StartPrerenderingOld(source_render_view_host);
return;
}
VLOG(1) << "Starting prerendering with NEW code";
DCHECK(profile_ != NULL);
DCHECK(!prerendering_has_started_);
DCHECK(prerender_contents_.get() == NULL);
DCHECK(source_render_view_host != NULL);
DCHECK(source_render_view_host->view() != NULL);
prerendering_has_started_ = true;
TabContents* new_contents = new TabContents(profile_, NULL, MSG_ROUTING_NONE,
NULL, NULL);
prerender_contents_.reset(new TabContentsWrapper(new_contents));
tab_contents_observer_registrar_.Observe(new_contents);
TabContents* source_tc =
source_render_view_host->delegate()->GetAsTabContents();
if (source_tc) {
// So that history merging will work, get the max page ID
// of the old page, and add a safety margin of 10 to it (for things
// such as redirects).
int32 max_page_id = source_tc->GetMaxPageID();
if (max_page_id != -1) {
prerender_contents_->controller().set_max_restored_page_id(
max_page_id + 10);
}
// Set the size of the new TC to that of the old TC.
gfx::Rect tab_bounds;
source_tc->view()->GetContainerBounds(&tab_bounds);
prerender_contents_->view()->SizeContents(tab_bounds.size());
}
// Register as an observer of the RenderViewHost so we get messages.
render_view_host_observer_.reset(
new PrerenderRenderViewHostObserver(this, render_view_host_mutable()));
int process_id;
int view_id;
CHECK(GetChildId(&process_id));
CHECK(GetRouteId(&view_id));
// Register this with the ResourceDispatcherHost as a prerender
// RenderViewHost. This must be done before the Navigate message to catch all
// resource requests, but as it is on the same thread as the Navigate message
// (IO) there is no race condition.
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&AddChildRoutePair,
g_browser_process->resource_dispatcher_host(),
process_id, view_id));
// Close ourselves when the application is shutting down.
notification_registrar_.Add(this, NotificationType::APP_TERMINATING,
NotificationService::AllSources());
// Register for our parent profile to shutdown, so we can shut ourselves down
// as well (should only be called for OTR profiles, as we should receive
// APP_TERMINATING before non-OTR profiles are destroyed).
// TODO(tburkard): figure out if this is needed.
notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
Source<Profile>(profile_));
// Register to cancel if Authentication is required.
notification_registrar_.Add(this, NotificationType::AUTH_NEEDED,
NotificationService::AllSources());
notification_registrar_.Add(this, NotificationType::AUTH_CANCELLED,
NotificationService::AllSources());
// Register all responses to see if we should cancel.
notification_registrar_.Add(this, NotificationType::DOWNLOAD_INITIATED,
NotificationService::AllSources());
// Register to inform new RenderViews that we're prerendering.
notification_registrar_.Add(
this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
Source<TabContents>(new_contents));
// Register for redirect notifications sourced from |this|.
notification_registrar_.Add(
this, NotificationType::RESOURCE_RECEIVED_REDIRECT,
Source<RenderViewHostDelegate>(GetRenderViewHostDelegate()));
// Register for new windows from any source.
notification_registrar_.Add(this,
NotificationType::CREATING_NEW_WINDOW_CANCELLED,
Source<TabContents>(new_contents));
DCHECK(load_start_time_.is_null());
load_start_time_ = base::TimeTicks::Now();
new_contents->controller().LoadURL(prerender_url_,
referrer_, PageTransition::LINK);
}
bool PrerenderContents::GetChildId(int* child_id) const {
CHECK(child_id);
const RenderViewHost* prerender_render_view_host = render_view_host();
if (prerender_render_view_host) {
*child_id = prerender_render_view_host->process()->id();
return true;
}
return false;
}
bool PrerenderContents::GetRouteId(int* route_id) const {
CHECK(route_id);
const RenderViewHost* prerender_render_view_host = render_view_host();
if (prerender_render_view_host) {
*route_id = prerender_render_view_host->routing_id();
return true;
}
return false;
}
void PrerenderContents::set_final_status(FinalStatus final_status) {
DCHECK(final_status >= FINAL_STATUS_USED && final_status < FINAL_STATUS_MAX);
DCHECK(final_status_ == FINAL_STATUS_MAX ||
final_status_ == FINAL_STATUS_CONTROL_GROUP);
// Don't override final_status_ if it's FINAL_STATUS_CONTROL_GROUP,
// otherwise data will be collected in the Prerender.FinalStatus histogram.
if (final_status_ == FINAL_STATUS_CONTROL_GROUP)
return;
final_status_ = final_status;
}
FinalStatus PrerenderContents::final_status() const {
return final_status_;
}
PrerenderContents::~PrerenderContents() {
DCHECK(final_status_ != FINAL_STATUS_MAX);
// If we haven't even started prerendering, we were just in the control
// group, which means we do not want to record the status.
if (prerendering_has_started())
RecordFinalStatus(final_status_);
if (render_view_host_ || prerender_contents_.get()) {
RenderViewHost* prerender_render_view_host = render_view_host_mutable();
int process_id = prerender_render_view_host->process()->id();
int view_id = prerender_render_view_host->routing_id();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&RemoveChildRoutePair,
g_browser_process->resource_dispatcher_host(),
process_id, view_id));
// Only delete the RenderViewHost if we own it.
if (render_view_host_)
render_view_host_->Shutdown();
}
}
RenderViewHostDelegate::View* PrerenderContents::GetViewDelegate() {
return this;
}
const GURL& PrerenderContents::GetURL() const {
return url_;
}
ViewType::Type PrerenderContents::GetRenderViewType() const {
return ViewType::BACKGROUND_CONTENTS;
}
int PrerenderContents::GetBrowserWindowID() const {
return extension_misc::kUnknownWindowId;
}
void PrerenderContents::DidNavigate(
RenderViewHost* render_view_host,
const ViewHostMsg_FrameNavigate_Params& params) {
DCHECK_EQ(render_view_host_, render_view_host);
// We only care when the outer frame changes.
if (!PageTransition::IsMainFrame(params.transition))
return;
// Store the navigation params.
navigate_params_.reset(new ViewHostMsg_FrameNavigate_Params());
*navigate_params_ = params;
if (!AddAliasURL(params.url))
return;
url_ = params.url;
}
void PrerenderContents::UpdateTitle(RenderViewHost* render_view_host,
int32 page_id,
const std::wstring& title) {
DCHECK_EQ(render_view_host_, render_view_host);
if (title.empty())
return;
title_ = WideToUTF16Hack(title);
page_id_ = page_id;
}
bool PrerenderContents::PreHandleKeyboardEvent(
const NativeWebKeyboardEvent& event,
bool* is_keyboard_shortcut) {
return false;
}
void PrerenderContents::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::PROFILE_DESTROYED:
Destroy(FINAL_STATUS_PROFILE_DESTROYED);
return;
case NotificationType::APP_TERMINATING:
Destroy(FINAL_STATUS_APP_TERMINATING);
return;
case NotificationType::AUTH_NEEDED:
case NotificationType::AUTH_CANCELLED: {
// Only respond to HTTP authentication notifications which
// are required for this prerendered page.
LoginNotificationDetails* details_ptr =
Details<LoginNotificationDetails>(details).ptr();
LoginHandler* handler = details_ptr->handler();
DCHECK(handler != NULL);
RenderViewHostDelegate* delegate = handler->GetRenderViewHostDelegate();
if (delegate == GetRenderViewHostDelegate()) {
Destroy(FINAL_STATUS_AUTH_NEEDED);
return;
}
break;
}
case NotificationType::DOWNLOAD_INITIATED: {
// If the download is started from a RenderViewHost that we are
// delegating, kill the prerender. This cancels any pending requests
// though the download never actually started thanks to the
// DownloadRequestLimiter.
DCHECK(NotificationService::NoDetails() == details);
RenderViewHost* render_view_host = Source<RenderViewHost>(source).ptr();
CHECK(render_view_host != NULL);
if (render_view_host->delegate() == GetRenderViewHostDelegate()) {
Destroy(FINAL_STATUS_DOWNLOAD);
return;
}
break;
}
case NotificationType::RESOURCE_RECEIVED_REDIRECT: {
// RESOURCE_RECEIVED_REDIRECT can come for any resource on a page.
// If it's a redirect on the top-level resource, the name needs
// to be remembered for future matching, and if it redirects to
// an https resource, it needs to be canceled. If a subresource
// is redirected, nothing changes.
DCHECK(Source<RenderViewHostDelegate>(source).ptr() ==
GetRenderViewHostDelegate());
ResourceRedirectDetails* resource_redirect_details =
Details<ResourceRedirectDetails>(details).ptr();
CHECK(resource_redirect_details);
if (resource_redirect_details->resource_type() ==
ResourceType::MAIN_FRAME) {
if (!AddAliasURL(resource_redirect_details->new_url()))
return;
}
break;
}
case NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB: {
if (prerender_contents_.get()) {
DCHECK_EQ(Source<TabContents>(source).ptr(),
prerender_contents_->tab_contents());
Details<RenderViewHost> new_render_view_host(details);
OnRenderViewHostCreated(new_render_view_host.ptr());
// When a new RenderView is created for a prerendering TabContents,
// tell the new RenderView it's being used for prerendering before any
// navigations occur. Note that this is always triggered before the
// first navigation, so there's no need to send the message just after
// the TabContents is created.
new_render_view_host->Send(
new ViewMsg_SetIsPrerendering(new_render_view_host->routing_id(),
true));
// Set the new TabContents and its RenderViewHost as hidden, to reduce
// resource usage. This can only be done after the first call to
// LoadURL, so there's an actual RenderViewHost with a
// RenderWidgetHostView to hide.
//
// Done here to prevent a race with loading the page.
prerender_contents_->tab_contents()->HideContents();
}
break;
}
case NotificationType::CREATING_NEW_WINDOW_CANCELLED: {
if (prerender_contents_.get()) {
CHECK(Source<TabContents>(source).ptr() ==
prerender_contents_->tab_contents());
// Since we don't want to permit child windows that would have a
// window.opener property, terminate prerendering.
Destroy(FINAL_STATUS_CREATE_NEW_WINDOW);
}
break;
}
default:
NOTREACHED() << "Unexpected notification sent.";
break;
}
}
void PrerenderContents::OnMessageBoxClosed(IPC::Message* reply_msg,
bool success,
const std::wstring& prompt) {
render_view_host_->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
}
gfx::NativeWindow PrerenderContents::GetMessageBoxRootWindow() {
NOTIMPLEMENTED();
return NULL;
}
TabContents* PrerenderContents::AsTabContents() {
return NULL;
}
ExtensionHost* PrerenderContents::AsExtensionHost() {
return NULL;
}
void PrerenderContents::UpdateInspectorSetting(const std::string& key,
const std::string& value) {
RenderViewHostDelegateHelper::UpdateInspectorSetting(profile_, key, value);
}
void PrerenderContents::ClearInspectorSettings() {
RenderViewHostDelegateHelper::ClearInspectorSettings(profile_);
}
void PrerenderContents::Close(RenderViewHost* render_view_host) {
Destroy(FINAL_STATUS_CLOSED);
}
RendererPreferences PrerenderContents::GetRendererPrefs(
Profile* profile) const {
RendererPreferences preferences;
renderer_preferences_util::UpdateFromSystemSettings(&preferences, profile);
return preferences;
}
WebPreferences PrerenderContents::GetWebkitPrefs() {
return RenderViewHostDelegateHelper::GetWebkitPrefs(profile_,
false); // is_web_ui
}
void PrerenderContents::CreateNewWindow(
int route_id,
const ViewHostMsg_CreateWindow_Params& params) {
// TODO(dominich): Remove when we switch to TabContents.
// Since we don't want to permit child windows that would have a
// window.opener property, terminate prerendering.
Destroy(FINAL_STATUS_CREATE_NEW_WINDOW);
}
void PrerenderContents::CreateNewWidget(int route_id,
WebKit::WebPopupType popup_type) {
NOTREACHED();
}
void PrerenderContents::CreateNewFullscreenWidget(int route_id) {
NOTREACHED();
}
void PrerenderContents::ShowCreatedWindow(int route_id,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture) {
// TODO(tburkard): need to figure out what the correct behavior here is
NOTIMPLEMENTED();
}
void PrerenderContents::ShowCreatedWidget(int route_id,
const gfx::Rect& initial_pos) {
NOTIMPLEMENTED();
}
void PrerenderContents::ShowCreatedFullscreenWidget(int route_id) {
NOTIMPLEMENTED();
}
void PrerenderContents::OnRenderViewHostCreated(
RenderViewHost* new_render_view_host) {
}
void PrerenderContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
bool is_main_frame,
const GURL& url) {
if (is_main_frame) {
if (!AddAliasURL(url))
return;
// Usually, this event fires if the user clicks or enters a new URL.
// Neither of these can happen in the case of an invisible prerender.
// So the cause is: Some JavaScript caused a new URL to be loaded. In that
// case, the spinner would start again in the browser, so we must reset
// has_stopped_loading_ so that the spinner won't be stopped.
has_stopped_loading_ = false;
}
}
void PrerenderContents::OnUpdateFaviconURL(
int32 page_id,
const std::vector<FaviconURL>& urls) {
VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_;
for (std::vector<FaviconURL>::const_iterator it = urls.begin();
it != urls.end(); ++it) {
if (it->icon_type == FaviconURL::FAVICON) {
icon_url_ = it->icon_url;
VLOG(1) << icon_url_;
return;
}
}
}
void PrerenderContents::OnMaybeCancelPrerenderForHTML5Media() {
Destroy(FINAL_STATUS_HTML5_MEDIA);
}
bool PrerenderContents::AddAliasURL(const GURL& url) {
if (!url.SchemeIs("http")) {
if (url.SchemeIs("https"))
Destroy(FINAL_STATUS_HTTPS);
else
Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME);
return false;
}
alias_urls_.push_back(url);
return true;
}
bool PrerenderContents::MatchesURL(const GURL& url) const {
return std::find_if(alias_urls_.begin(),
alias_urls_.end(),
PrerenderUrlPredicate(url)) != alias_urls_.end();
}
void PrerenderContents::OnJSOutOfMemory() {
Destroy(FINAL_STATUS_JS_OUT_OF_MEMORY);
}
void PrerenderContents::OnRunJavaScriptMessage(
const std::wstring& message,
const std::wstring& default_prompt,
const GURL& frame_url,
const int flags,
bool* did_suppress_message,
std::wstring* prompt_field) {
// Always suppress JavaScript messages if they're triggered by a page being
// prerendered.
*did_suppress_message = true;
// We still want to show the user the message when they navigate to this
// page, so cancel this prerender.
Destroy(FINAL_STATUS_JAVASCRIPT_ALERT);
}
void PrerenderContents::OnRenderViewGone(int status, int exit_code) {
Destroy(FINAL_STATUS_RENDERER_CRASHED);
}
void PrerenderContents::DidStopLoading() {
has_stopped_loading_ = true;
}
void PrerenderContents::Destroy(FinalStatus final_status) {
if (prerender_manager_->IsPendingDelete(this))
return;
OnDestroy();
prerender_manager_->MoveEntryToPendingDelete(this);
set_final_status(final_status);
// We may destroy the PrerenderContents before we have initialized the
// RenderViewHost. Otherwise set the Observer's PrerenderContents to NULL to
// avoid any more messages being sent.
if (render_view_host_observer_.get())
render_view_host_observer_->set_prerender_contents(NULL);
}
void PrerenderContents::RendererUnresponsive(RenderViewHost* render_view_host,
bool is_during_unload) {
DCHECK_EQ(render_view_host_, render_view_host);
Destroy(FINAL_STATUS_RENDERER_UNRESPONSIVE);
}
base::ProcessMetrics* PrerenderContents::MaybeGetProcessMetrics() {
if (process_metrics_.get() == NULL) {
// If a PrenderContents hasn't started prerending, don't be fully formed.
if (!render_view_host() || !render_view_host()->process())
return NULL;
base::ProcessHandle handle = render_view_host()->process()->GetHandle();
if (handle == base::kNullProcessHandle)
return NULL;
#if !defined(OS_MACOSX)
process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(handle));
#else
process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(
handle,
MachBroker::GetInstance()));
#endif
}
return process_metrics_.get();
}
void PrerenderContents::DestroyWhenUsingTooManyResources() {
base::ProcessMetrics* metrics = MaybeGetProcessMetrics();
if (metrics == NULL)
return;
size_t private_bytes, shared_bytes;
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
if (private_bytes >
prerender_manager_->max_prerender_memory_mb() * 1024 * 1024) {
Destroy(FINAL_STATUS_MEMORY_LIMIT_EXCEEDED);
}
}
}
TabContentsWrapper* PrerenderContents::ReleasePrerenderContents() {
render_view_host_observer_.reset();
return prerender_contents_.release();
}
RenderViewHostDelegate* PrerenderContents::GetRenderViewHostDelegate() {
if (UseTabContents()) {
if (!prerender_contents_.get())
return NULL;
return prerender_contents_->tab_contents();
} else {
return this;
}
}
RenderViewHost* PrerenderContents::render_view_host_mutable() {
return const_cast<RenderViewHost*>(render_view_host());
}
const RenderViewHost* PrerenderContents::render_view_host() const {
// TODO(mmenke): Replace with simple accessor once TabContents is always
// used.
if (UseTabContents()) {
if (!prerender_contents_.get())
return NULL;
return prerender_contents_->render_view_host();
}
return render_view_host_;
}
} // namespace prerender
|