/
tab_contents.cc
2359 lines (2033 loc) · 86 KB
/
tab_contents.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 (c) 2009 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/tab_contents/tab_contents.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/file_version_info.h"
#include "base/process_util.h"
#include "base/string16.h"
#include "base/time.h"
#include "chrome/browser/autofill_manager.h"
#include "chrome/browser/blocked_popup_container.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/cert_store.h"
#include "chrome/browser/debugger/devtools_manager.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/dom_ui/dom_ui.h"
#include "chrome/browser/dom_ui/dom_ui_factory.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/download_request_manager.h"
#include "chrome/browser/gears_integration.h"
#include "chrome/browser/google_util.h"
#include "chrome/browser/hung_renderer_dialog.h"
#include "chrome/browser/jsmessage_box_handler.h"
#include "chrome/browser/load_from_memory_cache_details.h"
#include "chrome/browser/load_notification_details.h"
#include "chrome/browser/password_manager/password_manager.h"
#include "chrome/browser/plugin_installer.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#include "chrome/browser/renderer_host/resource_request_details.h"
#include "chrome/browser/renderer_host/web_cache_manager.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/interstitial_page.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/provisional_load_details.h"
#include "chrome/browser/tab_contents/tab_contents_delegate.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/browser/tab_contents/thumbnail_generator.h"
#include "chrome/browser/thumbnail_store.h"
#include "chrome/browser/search_engines/template_url_fetcher.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/page_action.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/renderer_preferences.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "net/base/mime_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domain.h"
#include "webkit/glue/password_form.h"
#include "webkit/glue/webpreferences.h"
#if defined(OS_WIN)
// For CRect
#include <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
// TODO(port): some of these headers should be ported.
#include "chrome/browser/modal_html_dialog_delegate.h"
#include "views/controls/scrollbar/native_scroll_bar.h"
#endif
// Cross-Site Navigations
//
// If a TabContents is told to navigate to a different web site (as determined
// by SiteInstance), it will replace its current RenderViewHost with a new
// RenderViewHost dedicated to the new SiteInstance. This works as follows:
//
// - Navigate determines whether the destination is cross-site, and if so,
// it creates a pending_render_view_host_ and moves into the PENDING
// RendererState.
// - The pending RVH is "suspended," so that no navigation messages are sent to
// its renderer until the onbeforeunload JavaScript handler has a chance to
// run in the current RVH.
// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
// that it has a pending cross-site request. ResourceDispatcherHost will
// check for this when the response arrives.
// - The current RVH runs its onbeforeunload handler. If it returns false, we
// cancel all the pending logic and go back to NORMAL. Otherwise we allow
// the pending RVH to send the navigation request to its renderer.
// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
// checks CrossSiteRequestManager to see that the RVH responsible has a
// pending cross-site request, and then installs a CrossSiteEventHandler.
// - When RDH receives a response, the BufferedEventHandler determines whether
// it is a download. If so, it sends a message to the new renderer causing
// it to cancel the request, and the download proceeds in the download
// thread. For now, we stay in a PENDING state (with a pending RVH) until
// the next DidNavigate event for this TabContents. This isn't ideal, but it
// doesn't affect any functionality.
// - After RDH receives a response and determines that it is safe and not a
// download, it pauses the response to first run the old page's onunload
// handler. It does this by asynchronously calling the OnCrossSiteResponse
// method of TabContents on the UI thread, which sends a ClosePage message
// to the current RVH.
// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
// the ResourceDispatcherHost, who unpauses the response. Data is then sent
// to the pending RVH.
// - The pending renderer sends a FrameNavigate message that invokes the
// DidNavigate method. This replaces the current RVH with the
// pending RVH and goes back to the NORMAL RendererState.
namespace {
// Amount of time we wait between when a key event is received and the renderer
// is queried for its state and pushed to the NavigationEntry.
const int kQueryStateDelay = 5000;
const int kSyncWaitDelay = 40;
// If another javascript message box is displayed within
// kJavascriptMessageExpectedDelay of a previous javascript message box being
// dismissed, display an option to suppress future message boxes from this
// contents.
const int kJavascriptMessageExpectedDelay = 1000;
const char kLinkDoctorBaseURL[] =
"http://linkhelp.clients.google.com/tbproxy/lh/fixurl";
// The list of prefs we want to observe.
const wchar_t* kPrefsToObserve[] = {
prefs::kAlternateErrorPagesEnabled,
prefs::kWebKitJavaEnabled,
prefs::kWebKitJavascriptEnabled,
prefs::kWebKitLoadsImagesAutomatically,
prefs::kWebKitPluginsEnabled,
prefs::kWebKitUsesUniversalDetector,
prefs::kWebKitSerifFontFamily,
prefs::kWebKitSansSerifFontFamily,
prefs::kWebKitFixedFontFamily,
prefs::kWebKitDefaultFontSize,
prefs::kWebKitDefaultFixedFontSize,
prefs::kDefaultCharset
// kWebKitStandardFontIsSerif needs to be added
// if we let users pick which font to use, serif or sans-serif when
// no font is specified or a CSS generic family (serif or sans-serif)
// is not specified.
};
const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
// Returns true if the entry's transition type is FORM_SUBMIT.
bool IsFormSubmit(const NavigationEntry* entry) {
return (PageTransition::StripQualifier(entry->transition_type()) ==
PageTransition::FORM_SUBMIT);
}
#if defined(OS_WIN)
BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
// Note: erase is required to properly paint some widgets borders. This can
// be seen with textfields.
InvalidateRect(hwnd, NULL, TRUE);
return TRUE;
}
#endif
void MakeNavigateParams(const NavigationEntry& entry, bool reload,
ViewMsg_Navigate_Params* params) {
params->page_id = entry.page_id();
params->url = entry.url();
params->referrer = entry.referrer();
params->transition = entry.transition_type();
params->state = entry.content_state();
params->reload = reload;
params->request_time = base::Time::Now();
}
} // namespace
// -----------------------------------------------------------------------------
// static
int TabContents::find_request_id_counter_ = -1;
class TabContents::GearsCreateShortcutCallbackFunctor {
public:
explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
: contents_(contents) {}
void Run(const GearsShortcutData2& shortcut_data, bool success) {
if (contents_)
contents_->OnGearsCreateShortcutDone(shortcut_data, success);
delete this;
}
void Cancel() {
contents_ = NULL;
}
private:
TabContents* contents_;
};
TabContents::TabContents(Profile* profile,
SiteInstance* site_instance,
int routing_id,
base::WaitableEvent* modal_dialog_event)
: delegate_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
ALLOW_THIS_IN_INITIALIZER_LIST(view_(
TabContentsView::Create(this))),
ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
property_bag_(),
registrar_(),
ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
save_package_(),
cancelable_consumer_(),
autofill_manager_(),
password_manager_(),
plugin_installer_(),
ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
select_file_dialog_(),
pending_install_(),
is_loading_(false),
is_crashed_(false),
waiting_for_response_(false),
max_page_id_(-1),
current_load_start_(),
load_state_(net::LOAD_STATE_IDLE),
load_state_host_(),
received_page_title_(false),
is_starred_(false),
contents_mime_type_(),
encoding_(),
blocked_popups_(NULL),
infobar_delegates_(),
find_ui_active_(false),
find_op_aborted_(false),
current_find_request_id_(find_request_id_counter_++),
find_text_(),
last_search_case_sensitive_(false),
last_search_prepopulate_text_(NULL),
last_search_result_(),
capturing_contents_(false),
is_being_destroyed_(false),
notify_disconnection_(false),
history_requests_(),
#if defined(OS_WIN)
message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
#endif
last_javascript_message_dismissal_(),
suppress_javascript_messages_(false) {
pending_install_.page_id = 0;
pending_install_.callback_functor = NULL;
#if defined(OS_CHROMEOS)
// Make sure the thumbnailer is started before starting the render manager.
// The thumbnailer will want to listen for RVH creations, one of which will
// happen in RVHManager::Init.
ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
if (generator)
generator->StartThumbnailing();
#endif
render_manager_.Init(profile, site_instance, routing_id, modal_dialog_event);
view_->CreateView();
// Register for notifications about all interested prefs change.
PrefService* prefs = profile->GetPrefs();
if (prefs) {
for (int i = 0; i < kPrefsToObserveLength; ++i)
prefs->AddPrefObserver(kPrefsToObserve[i], this);
}
// Register for notifications about URL starredness changing on any profile.
registrar_.Add(this, NotificationType::URLS_STARRED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
NotificationService::AllSources());
// Keep a global copy of the previous search string (if any).
static string16 global_last_search = string16();
last_search_prepopulate_text_ = &global_last_search;
}
TabContents::~TabContents() {
is_being_destroyed_ = true;
// We don't want any notifications while we're runnign our destructor.
registrar_.RemoveAll();
// Unregister the notifications of all observed prefs change.
PrefService* prefs = profile()->GetPrefs();
if (prefs) {
for (int i = 0; i < kPrefsToObserveLength; ++i)
prefs->RemovePrefObserver(kPrefsToObserve[i], this);
}
// Clean up subwindows like plugins and the find in page bar.
view_->OnContentsDestroy();
NotifyDisconnected();
HungRendererDialog::HideForTabContents(this);
if (pending_install_.callback_functor)
pending_install_.callback_functor->Cancel();
// First cleanly close all child windows.
// TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
// some of these to close. CloseWindows is async, so it might get called
// twice before it runs.
int size = static_cast<int>(child_windows_.size());
for (int i = size - 1; i >= 0; --i) {
ConstrainedWindow* window = child_windows_[i];
if (window)
window->CloseConstrainedWindow();
}
if (blocked_popups_)
blocked_popups_->Destroy();
// Notify any observer that have a reference on this tab contents.
NotificationService::current()->Notify(
NotificationType::TAB_CONTENTS_DESTROYED,
Source<TabContents>(this),
NotificationService::NoDetails());
// Notify any lasting InfobarDelegates that have not yet been removed that
// whatever infobar they were handling in this TabContents has closed,
// because the TabContents is going away entirely.
// This must happen after the TAB_CONTENTS_DESTROYED notification as the
// notification may trigger infobars calls that access their delegate. (and
// some implementations of InfoBarDelegate do delete themselves on
// InfoBarClosed()).
for (int i = 0; i < infobar_delegate_count(); ++i) {
InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
delegate->InfoBarClosed();
}
infobar_delegates_.clear();
// TODO(brettw) this should be moved to the view.
#if defined(OS_WIN)
// If we still have a window handle, destroy it. GetNativeView can return
// NULL if this contents was part of a window that closed.
if (GetNativeView())
::DestroyWindow(GetNativeView());
#endif
}
// static
void TabContents::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
WebPreferences pref_defaults;
prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
pref_defaults.javascript_enabled);
prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
pref_defaults.web_security_enabled);
prefs->RegisterBooleanPref(
prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
pref_defaults.loads_images_automatically);
prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
pref_defaults.plugins_enabled);
prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
pref_defaults.dom_paste_enabled);
prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
pref_defaults.shrinks_standalone_images_to_fit);
prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
pref_defaults.inspector_settings);
prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
pref_defaults.text_areas_are_resizable);
prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
pref_defaults.java_enabled);
prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
IDS_ACCEPT_LANGUAGES);
prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
IDS_DEFAULT_ENCODING);
prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
IDS_STANDARD_FONT_IS_SERIF);
prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
IDS_FIXED_FONT_FAMILY);
prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
IDS_SERIF_FONT_FAMILY);
prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
IDS_SANS_SERIF_FONT_FAMILY);
prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
IDS_CURSIVE_FONT_FAMILY);
prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
IDS_FANTASY_FONT_FAMILY);
prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
IDS_DEFAULT_FONT_SIZE);
prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
IDS_DEFAULT_FIXED_FONT_SIZE);
prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
IDS_MINIMUM_FONT_SIZE);
prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
IDS_MINIMUM_LOGICAL_FONT_SIZE);
prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
IDS_USES_UNIVERSAL_DETECTOR);
prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
IDS_STATIC_ENCODING_LIST);
}
// Returns true if contains content rendered by an extension.
bool TabContents::HostsExtension() const {
return GetURL().SchemeIs(chrome::kExtensionScheme);
}
PasswordManager* TabContents::GetPasswordManager() {
if (password_manager_.get() == NULL)
password_manager_.reset(new PasswordManager(this));
return password_manager_.get();
}
PluginInstaller* TabContents::GetPluginInstaller() {
if (plugin_installer_.get() == NULL)
plugin_installer_.reset(new PluginInstaller(this));
return plugin_installer_.get();
}
const GURL& TabContents::GetURL() const {
// We may not have a navigation entry yet
NavigationEntry* entry = controller_.GetActiveEntry();
return entry ? entry->display_url() : GURL::EmptyGURL();
}
const string16& TabContents::GetTitle() const {
// Transient entries take precedence. They are used for interstitial pages
// that are shown on top of existing pages.
NavigationEntry* entry = controller_.GetTransientEntry();
if (entry)
return entry->GetTitleForDisplay(&controller_);
DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
render_manager_.pending_dom_ui() : render_manager_.dom_ui();
if (our_dom_ui) {
// Don't override the title in view source mode.
entry = controller_.GetActiveEntry();
if (!(entry && entry->IsViewSourceMode())) {
// Give the DOM UI the chance to override our title.
const string16& title = our_dom_ui->overridden_title();
if (!title.empty())
return title;
}
}
// We use the title for the last committed entry rather than a pending
// navigation entry. For example, when the user types in a URL, we want to
// keep the old page's title until the new load has committed and we get a new
// title.
entry = controller_.GetLastCommittedEntry();
if (entry)
return entry->GetTitleForDisplay(&controller_);
else if (controller_.LoadingURLLazily())
return controller_.GetLazyTitle();
return EmptyString16();
}
int32 TabContents::GetMaxPageID() {
if (GetSiteInstance())
return GetSiteInstance()->max_page_id();
else
return max_page_id_;
}
void TabContents::UpdateMaxPageID(int32 page_id) {
// Ensure both the SiteInstance and RenderProcessHost update their max page
// IDs in sync. Only TabContents will also have site instances, except during
// testing.
if (GetSiteInstance())
GetSiteInstance()->UpdateMaxPageID(page_id);
process()->UpdateMaxPageID(page_id);
}
SiteInstance* TabContents::GetSiteInstance() const {
return render_manager_.current_host()->site_instance();
}
const std::wstring TabContents::GetDefaultTitle() const {
return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
}
bool TabContents::ShouldDisplayURL() {
// Don't hide the url in view source mode.
NavigationEntry* entry = controller_.GetActiveEntry();
if (entry && entry->IsViewSourceMode())
return true;
DOMUI* dom_ui = GetDOMUIForCurrentState();
if (dom_ui)
return !dom_ui->should_hide_url();
return true;
}
SkBitmap TabContents::GetFavIcon() const {
// Like GetTitle(), we also want to use the favicon for the last committed
// entry rather than a pending navigation entry.
NavigationEntry* entry = controller_.GetTransientEntry();
if (entry)
return entry->favicon().bitmap();
entry = controller_.GetLastCommittedEntry();
if (entry)
return entry->favicon().bitmap();
else if (controller_.LoadingURLLazily())
return controller_.GetLazyFavIcon();
return SkBitmap();
}
bool TabContents::ShouldDisplayFavIcon() {
// Always display a throbber during pending loads.
if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
return true;
DOMUI* dom_ui = GetDOMUIForCurrentState();
if (dom_ui)
return !dom_ui->hide_favicon();
return true;
}
std::wstring TabContents::GetStatusText() const {
if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
return std::wstring();
switch (load_state_) {
case net::LOAD_STATE_WAITING_FOR_CACHE:
return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
case net::LOAD_STATE_RESOLVING_HOST:
return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
case net::LOAD_STATE_CONNECTING:
return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
case net::LOAD_STATE_SENDING_REQUEST:
return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
case net::LOAD_STATE_WAITING_FOR_RESPONSE:
return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
load_state_host_);
// Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
case net::LOAD_STATE_IDLE:
case net::LOAD_STATE_READING_RESPONSE:
break;
}
return std::wstring();
}
void TabContents::SetIsCrashed(bool state) {
if (state == is_crashed_)
return;
is_crashed_ = state;
NotifyNavigationStateChanged(INVALIDATE_TAB);
}
void TabContents::SetPageActionEnabled(const PageAction* page_action,
bool enable,
const std::string& title,
int icon_id) {
DCHECK(page_action);
if (!enable &&
enabled_page_actions_.end() == enabled_page_actions_.find(page_action)) {
return; // Don't need to disable twice.
}
if (enable) {
enabled_page_actions_[page_action].reset(
new PageActionState(title, icon_id));
} else {
enabled_page_actions_.erase(page_action);
}
}
const PageActionState* TabContents::GetPageActionState(
const PageAction* page_action) {
if (enabled_page_actions_.end() == enabled_page_actions_.find(page_action))
return NULL;
return enabled_page_actions_[page_action].get();
}
void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
if (delegate_)
delegate_->NavigationStateChanged(this, changed_flags);
}
void TabContents::DidBecomeSelected() {
controller_.SetActive(true);
if (render_widget_host_view())
render_widget_host_view()->DidBecomeSelected();
// If pid() is -1, that means the RenderProcessHost still hasn't been
// initialized. It'll register with CacheManagerHost when it is.
if (process()->pid() != -1)
WebCacheManager::GetInstance()->ObserveActivity(process()->pid());
}
void TabContents::WasHidden() {
if (!capturing_contents()) {
// |render_view_host()| can be NULL if the user middle clicks a link to open
// a tab in then background, then closes the tab before selecting it. This
// is because closing the tab calls TabContents::Destroy(), which removes
// the |render_view_host()|; then when we actually destroy the window,
// OnWindowPosChanged() notices and calls HideContents() (which calls us).
if (render_widget_host_view())
render_widget_host_view()->WasHidden();
}
NotificationService::current()->Notify(
NotificationType::TAB_CONTENTS_HIDDEN,
Source<TabContents>(this),
NotificationService::NoDetails());
}
void TabContents::Activate() {
if (delegate_)
delegate_->ActivateContents(this);
}
void TabContents::ShowContents() {
if (render_widget_host_view())
render_widget_host_view()->DidBecomeSelected();
}
void TabContents::HideContents() {
// TODO(pkasting): http://b/1239839 Right now we purposefully don't call
// our superclass HideContents(), because some callers want to be very picky
// about the order in which these get called. In addition to making the code
// here practically impossible to understand, this also means we end up
// calling TabContents::WasHidden() twice if callers call both versions of
// HideContents() on a TabContents.
WasHidden();
}
void TabContents::OpenURL(const GURL& url, const GURL& referrer,
WindowOpenDisposition disposition,
PageTransition::Type transition) {
if (delegate_)
delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
}
bool TabContents::NavigateToPendingEntry(bool reload) {
const NavigationEntry& entry = *controller_.pending_entry();
RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
if (!dest_render_view_host)
return false; // Unable to create the desired render view host.
// Tell DevTools agent that it is attached prior to the navigation.
DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
if (devtools_manager) { // NULL in unit tests.
devtools_manager->OnNavigatingToPendingEntry(
render_view_host(),
dest_render_view_host,
controller_.pending_entry()->url());
}
// Used for page load time metrics.
current_load_start_ = base::TimeTicks::Now();
// Navigate in the desired RenderViewHost.
ViewMsg_Navigate_Params navigate_params;
MakeNavigateParams(entry, reload, &navigate_params);
dest_render_view_host->Navigate(navigate_params);
if (entry.page_id() == -1) {
// HACK!! This code suppresses javascript: URLs from being added to
// session history, which is what we want to do for javascript: URLs that
// do not generate content. What we really need is a message from the
// renderer telling us that a new page was not created. The same message
// could be used for mailto: URLs and the like.
if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
return false;
}
// Clear any provisional password saves - this stops password infobars
// showing up on pages the user navigates to while the right page is
// loading.
GetPasswordManager()->ClearProvisionalSave();
if (reload && !profile()->IsOffTheRecord()) {
HistoryService* history =
profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
if (history)
history->SetFavIconOutOfDateForPage(entry.url());
}
return true;
}
void TabContents::Stop() {
render_manager_.Stop();
printing_.Stop();
}
void TabContents::Cut() {
render_view_host()->Cut();
}
void TabContents::Copy() {
render_view_host()->Copy();
}
void TabContents::Paste() {
render_view_host()->Paste();
}
void TabContents::DisassociateFromPopupCount() {
render_view_host()->DisassociateFromPopupCount();
}
TabContents* TabContents::Clone() {
// We create a new SiteInstance so that the new tab won't share processes
// with the old one. This can be changed in the future if we need it to share
// processes for some reason.
TabContents* tc = new TabContents(profile(),
SiteInstance::CreateSiteInstance(profile()),
MSG_ROUTING_NONE, NULL);
tc->controller().CopyStateFrom(controller_);
return tc;
}
void TabContents::CreateShortcut() {
NavigationEntry* entry = controller_.GetLastCommittedEntry();
if (!entry)
return;
// We only allow one pending install request. By resetting the page id we
// effectively cancel the pending install request.
pending_install_.page_id = entry->page_id();
pending_install_.icon = GetFavIcon();
pending_install_.title = UTF16ToWideHack(GetTitle());
pending_install_.url = GetURL();
if (pending_install_.callback_functor) {
pending_install_.callback_functor->Cancel();
pending_install_.callback_functor = NULL;
}
DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
if (pending_install_.title.empty())
pending_install_.title = UTF8ToWide(GetURL().spec());
// Request the application info. When done OnDidGetApplicationInfo is invoked
// and we'll create the shortcut.
render_view_host()->GetApplicationInfo(pending_install_.page_id);
}
#if defined(OS_WIN) || defined(OS_LINUX)
ConstrainedWindow* TabContents::CreateConstrainedDialog(
ConstrainedWindowDelegate* delegate) {
ConstrainedWindow* window =
ConstrainedWindow::CreateConstrainedDialog(this, delegate);
child_windows_.push_back(window);
return window;
}
#endif
void TabContents::AddNewContents(TabContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture,
const GURL& creator_url) {
if (!delegate_)
return;
if ((disposition == NEW_POPUP) && !user_gesture &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisablePopupBlocking)) {
// Unrequested popups from normal pages are constrained unless they're in
// the whitelist. The popup owner will handle checking this.
delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
initial_pos,
creator_url.is_valid() ? creator_url.host() : std::string());
} else {
new_contents->DisassociateFromPopupCount();
delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
user_gesture);
}
PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
}
void TabContents::CloseAllSuppressedPopups() {
if (blocked_popups_)
blocked_popups_->CloseAll();
}
void TabContents::PopupNotificationVisibilityChanged(bool visible) {
render_view_host()->PopupNotificationVisibilityChanged(visible);
}
gfx::NativeView TabContents::GetContentNativeView() {
return view_->GetContentNativeView();
}
gfx::NativeView TabContents::GetNativeView() const {
return view_->GetNativeView();
}
void TabContents::GetContainerBounds(gfx::Rect *out) const {
view_->GetContainerBounds(out);
}
void TabContents::Focus() {
view_->Focus();
}
void TabContents::FocusThroughTabTraversal(bool reverse) {
if (showing_interstitial_page()) {
render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
return;
}
render_view_host()->SetInitialFocus(reverse);
}
bool TabContents::FocusLocationBarByDefault() {
DOMUI* dom_ui = GetDOMUIForCurrentState();
if (dom_ui)
return dom_ui->focus_location_bar_by_default();
NavigationEntry* entry = controller_.GetActiveEntry();
if (entry && entry->url() == GURL("about:blank"))
return true;
return false;
}
void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
// Look through the existing InfoBarDelegates we have for a match. If we've
// already got one that matches, then we don't add the new one.
for (int i = 0; i < infobar_delegate_count(); ++i) {
if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
return;
}
infobar_delegates_.push_back(delegate);
NotificationService::current()->Notify(
NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
Source<TabContents>(this),
Details<InfoBarDelegate>(delegate));
// Add ourselves as an observer for navigations the first time a delegate is
// added. We use this notification to expire InfoBars that need to expire on
// page transitions.
if (infobar_delegates_.size() == 1) {
registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
Source<NavigationController>(&controller_));
}
}
void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
std::vector<InfoBarDelegate*>::iterator it =
find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
if (it != infobar_delegates_.end()) {
InfoBarDelegate* delegate = *it;
NotificationService::current()->Notify(
NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
Source<TabContents>(this),
Details<InfoBarDelegate>(delegate));
infobar_delegates_.erase(it);
// Remove ourselves as an observer if we are tracking no more InfoBars.
if (infobar_delegates_.empty()) {
registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
Source<NavigationController>(&controller_));
}
}
}
bool TabContents::IsBookmarkBarAlwaysVisible() {
// See GetDOMUIForCurrentState() comment for more info. This case is very
// similar, but for non-first loads, we want to use the committed entry. This
// is so the bookmarks bar disappears at the same time the page does.
if (controller_.GetLastCommittedEntry()) {
// Not the first load, always use the committed DOM UI.
if (render_manager_.dom_ui())
return render_manager_.dom_ui()->force_bookmark_bar_visible();
return false; // Default.
}
// When it's the first load, we know either the pending one or the committed
// one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
// of them will be valid, so we can just check both.
if (render_manager_.pending_dom_ui())
return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
if (render_manager_.dom_ui())
return render_manager_.dom_ui()->force_bookmark_bar_visible();
return false; // Default.
}
void TabContents::ToolbarSizeChanged(bool is_animating) {
TabContentsDelegate* d = delegate();
if (d)
d->ToolbarSizeChanged(this, is_animating);
}
void TabContents::OnStartDownload(DownloadItem* download) {
DCHECK(download);
// Download in a constrained popup is shown in the tab that opened it.
TabContents* tab_contents = delegate()->GetConstrainingContents(this);
if (tab_contents && tab_contents->delegate())
tab_contents->delegate()->OnStartDownload(download);
}
void TabContents::WillClose(ConstrainedWindow* window) {
ConstrainedWindowList::iterator it =
find(child_windows_.begin(), child_windows_.end(), window);
if (it != child_windows_.end())
child_windows_.erase(it);
}
void TabContents::WillCloseBlockedPopupContainer(
BlockedPopupContainer* container) {
DCHECK(blocked_popups_ == container);
blocked_popups_ = NULL;
}
void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
#if defined(OS_WIN)
UpdateWindow(GetNativeView());
#endif
}
void TabContents::StartFinding(string16 find_text,
bool forward_direction,
bool case_sensitive) {
// If find_text is empty, it means FindNext was pressed with a keyboard
// shortcut so unless we have something to search for we return early.
if (find_text.empty() && find_text_.empty()) {
if (last_search_prepopulate_text_->empty())
return;
// Try whatever we searched for last in any tab.
find_text = *last_search_prepopulate_text_;
}
// This is a FindNext operation if we are searching for the same text again,
// or if the passed in search text is empty (FindNext keyboard shortcut). The
// exception to this is if the Find was aborted (then we don't want FindNext
// because the highlighting has been cleared and we need it to reappear). We
// therefore treat FindNext after an aborted Find operation as a full fledged
// Find.
bool find_next = (find_text_ == find_text || find_text.empty()) &&
(last_search_case_sensitive_ == case_sensitive) &&
!find_op_aborted_;
if (!find_next)
current_find_request_id_ = find_request_id_counter_++;
if (!find_text.empty())
find_text_ = find_text;
last_search_case_sensitive_ = case_sensitive;
find_op_aborted_ = false;
// Keep track of what the last search was across the tabs.
*last_search_prepopulate_text_ = find_text;
render_view_host()->StartFinding(current_find_request_id_,
find_text_,
forward_direction,
case_sensitive,
find_next);
}
void TabContents::StopFinding(bool clear_selection) {
// When |clear_selection| is true, it means the find string has been cleared
// by the user, but the UI has not been dismissed.
if (!clear_selection)
find_ui_active_ = false;
find_op_aborted_ = true;
last_search_result_ = FindNotificationDetails();
render_view_host()->StopFinding(clear_selection);
}
void TabContents::GetPageLanguage() {
render_view_host()->GetPageLanguage();
}
void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
bool success,
const std::wstring& prompt) {
last_javascript_message_dismissal_ = base::TimeTicks::Now();
render_manager_.OnJavaScriptMessageBoxClosed(reply_msg, success, prompt);
}
void TabContents::OnJavaScriptMessageBoxWindowDestroyed() {
render_manager_.OnJavaScriptMessageBoxWindowDestroyed();
}
void TabContents::OnSavePage() {
// If we can not save the page, try to download it.
if (!SavePackage::IsSavableContents(contents_mime_type())) {
DownloadManager* dlm = profile()->GetDownloadManager();
const GURL& current_page_url = GetURL();
if (dlm && current_page_url.is_valid())
dlm->DownloadUrl(current_page_url, GURL(), "", this);