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