Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / chrome_web_ui_controller_factory.cc
blob0d5c74510b66f9a200149d487f23bc61447c20c8
1 // Copyright (c) 2012 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/webui/chrome_web_ui_controller_factory.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/about_flags.h"
15 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
16 #include "chrome/browser/engagement/site_engagement_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/webui/about_ui.h"
20 #include "chrome/browser/ui/webui/bookmarks_ui.h"
21 #include "chrome/browser/ui/webui/components_ui.h"
22 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
23 #include "chrome/browser/ui/webui/crashes_ui.h"
24 #include "chrome/browser/ui/webui/device_log_ui.h"
25 #include "chrome/browser/ui/webui/domain_reliability_internals_ui.h"
26 #include "chrome/browser/ui/webui/downloads_ui.h"
27 #include "chrome/browser/ui/webui/engagement/site_engagement_ui.h"
28 #include "chrome/browser/ui/webui/flags_ui.h"
29 #include "chrome/browser/ui/webui/flash_ui.h"
30 #include "chrome/browser/ui/webui/gcm_internals_ui.h"
31 #include "chrome/browser/ui/webui/help/help_ui.h"
32 #include "chrome/browser/ui/webui/history_ui.h"
33 #include "chrome/browser/ui/webui/identity_internals_ui.h"
34 #include "chrome/browser/ui/webui/instant_ui.h"
35 #include "chrome/browser/ui/webui/interstitials/interstitial_ui.h"
36 #include "chrome/browser/ui/webui/invalidations_ui.h"
37 #include "chrome/browser/ui/webui/local_state/local_state_ui.h"
38 #include "chrome/browser/ui/webui/memory_internals/memory_internals_ui.h"
39 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h"
40 #include "chrome/browser/ui/webui/omnibox/omnibox_ui.h"
41 #include "chrome/browser/ui/webui/options/options_ui.h"
42 #include "chrome/browser/ui/webui/password_manager_internals/password_manager_internals_ui.h"
43 #include "chrome/browser/ui/webui/plugins_ui.h"
44 #include "chrome/browser/ui/webui/predictors/predictors_ui.h"
45 #include "chrome/browser/ui/webui/profiler_ui.h"
46 #include "chrome/browser/ui/webui/settings/md_settings_ui.h"
47 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
48 #include "chrome/browser/ui/webui/signin/profile_signin_confirmation_ui.h"
49 #include "chrome/browser/ui/webui/signin/user_manager_ui.h"
50 #include "chrome/browser/ui/webui/signin_internals_ui.h"
51 #include "chrome/browser/ui/webui/supervised_user_internals_ui.h"
52 #include "chrome/browser/ui/webui/sync_internals_ui.h"
53 #include "chrome/browser/ui/webui/translate_internals/translate_internals_ui.h"
54 #include "chrome/browser/ui/webui/user_actions/user_actions_ui.h"
55 #include "chrome/browser/ui/webui/version_ui.h"
56 #include "chrome/common/chrome_switches.h"
57 #include "chrome/common/pref_names.h"
58 #include "chrome/common/url_constants.h"
59 #include "components/dom_distiller/core/dom_distiller_constants.h"
60 #include "components/dom_distiller/core/dom_distiller_features.h"
61 #include "components/dom_distiller/core/dom_distiller_service.h"
62 #include "components/dom_distiller/core/url_constants.h"
63 #include "components/dom_distiller/webui/dom_distiller_ui.h"
64 #include "components/favicon/core/favicon_service.h"
65 #include "components/favicon_base/favicon_util.h"
66 #include "components/favicon_base/select_favicon_frames.h"
67 #include "components/history/core/browser/history_types.h"
68 #include "components/password_manager/core/common/password_manager_switches.h"
69 #include "components/signin/core/common/profile_management_switches.h"
70 #include "content/public/browser/web_contents.h"
71 #include "content/public/browser/web_ui.h"
72 #include "content/public/common/content_client.h"
73 #include "content/public/common/url_utils.h"
74 #include "ui/gfx/favicon_size.h"
75 #include "ui/web_dialogs/web_dialog_ui.h"
76 #include "url/gurl.h"
78 #if !defined(DISABLE_NACL)
79 #include "chrome/browser/ui/webui/nacl_ui.h"
80 #endif
82 #if defined(ENABLE_CONFIGURATION_POLICY)
83 #include "chrome/browser/ui/webui/policy_ui.h"
84 #endif
86 #if defined(ENABLE_WEBRTC)
87 #include "chrome/browser/ui/webui/media/webrtc_logs_ui.h"
88 #endif
90 #if defined(ENABLE_PRINT_PREVIEW)
91 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
92 #endif
94 #if defined(ENABLE_MEDIA_ROUTER) && !defined(OS_ANDROID)
95 #include "chrome/browser/ui/webui/media_router/media_router_ui.h"
96 #endif
98 #if !defined(OS_ANDROID)
99 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
100 #include "chrome/browser/ui/webui/quota_internals/quota_internals_ui.h"
101 #include "chrome/browser/ui/webui/sync_file_system_internals/sync_file_system_internals_ui.h"
102 #include "chrome/browser/ui/webui/system_info_ui.h"
103 #include "chrome/browser/ui/webui/uber/uber_ui.h"
104 #endif
106 #if defined(OS_ANDROID) || defined(OS_IOS)
107 #include "chrome/browser/ui/webui/net_export_ui.h"
108 #else
109 #include "chrome/browser/devtools/device/webrtc/webrtc_device_provider.h"
110 #include "chrome/browser/signin/easy_unlock_service.h"
111 #include "chrome/browser/signin/easy_unlock_service_factory.h"
112 #include "chrome/browser/ui/webui/copresence_ui.h"
113 #include "chrome/browser/ui/webui/devtools_ui.h"
114 #include "chrome/browser/ui/webui/inspect_ui.h"
115 #endif
117 #if defined(OS_CHROMEOS)
118 #include "base/sys_info.h"
119 #include "chrome/browser/ui/webui/chromeos/bluetooth_pairing_ui.h"
120 #include "chrome/browser/ui/webui/chromeos/certificate_manager_dialog_ui.h"
121 #include "chrome/browser/ui/webui/chromeos/choose_mobile_network_ui.h"
122 #include "chrome/browser/ui/webui/chromeos/cryptohome_ui.h"
123 #include "chrome/browser/ui/webui/chromeos/drive_internals_ui.h"
124 #include "chrome/browser/ui/webui/chromeos/first_run/first_run_ui.h"
125 #include "chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.h"
126 #include "chrome/browser/ui/webui/chromeos/keyboard_overlay_ui.h"
127 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
128 #include "chrome/browser/ui/webui/chromeos/mobile_setup_ui.h"
129 #include "chrome/browser/ui/webui/chromeos/network_ui.h"
130 #include "chrome/browser/ui/webui/chromeos/nfc_debug_ui.h"
131 #include "chrome/browser/ui/webui/chromeos/power_ui.h"
132 #include "chrome/browser/ui/webui/chromeos/proxy_settings_ui.h"
133 #include "chrome/browser/ui/webui/chromeos/salsa_ui.h"
134 #include "chrome/browser/ui/webui/chromeos/set_time_ui.h"
135 #include "chrome/browser/ui/webui/chromeos/sim_unlock_ui.h"
136 #include "chrome/browser/ui/webui/chromeos/slow_trace_ui.h"
137 #include "chrome/browser/ui/webui/chromeos/slow_ui.h"
138 #include "chrome/browser/ui/webui/voice_search_ui.h"
139 #include "components/proximity_auth/webui/proximity_auth_ui.h"
140 #include "components/proximity_auth/webui/url_constants.h"
141 #endif
143 #if !defined(OFFICIAL_BUILD) && defined(OS_CHROMEOS) && !defined(NDEBUG)
144 #include "chrome/browser/ui/webui/chromeos/emulator/device_emulator_ui.h"
145 #endif
147 #if !defined(OS_CHROMEOS)
148 #include "chrome/browser/ui/webui/app_launcher_page_ui.h"
149 #endif
151 #if defined(USE_AURA)
152 #include "chrome/browser/ui/webui/gesture_config_ui.h"
153 #endif
155 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
156 #include "chrome/browser/ui/sync/sync_promo_ui.h"
157 #endif
159 #if defined(OS_WIN)
160 #include "chrome/browser/ui/webui/conflicts_ui.h"
161 #include "chrome/browser/ui/webui/set_as_default_browser_ui.h"
162 #endif
164 #if (defined(USE_NSS_CERTS) || defined(USE_OPENSSL_CERTS)) && defined(USE_AURA)
165 #include "chrome/browser/ui/webui/certificate_viewer_ui.h"
166 #endif
168 #if defined(ENABLE_SERVICE_DISCOVERY)
169 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui.h"
170 #endif
172 #if defined(ENABLE_APP_LIST)
173 #include "chrome/browser/ui/webui/app_list/start_page_ui.h"
174 #endif
176 #if defined(ENABLE_EXTENSIONS)
177 #include "chrome/browser/extensions/extension_web_ui.h"
178 #include "chrome/browser/ui/webui/extensions/extensions_ui.h"
179 #include "chrome/common/extensions/extension_constants.h"
180 #include "extensions/browser/extension_registry.h"
181 #include "extensions/browser/extension_system.h"
182 #include "extensions/common/constants.h"
183 #include "extensions/common/extension.h"
184 #include "extensions/common/feature_switch.h"
185 #include "extensions/common/manifest.h"
186 #endif
188 using content::WebUI;
189 using content::WebUIController;
190 using ui::WebDialogUI;
192 namespace {
194 // A function for creating a new WebUI. The caller owns the return value, which
195 // may be NULL (for example, if the URL refers to an non-existent extension).
196 typedef WebUIController* (*WebUIFactoryFunction)(WebUI* web_ui,
197 const GURL& url);
199 // Template for defining WebUIFactoryFunction.
200 template<class T>
201 WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
202 return new T(web_ui);
205 #if defined(ENABLE_EXTENSIONS)
206 // Special cases for extensions.
207 template<>
208 WebUIController* NewWebUI<ExtensionWebUI>(WebUI* web_ui,
209 const GURL& url) {
210 return new ExtensionWebUI(web_ui, url);
212 #endif // defined(ENABLE_EXTENSIONS)
214 // Special case for older about: handlers.
215 template<>
216 WebUIController* NewWebUI<AboutUI>(WebUI* web_ui, const GURL& url) {
217 return new AboutUI(web_ui, url.host());
220 #if defined(OS_CHROMEOS)
221 template<>
222 WebUIController* NewWebUI<chromeos::OobeUI>(WebUI* web_ui, const GURL& url) {
223 return new chromeos::OobeUI(web_ui, url);
226 // Special case for chrome://proximity_auth.
227 template <>
228 WebUIController* NewWebUI<proximity_auth::ProximityAuthUI>(WebUI* web_ui,
229 const GURL& url) {
230 content::BrowserContext* browser_context =
231 web_ui->GetWebContents()->GetBrowserContext();
232 return new proximity_auth::ProximityAuthUI(
233 web_ui, EasyUnlockServiceFactory::GetForBrowserContext(browser_context)
234 ->proximity_auth_client());
236 #endif
238 // Special cases for DOM distiller.
239 template<>
240 WebUIController* NewWebUI<dom_distiller::DomDistillerUi>(WebUI* web_ui,
241 const GURL& url) {
242 // The DomDistillerUi can not depend on components/dom_distiller/content,
243 // so inject the correct DomDistillerService from chrome/.
244 content::BrowserContext* browser_context =
245 web_ui->GetWebContents()->GetBrowserContext();
246 dom_distiller::DomDistillerService* service =
247 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
248 browser_context);
249 return new dom_distiller::DomDistillerUi(
250 web_ui, service, dom_distiller::kDomDistillerScheme);
253 #if defined(ENABLE_EXTENSIONS)
254 // Only create ExtensionWebUI for URLs that are allowed extension bindings,
255 // hosted by actual tabs.
256 bool NeedsExtensionWebUI(Profile* profile, const GURL& url) {
257 if (!profile)
258 return false;
260 const extensions::Extension* extension =
261 extensions::ExtensionRegistry::Get(profile)->enabled_extensions().
262 GetExtensionOrAppByURL(url);
263 // Allow bindings for all packaged extensions and component hosted apps.
264 return extension &&
265 (!extension->is_hosted_app() ||
266 extension->location() == extensions::Manifest::COMPONENT);
268 #endif
270 bool IsAboutUI(const GURL& url) {
271 return (url.host() == chrome::kChromeUIChromeURLsHost ||
272 url.host() == chrome::kChromeUICreditsHost ||
273 url.host() == chrome::kChromeUIDNSHost ||
274 url.host() == chrome::kChromeUIMemoryHost ||
275 url.host() == chrome::kChromeUIMemoryRedirectHost
276 #if !defined(OS_ANDROID)
277 || url.host() == chrome::kChromeUITermsHost
278 #endif
279 #if defined(OS_LINUX) || defined(OS_OPENBSD)
280 || url.host() == chrome::kChromeUILinuxProxyConfigHost ||
281 url.host() == chrome::kChromeUISandboxHost
282 #endif
283 #if defined(OS_CHROMEOS)
284 || url.host() == chrome::kChromeUIOSCreditsHost
285 #endif
286 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
287 || url.host() == chrome::kChromeUIDiscardsHost
288 #endif
289 ); // NOLINT
292 // Returns a function that can be used to create the right type of WebUI for a
293 // tab, based on its URL. Returns NULL if the URL doesn't have WebUI associated
294 // with it.
295 WebUIFactoryFunction GetWebUIFactoryFunction(WebUI* web_ui,
296 Profile* profile,
297 const GURL& url) {
298 #if defined(ENABLE_EXTENSIONS)
299 if (NeedsExtensionWebUI(profile, url))
300 return &NewWebUI<ExtensionWebUI>;
301 #endif
303 // This will get called a lot to check all URLs, so do a quick check of other
304 // schemes to filter out most URLs.
305 if (!url.SchemeIs(content::kChromeDevToolsScheme) &&
306 !url.SchemeIs(content::kChromeUIScheme)) {
307 return NULL;
310 /****************************************************************************
311 * Please keep this in alphabetical order. If #ifs or special logics are
312 * required, add it below in the appropriate section.
313 ***************************************************************************/
314 // We must compare hosts only since some of the Web UIs append extra stuff
315 // after the host name.
316 // All platform builds of Chrome will need to have a cloud printing
317 // dialog as backup. It's just that on Chrome OS, it's the only
318 // print dialog.
319 if (url.host() == chrome::kChromeUIComponentsHost)
320 return &NewWebUI<ComponentsUI>;
321 if (url.spec() == chrome::kChromeUIConstrainedHTMLTestURL)
322 return &NewWebUI<ConstrainedWebDialogUI>;
323 if (url.host() == chrome::kChromeUICrashesHost)
324 return &NewWebUI<CrashesUI>;
325 if (url.host() == chrome::kChromeUIDeviceLogHost)
326 return &NewWebUI<chromeos::DeviceLogUI>;
327 if (url.host() == chrome::kChromeUIDomainReliabilityInternalsHost)
328 return &NewWebUI<DomainReliabilityInternalsUI>;
329 if (url.host() == chrome::kChromeUIFlagsHost)
330 return &NewWebUI<FlagsUI>;
331 if (url.host() == chrome::kChromeUIHistoryFrameHost)
332 return &NewWebUI<HistoryUI>;
333 if (url.host() == chrome::kChromeUIInstantHost)
334 return &NewWebUI<InstantUI>;
335 if (url.host() == chrome::kChromeUIInterstitialHost)
336 return &NewWebUI<InterstitialUI>;
337 if (url.host() == chrome::kChromeUIInvalidationsHost)
338 return &NewWebUI<InvalidationsUI>;
339 if (url.host() == chrome::kChromeUILocalStateHost)
340 return &NewWebUI<LocalStateUI>;
341 if (url.host() == chrome::kChromeUIMemoryInternalsHost)
342 return &NewWebUI<MemoryInternalsUI>;
343 if (url.host() == chrome::kChromeUINetInternalsHost)
344 return &NewWebUI<NetInternalsUI>;
345 if (url.host() == chrome::kChromeUIOmniboxHost)
346 return &NewWebUI<OmniboxUI>;
347 if (url.host() == chrome::kChromeUIPasswordManagerInternalsHost)
348 return &NewWebUI<PasswordManagerInternalsUI>;
349 if (url.host() == chrome::kChromeUIPredictorsHost)
350 return &NewWebUI<PredictorsUI>;
351 if (url.host() == chrome::kChromeUIProfilerHost)
352 return &NewWebUI<ProfilerUI>;
353 if (url.host() == chrome::kChromeUISignInInternalsHost)
354 return &NewWebUI<SignInInternalsUI>;
355 if (url.host() == chrome::kChromeUISupervisedUserInternalsHost)
356 return &NewWebUI<SupervisedUserInternalsUI>;
357 if (url.host() == chrome::kChromeUISupervisedUserPassphrasePageHost)
358 return &NewWebUI<ConstrainedWebDialogUI>;
359 if (url.host() == chrome::kChromeUISyncInternalsHost)
360 return &NewWebUI<SyncInternalsUI>;
361 if (url.host() == chrome::kChromeUISyncResourcesHost)
362 return &NewWebUI<WebDialogUI>;
363 if (url.host() == chrome::kChromeUITranslateInternalsHost)
364 return &NewWebUI<TranslateInternalsUI>;
365 if (url.host() == chrome::kChromeUIUserActionsHost)
366 return &NewWebUI<UserActionsUI>;
367 if (url.host() == chrome::kChromeUIVersionHost)
368 return &NewWebUI<VersionUI>;
370 /****************************************************************************
371 * OS Specific #defines
372 ***************************************************************************/
373 #if !defined(OS_ANDROID)
374 #if !defined(OS_CHROMEOS)
375 // AppLauncherPage is not needed on Android or ChromeOS.
376 if (url.host() == chrome::kChromeUIAppLauncherPageHost &&
377 profile && extensions::ExtensionSystem::Get(profile)->
378 extension_service()) {
379 return &NewWebUI<AppLauncherPageUI>;
381 #endif
383 // Bookmarks are part of NTP on Android.
384 if (url.host() == chrome::kChromeUIBookmarksHost)
385 return &NewWebUI<BookmarksUI>;
386 // Downloads list on Android uses the built-in download manager.
387 if (url.host() == chrome::kChromeUIDownloadsHost)
388 return &NewWebUI<DownloadsUI>;
389 if (url.host() == chrome::kChromeUIGCMInternalsHost)
390 return &NewWebUI<GCMInternalsUI>;
391 // Help is implemented with native UI elements on Android.
392 if (url.host() == chrome::kChromeUIHelpFrameHost)
393 return &NewWebUI<HelpUI>;
394 // Identity API is not available on Android.
395 if (url.host() == chrome::kChromeUIIdentityInternalsHost)
396 return &NewWebUI<IdentityInternalsUI>;
397 if (url.host() == chrome::kChromeUINewTabHost)
398 return &NewWebUI<NewTabUI>;
399 if (url.host() == chrome::kChromeUIMdSettingsHost)
400 return &NewWebUI<settings::MdSettingsUI>;
401 // If the material design extensions page is enabled, it gets its own host.
402 // Otherwise, it's handled by the uber settings page.
403 if (url.host() == chrome::kChromeUIExtensionsHost &&
404 ::switches::MdExtensionsEnabled()) {
405 return &NewWebUI<extensions::ExtensionsUI>;
407 if (url.host() == chrome::kChromeUIQuotaInternalsHost)
408 return &NewWebUI<QuotaInternalsUI>;
409 // Settings are implemented with native UI elements on Android.
410 // Handle chrome://settings if settings in a window and about in settings
411 // are enabled.
412 if (url.host() == chrome::kChromeUISettingsFrameHost ||
413 (url.host() == chrome::kChromeUISettingsHost &&
414 ::switches::AboutInSettingsEnabled())) {
415 return &NewWebUI<options::OptionsUI>;
417 if (url.host() == chrome::kChromeUISyncFileSystemInternalsHost)
418 return &NewWebUI<SyncFileSystemInternalsUI>;
419 if (url.host() == chrome::kChromeUISystemInfoHost)
420 return &NewWebUI<SystemInfoUI>;
421 // Uber frame is not used on Android.
422 if (url.host() == chrome::kChromeUIUberFrameHost)
423 return &NewWebUI<UberFrameUI>;
424 // Uber page is not used on Android.
425 if (url.host() == chrome::kChromeUIUberHost)
426 return &NewWebUI<UberUI>;
427 #endif // !defined(OS_ANDROID)
428 #if defined(OS_WIN)
429 if (url.host() == chrome::kChromeUIConflictsHost)
430 return &NewWebUI<ConflictsUI>;
431 if (url.host() == chrome::kChromeUIMetroFlowHost)
432 return &NewWebUI<SetAsDefaultBrowserUI>;
433 #endif
434 #if defined(OS_CHROMEOS)
435 if (url.host() == chrome::kChromeUIBluetoothPairingHost)
436 return &NewWebUI<chromeos::BluetoothPairingUI>;
437 if (url.host() == chrome::kChromeUICertificateManagerHost)
438 return &NewWebUI<chromeos::CertificateManagerDialogUI>;
439 if (url.host() == chrome::kChromeUIChooseMobileNetworkHost)
440 return &NewWebUI<chromeos::ChooseMobileNetworkUI>;
441 if (url.host() == chrome::kChromeUICryptohomeHost)
442 return &NewWebUI<chromeos::CryptohomeUI>;
443 if (url.host() == chrome::kChromeUIDriveInternalsHost)
444 return &NewWebUI<chromeos::DriveInternalsUI>;
445 if (url.host() == chrome::kChromeUIImageBurnerHost)
446 return &NewWebUI<ImageBurnUI>;
447 if (url.host() == chrome::kChromeUIFirstRunHost)
448 return &NewWebUI<chromeos::FirstRunUI>;
449 if (url.host() == chrome::kChromeUIKeyboardOverlayHost)
450 return &NewWebUI<KeyboardOverlayUI>;
451 if (url.host() == chrome::kChromeUIMobileSetupHost)
452 return &NewWebUI<MobileSetupUI>;
453 if (url.host() == chrome::kChromeUINetworkHost)
454 return &NewWebUI<chromeos::NetworkUI>;
455 if (url.host() == chrome::kChromeUINfcDebugHost)
456 return &NewWebUI<chromeos::NfcDebugUI>;
457 if (url.host() == chrome::kChromeUIOobeHost)
458 return &NewWebUI<chromeos::OobeUI>;
459 if (url.host() == chrome::kChromeUIPowerHost)
460 return &NewWebUI<chromeos::PowerUI>;
461 if (url.host() == proximity_auth::kChromeUIProximityAuthHost)
462 return &NewWebUI<proximity_auth::ProximityAuthUI>;
463 if (url.host() == chrome::kChromeUIProxySettingsHost)
464 return &NewWebUI<chromeos::ProxySettingsUI>;
465 if (url.host() == chrome::kChromeUISalsaHost)
466 return &NewWebUI<SalsaUI>;
467 if (url.host() == chrome::kChromeUISetTimeHost)
468 return &NewWebUI<chromeos::SetTimeUI>;
469 if (url.host() == chrome::kChromeUISimUnlockHost)
470 return &NewWebUI<chromeos::SimUnlockUI>;
471 if (url.host() == chrome::kChromeUISlowHost)
472 return &NewWebUI<chromeos::SlowUI>;
473 if (url.host() == chrome::kChromeUISlowTraceHost)
474 return &NewWebUI<chromeos::SlowTraceController>;
475 if (url.host() == chrome::kChromeUIVoiceSearchHost)
476 return &NewWebUI<VoiceSearchUI>;
477 #endif // defined(OS_CHROMEOS)
478 #if !defined(OFFICIAL_BUILD) && defined(OS_CHROMEOS) && !defined(NDEBUG)
479 if (!base::SysInfo::IsRunningOnChromeOS()) {
480 if (url.host() == chrome::kChromeUIDeviceEmulatorHost)
481 return &NewWebUI<DeviceEmulatorUI>;
483 #endif // !defined(OFFICIAL_BUILD) && defined(OS_CHROMEOS) && !defined(NDEBUG)
484 #if defined(OS_ANDROID) || defined(OS_IOS)
485 if (url.host() == chrome::kChromeUINetExportHost)
486 return &NewWebUI<NetExportUI>;
487 #else
488 if (url.host() == chrome::kChromeUICopresenceHost)
489 return &NewWebUI<CopresenceUI>;
490 if (url.host() == chrome::kChromeUIChromeSigninHost)
491 return &NewWebUI<InlineLoginUI>;
492 if (url.SchemeIs(content::kChromeDevToolsScheme))
493 return &NewWebUI<DevToolsUI>;
494 if (url.host() == chrome::kChromeUIWebRTCDeviceProviderHost)
495 return &NewWebUI<WebRTCDeviceProvider::WebUI>;
497 // chrome://inspect isn't supported on Android nor iOS. Page debugging is
498 // handled by a remote devtools on the host machine, and other elements, i.e.
499 // extensions aren't supported.
500 if (url.host() == chrome::kChromeUIInspectHost)
501 return &NewWebUI<InspectUI>;
502 #endif
503 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
504 if (url.host() == chrome::kChromeUIUserManagerHost &&
505 switches::IsNewAvatarMenu()) {
506 return &NewWebUI<UserManagerUI>;
508 #endif
510 /****************************************************************************
511 * Other #defines and special logics.
512 ***************************************************************************/
513 #if !defined(DISABLE_NACL)
514 if (url.host() == chrome::kChromeUINaClHost)
515 return &NewWebUI<NaClUI>;
516 #endif
517 #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA)
518 if (url.host() == chrome::kChromeUITabModalConfirmDialogHost) {
519 return &NewWebUI<ConstrainedWebDialogUI>;
521 #endif
522 #if defined(USE_AURA)
523 if (url.host() == chrome::kChromeUIGestureConfigHost)
524 return &NewWebUI<GestureConfigUI>;
525 #endif
526 #if (defined(USE_NSS_CERTS) || defined(USE_OPENSSL_CERTS)) && defined(USE_AURA)
527 if (url.host() == chrome::kChromeUICertificateViewerHost)
528 return &NewWebUI<CertificateViewerUI>;
529 #if defined(OS_CHROMEOS)
530 if (url.host() == chrome::kChromeUICertificateViewerDialogHost)
531 return &NewWebUI<CertificateViewerModalDialogUI>;
532 #endif
533 #endif // (USE_NSS_CERTS || USE_OPENSSL_CERTS) && USE_AURA
535 #if defined(ENABLE_CONFIGURATION_POLICY)
536 if (url.host() == chrome::kChromeUIPolicyHost)
537 return &NewWebUI<PolicyUI>;
538 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
539 if (url.host() == chrome::kChromeUIProfileSigninConfirmationHost)
540 return &NewWebUI<ProfileSigninConfirmationUI>;
541 #endif
542 #endif // defined(ENABLE_CONFIGURATION_POLICY)
544 #if defined(ENABLE_APP_LIST)
545 if (url.host() == chrome::kChromeUIAppListStartPageHost)
546 return &NewWebUI<app_list::StartPageUI>;
547 #endif
548 #if defined(ENABLE_EXTENSIONS)
549 if (url.host() == chrome::kChromeUIExtensionsFrameHost)
550 return &NewWebUI<extensions::ExtensionsUI>;
551 #endif
552 #if defined(ENABLE_PLUGINS)
553 if (url.host() == chrome::kChromeUIFlashHost)
554 return &NewWebUI<FlashUI>;
555 if (url.host() == chrome::kChromeUIPluginsHost)
556 return &NewWebUI<PluginsUI>;
557 #endif
558 #if defined(ENABLE_PRINT_PREVIEW)
559 if (url.host() == chrome::kChromeUIPrintHost &&
560 !profile->GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled)) {
561 return &NewWebUI<PrintPreviewUI>;
563 #endif
564 #if defined(ENABLE_SERVICE_DISCOVERY)
565 if (url.host() == chrome::kChromeUIDevicesHost) {
566 return &NewWebUI<LocalDiscoveryUI>;
568 #endif
569 #if defined(ENABLE_WEBRTC)
570 if (url.host() == chrome::kChromeUIWebRtcLogsHost)
571 return &NewWebUI<WebRtcLogsUI>;
572 #endif
573 #if defined(ENABLE_MEDIA_ROUTER) && !defined(OS_ANDROID)
574 if (url.host() == chrome::kChromeUIMediaRouterHost &&
575 switches::MediaRouterEnabled()) {
576 return &NewWebUI<media_router::MediaRouterUI>;
578 #endif
579 if (IsAboutUI(url))
580 return &NewWebUI<AboutUI>;
582 if (dom_distiller::IsEnableDomDistillerSet() &&
583 url.host() == dom_distiller::kChromeUIDomDistillerHost) {
584 return &NewWebUI<dom_distiller::DomDistillerUi>;
587 if (SiteEngagementService::IsEnabled() &&
588 url.host() == chrome::kChromeUISiteEngagementHost) {
589 return &NewWebUI<SiteEngagementUI>;
592 return NULL;
595 void RunFaviconCallbackAsync(
596 const favicon_base::FaviconResultsCallback& callback,
597 const std::vector<favicon_base::FaviconRawBitmapResult>* results) {
598 base::ThreadTaskRunnerHandle::Get()->PostTask(
599 FROM_HERE,
600 base::Bind(&favicon::FaviconService::FaviconResultsCallbackRunner,
601 callback, base::Owned(results)));
604 } // namespace
606 WebUI::TypeID ChromeWebUIControllerFactory::GetWebUIType(
607 content::BrowserContext* browser_context, const GURL& url) const {
608 Profile* profile = Profile::FromBrowserContext(browser_context);
609 WebUIFactoryFunction function = GetWebUIFactoryFunction(NULL, profile, url);
610 return function ? reinterpret_cast<WebUI::TypeID>(function) : WebUI::kNoWebUI;
613 bool ChromeWebUIControllerFactory::UseWebUIForURL(
614 content::BrowserContext* browser_context, const GURL& url) const {
615 return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
618 bool ChromeWebUIControllerFactory::UseWebUIBindingsForURL(
619 content::BrowserContext* browser_context, const GURL& url) const {
620 bool needs_extensions_web_ui = false;
621 #if defined(ENABLE_EXTENSIONS)
622 // Extensions are rendered via WebUI in tabs, but don't actually need WebUI
623 // bindings (see the ExtensionWebUI constructor).
624 needs_extensions_web_ui =
625 NeedsExtensionWebUI(Profile::FromBrowserContext(browser_context), url);
626 #endif
627 return !needs_extensions_web_ui && UseWebUIForURL(browser_context, url);
630 WebUIController* ChromeWebUIControllerFactory::CreateWebUIControllerForURL(
631 WebUI* web_ui,
632 const GURL& url) const {
633 Profile* profile = Profile::FromWebUI(web_ui);
634 WebUIFactoryFunction function = GetWebUIFactoryFunction(web_ui, profile, url);
635 if (!function)
636 return NULL;
638 return (*function)(web_ui, url);
641 void ChromeWebUIControllerFactory::GetFaviconForURL(
642 Profile* profile,
643 const GURL& page_url,
644 const std::vector<int>& desired_sizes_in_pixel,
645 const favicon_base::FaviconResultsCallback& callback) const {
646 // Before determining whether page_url is an extension url, we must handle
647 // overrides. This changes urls in |kChromeUIScheme| to extension urls, and
648 // allows to use ExtensionWebUI::GetFaviconForURL.
649 GURL url(page_url);
650 #if defined(ENABLE_EXTENSIONS)
651 ExtensionWebUI::HandleChromeURLOverride(&url, profile);
653 // All extensions but the bookmark manager get their favicon from the icons
654 // part of the manifest.
655 if (url.SchemeIs(extensions::kExtensionScheme) &&
656 url.host() != extension_misc::kBookmarkManagerId) {
657 ExtensionWebUI::GetFaviconForURL(profile, url, callback);
658 return;
660 #endif
662 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results =
663 new std::vector<favicon_base::FaviconRawBitmapResult>();
665 // Use ui::GetSupportedScaleFactors instead of
666 // favicon_base::GetFaviconScales() because chrome favicons comes from
667 // resources.
668 std::vector<ui::ScaleFactor> resource_scale_factors =
669 ui::GetSupportedScaleFactors();
671 std::vector<gfx::Size> candidate_sizes;
672 for (size_t i = 0; i < resource_scale_factors.size(); ++i) {
673 float scale = ui::GetScaleForScaleFactor(resource_scale_factors[i]);
674 // Assume that GetFaviconResourceBytes() returns favicons which are
675 // |gfx::kFaviconSize| x |gfx::kFaviconSize| DIP.
676 int candidate_edge_size =
677 static_cast<int>(gfx::kFaviconSize * scale + 0.5f);
678 candidate_sizes.push_back(
679 gfx::Size(candidate_edge_size, candidate_edge_size));
681 std::vector<size_t> selected_indices;
682 SelectFaviconFrameIndices(
683 candidate_sizes, desired_sizes_in_pixel, &selected_indices, NULL);
684 for (size_t i = 0; i < selected_indices.size(); ++i) {
685 size_t selected_index = selected_indices[i];
686 ui::ScaleFactor selected_resource_scale =
687 resource_scale_factors[selected_index];
689 scoped_refptr<base::RefCountedMemory> bitmap(
690 GetFaviconResourceBytes(url, selected_resource_scale));
691 if (bitmap.get() && bitmap->size()) {
692 favicon_base::FaviconRawBitmapResult bitmap_result;
693 bitmap_result.bitmap_data = bitmap;
694 // Leave |bitmap_result|'s icon URL as the default of GURL().
695 bitmap_result.icon_type = favicon_base::FAVICON;
696 favicon_bitmap_results->push_back(bitmap_result);
698 bitmap_result.pixel_size = candidate_sizes[selected_index];
702 RunFaviconCallbackAsync(callback, favicon_bitmap_results);
705 // static
706 ChromeWebUIControllerFactory* ChromeWebUIControllerFactory::GetInstance() {
707 return base::Singleton<ChromeWebUIControllerFactory>::get();
710 ChromeWebUIControllerFactory::ChromeWebUIControllerFactory() {
713 ChromeWebUIControllerFactory::~ChromeWebUIControllerFactory() {
716 base::RefCountedMemory* ChromeWebUIControllerFactory::GetFaviconResourceBytes(
717 const GURL& page_url, ui::ScaleFactor scale_factor) const {
718 #if !defined(OS_ANDROID) // Bookmarks are part of NTP on Android.
719 // The bookmark manager is a chrome extension, so we have to check for it
720 // before we check for extension scheme.
721 if (page_url.host() == extension_misc::kBookmarkManagerId)
722 return BookmarksUI::GetFaviconResourceBytes(scale_factor);
724 // The extension scheme is handled in GetFaviconForURL.
725 if (page_url.SchemeIs(extensions::kExtensionScheme)) {
726 NOTREACHED();
727 return NULL;
729 #endif
731 if (!content::HasWebUIScheme(page_url))
732 return NULL;
734 if (page_url.host() == chrome::kChromeUIComponentsHost)
735 return ComponentsUI::GetFaviconResourceBytes(scale_factor);
737 #if defined(OS_WIN)
738 if (page_url.host() == chrome::kChromeUIConflictsHost)
739 return ConflictsUI::GetFaviconResourceBytes(scale_factor);
740 #endif
742 if (page_url.host() == chrome::kChromeUICrashesHost)
743 return CrashesUI::GetFaviconResourceBytes(scale_factor);
745 if (page_url.host() == chrome::kChromeUIFlagsHost)
746 return FlagsUI::GetFaviconResourceBytes(scale_factor);
748 if (page_url.host() == chrome::kChromeUIHistoryHost)
749 return HistoryUI::GetFaviconResourceBytes(scale_factor);
751 #if !defined(OS_ANDROID)
752 #if !defined(OS_CHROMEOS)
753 // The Apps launcher page is not available on android or ChromeOS.
754 if (page_url.host() == chrome::kChromeUIAppLauncherPageHost)
755 return AppLauncherPageUI::GetFaviconResourceBytes(scale_factor);
756 #endif
758 // Flash is not available on android.
759 if (page_url.host() == chrome::kChromeUIFlashHost)
760 return FlashUI::GetFaviconResourceBytes(scale_factor);
762 // Android uses the native download manager.
763 if (page_url.host() == chrome::kChromeUIDownloadsHost)
764 return DownloadsUI::GetFaviconResourceBytes(scale_factor);
766 // Android doesn't use the Options pages.
767 if (page_url.host() == chrome::kChromeUISettingsHost ||
768 page_url.host() == chrome::kChromeUISettingsFrameHost)
769 return options::OptionsUI::GetFaviconResourceBytes(scale_factor);
771 #if defined(ENABLE_EXTENSIONS)
772 if (page_url.host() == chrome::kChromeUIExtensionsHost ||
773 page_url.host() == chrome::kChromeUIExtensionsFrameHost)
774 return extensions::ExtensionsUI::GetFaviconResourceBytes(scale_factor);
775 #endif
777 // Android doesn't use the plugins pages.
778 if (page_url.host() == chrome::kChromeUIPluginsHost)
779 return PluginsUI::GetFaviconResourceBytes(scale_factor);
781 #endif
783 return NULL;