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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
|
// 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_manager.h"
#include <string>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/time.h"
#include "base/values.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_final_status.h"
#include "chrome/browser/prerender/prerender_history.h"
#include "chrome/browser/prerender/prerender_observer.h"
#include "chrome/browser/prerender/prerender_tracker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_process_host.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/tab_contents/render_view_host_manager.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/tab_contents/tab_contents_delegate.h"
#include "content/common/notification_service.h"
#include "googleurl/src/url_canon.h"
#include "googleurl/src/url_parse.h"
#include "googleurl/src/url_util.h"
namespace prerender {
namespace {
// Time window for which we will record windowed PLT's from the last
// observed link rel=prefetch tag.
const int kWindowDurationSeconds = 30;
// Time interval at which periodic cleanups are performed.
const int kPeriodicCleanupIntervalMs = 1000;
// Time interval before a new prerender is allowed.
const int kMinTimeBetweenPrerendersMs = 500;
// Time window for which we record old navigations, in milliseconds.
const int kNavigationRecordWindowMs = 5000;
// Valid HTTP methods for prerendering.
const char* const kValidHttpMethods[] = {
"GET",
"HEAD",
"OPTIONS",
"POST",
"TRACE",
};
// Length of prerender history, for display in chrome://net-internals
const int kHistoryLength = 100;
} // namespace
class PrerenderManager::OnCloseTabContentsDeleter : public TabContentsDelegate {
public:
OnCloseTabContentsDeleter(PrerenderManager* manager,
TabContentsWrapper* tab)
: manager_(manager),
tab_(tab) {
tab_->tab_contents()->set_delegate(this);
}
virtual void CloseContents(TabContents* source) OVERRIDE {
tab_->tab_contents()->set_delegate(NULL);
manager_->ScheduleDeleteOldTabContents(tab_.release(), this);
}
virtual bool ShouldSuppressDialogs() OVERRIDE {
return true;
}
// TabContentsDelegate implementation (pure virtual methods). Since we are
// waiting for the tab to close, none of this matters.
virtual void OpenURLFromTab(TabContents*, const GURL&, const GURL&,
WindowOpenDisposition, PageTransition::Type) OVERRIDE {}
virtual void NavigationStateChanged(const TabContents*, unsigned) OVERRIDE {}
virtual void AddNewContents(TabContents*, TabContents*, WindowOpenDisposition,
const gfx::Rect&, bool) OVERRIDE {}
virtual void ActivateContents(TabContents*) OVERRIDE {}
virtual void DeactivateContents(TabContents*) OVERRIDE {}
virtual void LoadingStateChanged(TabContents*) OVERRIDE {}
virtual void MoveContents(TabContents*, const gfx::Rect&) OVERRIDE {}
virtual void UpdateTargetURL(TabContents*, const GURL&) OVERRIDE {}
private:
PrerenderManager* manager_;
scoped_ptr<TabContentsWrapper> tab_;
DISALLOW_COPY_AND_ASSIGN(OnCloseTabContentsDeleter);
};
// static
int PrerenderManager::prerenders_per_session_count_ = 0;
// static
PrerenderManager::PrerenderManagerMode PrerenderManager::mode_ =
PRERENDER_MODE_ENABLED;
// static
PrerenderManager::PrerenderManagerMode PrerenderManager::GetMode() {
return mode_;
}
// static
void PrerenderManager::SetMode(PrerenderManagerMode mode) {
mode_ = mode;
}
// static
bool PrerenderManager::IsPrerenderingPossible() {
return
GetMode() == PRERENDER_MODE_ENABLED ||
GetMode() == PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP ||
GetMode() == PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP;
}
// static
bool PrerenderManager::IsControlGroup() {
return GetMode() == PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP;
}
// static
bool PrerenderManager::MaybeGetQueryStringBasedAliasURL(
const GURL& url, GURL* alias_url) {
DCHECK(alias_url);
url_parse::Parsed parsed;
url_parse::ParseStandardURL(url.spec().c_str(), url.spec().length(),
&parsed);
url_parse::Component query = parsed.query;
url_parse::Component key, value;
while (url_parse::ExtractQueryKeyValue(url.spec().c_str(), &query, &key,
&value)) {
if (key.len != 3 || strncmp(url.spec().c_str() + key.begin, "url", key.len))
continue;
// We found a url= query string component.
if (value.len < 1)
continue;
url_canon::RawCanonOutputW<1024> decoded_url;
url_util::DecodeURLEscapeSequences(url.spec().c_str() + value.begin,
value.len, &decoded_url);
GURL new_url(string16(decoded_url.data(), decoded_url.length()));
if (!new_url.is_empty() && new_url.is_valid()) {
*alias_url = new_url;
return true;
}
return false;
}
return false;
}
// static
bool PrerenderManager::IsValidHttpMethod(const std::string& method) {
// method has been canonicalized to upper case at this point so we can just
// compare them.
DCHECK_EQ(method, StringToUpperASCII(method));
for (size_t i = 0; i < arraysize(kValidHttpMethods); ++i) {
if (method.compare(kValidHttpMethods[i]) == 0)
return true;
}
return false;
}
struct PrerenderManager::PrerenderContentsData {
PrerenderContents* contents_;
base::Time start_time_;
PrerenderContentsData(PrerenderContents* contents, base::Time start_time)
: contents_(contents),
start_time_(start_time) {
}
};
struct PrerenderManager::NavigationRecord {
GURL url_;
base::TimeTicks time_;
NavigationRecord(const GURL& url, base::TimeTicks time)
: url_(url),
time_(time) {
}
};
struct PrerenderManager::PendingContentsData {
PendingContentsData(const GURL& url,
const GURL& referrer,
Origin origin)
: url_(url), referrer_(referrer), origin_(origin) { }
~PendingContentsData() {}
GURL url_;
GURL referrer_;
Origin origin_;
};
PrerenderManager::PrerenderManager(Profile* profile,
PrerenderTracker* prerender_tracker)
: enabled_(true),
profile_(profile),
prerender_tracker_(prerender_tracker),
prerender_contents_factory_(PrerenderContents::CreateFactory()),
last_prerender_start_time_(GetCurrentTimeTicks() -
base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs)),
runnable_method_factory_(this),
prerender_history_(new PrerenderHistory(kHistoryLength)) {
// There are some assumptions that the PrerenderManager is on the UI thread.
// Any other checks simply make sure that the PrerenderManager is accessed on
// the same thread that it was created on.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
PrerenderManager::~PrerenderManager() {
DestroyAllContents(FINAL_STATUS_MANAGER_SHUTDOWN);
}
void PrerenderManager::SetPrerenderContentsFactory(
PrerenderContents::Factory* prerender_contents_factory) {
DCHECK(CalledOnValidThread());
prerender_contents_factory_.reset(prerender_contents_factory);
}
bool PrerenderManager::AddPrerenderFromLinkRelPrerender(int process_id,
int route_id,
const GURL& url,
const GURL& referrer) {
std::pair<int, int> child_route_id_pair = std::make_pair(process_id,
route_id);
return AddPrerender(ORIGIN_LINK_REL_PRERENDER, child_route_id_pair,
url, referrer);
}
bool PrerenderManager::AddPrerenderFromOmnibox(const GURL& url) {
CommandLine* cl = CommandLine::ForCurrentProcess();
if (!cl->HasSwitch(switches::kPrerenderFromOmnibox))
return false;
return AddPrerender(ORIGIN_OMNIBOX, std::make_pair(-1, -1), url, GURL());
}
bool PrerenderManager::AddPrerender(
Origin origin,
const std::pair<int, int>& child_route_id_pair,
const GURL& url_arg,
const GURL& referrer) {
DCHECK(CalledOnValidThread());
// If we observe multiple tags within the 30 second window, we will still
// reset the window to begin at the most recent occurrence, so that we will
// always be in a window in the 30 seconds from each occurrence.
last_prerender_seen_time_ = GetCurrentTimeTicks();
// If the referring page is prerendering, defer the prerender.
if (FindPrerenderContentsForChildRouteIdPair(child_route_id_pair) !=
prerender_list_.end()) {
AddPendingPrerender(origin, child_route_id_pair, url_arg, referrer);
return true;
}
DeleteOldEntries();
DeletePendingDeleteEntries();
GURL url = url_arg;
GURL alias_url;
if (IsControlGroup() &&
PrerenderManager::MaybeGetQueryStringBasedAliasURL(
url, &alias_url)) {
url = alias_url;
}
if (FindEntry(url))
return false;
// Do not prerender if there are too many render processes, and we would
// have to use an existing one. We do not want prerendering to happen in
// a shared process, so that we can always reliably lower the CPU
// priority for prerendering.
// In single-process mode, ShouldTryToUseExistingProcessHost() always returns
// true, so that case needs to be explicitly checked for.
// TODO(tburkard): Figure out how to cancel prerendering in the opposite
// case, when a new tab is added to a process used for prerendering.
if (RenderProcessHost::ShouldTryToUseExistingProcessHost() &&
!RenderProcessHost::run_renderer_in_process()) {
RecordFinalStatus(origin, FINAL_STATUS_TOO_MANY_PROCESSES);
return false;
}
// Check if enough time has passed since the last prerender.
if (!DoesRateLimitAllowPrerender()) {
// Cancel the prerender. We could add it to the pending prerender list but
// this doesn't make sense as the next prerender request will be triggered
// by a navigation and is unlikely to be the same site.
RecordFinalStatus(origin, FINAL_STATUS_RATE_LIMIT_EXCEEDED);
return false;
}
RenderViewHost* source_render_view_host = NULL;
// This test should fail only during unit tests.
if (child_route_id_pair.first != -1) {
source_render_view_host =
RenderViewHost::FromID(child_route_id_pair.first,
child_route_id_pair.second);
// Don't prerender page if parent RenderViewHost no longer exists, or it has
// no view. The latter should only happen when the RenderView has closed.
if (!source_render_view_host || !source_render_view_host->view()) {
RecordFinalStatus(origin, FINAL_STATUS_SOURCE_RENDER_VIEW_CLOSED);
return false;
}
}
PrerenderContents* prerender_contents =
CreatePrerenderContents(url, referrer, origin);
if (!prerender_contents || !prerender_contents->Init())
return false;
// TODO(cbentzel): Move invalid checks here instead of PrerenderContents?
PrerenderContentsData data(prerender_contents, GetCurrentTime());
prerender_list_.push_back(data);
if (IsControlGroup()) {
data.contents_->set_final_status(FINAL_STATUS_CONTROL_GROUP);
} else {
last_prerender_start_time_ = GetCurrentTimeTicks();
data.contents_->StartPrerendering(source_render_view_host);
}
while (prerender_list_.size() > config_.max_elements) {
data = prerender_list_.front();
prerender_list_.pop_front();
data.contents_->Destroy(FINAL_STATUS_EVICTED);
}
StartSchedulingPeriodicCleanups();
return true;
}
void PrerenderManager::AddPendingPrerender(
Origin origin,
const std::pair<int, int>& child_route_id_pair,
const GURL& url,
const GURL& referrer) {
DCHECK(FindPrerenderContentsForChildRouteIdPair(child_route_id_pair) !=
prerender_list_.end());
PendingPrerenderList::iterator it =
pending_prerender_list_.find(child_route_id_pair);
if (it == pending_prerender_list_.end()) {
PendingPrerenderList::value_type el = std::make_pair(child_route_id_pair,
std::vector<PendingContentsData>());
it = pending_prerender_list_.insert(el).first;
}
it->second.push_back(PendingContentsData(url, referrer, origin));
}
std::list<PrerenderManager::PrerenderContentsData>::iterator
PrerenderManager::FindPrerenderContentsForChildRouteIdPair(
const std::pair<int, int>& child_route_id_pair) {
std::list<PrerenderContentsData>::iterator it = prerender_list_.begin();
for (; it != prerender_list_.end(); ++it) {
PrerenderContents* prerender_contents = it->contents_;
int child_id;
int route_id;
bool has_child_id = prerender_contents->GetChildId(&child_id);
bool has_route_id = has_child_id &&
prerender_contents->GetRouteId(&route_id);
if (has_child_id && has_route_id &&
child_id == child_route_id_pair.first &&
route_id == child_route_id_pair.second) {
break;
}
}
return it;
}
void PrerenderManager::DestroyPrerenderForRenderView(
int process_id, int view_id, FinalStatus final_status) {
DCHECK(CalledOnValidThread());
std::list<PrerenderContentsData>::iterator it =
FindPrerenderContentsForChildRouteIdPair(
std::make_pair(process_id, view_id));
if (it != prerender_list_.end()) {
PrerenderContents* prerender_contents = it->contents_;
prerender_contents->Destroy(final_status);
}
}
void PrerenderManager::DeleteOldEntries() {
DCHECK(CalledOnValidThread());
while (!prerender_list_.empty()) {
PrerenderContentsData data = prerender_list_.front();
if (IsPrerenderElementFresh(data.start_time_))
return;
data.contents_->Destroy(FINAL_STATUS_TIMED_OUT);
}
MaybeStopSchedulingPeriodicCleanups();
}
PrerenderContents* PrerenderManager::GetEntryButNotSpecifiedTC(
const GURL& url,
TabContents *tc) {
DCHECK(CalledOnValidThread());
DeleteOldEntries();
DeletePendingDeleteEntries();
for (std::list<PrerenderContentsData>::iterator it = prerender_list_.begin();
it != prerender_list_.end();
++it) {
PrerenderContents* prerender_contents = it->contents_;
if (prerender_contents->MatchesURL(url, NULL)) {
if (!prerender_contents->prerender_contents() ||
!tc ||
prerender_contents->prerender_contents()->tab_contents() != tc) {
prerender_list_.erase(it);
return prerender_contents;
}
}
}
// Entry not found.
return NULL;
}
PrerenderContents* PrerenderManager::GetEntry(const GURL& url) {
return GetEntryButNotSpecifiedTC(url, NULL);
}
bool PrerenderManager::MaybeUsePrerenderedPage(TabContents* tab_contents,
const GURL& url,
bool has_opener_set) {
DCHECK(CalledOnValidThread());
RecordNavigation(url);
scoped_ptr<PrerenderContents> prerender_contents(
GetEntryButNotSpecifiedTC(url, tab_contents));
if (prerender_contents.get() == NULL)
return false;
// Do not use the prerendered version if the opener window.property was
// supposed to be set.
if (has_opener_set) {
prerender_contents.release()->Destroy(FINAL_STATUS_WINDOW_OPENER);
return false;
}
// Even if we match, the location.hash might be different. Record this as a
// separate final status.
GURL matching_url;
bool url_matches = prerender_contents->MatchesURL(url, &matching_url);
DCHECK(url_matches);
if (url_matches && url.ref() != matching_url.ref()) {
prerender_contents.release()->Destroy(FINAL_STATUS_FRAGMENT_MISMATCH);
return false;
}
// If we are just in the control group (which can be detected by noticing
// that prerendering hasn't even started yet), record that |tab_contents| now
// would be showing a prerendered contents, but otherwise, don't do anything.
if (!prerender_contents->prerendering_has_started()) {
MarkTabContentsAsWouldBePrerendered(tab_contents);
return false;
}
if (prerender_contents->starting_page_id() <=
tab_contents->GetMaxPageID()) {
prerender_contents.release()->Destroy(FINAL_STATUS_PAGE_ID_CONFLICT);
return false;
}
int child_id, route_id;
CHECK(prerender_contents->GetChildId(&child_id));
CHECK(prerender_contents->GetRouteId(&route_id));
// Try to set the prerendered page as used, so any subsequent attempts to
// cancel on other threads will fail. If this fails because the prerender
// was already cancelled, possibly on another thread, fail.
if (!prerender_tracker_->TryUse(child_id, route_id))
return false;
if (!prerender_contents->load_start_time().is_null())
RecordTimeUntilUsed(GetCurrentTimeTicks() -
prerender_contents->load_start_time());
UMA_HISTOGRAM_COUNTS("Prerender.PrerendersPerSessionCount",
++prerenders_per_session_count_);
prerender_contents->set_final_status(FINAL_STATUS_USED);
RenderViewHost* render_view_host =
prerender_contents->prerender_contents()->render_view_host();
DCHECK(render_view_host);
render_view_host->Send(
new ViewMsg_SetIsPrerendering(render_view_host->routing_id(), false));
TabContentsWrapper* new_tab_contents =
prerender_contents->ReleasePrerenderContents();
TabContentsWrapper* old_tab_contents =
TabContentsWrapper::GetCurrentWrapperForContents(tab_contents);
DCHECK(new_tab_contents);
DCHECK(old_tab_contents);
MarkTabContentsAsPrerendered(new_tab_contents->tab_contents());
// Merge the browsing history.
new_tab_contents->controller().CopyStateFromAndPrune(
&old_tab_contents->controller(),
false);
old_tab_contents->delegate()->SwapTabContents(old_tab_contents,
new_tab_contents);
prerender_contents->CommitHistory(new_tab_contents);
GURL icon_url = prerender_contents->icon_url();
if (!icon_url.is_empty()) {
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON));
new_tab_contents->favicon_tab_helper()->OnUpdateFaviconURL(
prerender_contents->page_id(),
urls);
}
// Update PPLT metrics:
// If the tab has finished loading, record a PPLT of 0.
// If the tab is still loading, reset its start time to the current time.
PrerenderObserver* prerender_observer =
new_tab_contents->prerender_observer();
DCHECK(prerender_observer != NULL);
prerender_observer->PrerenderSwappedIn();
// See if we have any pending prerender requests for this routing id and start
// the preload if we do.
std::pair<int, int> child_route_pair = std::make_pair(child_id, route_id);
PendingPrerenderList::iterator pending_it =
pending_prerender_list_.find(child_route_pair);
if (pending_it != pending_prerender_list_.end()) {
for (std::vector<PendingContentsData>::iterator content_it =
pending_it->second.begin();
content_it != pending_it->second.end(); ++content_it) {
AddPrerender(content_it->origin_, child_route_pair,
content_it->url_, content_it->referrer_);
}
pending_prerender_list_.erase(pending_it);
}
if (old_tab_contents->tab_contents()->NeedToFireBeforeUnload()) {
// Schedule the delete to occur after the tab has run its unload handlers.
on_close_tab_contents_deleters_.push_back(
new OnCloseTabContentsDeleter(this, old_tab_contents));
old_tab_contents->render_view_host()->FirePageBeforeUnload(false);
} else {
// No unload handler to run, so delete asap.
ScheduleDeleteOldTabContents(old_tab_contents, NULL);
}
// TODO(cbentzel): Should prerender_contents move to the pending delete
// list, instead of deleting directly here?
AddToHistory(prerender_contents.get());
return true;
}
void PrerenderManager::MoveEntryToPendingDelete(PrerenderContents* entry) {
DCHECK(CalledOnValidThread());
DCHECK(!IsPendingDelete(entry));
for (std::list<PrerenderContentsData>::iterator it = prerender_list_.begin();
it != prerender_list_.end();
++it) {
if (it->contents_ == entry) {
RemovePendingPrerender(entry);
prerender_list_.erase(it);
break;
}
}
pending_delete_list_.push_back(entry);
// Destroy the old TabContents relatively promptly to reduce resource usage,
// and in the case of HTML5 media, reduce the change of playing any sound.
PostCleanupTask();
}
base::Time PrerenderManager::GetCurrentTime() const {
return base::Time::Now();
}
base::TimeTicks PrerenderManager::GetCurrentTimeTicks() const {
return base::TimeTicks::Now();
}
bool PrerenderManager::IsPrerenderElementFresh(const base::Time start) const {
DCHECK(CalledOnValidThread());
base::Time now = GetCurrentTime();
return (now - start < config_.max_age);
}
PrerenderContents* PrerenderManager::CreatePrerenderContents(
const GURL& url,
const GURL& referrer,
Origin origin) {
DCHECK(CalledOnValidThread());
return prerender_contents_factory_->CreatePrerenderContents(
this, prerender_tracker_, profile_, url, referrer, origin);
}
bool PrerenderManager::IsPendingDelete(PrerenderContents* entry) const {
DCHECK(CalledOnValidThread());
for (std::list<PrerenderContents*>::const_iterator it =
pending_delete_list_.begin();
it != pending_delete_list_.end();
++it) {
if (*it == entry)
return true;
}
return false;
}
void PrerenderManager::DeletePendingDeleteEntries() {
while (!pending_delete_list_.empty()) {
PrerenderContents* contents = pending_delete_list_.front();
pending_delete_list_.pop_front();
AddToHistory(contents);
delete contents;
}
}
// Helper macro for histograms.
#define RECORD_PLT(tag, perceived_page_load_time) { \
UMA_HISTOGRAM_CUSTOM_TIMES( \
base::FieldTrial::MakeName(std::string("Prerender.") + tag, \
"Prefetch"), \
perceived_page_load_time, \
base::TimeDelta::FromMilliseconds(10), \
base::TimeDelta::FromSeconds(60), \
100); \
}
// static
void PrerenderManager::RecordPerceivedPageLoadTime(
base::TimeDelta perceived_page_load_time,
TabContents* tab_contents) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
PrerenderManager* prerender_manager =
tab_contents->profile()->GetPrerenderManager();
if (!prerender_manager)
return;
if (!prerender_manager->is_enabled())
return;
bool within_window = prerender_manager->WithinWindow();
RECORD_PLT("PerceivedPLT", perceived_page_load_time);
if (within_window)
RECORD_PLT("PerceivedPLTWindowed", perceived_page_load_time);
if (prerender_manager &&
((mode_ == PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP &&
prerender_manager->WouldTabContentsBePrerendered(tab_contents)) ||
(mode_ == PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP &&
prerender_manager->IsTabContentsPrerendered(tab_contents)))) {
RECORD_PLT("PerceivedPLTMatched", perceived_page_load_time);
} else {
if (within_window)
RECORD_PLT("PerceivedPLTWindowNotMatched", perceived_page_load_time);
}
}
bool PrerenderManager::is_enabled() const {
DCHECK(CalledOnValidThread());
return enabled_;
}
void PrerenderManager::set_enabled(bool enabled) {
DCHECK(CalledOnValidThread());
enabled_ = enabled;
}
PrerenderContents* PrerenderManager::FindEntry(const GURL& url) {
DCHECK(CalledOnValidThread());
for (std::list<PrerenderContentsData>::iterator it = prerender_list_.begin();
it != prerender_list_.end();
++it) {
if (it->contents_->MatchesURL(url, NULL))
return it->contents_;
}
// Entry not found.
return NULL;
}
PrerenderManager::PendingContentsData*
PrerenderManager::FindPendingEntry(const GURL& url) {
DCHECK(CalledOnValidThread());
for (PendingPrerenderList::iterator map_it = pending_prerender_list_.begin();
map_it != pending_prerender_list_.end();
++map_it) {
for (std::vector<PendingContentsData>::iterator content_it =
map_it->second.begin();
content_it != map_it->second.end();
++content_it) {
if (content_it->url_ == url) {
return &(*content_it);
}
}
}
return NULL;
}
void PrerenderManager::RemovePendingPrerender(PrerenderContents* entry) {
DCHECK(CalledOnValidThread());
int child_id;
int route_id;
bool has_child_id = entry->GetChildId(&child_id);
bool has_route_id = has_child_id && entry->GetRouteId(&route_id);
// If the entry doesn't have a RenderViewHost then it didn't start
// prerendering and there shouldn't be any pending preloads to remove.
if (has_child_id && has_route_id) {
std::pair<int, int> child_route_pair = std::make_pair(child_id, route_id);
pending_prerender_list_.erase(child_route_pair);
}
}
bool PrerenderManager::WithinWindow() const {
DCHECK(CalledOnValidThread());
if (last_prerender_seen_time_.is_null())
return false;
base::TimeDelta elapsed_time =
base::TimeTicks::Now() - last_prerender_seen_time_;
return elapsed_time <= base::TimeDelta::FromSeconds(kWindowDurationSeconds);
}
bool PrerenderManager::DoesRateLimitAllowPrerender() const {
DCHECK(CalledOnValidThread());
base::TimeDelta elapsed_time =
GetCurrentTimeTicks() - last_prerender_start_time_;
UMA_HISTOGRAM_TIMES("Prerender.TimeBetweenPrerenderRequests",
elapsed_time);
if (!config_.rate_limit_enabled)
return true;
return elapsed_time >
base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs);
}
void PrerenderManager::StartSchedulingPeriodicCleanups() {
DCHECK(CalledOnValidThread());
if (repeating_timer_.IsRunning())
return;
repeating_timer_.Start(
base::TimeDelta::FromMilliseconds(kPeriodicCleanupIntervalMs),
this,
&PrerenderManager::PeriodicCleanup);
}
void PrerenderManager::MaybeStopSchedulingPeriodicCleanups() {
if (!prerender_list_.empty())
return;
DCHECK(CalledOnValidThread());
repeating_timer_.Stop();
}
void PrerenderManager::DeleteOldTabContents() {
while (!old_tab_contents_list_.empty()) {
TabContentsWrapper* tab_contents = old_tab_contents_list_.front();
old_tab_contents_list_.pop_front();
// TODO(dominich): should we use Instant Unload Handler here?
delete tab_contents;
}
}
bool PrerenderManager::IsOldRenderViewHost(
const RenderViewHost* render_view_host) const {
for (std::list<TabContentsWrapper*>::const_iterator it =
old_tab_contents_list_.begin();
it != old_tab_contents_list_.end();
++it) {
if ((*it)->tab_contents()->render_view_host() == render_view_host)
return true;
}
return false;
}
void PrerenderManager::PeriodicCleanup() {
DCHECK(CalledOnValidThread());
DeleteOldTabContents();
DeleteOldEntries();
// Grab a copy of the current PrerenderContents pointers, so that we
// will not interfere with potential deletions of the list.
std::vector<PrerenderContents*> prerender_contents;
for (std::list<PrerenderContentsData>::iterator it = prerender_list_.begin();
it != prerender_list_.end();
++it) {
prerender_contents.push_back(it->contents_);
}
for (std::vector<PrerenderContents*>::iterator it =
prerender_contents.begin();
it != prerender_contents.end();
++it) {
(*it)->DestroyWhenUsingTooManyResources();
}
DeletePendingDeleteEntries();
}
void PrerenderManager::PostCleanupTask() {
DCHECK(CalledOnValidThread());
MessageLoop::current()->PostTask(
FROM_HERE,
runnable_method_factory_.NewRunnableMethod(
&PrerenderManager::PeriodicCleanup));
}
bool PrerenderManager::IsTabContentsPrerendering(
TabContents* tab_contents) const {
DCHECK(CalledOnValidThread());
for (std::list<PrerenderContentsData>::const_iterator it =
prerender_list_.begin();
it != prerender_list_.end();
++it) {
TabContentsWrapper* prerender_tab_contents_wrapper =
it->contents_->prerender_contents();
if (prerender_tab_contents_wrapper &&
prerender_tab_contents_wrapper->tab_contents() == tab_contents) {
return true;
}
}
// Also look through the pending-deletion list.
for (std::list<PrerenderContents*>::const_iterator it =
pending_delete_list_.begin();
it != pending_delete_list_.end();
++it) {
TabContentsWrapper* prerender_tab_contents_wrapper =
(*it)->prerender_contents();
if (prerender_tab_contents_wrapper &&
prerender_tab_contents_wrapper->tab_contents() == tab_contents)
return true;
}
return false;
}
void PrerenderManager::MarkTabContentsAsPrerendered(TabContents* tab_contents) {
DCHECK(CalledOnValidThread());
prerendered_tab_contents_set_.insert(tab_contents);
}
void PrerenderManager::MarkTabContentsAsWouldBePrerendered(
TabContents* tab_contents) {
DCHECK(CalledOnValidThread());
would_be_prerendered_tab_contents_set_.insert(tab_contents);
}
void PrerenderManager::MarkTabContentsAsNotPrerendered(
TabContents* tab_contents) {
DCHECK(CalledOnValidThread());
prerendered_tab_contents_set_.erase(tab_contents);
would_be_prerendered_tab_contents_set_.erase(tab_contents);
}
bool PrerenderManager::IsTabContentsPrerendered(
TabContents* tab_contents) const {
DCHECK(CalledOnValidThread());
return prerendered_tab_contents_set_.count(tab_contents) > 0;
}
bool PrerenderManager::WouldTabContentsBePrerendered(
TabContents* tab_contents) const {
DCHECK(CalledOnValidThread());
return would_be_prerendered_tab_contents_set_.count(tab_contents) > 0;
}
bool PrerenderManager::HasRecentlyBeenNavigatedTo(const GURL& url) {
DCHECK(CalledOnValidThread());
CleanUpOldNavigations();
for (std::list<NavigationRecord>::const_iterator it = navigations_.begin();
it != navigations_.end();
++it) {
if (it->url_ == url)
return true;
}
return false;
}
void PrerenderManager::CleanUpOldNavigations() {
DCHECK(CalledOnValidThread());
// Cutoff. Navigations before this cutoff can be discarded.
base::TimeTicks cutoff = GetCurrentTimeTicks() -
base::TimeDelta::FromMilliseconds(kNavigationRecordWindowMs);
while (!navigations_.empty()) {
if (navigations_.front().time_ > cutoff)
break;
navigations_.pop_front();
}
}
void PrerenderManager::ScheduleDeleteOldTabContents(
TabContentsWrapper* tab,
OnCloseTabContentsDeleter* deleter) {
old_tab_contents_list_.push_back(tab);
PostCleanupTask();
if (deleter) {
ScopedVector<OnCloseTabContentsDeleter>::iterator i = std::find(
on_close_tab_contents_deleters_.begin(),
on_close_tab_contents_deleters_.end(),
deleter);
DCHECK(i != on_close_tab_contents_deleters_.end());
on_close_tab_contents_deleters_.erase(i);
}
}
Value* PrerenderManager::GetAsValue() const {
DCHECK(CalledOnValidThread());
DictionaryValue* dict_value = new DictionaryValue();
dict_value->Set("history", prerender_history_->GetEntriesAsValue());
dict_value->Set("active", GetActivePrerendersAsValue());
dict_value->SetBoolean("enabled", enabled_);
return dict_value;
}
void PrerenderManager::ClearData(int clear_flags) {
DCHECK_GE(clear_flags, 0);
DCHECK_LT(clear_flags, CLEAR_MAX);
if (clear_flags & CLEAR_PRERENDER_CONTENTS)
DestroyAllContents(FINAL_STATUS_CACHE_OR_HISTORY_CLEARED);
// This has to be second, since destroying prerenders can add to the history.
if (clear_flags & CLEAR_PRERENDER_HISTORY)
prerender_history_->Clear();
}
void PrerenderManager::AddToHistory(PrerenderContents* contents) {
PrerenderHistory::Entry entry(contents->prerender_url(),
contents->final_status(),
contents->origin(),
base::Time::Now());
prerender_history_->AddEntry(entry);
}
void PrerenderManager::RecordNavigation(const GURL& url) {
DCHECK(CalledOnValidThread());
navigations_.push_back(NavigationRecord(url, GetCurrentTimeTicks()));
CleanUpOldNavigations();
}
Value* PrerenderManager::GetActivePrerendersAsValue() const {
ListValue* list_value = new ListValue();
for (std::list<PrerenderContentsData>::const_iterator it =
prerender_list_.begin();
it != prerender_list_.end();
++it) {
Value* prerender_value = it->contents_->GetAsValue();
if (!prerender_value)
continue;
list_value->Append(prerender_value);
}
return list_value;
}
void PrerenderManager::DestroyAllContents(FinalStatus final_status) {
DeleteOldTabContents();
while (!prerender_list_.empty()) {
PrerenderContentsData data = prerender_list_.front();
prerender_list_.pop_front();
data.contents_->Destroy(final_status);
}
DeletePendingDeleteEntries();
}
void PrerenderManager::RecordTimeUntilUsed(base::TimeDelta time_until_used) {
DCHECK(CalledOnValidThread());
UMA_HISTOGRAM_CUSTOM_TIMES(
"Prerender.TimeUntilUsed",
time_until_used,
base::TimeDelta::FromMilliseconds(10),
config_.max_age,
50);
}
} // namespace prerender
|