1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/tab_helpers.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/content_settings/chrome_content_settings_client.h"
10 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
11 #include "chrome/browser/favicon/favicon_tab_helper.h"
12 #include "chrome/browser/history/history_tab_helper.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/net/net_error_tab_helper.h"
15 #include "chrome/browser/net/predictor_tab_helper.h"
16 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
18 #include "chrome/browser/predictors/resource_prefetch_predictor_tab_helper.h"
19 #include "chrome/browser/prerender/prerender_tab_helper.h"
20 #include "chrome/browser/sessions/session_tab_helper.h"
21 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h"
22 #include "chrome/browser/translate/chrome_translate_client.h"
23 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
24 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
25 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
26 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
27 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
28 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
29 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h"
30 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
31 #include "chrome/browser/ui/search/search_tab_helper.h"
32 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
33 #include "chrome/browser/ui/tab_dialogs.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
36 #include "components/autofill/core/browser/autofill_manager.h"
37 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
38 #include "components/password_manager/core/browser/password_manager.h"
39 #include "content/public/browser/web_contents.h"
41 #if defined(OS_ANDROID)
42 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h"
43 #include "chrome/browser/enhanced_bookmarks/android/enhanced_bookmark_tab_helper.h"
44 #include "chrome/browser/ui/android/context_menu_helper.h"
45 #include "chrome/browser/ui/android/window_android_helper.h"
47 #include "chrome/browser/external_protocol/external_protocol_observer.h"
48 #include "chrome/browser/plugins/plugin_observer.h"
49 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
50 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
51 #include "chrome/browser/ui/hung_plugin_tab_helper.h"
52 #include "chrome/browser/ui/sad_tab_helper.h"
53 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
54 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
55 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
56 #include "components/pdf/browser/pdf_web_contents_helper.h"
57 #include "components/ui/zoom/zoom_controller.h"
58 #include "components/web_modal/web_contents_modal_dialog_manager.h"
59 #endif // defined(OS_ANDROID)
62 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
65 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
66 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
69 #if defined(ENABLE_EXTENSIONS)
70 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
71 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
72 #include "chrome/browser/extensions/tab_helper.h"
73 #include "extensions/browser/view_type_utils.h"
76 #if defined(ENABLE_SUPERVISED_USERS)
77 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
80 #if defined(ENABLE_PRINTING)
81 #if defined(ENABLE_PRINT_PREVIEW)
82 #include "chrome/browser/printing/print_preview_message_handler.h"
83 #include "chrome/browser/printing/print_view_manager.h"
85 #include "chrome/browser/printing/print_view_manager_basic.h"
86 #endif // defined(ENABLE_PRINT_PREVIEW)
87 #endif // defined(ENABLE_PRINTING)
89 using content::WebContents
;
93 const char kTabContentsAttachedTabHelpersUserDataKey
[] =
94 "TabContentsAttachedTabHelpers";
99 void TabHelpers::AttachTabHelpers(WebContents
* web_contents
) {
100 // If already adopted, nothing to be done.
101 base::SupportsUserData::Data
* adoption_tag
=
102 web_contents
->GetUserData(&kTabContentsAttachedTabHelpersUserDataKey
);
107 web_contents
->SetUserData(&kTabContentsAttachedTabHelpersUserDataKey
,
108 new base::SupportsUserData::Data());
110 #if defined(ENABLE_EXTENSIONS)
111 // Set the view type.
112 extensions::SetViewType(web_contents
, extensions::VIEW_TYPE_TAB_CONTENTS
);
115 // Create all the tab helpers.
117 // SessionTabHelper comes first because it sets up the tab ID, and other
118 // helpers may rely on that.
119 SessionTabHelper::CreateForWebContents(web_contents
);
120 #if !defined(OS_ANDROID)
121 // ZoomController comes before common tab helpers since ChromeAutofillClient
122 // may want to register as a ZoomObserver with it.
123 ui_zoom::ZoomController::CreateForWebContents(web_contents
);
126 // --- Common tab helpers ---
128 autofill::ChromeAutofillClient::CreateForWebContents(web_contents
);
129 autofill::ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
131 autofill::ChromeAutofillClient::FromWebContents(web_contents
),
132 g_browser_process
->GetApplicationLocale(),
133 autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
);
134 BookmarkTabHelper::CreateForWebContents(web_contents
);
135 chrome_browser_net::NetErrorTabHelper::CreateForWebContents(web_contents
);
136 chrome_browser_net::PredictorTabHelper::CreateForWebContents(web_contents
);
137 ChromeContentSettingsClient::CreateForWebContents(web_contents
);
138 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
140 autofill::ChromeAutofillClient::FromWebContents(web_contents
));
141 ChromeTranslateClient::CreateForWebContents(web_contents
);
142 CoreTabHelper::CreateForWebContents(web_contents
);
143 FaviconTabHelper::CreateForWebContents(web_contents
);
144 FindTabHelper::CreateForWebContents(web_contents
);
145 HistoryTabHelper::CreateForWebContents(web_contents
);
146 InfoBarService::CreateForWebContents(web_contents
);
147 ManagePasswordsUIController::CreateForWebContents(web_contents
);
148 NavigationCorrectionTabObserver::CreateForWebContents(web_contents
);
149 NavigationMetricsRecorder::CreateForWebContents(web_contents
);
150 PopupBlockerTabHelper::CreateForWebContents(web_contents
);
151 PrefsTabHelper::CreateForWebContents(web_contents
);
152 prerender::PrerenderTabHelper::CreateForWebContents(web_contents
);
153 SearchTabHelper::CreateForWebContents(web_contents
);
154 // TODO(vabr): Remove TabSpecificContentSettings from here once their function
155 // is taken over by ChromeContentSettingsClient. http://crbug.com/387075
156 TabSpecificContentSettings::CreateForWebContents(web_contents
);
158 // --- Platform-specific tab helpers ---
160 #if defined(OS_ANDROID)
161 ContextMenuHelper::CreateForWebContents(web_contents
);
162 EnhancedBookmarkTabHelper::CreateForWebContents(web_contents
);
163 SingleTabModeTabHelper::CreateForWebContents(web_contents
);
164 WindowAndroidHelper::CreateForWebContents(web_contents
);
166 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
168 extensions::WebNavigationTabObserver::CreateForWebContents(web_contents
);
169 ExternalProtocolObserver::CreateForWebContents(web_contents
);
170 HungPluginTabHelper::CreateForWebContents(web_contents
);
171 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
173 scoped_ptr
<pdf::PDFWebContentsHelperClient
>(
174 new ChromePDFWebContentsHelperClient()));
175 PermissionBubbleManager::CreateForWebContents(web_contents
);
176 PluginObserver::CreateForWebContents(web_contents
);
177 SadTabHelper::CreateForWebContents(web_contents
);
178 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(web_contents
);
179 SearchEngineTabHelper::CreateForWebContents(web_contents
);
180 TabContentsSyncedTabDelegate::CreateForWebContents(web_contents
);
181 TabDialogs::CreateForWebContents(web_contents
);
182 ThumbnailTabHelper::CreateForWebContents(web_contents
);
183 web_modal::WebContentsModalDialogManager::CreateForWebContents(web_contents
);
187 MetroPinTabHelper::CreateForWebContents(web_contents
);
190 // --- Feature tab helpers behind flags ---
192 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
193 CaptivePortalTabHelper::CreateForWebContents(web_contents
);
196 #if defined(ENABLE_EXTENSIONS)
197 extensions::TabHelper::CreateForWebContents(web_contents
);
200 #if defined(ENABLE_SUPERVISED_USERS)
201 SupervisedUserNavigationObserver::CreateForWebContents(web_contents
);
204 #if defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
205 #if defined(ENABLE_PRINT_PREVIEW)
206 printing::PrintViewManager::CreateForWebContents(web_contents
);
207 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents
);
209 printing::PrintViewManagerBasic::CreateForWebContents(web_contents
);
210 #endif // defined(ENABLE_PRINT_PREVIEW)
211 #endif // defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
213 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
214 switches::kEnableDomDistiller
)) {
215 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
219 if (predictors::ResourcePrefetchPredictorFactory::GetForProfile(
220 web_contents
->GetBrowserContext())) {
221 predictors::ResourcePrefetchPredictorTabHelper::CreateForWebContents(