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