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/engagement/site_engagement_helper.h"
12 #include "chrome/browser/engagement/site_engagement_service.h"
13 #include "chrome/browser/external_protocol/external_protocol_observer.h"
14 #include "chrome/browser/favicon/favicon_helper.h"
15 #include "chrome/browser/history/history_tab_helper.h"
16 #include "chrome/browser/history/top_sites_factory.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/net/net_error_tab_helper.h"
19 #include "chrome/browser/net/predictor_tab_helper.h"
20 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
21 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
22 #include "chrome/browser/predictors/resource_prefetch_predictor_tab_helper.h"
23 #include "chrome/browser/prerender/prerender_tab_helper.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/sessions/session_tab_helper.h"
26 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h"
27 #include "chrome/browser/translate/chrome_translate_client.h"
28 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
29 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
30 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
31 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
32 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
33 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
34 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h"
35 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
36 #include "chrome/browser/ui/search/search_tab_helper.h"
37 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
38 #include "chrome/browser/ui/tab_dialogs.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
41 #include "components/autofill/core/browser/autofill_manager.h"
42 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
43 #include "components/history/content/browser/web_contents_top_sites_observer.h"
44 #include "components/history/core/browser/top_sites.h"
45 #include "components/password_manager/core/browser/password_manager.h"
46 #include "content/public/browser/web_contents.h"
48 #if defined(OS_ANDROID)
49 #include "chrome/browser/android/voice_search_tab_helper.h"
50 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h"
51 #include "chrome/browser/enhanced_bookmarks/android/enhanced_bookmark_tab_helper.h"
52 #include "chrome/browser/ui/android/context_menu_helper.h"
53 #include "chrome/browser/ui/android/window_android_helper.h"
55 #include "chrome/browser/banners/app_banner_manager_desktop.h"
56 #include "chrome/browser/plugins/plugin_observer.h"
57 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
58 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
59 #include "chrome/browser/ui/hung_plugin_tab_helper.h"
60 #include "chrome/browser/ui/sad_tab_helper.h"
61 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
62 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
63 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
64 #include "components/pdf/browser/pdf_web_contents_helper.h"
65 #include "components/ui/zoom/zoom_controller.h"
66 #include "components/web_modal/web_contents_modal_dialog_manager.h"
67 #endif // defined(OS_ANDROID)
70 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
73 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
74 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
77 #if defined(ENABLE_EXTENSIONS)
78 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
79 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
80 #include "chrome/browser/extensions/tab_helper.h"
81 #include "extensions/browser/view_type_utils.h"
84 #if defined(ENABLE_SUPERVISED_USERS)
85 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
88 #if defined(ENABLE_PRINTING)
89 #if defined(ENABLE_PRINT_PREVIEW)
90 #include "chrome/browser/printing/print_preview_message_handler.h"
91 #include "chrome/browser/printing/print_view_manager.h"
93 #include "chrome/browser/printing/print_view_manager_basic.h"
94 #endif // defined(ENABLE_PRINT_PREVIEW)
95 #endif // defined(ENABLE_PRINTING)
97 using content::WebContents
;
101 const char kTabContentsAttachedTabHelpersUserDataKey
[] =
102 "TabContentsAttachedTabHelpers";
107 void TabHelpers::AttachTabHelpers(WebContents
* web_contents
) {
108 // If already adopted, nothing to be done.
109 base::SupportsUserData::Data
* adoption_tag
=
110 web_contents
->GetUserData(&kTabContentsAttachedTabHelpersUserDataKey
);
115 web_contents
->SetUserData(&kTabContentsAttachedTabHelpersUserDataKey
,
116 new base::SupportsUserData::Data());
118 #if defined(ENABLE_EXTENSIONS)
119 // Set the view type.
120 extensions::SetViewType(web_contents
, extensions::VIEW_TYPE_TAB_CONTENTS
);
123 // Create all the tab helpers.
125 // SessionTabHelper comes first because it sets up the tab ID, and other
126 // helpers may rely on that.
127 SessionTabHelper::CreateForWebContents(web_contents
);
128 #if !defined(OS_ANDROID)
129 // ZoomController comes before common tab helpers since ChromeAutofillClient
130 // may want to register as a ZoomObserver with it.
131 ui_zoom::ZoomController::CreateForWebContents(web_contents
);
134 // --- Common tab helpers ---
136 autofill::ChromeAutofillClient::CreateForWebContents(web_contents
);
137 autofill::ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
139 autofill::ChromeAutofillClient::FromWebContents(web_contents
),
140 g_browser_process
->GetApplicationLocale(),
141 autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
);
142 BookmarkTabHelper::CreateForWebContents(web_contents
);
143 chrome_browser_net::NetErrorTabHelper::CreateForWebContents(web_contents
);
144 chrome_browser_net::PredictorTabHelper::CreateForWebContents(web_contents
);
145 ChromeContentSettingsClient::CreateForWebContents(web_contents
);
146 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
148 autofill::ChromeAutofillClient::FromWebContents(web_contents
));
149 ChromeTranslateClient::CreateForWebContents(web_contents
);
150 CoreTabHelper::CreateForWebContents(web_contents
);
151 ExternalProtocolObserver::CreateForWebContents(web_contents
);
152 favicon::CreateContentFaviconDriverForWebContents(web_contents
);
153 FindTabHelper::CreateForWebContents(web_contents
);
154 history::WebContentsTopSitesObserver::CreateForWebContents(
155 web_contents
, TopSitesFactory::GetForProfile(
156 Profile::FromBrowserContext(
157 web_contents
->GetBrowserContext())).get());
158 HistoryTabHelper::CreateForWebContents(web_contents
);
159 InfoBarService::CreateForWebContents(web_contents
);
160 ManagePasswordsUIController::CreateForWebContents(web_contents
);
161 NavigationCorrectionTabObserver::CreateForWebContents(web_contents
);
162 NavigationMetricsRecorder::CreateForWebContents(web_contents
);
163 PopupBlockerTabHelper::CreateForWebContents(web_contents
);
164 PrefsTabHelper::CreateForWebContents(web_contents
);
165 prerender::PrerenderTabHelper::CreateForWebContents(web_contents
);
166 SearchTabHelper::CreateForWebContents(web_contents
);
167 if (SiteEngagementService::IsEnabled())
168 SiteEngagementHelper::CreateForWebContents(web_contents
);
169 // TODO(vabr): Remove TabSpecificContentSettings from here once their function
170 // is taken over by ChromeContentSettingsClient. http://crbug.com/387075
171 TabSpecificContentSettings::CreateForWebContents(web_contents
);
173 // --- Platform-specific tab helpers ---
175 #if defined(OS_ANDROID)
176 ContextMenuHelper::CreateForWebContents(web_contents
);
177 EnhancedBookmarkTabHelper::CreateForWebContents(web_contents
);
178 SingleTabModeTabHelper::CreateForWebContents(web_contents
);
179 VoiceSearchTabHelper::CreateForWebContents(web_contents
);
180 WindowAndroidHelper::CreateForWebContents(web_contents
);
182 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
184 extensions::WebNavigationTabObserver::CreateForWebContents(web_contents
);
185 HungPluginTabHelper::CreateForWebContents(web_contents
);
186 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
188 scoped_ptr
<pdf::PDFWebContentsHelperClient
>(
189 new ChromePDFWebContentsHelperClient()));
190 PermissionBubbleManager::CreateForWebContents(web_contents
);
191 PluginObserver::CreateForWebContents(web_contents
);
192 SadTabHelper::CreateForWebContents(web_contents
);
193 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(web_contents
);
194 SearchEngineTabHelper::CreateForWebContents(web_contents
);
195 TabContentsSyncedTabDelegate::CreateForWebContents(web_contents
);
196 TabDialogs::CreateForWebContents(web_contents
);
197 ThumbnailTabHelper::CreateForWebContents(web_contents
);
198 web_modal::WebContentsModalDialogManager::CreateForWebContents(web_contents
);
200 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kEnableAddToShelf
)) {
202 banners::AppBannerManagerDesktop::CreateForWebContents(web_contents
);
207 MetroPinTabHelper::CreateForWebContents(web_contents
);
210 // --- Feature tab helpers behind flags ---
212 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
213 CaptivePortalTabHelper::CreateForWebContents(web_contents
);
216 #if defined(ENABLE_EXTENSIONS)
217 extensions::TabHelper::CreateForWebContents(web_contents
);
220 #if defined(ENABLE_SUPERVISED_USERS)
221 SupervisedUserNavigationObserver::CreateForWebContents(web_contents
);
224 #if defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
225 #if defined(ENABLE_PRINT_PREVIEW)
226 printing::PrintViewManager::CreateForWebContents(web_contents
);
227 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents
);
229 printing::PrintViewManagerBasic::CreateForWebContents(web_contents
);
230 #endif // defined(ENABLE_PRINT_PREVIEW)
231 #endif // defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
233 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
234 switches::kEnableDomDistiller
)) {
235 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
239 if (predictors::ResourcePrefetchPredictorFactory::GetForProfile(
240 web_contents
->GetBrowserContext())) {
241 predictors::ResourcePrefetchPredictorTabHelper::CreateForWebContents(