Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / tab_helpers.cc
blobfcf29730f8b9d8165b0179b6ffd06061f8c13d6c
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/password_manager/chrome_password_manager_client.h"
16 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor_tab_helper.h"
18 #include "chrome/browser/prerender/prerender_tab_helper.h"
19 #include "chrome/browser/sessions/session_tab_helper.h"
20 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h"
21 #include "chrome/browser/translate/chrome_translate_client.h"
22 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
23 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
24 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
25 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
26 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
27 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h"
28 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
29 #include "chrome/browser/ui/search/search_tab_helper.h"
30 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "components/autofill/content/browser/content_autofill_driver.h"
33 #include "components/autofill/core/browser/autofill_manager.h"
34 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
35 #include "components/password_manager/core/browser/password_manager.h"
36 #include "content/public/browser/web_contents.h"
38 #if defined(OS_ANDROID)
39 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h"
40 #include "chrome/browser/ui/android/context_menu_helper.h"
41 #include "chrome/browser/ui/android/window_android_helper.h"
42 #else
43 #include "chrome/browser/external_protocol/external_protocol_observer.h"
44 #include "chrome/browser/net/predictor_tab_helper.h"
45 #include "chrome/browser/plugins/plugin_observer.h"
46 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
47 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h"
48 #include "chrome/browser/ui/hung_plugin_tab_helper.h"
49 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
50 #include "chrome/browser/ui/sad_tab_helper.h"
51 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
52 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
53 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
54 #include "chrome/browser/ui/zoom/zoom_controller.h"
55 #include "components/pdf/browser/pdf_web_contents_helper.h"
56 #include "components/web_modal/web_contents_modal_dialog_manager.h"
57 #endif // defined(OS_ANDROID)
59 #if defined(OS_WIN)
60 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
61 #endif
63 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
64 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
65 #endif
67 #if defined(ENABLE_EXTENSIONS)
68 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
69 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
70 #include "chrome/browser/extensions/tab_helper.h"
71 #include "extensions/browser/view_type_utils.h"
72 #endif
74 #if defined(ENABLE_MANAGED_USERS)
75 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
76 #endif
78 #if defined(ENABLE_PRINTING)
79 #if defined(ENABLE_FULL_PRINTING)
80 #include "chrome/browser/printing/print_preview_message_handler.h"
81 #include "chrome/browser/printing/print_view_manager.h"
82 #else
83 #include "chrome/browser/printing/print_view_manager_basic.h"
84 #endif // defined(ENABLE_FULL_PRINTING)
85 #endif // defined(ENABLE_PRINTING)
87 #if defined(ENABLE_ONE_CLICK_SIGNIN)
88 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
89 #endif
91 using content::WebContents;
93 namespace {
95 const char kTabContentsAttachedTabHelpersUserDataKey[] =
96 "TabContentsAttachedTabHelpers";
98 } // namespace
100 // static
101 void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
102 // If already adopted, nothing to be done.
103 base::SupportsUserData::Data* adoption_tag =
104 web_contents->GetUserData(&kTabContentsAttachedTabHelpersUserDataKey);
105 if (adoption_tag)
106 return;
108 // Mark as adopted.
109 web_contents->SetUserData(&kTabContentsAttachedTabHelpersUserDataKey,
110 new base::SupportsUserData::Data());
112 #if defined(ENABLE_EXTENSIONS)
113 // Set the view type.
114 extensions::SetViewType(web_contents, extensions::VIEW_TYPE_TAB_CONTENTS);
115 #endif
117 // Create all the tab helpers.
119 // SessionTabHelper comes first because it sets up the tab ID, and other
120 // helpers may rely on that.
121 SessionTabHelper::CreateForWebContents(web_contents);
122 #if !defined(OS_ANDROID)
123 // ZoomController comes before common tab helpers since ChromeAutofillClient
124 // may want to register as a ZoomObserver with it.
125 ZoomController::CreateForWebContents(web_contents);
126 #endif
128 // --- Common tab helpers ---
130 autofill::ChromeAutofillClient::CreateForWebContents(web_contents);
131 autofill::ContentAutofillDriver::CreateForWebContentsAndDelegate(
132 web_contents,
133 autofill::ChromeAutofillClient::FromWebContents(web_contents),
134 g_browser_process->GetApplicationLocale(),
135 autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
136 BookmarkTabHelper::CreateForWebContents(web_contents);
137 chrome_browser_net::NetErrorTabHelper::CreateForWebContents(web_contents);
138 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
139 web_contents,
140 autofill::ChromeAutofillClient::FromWebContents(web_contents));
141 CoreTabHelper::CreateForWebContents(web_contents);
142 FaviconTabHelper::CreateForWebContents(web_contents);
143 FindTabHelper::CreateForWebContents(web_contents);
144 HistoryTabHelper::CreateForWebContents(web_contents);
145 InfoBarService::CreateForWebContents(web_contents);
146 NavigationCorrectionTabObserver::CreateForWebContents(web_contents);
147 NavigationMetricsRecorder::CreateForWebContents(web_contents);
148 PopupBlockerTabHelper::CreateForWebContents(web_contents);
149 PrefsTabHelper::CreateForWebContents(web_contents);
150 prerender::PrerenderTabHelper::CreateForWebContentsWithPasswordManager(
151 web_contents,
152 ChromePasswordManagerClient::GetManagerFromWebContents(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);
157 ChromeContentSettingsClient::CreateForWebContents(web_contents);
158 ChromeTranslateClient::CreateForWebContents(web_contents);
160 // --- Platform-specific tab helpers ---
162 #if defined(OS_ANDROID)
163 ContextMenuHelper::CreateForWebContents(web_contents);
164 SingleTabModeTabHelper::CreateForWebContents(web_contents);
165 WindowAndroidHelper::CreateForWebContents(web_contents);
166 #else
167 chrome_browser_net::PredictorTabHelper::CreateForWebContents(web_contents);
168 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
169 web_contents);
170 extensions::WebNavigationTabObserver::CreateForWebContents(web_contents);
171 ExternalProtocolObserver::CreateForWebContents(web_contents);
172 HungPluginTabHelper::CreateForWebContents(web_contents);
173 ManagePasswordsUIController::CreateForWebContents(web_contents);
174 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
175 web_contents,
176 scoped_ptr<pdf::PDFWebContentsHelperClient>(
177 new ChromePDFWebContentsHelperClient()));
178 PermissionBubbleManager::CreateForWebContents(web_contents);
179 PluginObserver::CreateForWebContents(web_contents);
180 SadTabHelper::CreateForWebContents(web_contents);
181 safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(web_contents);
182 SearchEngineTabHelper::CreateForWebContents(web_contents);
183 TabContentsSyncedTabDelegate::CreateForWebContents(web_contents);
184 ThumbnailTabHelper::CreateForWebContents(web_contents);
185 web_modal::WebContentsModalDialogManager::CreateForWebContents(web_contents);
186 #endif
188 #if defined(OS_WIN)
189 MetroPinTabHelper::CreateForWebContents(web_contents);
190 #endif
192 // --- Feature tab helpers behind flags ---
194 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
195 CaptivePortalTabHelper::CreateForWebContents(web_contents);
196 #endif
198 #if defined(ENABLE_EXTENSIONS)
199 extensions::TabHelper::CreateForWebContents(web_contents);
200 #endif
202 #if defined(ENABLE_MANAGED_USERS)
203 SupervisedUserNavigationObserver::CreateForWebContents(web_contents);
204 #endif
206 #if defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
207 #if defined(ENABLE_FULL_PRINTING)
208 printing::PrintViewManager::CreateForWebContents(web_contents);
209 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
210 #else
211 printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
212 #endif // defined(ENABLE_FULL_PRINTING)
213 #endif // defined(ENABLE_PRINTING) && !defined(OS_ANDROID)
215 if (CommandLine::ForCurrentProcess()->HasSwitch(
216 switches::kEnableDomDistiller)) {
217 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
218 web_contents);
221 #if defined(ENABLE_ONE_CLICK_SIGNIN)
222 // If this is not an incognito window, setup to handle one-click login.
223 // We don't want to check that the profile is already connected at this time
224 // because the connected state may change while this tab is open. Having a
225 // one-click signin helper attached does not cause problems if the profile
226 // happens to be already connected.
227 if (OneClickSigninHelper::CanOffer(web_contents,
228 OneClickSigninHelper::CAN_OFFER_FOR_ALL,
229 std::string(),
230 NULL)) {
231 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(
232 web_contents,
233 ChromePasswordManagerClient::GetManagerFromWebContents(web_contents));
235 #endif
237 if (predictors::ResourcePrefetchPredictorFactory::GetForProfile(
238 web_contents->GetBrowserContext())) {
239 predictors::ResourcePrefetchPredictorTabHelper::CreateForWebContents(
240 web_contents);