forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation_controller_impl.cc
1692 lines (1455 loc) · 63 KB
/
navigation_controller_impl.cc
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
992
993
994
995
996
997
998
999
1000
// Copyright 2013 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/frame_host/navigation_controller_impl.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h" // Temporary
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/browser_url_handler_impl.h"
#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
#include "content/browser/dom_storage/session_storage_namespace_impl.h"
#include "content/browser/frame_host/debug_urls.h"
#include "content/browser/frame_host/interstitial_page_impl.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/frame_host/navigation_entry_screenshot_manager.h"
#include "content/browser/renderer_host/render_view_host_impl.h" // Temporary
#include "content/browser/site_instance_impl.h"
#include "content/common/view_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/url_constants.h"
#include "net/base/escape.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
#include "skia/ext/platform_canvas.h"
namespace content {
namespace {
const int kInvalidateAll = 0xFFFFFFFF;
// Invoked when entries have been pruned, or removed. For example, if the
// current entries are [google, digg, yahoo], with the current entry google,
// and the user types in cnet, then digg and yahoo are pruned.
void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
bool from_front,
int count) {
PrunedDetails details;
details.from_front = from_front;
details.count = count;
NotificationService::current()->Notify(
NOTIFICATION_NAV_LIST_PRUNED,
Source<NavigationController>(nav_controller),
Details<PrunedDetails>(&details));
}
// Ensure the given NavigationEntry has a valid state, so that WebKit does not
// get confused if we navigate back to it.
//
// An empty state is treated as a new navigation by WebKit, which would mean
// losing the navigation entries and generating a new navigation entry after
// this one. We don't want that. To avoid this we create a valid state which
// WebKit will not treat as a new navigation.
void SetPageStateIfEmpty(NavigationEntryImpl* entry) {
if (!entry->GetPageState().IsValid())
entry->SetPageState(PageState::CreateFromURL(entry->GetURL()));
}
NavigationEntryImpl::RestoreType ControllerRestoreTypeToEntryType(
NavigationController::RestoreType type) {
switch (type) {
case NavigationController::RESTORE_CURRENT_SESSION:
return NavigationEntryImpl::RESTORE_CURRENT_SESSION;
case NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY:
return NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY;
case NavigationController::RESTORE_LAST_SESSION_CRASHED:
return NavigationEntryImpl::RESTORE_LAST_SESSION_CRASHED;
}
NOTREACHED();
return NavigationEntryImpl::RESTORE_CURRENT_SESSION;
}
// Configure all the NavigationEntries in entries for restore. This resets
// the transition type to reload and makes sure the content state isn't empty.
void ConfigureEntriesForRestore(
std::vector<linked_ptr<NavigationEntryImpl> >* entries,
NavigationController::RestoreType type) {
for (size_t i = 0; i < entries->size(); ++i) {
// Use a transition type of reload so that we don't incorrectly increase
// the typed count.
(*entries)[i]->SetTransitionType(PAGE_TRANSITION_RELOAD);
(*entries)[i]->set_restore_type(ControllerRestoreTypeToEntryType(type));
// NOTE(darin): This code is only needed for backwards compat.
SetPageStateIfEmpty((*entries)[i].get());
}
}
// See NavigationController::IsURLInPageNavigation for how this works and why.
bool AreURLsInPageNavigation(const GURL& existing_url,
const GURL& new_url,
bool renderer_says_in_page,
NavigationType navigation_type) {
if (existing_url == new_url)
return renderer_says_in_page;
if (!new_url.has_ref()) {
// When going back from the ref URL to the non ref one the navigation type
// is IN_PAGE.
return navigation_type == NAVIGATION_TYPE_IN_PAGE;
}
url_canon::Replacements<char> replacements;
replacements.ClearRef();
return existing_url.ReplaceComponents(replacements) ==
new_url.ReplaceComponents(replacements);
}
// Determines whether or not we should be carrying over a user agent override
// between two NavigationEntries.
bool ShouldKeepOverride(const NavigationEntry* last_entry) {
return last_entry && last_entry->GetIsOverridingUserAgent();
}
} // namespace
// NavigationControllerImpl ----------------------------------------------------
const size_t kMaxEntryCountForTestingNotSet = -1;
// static
size_t NavigationControllerImpl::max_entry_count_for_testing_ =
kMaxEntryCountForTestingNotSet;
// Should Reload check for post data? The default is true, but is set to false
// when testing.
static bool g_check_for_repost = true;
// static
NavigationEntry* NavigationController::CreateNavigationEntry(
const GURL& url,
const Referrer& referrer,
PageTransition transition,
bool is_renderer_initiated,
const std::string& extra_headers,
BrowserContext* browser_context) {
// Allow the browser URL handler to rewrite the URL. This will, for example,
// remove "view-source:" from the beginning of the URL to get the URL that
// will actually be loaded. This real URL won't be shown to the user, just
// used internally.
GURL loaded_url(url);
bool reverse_on_redirect = false;
BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
&loaded_url, browser_context, &reverse_on_redirect);
NavigationEntryImpl* entry = new NavigationEntryImpl(
NULL, // The site instance for tabs is sent on navigation
// (WebContents::GetSiteInstance).
-1,
loaded_url,
referrer,
base::string16(),
transition,
is_renderer_initiated);
entry->SetVirtualURL(url);
entry->set_user_typed_url(url);
entry->set_update_virtual_url_with_url(reverse_on_redirect);
entry->set_extra_headers(extra_headers);
return entry;
}
// static
void NavigationController::DisablePromptOnRepost() {
g_check_for_repost = false;
}
base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
base::Time t) {
// If |t| is between the water marks, we're in a run of duplicates
// or just getting out of it, so increase the high-water mark to get
// a time that probably hasn't been used before and return it.
if (low_water_mark_ <= t && t <= high_water_mark_) {
high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
return high_water_mark_;
}
// Otherwise, we're clear of the last duplicate run, so reset the
// water marks.
low_water_mark_ = high_water_mark_ = t;
return t;
}
NavigationControllerImpl::NavigationControllerImpl(
NavigationControllerDelegate* delegate,
BrowserContext* browser_context)
: browser_context_(browser_context),
pending_entry_(NULL),
last_committed_entry_index_(-1),
pending_entry_index_(-1),
transient_entry_index_(-1),
delegate_(delegate),
max_restored_page_id_(-1),
ssl_manager_(this),
needs_reload_(false),
is_initial_navigation_(true),
pending_reload_(NO_RELOAD),
get_timestamp_callback_(base::Bind(&base::Time::Now)),
screenshot_manager_(new NavigationEntryScreenshotManager(this)) {
DCHECK(browser_context_);
}
NavigationControllerImpl::~NavigationControllerImpl() {
DiscardNonCommittedEntriesInternal();
}
WebContents* NavigationControllerImpl::GetWebContents() const {
return delegate_->GetWebContents();
}
BrowserContext* NavigationControllerImpl::GetBrowserContext() const {
return browser_context_;
}
void NavigationControllerImpl::SetBrowserContext(
BrowserContext* browser_context) {
browser_context_ = browser_context;
}
void NavigationControllerImpl::Restore(
int selected_navigation,
RestoreType type,
std::vector<NavigationEntry*>* entries) {
// Verify that this controller is unused and that the input is valid.
DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
DCHECK(selected_navigation >= 0 &&
selected_navigation < static_cast<int>(entries->size()));
needs_reload_ = true;
for (size_t i = 0; i < entries->size(); ++i) {
NavigationEntryImpl* entry =
NavigationEntryImpl::FromNavigationEntry((*entries)[i]);
entries_.push_back(linked_ptr<NavigationEntryImpl>(entry));
}
entries->clear();
// And finish the restore.
FinishRestore(selected_navigation, type);
}
void NavigationControllerImpl::Reload(bool check_for_repost) {
ReloadInternal(check_for_repost, RELOAD);
}
void NavigationControllerImpl::ReloadIgnoringCache(bool check_for_repost) {
ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
}
void NavigationControllerImpl::ReloadOriginalRequestURL(bool check_for_repost) {
ReloadInternal(check_for_repost, RELOAD_ORIGINAL_REQUEST_URL);
}
void NavigationControllerImpl::ReloadInternal(bool check_for_repost,
ReloadType reload_type) {
if (transient_entry_index_ != -1) {
// If an interstitial is showing, treat a reload as a navigation to the
// transient entry's URL.
NavigationEntryImpl* transient_entry =
NavigationEntryImpl::FromNavigationEntry(GetTransientEntry());
if (!transient_entry)
return;
LoadURL(transient_entry->GetURL(),
Referrer(),
PAGE_TRANSITION_RELOAD,
transient_entry->extra_headers());
return;
}
NavigationEntryImpl* entry = NULL;
int current_index = -1;
// If we are reloading the initial navigation, just use the current
// pending entry. Otherwise look up the current entry.
if (IsInitialNavigation() && pending_entry_) {
entry = pending_entry_;
// The pending entry might be in entries_ (e.g., after a Clone), so we
// should also update the current_index.
current_index = pending_entry_index_;
} else {
DiscardNonCommittedEntriesInternal();
current_index = GetCurrentEntryIndex();
if (current_index != -1) {
entry = NavigationEntryImpl::FromNavigationEntry(
GetEntryAtIndex(current_index));
}
}
// If we are no where, then we can't reload. TODO(darin): We should add a
// CanReload method.
if (!entry)
return;
if (reload_type == NavigationControllerImpl::RELOAD_ORIGINAL_REQUEST_URL &&
entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
// We may have been redirected when navigating to the current URL.
// Use the URL the user originally intended to visit, if it's valid and if a
// POST wasn't involved; the latter case avoids issues with sending data to
// the wrong page.
entry->SetURL(entry->GetOriginalRequestURL());
}
if (g_check_for_repost && check_for_repost &&
entry->GetHasPostData()) {
// The user is asking to reload a page with POST data. Prompt to make sure
// they really want to do this. If they do, the dialog will call us back
// with check_for_repost = false.
delegate_->NotifyBeforeFormRepostWarningShow();
pending_reload_ = reload_type;
delegate_->ActivateAndShowRepostFormWarningDialog();
} else {
if (!IsInitialNavigation())
DiscardNonCommittedEntriesInternal();
// If we are reloading an entry that no longer belongs to the current
// site instance (for example, refreshing a page for just installed app),
// the reload must happen in a new process.
// The new entry must have a new page_id and site instance, so it behaves
// as new navigation (which happens to clear forward history).
// Tabs that are discarded due to low memory conditions may not have a site
// instance, and should not be treated as a cross-site reload.
SiteInstanceImpl* site_instance = entry->site_instance();
if (site_instance &&
site_instance->HasWrongProcessForURL(entry->GetURL())) {
// Create a navigation entry that resembles the current one, but do not
// copy page id, site instance, content state, or timestamp.
NavigationEntryImpl* nav_entry = NavigationEntryImpl::FromNavigationEntry(
CreateNavigationEntry(
entry->GetURL(), entry->GetReferrer(), entry->GetTransitionType(),
false, entry->extra_headers(), browser_context_));
// Mark the reload type as NO_RELOAD, so navigation will not be considered
// a reload in the renderer.
reload_type = NavigationController::NO_RELOAD;
nav_entry->set_should_replace_entry(true);
pending_entry_ = nav_entry;
} else {
pending_entry_ = entry;
pending_entry_index_ = current_index;
// The title of the page being reloaded might have been removed in the
// meanwhile, so we need to revert to the default title upon reload and
// invalidate the previously cached title (SetTitle will do both).
// See Chromium issue 96041.
pending_entry_->SetTitle(base::string16());
pending_entry_->SetTransitionType(PAGE_TRANSITION_RELOAD);
}
NavigateToPendingEntry(reload_type);
}
}
void NavigationControllerImpl::CancelPendingReload() {
DCHECK(pending_reload_ != NO_RELOAD);
pending_reload_ = NO_RELOAD;
}
void NavigationControllerImpl::ContinuePendingReload() {
if (pending_reload_ == NO_RELOAD) {
NOTREACHED();
} else {
ReloadInternal(false, pending_reload_);
pending_reload_ = NO_RELOAD;
}
}
bool NavigationControllerImpl::IsInitialNavigation() const {
return is_initial_navigation_;
}
NavigationEntryImpl* NavigationControllerImpl::GetEntryWithPageID(
SiteInstance* instance, int32 page_id) const {
int index = GetEntryIndexWithPageID(instance, page_id);
return (index != -1) ? entries_[index].get() : NULL;
}
void NavigationControllerImpl::LoadEntry(NavigationEntryImpl* entry) {
// When navigating to a new page, we don't know for sure if we will actually
// end up leaving the current page. The new page load could for example
// result in a download or a 'no content' response (e.g., a mailto: URL).
SetPendingEntry(entry);
NavigateToPendingEntry(NO_RELOAD);
}
void NavigationControllerImpl::SetPendingEntry(NavigationEntryImpl* entry) {
DiscardNonCommittedEntriesInternal();
pending_entry_ = entry;
NotificationService::current()->Notify(
NOTIFICATION_NAV_ENTRY_PENDING,
Source<NavigationController>(this),
Details<NavigationEntry>(entry));
}
NavigationEntry* NavigationControllerImpl::GetActiveEntry() const {
if (transient_entry_index_ != -1)
return entries_[transient_entry_index_].get();
if (pending_entry_)
return pending_entry_;
return GetLastCommittedEntry();
}
NavigationEntry* NavigationControllerImpl::GetVisibleEntry() const {
if (transient_entry_index_ != -1)
return entries_[transient_entry_index_].get();
// The pending entry is safe to return for new (non-history), browser-
// initiated navigations. Most renderer-initiated navigations should not
// show the pending entry, to prevent URL spoof attacks.
//
// We make an exception for renderer-initiated navigations in new tabs, as
// long as no other page has tried to access the initial empty document in
// the new tab. If another page modifies this blank page, a URL spoof is
// possible, so we must stop showing the pending entry.
RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
delegate_->GetRenderViewHost());
bool safe_to_show_pending =
pending_entry_ &&
// Require a new navigation.
pending_entry_->GetPageID() == -1 &&
// Require either browser-initiated or an unmodified new tab.
(!pending_entry_->is_renderer_initiated() ||
(IsInitialNavigation() &&
!GetLastCommittedEntry() &&
!rvh->has_accessed_initial_document()));
// Also allow showing the pending entry for history navigations in a new tab,
// such as Ctrl+Back. In this case, no existing page is visible and no one
// can script the new tab before it commits.
if (!safe_to_show_pending &&
pending_entry_ &&
pending_entry_->GetPageID() != -1 &&
IsInitialNavigation() &&
!pending_entry_->is_renderer_initiated())
safe_to_show_pending = true;
if (safe_to_show_pending)
return pending_entry_;
return GetLastCommittedEntry();
}
int NavigationControllerImpl::GetCurrentEntryIndex() const {
if (transient_entry_index_ != -1)
return transient_entry_index_;
if (pending_entry_index_ != -1)
return pending_entry_index_;
return last_committed_entry_index_;
}
NavigationEntry* NavigationControllerImpl::GetLastCommittedEntry() const {
if (last_committed_entry_index_ == -1)
return NULL;
return entries_[last_committed_entry_index_].get();
}
bool NavigationControllerImpl::CanViewSource() const {
const std::string& mime_type = delegate_->GetContentsMimeType();
bool is_viewable_mime_type = net::IsSupportedNonImageMimeType(mime_type) &&
!net::IsSupportedMediaMimeType(mime_type);
NavigationEntry* visible_entry = GetVisibleEntry();
return visible_entry && !visible_entry->IsViewSourceMode() &&
is_viewable_mime_type && !delegate_->GetInterstitialPage();
}
int NavigationControllerImpl::GetLastCommittedEntryIndex() const {
return last_committed_entry_index_;
}
int NavigationControllerImpl::GetEntryCount() const {
DCHECK(entries_.size() <= max_entry_count());
return static_cast<int>(entries_.size());
}
NavigationEntry* NavigationControllerImpl::GetEntryAtIndex(
int index) const {
return entries_.at(index).get();
}
NavigationEntry* NavigationControllerImpl::GetEntryAtOffset(
int offset) const {
int index = GetIndexForOffset(offset);
if (index < 0 || index >= GetEntryCount())
return NULL;
return entries_[index].get();
}
int NavigationControllerImpl::GetIndexForOffset(int offset) const {
return GetCurrentEntryIndex() + offset;
}
void NavigationControllerImpl::TakeScreenshot() {
screenshot_manager_->TakeScreenshot();
}
void NavigationControllerImpl::SetScreenshotManager(
NavigationEntryScreenshotManager* manager) {
screenshot_manager_.reset(manager ? manager :
new NavigationEntryScreenshotManager(this));
}
bool NavigationControllerImpl::CanGoBack() const {
return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
}
bool NavigationControllerImpl::CanGoForward() const {
int index = GetCurrentEntryIndex();
return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
}
bool NavigationControllerImpl::CanGoToOffset(int offset) const {
int index = GetIndexForOffset(offset);
return index >= 0 && index < GetEntryCount();
}
void NavigationControllerImpl::GoBack() {
if (!CanGoBack()) {
NOTREACHED();
return;
}
// Base the navigation on where we are now...
int current_index = GetCurrentEntryIndex();
DiscardNonCommittedEntries();
pending_entry_index_ = current_index - 1;
entries_[pending_entry_index_]->SetTransitionType(
PageTransitionFromInt(
entries_[pending_entry_index_]->GetTransitionType() |
PAGE_TRANSITION_FORWARD_BACK));
NavigateToPendingEntry(NO_RELOAD);
}
void NavigationControllerImpl::GoForward() {
if (!CanGoForward()) {
NOTREACHED();
return;
}
bool transient = (transient_entry_index_ != -1);
// Base the navigation on where we are now...
int current_index = GetCurrentEntryIndex();
DiscardNonCommittedEntries();
pending_entry_index_ = current_index;
// If there was a transient entry, we removed it making the current index
// the next page.
if (!transient)
pending_entry_index_++;
entries_[pending_entry_index_]->SetTransitionType(
PageTransitionFromInt(
entries_[pending_entry_index_]->GetTransitionType() |
PAGE_TRANSITION_FORWARD_BACK));
NavigateToPendingEntry(NO_RELOAD);
}
void NavigationControllerImpl::GoToIndex(int index) {
if (index < 0 || index >= static_cast<int>(entries_.size())) {
NOTREACHED();
return;
}
if (transient_entry_index_ != -1) {
if (index == transient_entry_index_) {
// Nothing to do when navigating to the transient.
return;
}
if (index > transient_entry_index_) {
// Removing the transient is goint to shift all entries by 1.
index--;
}
}
DiscardNonCommittedEntries();
pending_entry_index_ = index;
entries_[pending_entry_index_]->SetTransitionType(
PageTransitionFromInt(
entries_[pending_entry_index_]->GetTransitionType() |
PAGE_TRANSITION_FORWARD_BACK));
NavigateToPendingEntry(NO_RELOAD);
}
void NavigationControllerImpl::GoToOffset(int offset) {
if (!CanGoToOffset(offset))
return;
GoToIndex(GetIndexForOffset(offset));
}
bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
if (index == last_committed_entry_index_ ||
index == pending_entry_index_)
return false;
RemoveEntryAtIndexInternal(index);
return true;
}
void NavigationControllerImpl::UpdateVirtualURLToURL(
NavigationEntryImpl* entry, const GURL& new_url) {
GURL new_virtual_url(new_url);
if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
&new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
entry->SetVirtualURL(new_virtual_url);
}
}
void NavigationControllerImpl::LoadURL(
const GURL& url,
const Referrer& referrer,
PageTransition transition,
const std::string& extra_headers) {
LoadURLParams params(url);
params.referrer = referrer;
params.transition_type = transition;
params.extra_headers = extra_headers;
LoadURLWithParams(params);
}
void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
TRACE_EVENT0("browser", "NavigationControllerImpl::LoadURLWithParams");
if (HandleDebugURL(params.url, params.transition_type))
return;
// Checks based on params.load_type.
switch (params.load_type) {
case LOAD_TYPE_DEFAULT:
break;
case LOAD_TYPE_BROWSER_INITIATED_HTTP_POST:
if (!params.url.SchemeIs(kHttpScheme) &&
!params.url.SchemeIs(kHttpsScheme)) {
NOTREACHED() << "Http post load must use http(s) scheme.";
return;
}
break;
case LOAD_TYPE_DATA:
if (!params.url.SchemeIs(chrome::kDataScheme)) {
NOTREACHED() << "Data load must use data scheme.";
return;
}
break;
default:
NOTREACHED();
break;
};
// The user initiated a load, we don't need to reload anymore.
needs_reload_ = false;
bool override = false;
switch (params.override_user_agent) {
case UA_OVERRIDE_INHERIT:
override = ShouldKeepOverride(GetLastCommittedEntry());
break;
case UA_OVERRIDE_TRUE:
override = true;
break;
case UA_OVERRIDE_FALSE:
override = false;
break;
default:
NOTREACHED();
break;
}
NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
CreateNavigationEntry(
params.url,
params.referrer,
params.transition_type,
params.is_renderer_initiated,
params.extra_headers,
browser_context_));
if (params.frame_tree_node_id != -1)
entry->set_frame_tree_node_id(params.frame_tree_node_id);
if (params.redirect_chain.size() > 0)
entry->set_redirect_chain(params.redirect_chain);
if (params.should_replace_current_entry)
entry->set_should_replace_entry(true);
entry->set_should_clear_history_list(params.should_clear_history_list);
entry->SetIsOverridingUserAgent(override);
entry->set_transferred_global_request_id(
params.transferred_global_request_id);
entry->SetFrameToNavigate(params.frame_name);
switch (params.load_type) {
case LOAD_TYPE_DEFAULT:
break;
case LOAD_TYPE_BROWSER_INITIATED_HTTP_POST:
entry->SetHasPostData(true);
entry->SetBrowserInitiatedPostData(
params.browser_initiated_post_data.get());
break;
case LOAD_TYPE_DATA:
entry->SetBaseURLForDataURL(params.base_url_for_data_url);
entry->SetVirtualURL(params.virtual_url_for_data_url);
entry->SetCanLoadLocalResources(params.can_load_local_resources);
break;
default:
NOTREACHED();
break;
};
LoadEntry(entry);
}
bool NavigationControllerImpl::RendererDidNavigate(
const ViewHostMsg_FrameNavigate_Params& params,
LoadCommittedDetails* details) {
is_initial_navigation_ = false;
// Save the previous state before we clobber it.
if (GetLastCommittedEntry()) {
details->previous_url = GetLastCommittedEntry()->GetURL();
details->previous_entry_index = GetLastCommittedEntryIndex();
} else {
details->previous_url = GURL();
details->previous_entry_index = -1;
}
// If we have a pending entry at this point, it should have a SiteInstance.
// Restored entries start out with a null SiteInstance, but we should have
// assigned one in NavigateToPendingEntry.
DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance());
// If we are doing a cross-site reload, we need to replace the existing
// navigation entry, not add another entry to the history. This has the side
// effect of removing forward browsing history, if such existed.
// Or if we are doing a cross-site redirect navigation,
// we will do a similar thing.
details->did_replace_entry =
pending_entry_ && pending_entry_->should_replace_entry();
// Do navigation-type specific actions. These will make and commit an entry.
details->type = ClassifyNavigation(params);
// is_in_page must be computed before the entry gets committed.
details->is_in_page = IsURLInPageNavigation(
params.url, params.was_within_same_page, details->type);
switch (details->type) {
case NAVIGATION_TYPE_NEW_PAGE:
RendererDidNavigateToNewPage(params, details->did_replace_entry);
break;
case NAVIGATION_TYPE_EXISTING_PAGE:
RendererDidNavigateToExistingPage(params);
break;
case NAVIGATION_TYPE_SAME_PAGE:
RendererDidNavigateToSamePage(params);
break;
case NAVIGATION_TYPE_IN_PAGE:
RendererDidNavigateInPage(params, &details->did_replace_entry);
break;
case NAVIGATION_TYPE_NEW_SUBFRAME:
RendererDidNavigateNewSubframe(params);
break;
case NAVIGATION_TYPE_AUTO_SUBFRAME:
if (!RendererDidNavigateAutoSubframe(params))
return false;
break;
case NAVIGATION_TYPE_NAV_IGNORE:
// If a pending navigation was in progress, this canceled it. We should
// discard it and make sure it is removed from the URL bar. After that,
// there is nothing we can do with this navigation, so we just return to
// the caller that nothing has happened.
if (pending_entry_) {
DiscardNonCommittedEntries();
delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
}
return false;
default:
NOTREACHED();
}
// At this point, we know that the navigation has just completed, so
// record the time.
//
// TODO(akalin): Use "sane time" as described in
// http://www.chromium.org/developers/design-documents/sane-time .
base::Time timestamp =
time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
DVLOG(1) << "Navigation finished at (smoothed) timestamp "
<< timestamp.ToInternalValue();
// We should not have a pending entry anymore. Clear it again in case any
// error cases above forgot to do so.
DiscardNonCommittedEntriesInternal();
// All committed entries should have nonempty content state so WebKit doesn't
// get confused when we go back to them (see the function for details).
DCHECK(params.page_state.IsValid());
NavigationEntryImpl* active_entry =
NavigationEntryImpl::FromNavigationEntry(GetLastCommittedEntry());
active_entry->SetTimestamp(timestamp);
active_entry->SetHttpStatusCode(params.http_status_code);
active_entry->SetPageState(params.page_state);
// Once it is committed, we no longer need to track several pieces of state on
// the entry.
active_entry->ResetForCommit();
// The active entry's SiteInstance should match our SiteInstance.
CHECK(active_entry->site_instance() == delegate_->GetSiteInstance());
// Remember the bindings the renderer process has at this point, so that
// we do not grant this entry additional bindings if we come back to it.
active_entry->SetBindings(
delegate_->GetRenderViewHost()->GetEnabledBindings());
// Now prep the rest of the details for the notification and broadcast.
details->entry = active_entry;
details->is_main_frame =
PageTransitionIsMainFrame(params.transition);
details->serialized_security_info = params.security_info;
details->http_status_code = params.http_status_code;
NotifyNavigationEntryCommitted(details);
return true;
}
NavigationType NavigationControllerImpl::ClassifyNavigation(
const ViewHostMsg_FrameNavigate_Params& params) const {
if (params.page_id == -1) {
// The renderer generates the page IDs, and so if it gives us the invalid
// page ID (-1) we know it didn't actually navigate. This happens in a few
// cases:
//
// - If a page makes a popup navigated to about blank, and then writes
// stuff like a subframe navigated to a real page. We'll get the commit
// for the subframe, but there won't be any commit for the outer page.
//
// - We were also getting these for failed loads (for example, bug 21849).
// The guess is that we get a "load commit" for the alternate error page,
// but that doesn't affect the page ID, so we get the "old" one, which
// could be invalid. This can also happen for a cross-site transition
// that causes us to swap processes. Then the error page load will be in
// a new process with no page IDs ever assigned (and hence a -1 value),
// yet the navigation controller still might have previous pages in its
// list.
//
// In these cases, there's nothing we can do with them, so ignore.
return NAVIGATION_TYPE_NAV_IGNORE;
}
if (params.page_id > delegate_->GetMaxPageID()) {
// Greater page IDs than we've ever seen before are new pages. We may or may
// not have a pending entry for the page, and this may or may not be the
// main frame.
if (PageTransitionIsMainFrame(params.transition))
return NAVIGATION_TYPE_NEW_PAGE;
// When this is a new subframe navigation, we should have a committed page
// for which it's a suframe in. This may not be the case when an iframe is
// navigated on a popup navigated to about:blank (the iframe would be
// written into the popup by script on the main page). For these cases,
// there isn't any navigation stuff we can do, so just ignore it.
if (!GetLastCommittedEntry())
return NAVIGATION_TYPE_NAV_IGNORE;
// Valid subframe navigation.
return NAVIGATION_TYPE_NEW_SUBFRAME;
}
// We only clear the session history when navigating to a new page.
DCHECK(!params.history_list_was_cleared);
// Now we know that the notification is for an existing page. Find that entry.
int existing_entry_index = GetEntryIndexWithPageID(
delegate_->GetSiteInstance(),
params.page_id);
if (existing_entry_index == -1) {
// The page was not found. It could have been pruned because of the limit on
// back/forward entries (not likely since we'll usually tell it to navigate
// to such entries). It could also mean that the renderer is smoking crack.
NOTREACHED();
// Because the unknown entry has committed, we risk showing the wrong URL in
// release builds. Instead, we'll kill the renderer process to be safe.
LOG(ERROR) << "terminating renderer for bad navigation: " << params.url;
RecordAction(UserMetricsAction("BadMessageTerminate_NC"));
// Temporary code so we can get more information. Format:
// http://url/foo.html#page1#max3#frame1#ids:2_Nx,1_1x,3_2
std::string temp = params.url.spec();
temp.append("#page");
temp.append(base::IntToString(params.page_id));
temp.append("#max");
temp.append(base::IntToString(delegate_->GetMaxPageID()));
temp.append("#frame");
temp.append(base::IntToString(params.frame_id));
temp.append("#ids");
for (int i = 0; i < static_cast<int>(entries_.size()); ++i) {
// Append entry metadata (e.g., 3_7x):
// 3: page_id
// 7: SiteInstance ID, or N for null
// x: appended if not from the current SiteInstance
temp.append(base::IntToString(entries_[i]->GetPageID()));
temp.append("_");
if (entries_[i]->site_instance())
temp.append(base::IntToString(entries_[i]->site_instance()->GetId()));
else
temp.append("N");
if (entries_[i]->site_instance() != delegate_->GetSiteInstance())
temp.append("x");
temp.append(",");
}
GURL url(temp);
static_cast<RenderViewHostImpl*>(
delegate_->GetRenderViewHost())->Send(
new ViewMsg_TempCrashWithData(url));
return NAVIGATION_TYPE_NAV_IGNORE;
}
NavigationEntryImpl* existing_entry = entries_[existing_entry_index].get();
if (!PageTransitionIsMainFrame(params.transition)) {
// All manual subframes would get new IDs and were handled above, so we
// know this is auto. Since the current page was found in the navigation
// entry list, we're guaranteed to have a last committed entry.
DCHECK(GetLastCommittedEntry());
return NAVIGATION_TYPE_AUTO_SUBFRAME;
}
// Anything below here we know is a main frame navigation.
if (pending_entry_ &&
!pending_entry_->is_renderer_initiated() &&
existing_entry != pending_entry_ &&
pending_entry_->GetPageID() == -1 &&
existing_entry == GetLastCommittedEntry()) {
// In this case, we have a pending entry for a URL but WebCore didn't do a
// new navigation. This happens when you press enter in the URL bar to
// reload. We will create a pending entry, but WebKit will convert it to
// a reload since it's the same page and not create a new entry for it
// (the user doesn't want to have a new back/forward entry when they do
// this). If this matches the last committed entry, we want to just ignore
// the pending entry and go back to where we were (the "existing entry").
return NAVIGATION_TYPE_SAME_PAGE;
}
// Any toplevel navigations with the same base (minus the reference fragment)
// are in-page navigations. We weeded out subframe navigations above. Most of
// the time this doesn't matter since WebKit doesn't tell us about subframe
// navigations that don't actually navigate, but it can happen when there is
// an encoding override (it always sends a navigation request).
if (AreURLsInPageNavigation(existing_entry->GetURL(), params.url,
params.was_within_same_page,
NAVIGATION_TYPE_UNKNOWN)) {
return NAVIGATION_TYPE_IN_PAGE;
}
// Since we weeded out "new" navigations above, we know this is an existing
// (back/forward) navigation.
return NAVIGATION_TYPE_EXISTING_PAGE;
}
void NavigationControllerImpl::RendererDidNavigateToNewPage(
const ViewHostMsg_FrameNavigate_Params& params, bool replace_entry) {
NavigationEntryImpl* new_entry;
bool update_virtual_url;
// Only make a copy of the pending entry if it is appropriate for the new page
// that was just loaded. We verify this at a coarse grain by checking that
// the SiteInstance hasn't been assigned to something else.
if (pending_entry_ &&
(!pending_entry_->site_instance() ||
pending_entry_->site_instance() == delegate_->GetSiteInstance())) {
new_entry = new NavigationEntryImpl(*pending_entry_);
// Don't use the page type from the pending entry. Some interstitial page
// may have set the type to interstitial. Once we commit, however, the page
// type must always be normal.
new_entry->set_page_type(PAGE_TYPE_NORMAL);
update_virtual_url = new_entry->update_virtual_url_with_url();
} else {
new_entry = new NavigationEntryImpl;
// Find out whether the new entry needs to update its virtual URL on URL
// change and set up the entry accordingly. This is needed to correctly
// update the virtual URL when replaceState is called after a pushState.
GURL url = params.url;
bool needs_update = false;
BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
&url, browser_context_, &needs_update);
new_entry->set_update_virtual_url_with_url(needs_update);
// When navigating to a new page, give the browser URL handler a chance to