Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chrome_web_ui_controller_factory.cc
blob9a64aa403e77809d5a99046336844c187297fd8d
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 "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/about_flags.h"
13 #include "chrome/browser/bookmarks/enhanced_bookmarks_features.h"
14 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_web_ui.h"
17 #include "chrome/browser/history/history_types.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/ui/webui/about_ui.h"
21 #include "chrome/browser/ui/webui/app_launcher_page_ui.h"
22 #include "chrome/browser/ui/webui/bookmarks_ui.h"
23 #include "chrome/browser/ui/webui/components_ui.h"
24 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
25 #include "chrome/browser/ui/webui/crashes_ui.h"
26 #include "chrome/browser/ui/webui/devtools_ui.h"
27 #include "chrome/browser/ui/webui/downloads_ui.h"
28 #include "chrome/browser/ui/webui/extensions/extension_info_ui.h"
29 #include "chrome/browser/ui/webui/extensions/extensions_ui.h"
30 #include "chrome/browser/ui/webui/flags_ui.h"
31 #include "chrome/browser/ui/webui/flash_ui.h"
32 #include "chrome/browser/ui/webui/help/help_ui.h"
33 #include "chrome/browser/ui/webui/history_ui.h"
34 #include "chrome/browser/ui/webui/identity_internals_ui.h"
35 #include "chrome/browser/ui/webui/inspect_ui.h"
36 #include "chrome/browser/ui/webui/instant_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/ntp/new_tab_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/performance_monitor/performance_monitor_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/quota_internals/quota_internals_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/sync_internals_ui.h"
52 #include "chrome/browser/ui/webui/system_info_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/extensions/extension_constants.h"
58 #include "chrome/common/pref_names.h"
59 #include "chrome/common/profile_management_switches.h"
60 #include "chrome/common/url_constants.h"
61 #include "components/dom_distiller/core/dom_distiller_constants.h"
62 #include "components/dom_distiller/core/dom_distiller_service.h"
63 #include "components/dom_distiller/webui/dom_distiller_ui.h"
64 #include "content/public/browser/web_contents.h"
65 #include "content/public/browser/web_ui.h"
66 #include "content/public/common/content_client.h"
67 #include "content/public/common/url_utils.h"
68 #include "extensions/common/constants.h"
69 #include "extensions/common/feature_switch.h"
70 #include "ui/gfx/favicon_size.h"
71 #include "ui/web_dialogs/web_dialog_ui.h"
72 #include "url/gurl.h"
74 #if !defined(DISABLE_NACL)
75 #include "chrome/browser/ui/webui/nacl_ui.h"
76 #endif
78 #if defined(ENABLE_CONFIGURATION_POLICY)
79 #include "chrome/browser/ui/webui/policy_ui.h"
80 #endif
82 #if defined(ENABLE_WEBRTC)
83 #include "chrome/browser/ui/webui/media/webrtc_logs_ui.h"
84 #endif
86 #if defined(ENABLE_FULL_PRINTING)
87 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
88 #endif
90 #if defined(OS_ANDROID)
91 #include "chrome/browser/ui/webui/welcome_ui_android.h"
92 #else
93 #include "chrome/browser/ui/webui/suggestions_internals/suggestions_internals_ui.h"
94 #include "chrome/browser/ui/webui/sync_file_system_internals/sync_file_system_internals_ui.h"
95 #include "chrome/browser/ui/webui/uber/uber_ui.h"
96 #endif
98 #if defined(OS_ANDROID) || defined(OS_IOS)
99 #include "chrome/browser/ui/webui/net_export_ui.h"
100 #endif
102 #if defined(OS_CHROMEOS)
103 #include "chrome/browser/ui/webui/chromeos/bluetooth_pairing_ui.h"
104 #include "chrome/browser/ui/webui/chromeos/charger_replacement_ui.h"
105 #include "chrome/browser/ui/webui/chromeos/choose_mobile_network_ui.h"
106 #include "chrome/browser/ui/webui/chromeos/cryptohome_ui.h"
107 #include "chrome/browser/ui/webui/chromeos/drive_internals_ui.h"
108 #include "chrome/browser/ui/webui/chromeos/first_run/first_run_ui.h"
109 #include "chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.h"
110 #include "chrome/browser/ui/webui/chromeos/keyboard_overlay_ui.h"
111 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
112 #include "chrome/browser/ui/webui/chromeos/mobile_setup_ui.h"
113 #include "chrome/browser/ui/webui/chromeos/network_ui.h"
114 #include "chrome/browser/ui/webui/chromeos/power_ui.h"
115 #include "chrome/browser/ui/webui/chromeos/proxy_settings_ui.h"
116 #include "chrome/browser/ui/webui/chromeos/salsa_ui.h"
117 #include "chrome/browser/ui/webui/chromeos/sim_unlock_ui.h"
118 #include "chrome/browser/ui/webui/chromeos/slow_trace_ui.h"
119 #include "chrome/browser/ui/webui/chromeos/slow_ui.h"
120 #endif
122 #if defined(USE_AURA)
123 #include "chrome/browser/ui/webui/gesture_config_ui.h"
124 #include "ui/keyboard/keyboard_constants.h"
125 #include "ui/keyboard/keyboard_ui_controller.h"
126 #endif
128 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
129 #include "chrome/browser/ui/sync/sync_promo_ui.h"
130 #endif
132 #if defined(OS_WIN)
133 #include "chrome/browser/ui/webui/conflicts_ui.h"
134 #include "chrome/browser/ui/webui/set_as_default_browser_ui.h"
135 #endif
137 #if (defined(USE_NSS) || defined(USE_OPENSSL)) && defined(USE_AURA)
138 #include "chrome/browser/ui/webui/certificate_viewer_ui.h"
139 #endif
141 #if defined(ENABLE_MDNS)
142 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui.h"
143 #endif
145 #if defined(ENABLE_APP_LIST)
146 #include "chrome/browser/ui/webui/app_list/start_page_ui.h"
147 #endif
149 using content::WebUI;
150 using content::WebUIController;
151 using ui::ExternalWebDialogUI;
152 using ui::WebDialogUI;
154 namespace {
156 // A function for creating a new WebUI. The caller owns the return value, which
157 // may be NULL (for example, if the URL refers to an non-existent extension).
158 typedef WebUIController* (*WebUIFactoryFunction)(WebUI* web_ui,
159 const GURL& url);
161 // Template for defining WebUIFactoryFunction.
162 template<class T>
163 WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
164 return new T(web_ui);
167 #if defined(ENABLE_EXTENSIONS)
168 // Special cases for extensions.
169 template<>
170 WebUIController* NewWebUI<ExtensionWebUI>(WebUI* web_ui,
171 const GURL& url) {
172 return new ExtensionWebUI(web_ui, url);
175 template<>
176 WebUIController* NewWebUI<extensions::ExtensionInfoUI>(WebUI* web_ui,
177 const GURL& url) {
178 return new extensions::ExtensionInfoUI(web_ui, url);
180 #endif // defined(ENABLE_EXTENSIONS)
182 // Special case for older about: handlers.
183 template<>
184 WebUIController* NewWebUI<AboutUI>(WebUI* web_ui, const GURL& url) {
185 return new AboutUI(web_ui, url.host());
188 #if defined(OS_CHROMEOS)
189 template<>
190 WebUIController* NewWebUI<chromeos::OobeUI>(WebUI* web_ui, const GURL& url) {
191 return new chromeos::OobeUI(web_ui, url);
193 #endif
195 // Special cases for DOM distiller.
196 template<>
197 WebUIController* NewWebUI<dom_distiller::DomDistillerUi>(WebUI* web_ui,
198 const GURL& url) {
199 // The DomDistillerUi can not depend on components/dom_distiller/content,
200 // so inject the correct DomDistillerService from chrome/.
201 content::BrowserContext* browser_context =
202 web_ui->GetWebContents()->GetBrowserContext();
203 dom_distiller::DomDistillerService* service =
204 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
205 browser_context);
206 // TODO(nyquist): Add real scheme.
207 return new dom_distiller::DomDistillerUi(web_ui, service, "dummy");
210 // Only create ExtensionWebUI for URLs that are allowed extension bindings,
211 // hosted by actual tabs.
212 bool NeedsExtensionWebUI(Profile* profile, const GURL& url) {
213 ExtensionService* service = profile ? profile->GetExtensionService() : NULL;
214 return service && service->ExtensionBindingsAllowed(url);
217 // Returns a function that can be used to create the right type of WebUI for a
218 // tab, based on its URL. Returns NULL if the URL doesn't have WebUI associated
219 // with it.
220 WebUIFactoryFunction GetWebUIFactoryFunction(WebUI* web_ui,
221 Profile* profile,
222 const GURL& url) {
223 #if defined(ENABLE_EXTENSIONS)
224 if (NeedsExtensionWebUI(profile, url))
225 return &NewWebUI<ExtensionWebUI>;
226 #endif
228 // This will get called a lot to check all URLs, so do a quick check of other
229 // schemes to filter out most URLs.
230 if (!url.SchemeIs(chrome::kChromeDevToolsScheme) &&
231 !url.SchemeIs(chrome::kChromeUIScheme)) {
232 return NULL;
235 /****************************************************************************
236 * Please keep this in alphabetical order. If #ifs or special logics are
237 * required, add it below in the appropriate section.
238 ***************************************************************************/
239 // We must compare hosts only since some of the Web UIs append extra stuff
240 // after the host name.
241 // All platform builds of Chrome will need to have a cloud printing
242 // dialog as backup. It's just that on Chrome OS, it's the only
243 // print dialog.
244 if (url.host() == chrome::kChromeUICloudPrintResourcesHost)
245 return &NewWebUI<ExternalWebDialogUI>;
246 if (url.host() == chrome::kChromeUICloudPrintSetupHost)
247 return &NewWebUI<WebDialogUI>;
248 if (url.host() == chrome::kChromeUIComponentsHost)
249 return &NewWebUI<ComponentsUI>;
250 if (url.spec() == chrome::kChromeUIConstrainedHTMLTestURL)
251 return &NewWebUI<ConstrainedWebDialogUI>;
252 if (url.host() == chrome::kChromeUICrashesHost)
253 return &NewWebUI<CrashesUI>;
254 #if defined(ENABLE_MDNS)
255 if (url.host() == chrome::kChromeUIDevicesHost &&
256 !CommandLine::ForCurrentProcess()->HasSwitch(
257 switches::kDisableDeviceDiscovery)) {
258 return &NewWebUI<LocalDiscoveryUI>;
260 #endif
261 if (url.host() == chrome::kChromeUIFlagsHost)
262 return &NewWebUI<FlagsUI>;
263 if (url.host() == chrome::kChromeUIHistoryFrameHost)
264 return &NewWebUI<HistoryUI>;
265 if (url.host() == chrome::kChromeUIInstantHost)
266 return &NewWebUI<InstantUI>;
267 if (url.host() == chrome::kChromeUIManagedUserPassphrasePageHost)
268 return &NewWebUI<ConstrainedWebDialogUI>;
269 if (url.host() == chrome::kChromeUIMemoryInternalsHost)
270 return &NewWebUI<MemoryInternalsUI>;
271 #if !defined(DISABLE_NACL)
272 if (url.host() == chrome::kChromeUINaClHost)
273 return &NewWebUI<NaClUI>;
274 #endif
275 #if defined(OS_ANDROID) || defined(OS_IOS)
276 if (url.host() == chrome::kChromeUINetExportHost)
277 return &NewWebUI<NetExportUI>;
278 #endif
279 if (url.host() == chrome::kChromeUINetInternalsHost)
280 return &NewWebUI<NetInternalsUI>;
281 if (url.host() == chrome::kChromeUINewTabHost)
282 return &NewWebUI<NewTabUI>;
283 if (url.host() == chrome::kChromeUIOmniboxHost)
284 return &NewWebUI<OmniboxUI>;
285 if (url.host() == chrome::kChromeUIPredictorsHost)
286 return &NewWebUI<PredictorsUI>;
287 if (url.host() == chrome::kChromeUIProfilerHost)
288 return &NewWebUI<ProfilerUI>;
289 if (url.host() == chrome::kChromeUIQuotaInternalsHost)
290 return &NewWebUI<QuotaInternalsUI>;
291 if (url.host() == chrome::kChromeUISignInInternalsHost)
292 return &NewWebUI<SignInInternalsUI>;
293 if (url.host() == chrome::kChromeUISyncInternalsHost)
294 return &NewWebUI<SyncInternalsUI>;
295 if (url.host() == chrome::kChromeUISyncResourcesHost)
296 return &NewWebUI<WebDialogUI>;
297 if (url.host() == chrome::kChromeUITranslateInternalsHost)
298 return &NewWebUI<TranslateInternalsUI>;
299 if (url.host() == chrome::kChromeUIUserActionsHost)
300 return &NewWebUI<UserActionsUI>;
301 if (url.host() == chrome::kChromeUIVersionHost)
302 return &NewWebUI<VersionUI>;
303 #if defined(ENABLE_WEBRTC)
304 if (url.host() == chrome::kChromeUIWebRtcLogsHost)
305 return &NewWebUI<WebRtcLogsUI>;
306 #endif
307 #if defined(ENABLE_APP_LIST)
308 if (url.host() == chrome::kChromeUIAppListStartPageHost)
309 return &NewWebUI<app_list::StartPageUI>;
310 #endif
312 /****************************************************************************
313 * OS Specific #defines
314 ***************************************************************************/
315 #if defined(OS_ANDROID)
316 if (url.host() == chrome::kChromeUIWelcomeHost)
317 return &NewWebUI<WelcomeUI>;
318 #else
319 // AppLauncherPage is not needed on Android.
320 if (url.host() == chrome::kChromeUIAppLauncherPageHost &&
321 profile && profile->GetExtensionService()) {
322 return &NewWebUI<AppLauncherPageUI>;
324 // Bookmarks are part of NTP on Android.
325 if (url.host() == chrome::kChromeUIBookmarksHost)
326 return &NewWebUI<BookmarksUI>;
327 if (url.SchemeIs(chrome::kChromeDevToolsScheme))
328 return &NewWebUI<DevToolsUI>;
329 // Downloads list on Android uses the built-in download manager.
330 if (url.host() == chrome::kChromeUIDownloadsHost)
331 return &NewWebUI<DownloadsUI>;
332 #if defined(ENABLE_ENHANCED_BOOKMARKS)
333 // Bookmarks are part of NTP on Android.
334 if (url.host() == chrome::kChromeUIEnhancedBookmarksHost)
335 return &NewWebUI<BookmarksUI>;
336 #endif
337 // Flash is not available on android.
338 if (url.host() == chrome::kChromeUIFlashHost)
339 return &NewWebUI<FlashUI>;
340 // Help is implemented with native UI elements on Android.
341 if (url.host() == chrome::kChromeUIHelpFrameHost)
342 return &NewWebUI<HelpUI>;
343 // Identity API is not available on Android.
344 if (url.host() == chrome::kChromeUIIdentityInternalsHost)
345 return &NewWebUI<IdentityInternalsUI>;
346 // chrome://inspect isn't supported on Android. Page debugging is handled by a
347 // remote devtools on the host machine, and other elements (Shared Workers,
348 // extensions, etc) aren't supported.
349 if (url.host() == chrome::kChromeUIInspectHost)
350 return &NewWebUI<InspectUI>;
351 // Performance monitoring page is not on Android for now.
352 if (url.host() == chrome::kChromeUIPerformanceMonitorHost)
353 return &NewWebUI<performance_monitor::PerformanceMonitorUI>;
354 // Android does not support plugins for now.
355 if (url.host() == chrome::kChromeUIPluginsHost)
356 return &NewWebUI<PluginsUI>;
357 // Settings are implemented with native UI elements on Android.
358 if (url.host() == chrome::kChromeUISettingsFrameHost)
359 return &NewWebUI<options::OptionsUI>;
360 if (url.host() == chrome::kChromeUISuggestionsInternalsHost)
361 return &NewWebUI<SuggestionsInternalsUI>;
362 if (url.host() == chrome::kChromeUISyncFileSystemInternalsHost)
363 return &NewWebUI<SyncFileSystemInternalsUI>;
364 if (url.host() == chrome::kChromeUISystemInfoHost)
365 return &NewWebUI<SystemInfoUI>;
366 // Uber frame is not used on Android.
367 if (url.host() == chrome::kChromeUIUberFrameHost)
368 return &NewWebUI<UberFrameUI>;
369 // Uber page is not used on Android.
370 if (url.host() == chrome::kChromeUIUberHost)
371 return &NewWebUI<UberUI>;
372 #endif
373 #if defined(OS_WIN)
374 if (url.host() == chrome::kChromeUIConflictsHost)
375 return &NewWebUI<ConflictsUI>;
376 if (url.host() == chrome::kChromeUIMetroFlowHost)
377 return &NewWebUI<SetAsDefaultBrowserUI>;
378 #endif
379 #if (defined(USE_NSS) || defined(USE_OPENSSL)) && defined(USE_AURA)
380 if (url.host() == chrome::kChromeUICertificateViewerHost)
381 return &NewWebUI<CertificateViewerUI>;
382 #endif
383 #if defined(OS_CHROMEOS)
384 if (url.host() == chrome::kChromeUIBluetoothPairingHost)
385 return &NewWebUI<chromeos::BluetoothPairingUI>;
386 if (url.host() == chrome::kChromeUIChargerReplacementHost)
387 return &NewWebUI<chromeos::ChargerReplacementUI>;
388 if (url.host() == chrome::kChromeUIChooseMobileNetworkHost)
389 return &NewWebUI<chromeos::ChooseMobileNetworkUI>;
390 if (url.host() == chrome::kChromeUICryptohomeHost)
391 return &NewWebUI<chromeos::CryptohomeUI>;
392 if (url.host() == chrome::kChromeUIDriveInternalsHost)
393 return &NewWebUI<chromeos::DriveInternalsUI>;
394 if (url.host() == chrome::kChromeUIFirstRunHost)
395 return &NewWebUI<chromeos::FirstRunUI>;
396 if (url.host() == chrome::kChromeUIImageBurnerHost)
397 return &NewWebUI<ImageBurnUI>;
398 if (url.host() == chrome::kChromeUIKeyboardOverlayHost)
399 return &NewWebUI<KeyboardOverlayUI>;
400 if (url.host() == chrome::kChromeUIMobileSetupHost)
401 return &NewWebUI<MobileSetupUI>;
402 if (url.host() == chrome::kChromeUIOobeHost)
403 return &NewWebUI<chromeos::OobeUI>;
404 if (url.host() == chrome::kChromeUIProxySettingsHost)
405 return &NewWebUI<chromeos::ProxySettingsUI>;
406 if (url.host() == chrome::kChromeUISalsaHost)
407 return &NewWebUI<SalsaUI>;
408 if (url.host() == chrome::kChromeUISimUnlockHost)
409 return &NewWebUI<chromeos::SimUnlockUI>;
410 if (url.host() == chrome::kChromeUISlowHost)
411 return &NewWebUI<chromeos::SlowUI>;
412 if (url.host() == chrome::kChromeUISlowTraceHost)
413 return &NewWebUI<chromeos::SlowTraceController>;
414 if (url.host() == chrome::kChromeUINetworkHost)
415 return &NewWebUI<chromeos::NetworkUI>;
416 if (url.host() == chrome::kChromeUIPowerHost)
417 return &NewWebUI<chromeos::PowerUI>;
418 #endif // defined(OS_CHROMEOS)
419 #if !defined(OS_ANDROID) && !defined(OS_IOS)
420 if (url.host() == chrome::kChromeUIChromeSigninHost)
421 return &NewWebUI<InlineLoginUI>;
422 #endif
424 /****************************************************************************
425 * Other #defines and special logics.
426 ***************************************************************************/
427 #if defined(ENABLE_CONFIGURATION_POLICY)
428 if (url.host() == chrome::kChromeUIPolicyHost)
429 return &NewWebUI<PolicyUI>;
431 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
432 if (url.host() == chrome::kChromeUIProfileSigninConfirmationHost)
433 return &NewWebUI<ProfileSigninConfirmationUI>;
434 #endif
436 #endif // defined(ENABLE_CONFIGURATION_POLICY)
438 #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA)
439 if (url.host() == chrome::kChromeUITabModalConfirmDialogHost) {
440 return &NewWebUI<ConstrainedWebDialogUI>;
442 #endif
444 #if defined(USE_AURA)
445 if (url.host() == chrome::kChromeUIGestureConfigHost)
446 return &NewWebUI<GestureConfigUI>;
447 if (url.host() == keyboard::kKeyboardWebUIHost)
448 return &NewWebUI<keyboard::KeyboardUIController>;
449 #endif
451 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
452 if (url.host() == chrome::kChromeUIUserManagerHost &&
453 switches::IsNewProfileManagement()) {
454 return &NewWebUI<UserManagerUI>;
456 #endif
458 if (url.host() == chrome::kChromeUIChromeURLsHost ||
459 url.host() == chrome::kChromeUICreditsHost ||
460 url.host() == chrome::kChromeUIDNSHost ||
461 url.host() == chrome::kChromeUIMemoryHost ||
462 url.host() == chrome::kChromeUIMemoryRedirectHost ||
463 url.host() == chrome::kChromeUIStatsHost ||
464 url.host() == chrome::kChromeUITermsHost
465 #if defined(OS_LINUX) || defined(OS_OPENBSD)
466 || url.host() == chrome::kChromeUILinuxProxyConfigHost
467 || url.host() == chrome::kChromeUISandboxHost
468 #endif
469 #if defined(OS_CHROMEOS)
470 || url.host() == chrome::kChromeUIDiscardsHost
471 || url.host() == chrome::kChromeUIOSCreditsHost
472 #endif
473 #if defined(WEBUI_TASK_MANAGER)
474 || url.host() == chrome::kChromeUITaskManagerHost
475 #endif
477 return &NewWebUI<AboutUI>;
480 #if defined(ENABLE_EXTENSIONS)
481 if (url.host() == chrome::kChromeUIExtensionInfoHost &&
482 extensions::FeatureSwitch::script_badges()->IsEnabled()) {
483 return &NewWebUI<extensions::ExtensionInfoUI>;
485 if (url.host() == chrome::kChromeUIExtensionsFrameHost)
486 return &NewWebUI<extensions::ExtensionsUI>;
487 #endif
488 #if defined(ENABLE_FULL_PRINTING)
489 if (url.host() == chrome::kChromeUIPrintHost &&
490 !profile->GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled))
491 return &NewWebUI<PrintPreviewUI>;
492 #endif
494 if (IsEnableDomDistillerSet() &&
495 url.host() == dom_distiller::kChromeUIDomDistillerHost) {
496 return &NewWebUI<dom_distiller::DomDistillerUi>;
499 return NULL;
502 void RunFaviconCallbackAsync(
503 const FaviconService::FaviconResultsCallback& callback,
504 const std::vector<chrome::FaviconBitmapResult>* results) {
505 base::MessageLoopProxy::current()->PostTask(
506 FROM_HERE,
507 base::Bind(&FaviconService::FaviconResultsCallbackRunner,
508 callback, base::Owned(results)));
511 } // namespace
513 WebUI::TypeID ChromeWebUIControllerFactory::GetWebUIType(
514 content::BrowserContext* browser_context, const GURL& url) const {
515 Profile* profile = Profile::FromBrowserContext(browser_context);
516 WebUIFactoryFunction function = GetWebUIFactoryFunction(NULL, profile, url);
517 return function ? reinterpret_cast<WebUI::TypeID>(function) : WebUI::kNoWebUI;
520 bool ChromeWebUIControllerFactory::UseWebUIForURL(
521 content::BrowserContext* browser_context, const GURL& url) const {
522 return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
525 bool ChromeWebUIControllerFactory::UseWebUIBindingsForURL(
526 content::BrowserContext* browser_context, const GURL& url) const {
527 // Extensions are rendered via WebUI in tabs, but don't actually need WebUI
528 // bindings (see the ExtensionWebUI constructor).
529 return
530 !NeedsExtensionWebUI(Profile::FromBrowserContext(browser_context), url) &&
531 UseWebUIForURL(browser_context, url);
534 WebUIController* ChromeWebUIControllerFactory::CreateWebUIControllerForURL(
535 WebUI* web_ui,
536 const GURL& url) const {
537 Profile* profile = Profile::FromWebUI(web_ui);
538 WebUIFactoryFunction function = GetWebUIFactoryFunction(web_ui, profile, url);
539 if (!function)
540 return NULL;
542 return (*function)(web_ui, url);
545 void ChromeWebUIControllerFactory::GetFaviconForURL(
546 Profile* profile,
547 const GURL& page_url,
548 const std::vector<ui::ScaleFactor>& scale_factors,
549 const FaviconService::FaviconResultsCallback& callback) const {
550 // Before determining whether page_url is an extension url, we must handle
551 // overrides. This changes urls in |kChromeUIScheme| to extension urls, and
552 // allows to use ExtensionWebUI::GetFaviconForURL.
553 GURL url(page_url);
554 ExtensionWebUI::HandleChromeURLOverride(&url, profile);
556 // All extensions but the bookmark manager get their favicon from the icons
557 // part of the manifest.
558 if (url.SchemeIs(extensions::kExtensionScheme) &&
559 url.host() != extension_misc::kBookmarkManagerId) {
560 #if defined(ENABLE_EXTENSIONS)
561 ExtensionWebUI::GetFaviconForURL(profile, url, callback);
562 #else
563 RunFaviconCallbackAsync(callback,
564 new std::vector<chrome::FaviconBitmapResult>());
565 #endif
566 return;
569 std::vector<chrome::FaviconBitmapResult>* favicon_bitmap_results =
570 new std::vector<chrome::FaviconBitmapResult>();
572 for (size_t i = 0; i < scale_factors.size(); ++i) {
573 scoped_refptr<base::RefCountedMemory> bitmap(GetFaviconResourceBytes(
574 url, scale_factors[i]));
575 if (bitmap.get() && bitmap->size()) {
576 chrome::FaviconBitmapResult bitmap_result;
577 bitmap_result.bitmap_data = bitmap;
578 // Leave |bitmap_result|'s icon URL as the default of GURL().
579 bitmap_result.icon_type = chrome::FAVICON;
580 favicon_bitmap_results->push_back(bitmap_result);
582 // Assume that |bitmap| is |gfx::kFaviconSize| x |gfx::kFaviconSize|
583 // DIP.
584 float scale = ui::GetImageScale(scale_factors[i]);
585 int edge_pixel_size =
586 static_cast<int>(gfx::kFaviconSize * scale + 0.5f);
587 bitmap_result.pixel_size = gfx::Size(edge_pixel_size, edge_pixel_size);
591 RunFaviconCallbackAsync(callback, favicon_bitmap_results);
594 // static
595 ChromeWebUIControllerFactory* ChromeWebUIControllerFactory::GetInstance() {
596 return Singleton<ChromeWebUIControllerFactory>::get();
599 ChromeWebUIControllerFactory::ChromeWebUIControllerFactory() {
602 ChromeWebUIControllerFactory::~ChromeWebUIControllerFactory() {
605 base::RefCountedMemory* ChromeWebUIControllerFactory::GetFaviconResourceBytes(
606 const GURL& page_url, ui::ScaleFactor scale_factor) const {
607 #if !defined(OS_ANDROID) // Bookmarks are part of NTP on Android.
608 // The bookmark manager is a chrome extension, so we have to check for it
609 // before we check for extension scheme.
610 if (page_url.host() == extension_misc::kBookmarkManagerId)
611 return BookmarksUI::GetFaviconResourceBytes(scale_factor);
613 // The extension scheme is handled in GetFaviconForURL.
614 if (page_url.SchemeIs(extensions::kExtensionScheme)) {
615 NOTREACHED();
616 return NULL;
618 #endif
620 if (!content::HasWebUIScheme(page_url))
621 return NULL;
623 if (page_url.host() == chrome::kChromeUIComponentsHost)
624 return ComponentsUI::GetFaviconResourceBytes(scale_factor);
626 #if defined(OS_WIN)
627 if (page_url.host() == chrome::kChromeUIConflictsHost)
628 return ConflictsUI::GetFaviconResourceBytes(scale_factor);
629 #endif
631 if (page_url.host() == chrome::kChromeUICrashesHost)
632 return CrashesUI::GetFaviconResourceBytes(scale_factor);
634 if (page_url.host() == chrome::kChromeUIFlagsHost)
635 return FlagsUI::GetFaviconResourceBytes(scale_factor);
637 if (page_url.host() == chrome::kChromeUIHistoryHost)
638 return HistoryUI::GetFaviconResourceBytes(scale_factor);
640 #if !defined(OS_ANDROID)
641 // The Apps launcher page is not available on android.
642 if (page_url.host() == chrome::kChromeUIAppLauncherPageHost)
643 return AppLauncherPageUI::GetFaviconResourceBytes(scale_factor);
645 // Flash is not available on android.
646 if (page_url.host() == chrome::kChromeUIFlashHost)
647 return FlashUI::GetFaviconResourceBytes(scale_factor);
649 // Android uses the native download manager.
650 if (page_url.host() == chrome::kChromeUIDownloadsHost)
651 return DownloadsUI::GetFaviconResourceBytes(scale_factor);
653 // Android doesn't use the Options pages.
654 if (page_url.host() == chrome::kChromeUISettingsHost ||
655 page_url.host() == chrome::kChromeUISettingsFrameHost)
656 return options::OptionsUI::GetFaviconResourceBytes(scale_factor);
658 #if defined(ENABLE_EXTENSIONS)
659 if (page_url.host() == chrome::kChromeUIExtensionsHost ||
660 page_url.host() == chrome::kChromeUIExtensionsFrameHost)
661 return extensions::ExtensionsUI::GetFaviconResourceBytes(scale_factor);
662 #endif
664 // Android doesn't use the plugins pages.
665 if (page_url.host() == chrome::kChromeUIPluginsHost)
666 return PluginsUI::GetFaviconResourceBytes(scale_factor);
668 #endif
670 return NULL;