Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / tab_helpers.cc
blob5f477c0dc193476a09ec612b226bb6f4a0527569
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_utils.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/tracing/navigation_tracing.h"
28 #include "chrome/browser/translate/chrome_translate_client.h"
29 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
30 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
31 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
32 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
33 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
34 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
35 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h"
36 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
37 #include "chrome/browser/ui/search/search_tab_helper.h"
38 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
39 #include "chrome/browser/ui/tab_dialogs.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
42 #include "components/autofill/core/browser/autofill_manager.h"
43 #include "components/dom_distiller/content/browser/web_contents_main_frame_observer.h"
44 #include "components/dom_distiller/core/dom_distiller_switches.h"
45 #include "components/history/content/browser/web_contents_top_sites_observer.h"
46 #include "components/history/core/browser/top_sites.h"
47 #include "components/password_manager/core/browser/password_manager.h"
48 #include "components/tracing/tracing_switches.h"
49 #include "content/public/browser/web_contents.h"
51 #if defined(OS_ANDROID)
52 #include "chrome/browser/android/offline_pages/offline_page_web_contents_observer.h"
53 #include "chrome/browser/android/voice_search_tab_helper.h"
54 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h"
55 #include "chrome/browser/ui/android/context_menu_helper.h"
56 #include "chrome/browser/ui/android/window_android_helper.h"
57 #else
58 #include "chrome/browser/banners/app_banner_manager_desktop.h"
59 #include "chrome/browser/plugins/plugin_observer.h"
60 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
61 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
62 #include "chrome/browser/ui/hung_plugin_tab_helper.h"
63 #include "chrome/browser/ui/sad_tab_helper.h"
64 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
65 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
66 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
67 #include "components/pdf/browser/pdf_web_contents_helper.h"
68 #include "components/ui/zoom/zoom_controller.h"
69 #include "components/web_modal/web_contents_modal_dialog_manager.h"
70 #endif // defined(OS_ANDROID)
72 #if defined(OS_WIN)
73 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
74 #endif
76 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
77 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
78 #endif
80 #if defined(ENABLE_EXTENSIONS)
81 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
82 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
83 #include "chrome/browser/extensions/tab_helper.h"
84 #include "extensions/browser/view_type_utils.h"
85 #endif
87 #if defined(ENABLE_SUPERVISED_USERS)
88 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
89 #endif
91 #if defined(ENABLE_PRINTING)
92 #if defined(ENABLE_PRINT_PREVIEW)
93 #include "chrome/browser/printing/print_preview_message_handler.h"
94 #include "chrome/browser/printing/print_view_manager.h"
95 #else
96 #include "chrome/browser/printing/print_view_manager_basic.h"
97 #endif // defined(ENABLE_PRINT_PREVIEW)
98 #endif // defined(ENABLE_PRINTING)
100 using content::WebContents;
102 namespace {
104 const char kTabContentsAttachedTabHelpersUserDataKey[] =
105 "TabContentsAttachedTabHelpers";
107 } // namespace
109 // static
110 void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
111 // If already adopted, nothing to be done.
112 base::SupportsUserData::Data* adoption_tag =
113 web_contents->GetUserData(&kTabContentsAttachedTabHelpersUserDataKey);
114 if (adoption_tag)
115 return;
117 // Mark as adopted.
118 web_contents->SetUserData(&kTabContentsAttachedTabHelpersUserDataKey,
119 new base::SupportsUserData::Data());
121 #if defined(ENABLE_EXTENSIONS)
122 // Set the view type.
123 extensions::SetViewType(web_contents, extensions::VIEW_TYPE_TAB_CONTENTS);
124 #endif
126 // Create all the tab helpers.
128 // SessionTabHelper comes first because it sets up the tab ID, and other
129 // helpers may rely on that.
130 SessionTabHelper::CreateForWebContents(web_contents);
131 #if !defined(OS_ANDROID)
132 // ZoomController comes before common tab helpers since ChromeAutofillClient
133 // may want to register as a ZoomObserver with it.
134 ui_zoom::ZoomController::CreateForWebContents(web_contents);
135 #endif
137 // --- Common tab helpers ---
139 autofill::ChromeAutofillClient::CreateForWebContents(web_contents);
140 autofill::ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
141 web_contents,
142 autofill::ChromeAutofillClient::FromWebContents(web_contents),
143 g_browser_process->GetApplicationLocale(),
144 autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
145 BookmarkTabHelper::CreateForWebContents(web_contents);
146 chrome_browser_net::NetErrorTabHelper::CreateForWebContents(web_contents);
147 chrome_browser_net::PredictorTabHelper::CreateForWebContents(web_contents);
148 ChromeContentSettingsClient::CreateForWebContents(web_contents);
149 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
150 web_contents,
151 autofill::ChromeAutofillClient::FromWebContents(web_contents));
152 ChromeTranslateClient::CreateForWebContents(web_contents);
153 CoreTabHelper::CreateForWebContents(web_contents);
154 ExternalProtocolObserver::CreateForWebContents(web_contents);
155 favicon::CreateContentFaviconDriverForWebContents(web_contents);
156 FindTabHelper::CreateForWebContents(web_contents);
157 history::WebContentsTopSitesObserver::CreateForWebContents(
158 web_contents, TopSitesFactory::GetForProfile(
159 Profile::FromBrowserContext(
160 web_contents->GetBrowserContext())).get());
161 HistoryTabHelper::CreateForWebContents(web_contents);
162 InfoBarService::CreateForWebContents(web_contents);
163 ManagePasswordsUIController::CreateForWebContents(web_contents);
164 NavigationCorrectionTabObserver::CreateForWebContents(web_contents);
165 NavigationMetricsRecorder::CreateForWebContents(web_contents);
166 PopupBlockerTabHelper::CreateForWebContents(web_contents);
167 PrefsTabHelper::CreateForWebContents(web_contents);
168 prerender::PrerenderTabHelper::CreateForWebContents(web_contents);
169 SearchTabHelper::CreateForWebContents(web_contents);
170 if (SiteEngagementService::IsEnabled())
171 SiteEngagementHelper::CreateForWebContents(web_contents);
172 // TODO(vabr): Remove TabSpecificContentSettings from here once their function
173 // is taken over by ChromeContentSettingsClient. http://crbug.com/387075
174 TabSpecificContentSettings::CreateForWebContents(web_contents);
176 // --- Platform-specific tab helpers ---
178 #if defined(OS_ANDROID)
179 ContextMenuHelper::CreateForWebContents(web_contents);
180 offline_pages::OfflinePageWebContentsObserver::CreateForWebContents(
181 web_contents);
182 SingleTabModeTabHelper::CreateForWebContents(web_contents);
183 VoiceSearchTabHelper::CreateForWebContents(web_contents);
184 WindowAndroidHelper::CreateForWebContents(web_contents);
185 #else
186 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
187 web_contents);
188 extensions::WebNavigationTabObserver::CreateForWebContents(web_contents);
189 HungPluginTabHelper::CreateForWebContents(web_contents);
190 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
191 web_contents,
192 scoped_ptr<pdf::PDFWebContentsHelperClient>(
193 new ChromePDFWebContentsHelperClient()));
194 PermissionBubbleManager::CreateForWebContents(web_contents);
195 PluginObserver::CreateForWebContents(web_contents);
196 SadTabHelper::CreateForWebContents(web_contents);
197 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(web_contents);
198 SearchEngineTabHelper::CreateForWebContents(web_contents);
199 TabContentsSyncedTabDelegate::CreateForWebContents(web_contents);
200 TabDialogs::CreateForWebContents(web_contents);
201 ThumbnailTabHelper::CreateForWebContents(web_contents);
202 web_modal::WebContentsModalDialogManager::CreateForWebContents(web_contents);
204 if (banners::AppBannerManagerDesktop::IsEnabled()) {
205 banners::AppBannerManagerDesktop::CreateForWebContents(web_contents);
207 #endif
209 #if defined(OS_WIN)
210 MetroPinTabHelper::CreateForWebContents(web_contents);
211 #endif
213 // --- Feature tab helpers behind flags ---
215 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
216 CaptivePortalTabHelper::CreateForWebContents(web_contents);
217 #endif
219 #if defined(ENABLE_EXTENSIONS)
220 extensions::TabHelper::CreateForWebContents(web_contents);
221 #endif
223 #if defined(ENABLE_SUPERVISED_USERS)
224 SupervisedUserNavigationObserver::CreateForWebContents(web_contents);
225 #endif
227 #if defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
228 #if defined(ENABLE_PRINT_PREVIEW)
229 printing::PrintViewManager::CreateForWebContents(web_contents);
230 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
231 #else
232 printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
233 #endif // defined(ENABLE_PRINT_PREVIEW)
234 #endif // defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
236 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
237 switches::kEnableDomDistiller)) {
238 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
239 web_contents);
242 if (predictors::ResourcePrefetchPredictorFactory::GetForProfile(
243 web_contents->GetBrowserContext())) {
244 predictors::ResourcePrefetchPredictorTabHelper::CreateForWebContents(
245 web_contents);
248 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
249 switches::kEnableNavigationTracing) &&
250 base::CommandLine::ForCurrentProcess()->HasSwitch(
251 switches::kTraceUploadURL)) {
252 tracing::NavigationTracingObserver::CreateForWebContents(web_contents);