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