1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/chrome_web_ui_factory.h"
#include "base/command_line.h"
#include "chrome/browser/about_flags.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_web_ui.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/webui/about_ui.h"
#include "chrome/browser/ui/webui/bookmarks_ui.h"
#include "chrome/browser/ui/webui/bug_report_ui.h"
#include "chrome/browser/ui/webui/constrained_html_ui.h"
#include "chrome/browser/ui/webui/crashes_ui.h"
#include "chrome/browser/ui/webui/devtools_ui.h"
#include "chrome/browser/ui/webui/downloads_ui.h"
#include "chrome/browser/ui/webui/edit_search_engine_dialog_ui_webui.h"
#include "chrome/browser/ui/webui/extensions/extensions_ui.h"
#include "chrome/browser/ui/webui/task_manager_ui.h"
#include "chrome/browser/ui/webui/flags_ui.h"
#include "chrome/browser/ui/webui/flash_ui.h"
#include "chrome/browser/ui/webui/gpu_internals_ui.h"
#include "chrome/browser/ui/webui/history_ui.h"
#include "chrome/browser/ui/webui/html_dialog_ui.h"
#include "chrome/browser/ui/webui/hung_renderer_dialog_ui.h"
#include "chrome/browser/ui/webui/input_window_dialog_ui.h"
#include "chrome/browser/ui/webui/media/media_internals_ui.h"
#include "chrome/browser/ui/webui/net_internals_ui.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "chrome/browser/ui/webui/options2/options_ui2.h"
#include "chrome/browser/ui/webui/plugins_ui.h"
#include "chrome/browser/ui/webui/policy_ui.h"
#include "chrome/browser/ui/webui/print_preview_ui.h"
#include "chrome/browser/ui/webui/profiler_ui.h"
#include "chrome/browser/ui/webui/quota_internals_ui.h"
#include "chrome/browser/ui/webui/sessions_ui.h"
#include "chrome/browser/ui/webui/sync_internals_ui.h"
#include "chrome/browser/ui/webui/test_chrome_web_ui_factory.h"
#include "chrome/browser/ui/webui/tracing_ui.h"
#include "chrome/browser/ui/webui/uber/uber_ui.h"
#include "chrome/browser/ui/webui/workers_ui.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/webui/web_ui.h"
#include "googleurl/src/gurl.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/ui/webui/chromeos/choose_mobile_network_ui.h"
#include "chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.h"
#include "chrome/browser/ui/webui/chromeos/keyboard_overlay_ui.h"
#include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
#include "chrome/browser/ui/webui/chromeos/mobile_setup_ui.h"
#include "chrome/browser/ui/webui/chromeos/proxy_settings_ui.h"
#include "chrome/browser/ui/webui/chromeos/register_page_ui.h"
#include "chrome/browser/ui/webui/chromeos/sim_unlock_ui.h"
#include "chrome/browser/ui/webui/chromeos/system_info_ui.h"
#include "chrome/browser/ui/webui/active_downloads_ui.h"
#else
#include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h"
#endif
#if defined(USE_VIRTUAL_KEYBOARD)
#include "chrome/browser/ui/webui/keyboard_ui.h"
#endif
#if defined(OS_WIN)
#include "chrome/browser/ui/webui/conflicts_ui.h"
#endif
#if defined(OS_POSIX) && !defined(OS_MACOSX)
#include "chrome/browser/ui/webui/certificate_viewer_ui.h"
#endif
#if defined(USE_AURA)
#include "chrome/browser/ui/webui/aura/app_list_ui.h"
#endif
namespace {
// A function for creating a new WebUI. The caller owns the return value, which
// may be NULL (for example, if the URL refers to an non-existent extension).
typedef ChromeWebUI* (*WebUIFactoryFunction)(TabContents* tab_contents,
const GURL& url);
// Template for defining WebUIFactoryFunction.
template<class T>
ChromeWebUI* NewWebUI(TabContents* contents, const GURL& url) {
return new T(contents);
}
// Special case for extensions.
template<>
ChromeWebUI* NewWebUI<ExtensionWebUI>(TabContents* contents, const GURL& url) {
return new ExtensionWebUI(contents, url);
}
// Special case for older about: handlers.
template<>
ChromeWebUI* NewWebUI<AboutUI>(TabContents* contents, const GURL& url) {
return new AboutUI(contents, url.host());
}
// Only create ExtensionWebUI for URLs that are allowed extension bindings,
// hosted by actual tabs. If tab_contents has no wrapper, it likely refers
// to another container type, like an extension background page. If there is
// no tab_contents (it's not accessible when calling GetWebUIType and related
// functions) then we conservatively assume that we need a WebUI.
bool NeedsExtensionWebUI(TabContents* tab_contents,
Profile* profile,
const GURL& url) {
ExtensionService* service = profile ? profile->GetExtensionService() : NULL;
return service && service->ExtensionBindingsAllowed(url) &&
(!tab_contents ||
TabContentsWrapper::GetCurrentWrapperForContents(tab_contents));
}
// Returns a function that can be used to create the right type of WebUI for a
// tab, based on its URL. Returns NULL if the URL doesn't have WebUI associated
// with it.
WebUIFactoryFunction GetWebUIFactoryFunction(TabContents* tab_contents,
Profile* profile,
const GURL& url) {
if (url.host() == chrome::kChromeUIDialogHost)
return &NewWebUI<ConstrainedHtmlUI>;
if (NeedsExtensionWebUI(tab_contents, profile, url))
return &NewWebUI<ExtensionWebUI>;
// All platform builds of Chrome will need to have a cloud printing
// dialog as backup. It's just that on Chrome OS, it's the only
// print dialog.
if (url.host() == chrome::kChromeUICloudPrintResourcesHost)
return &NewWebUI<ExternalHtmlDialogUI>;
// This will get called a lot to check all URLs, so do a quick check of other
// schemes to filter out most URLs.
if (!url.SchemeIs(chrome::kChromeDevToolsScheme) &&
!url.SchemeIs(chrome::kChromeInternalScheme) &&
!url.SchemeIs(chrome::kChromeUIScheme)) {
return NULL;
}
if (url.host() == chrome::kChromeUISyncResourcesHost ||
url.host() == chrome::kChromeUICloudPrintSetupHost) {
return &NewWebUI<HtmlDialogUI>;
}
// Special case the new tab page. In older versions of Chrome, the new tab
// page was hosted at chrome-internal:<blah>. This might be in people's saved
// sessions or bookmarks, so we say any URL with that scheme triggers the new
// tab page.
if (url.host() == chrome::kChromeUINewTabHost ||
url.SchemeIs(chrome::kChromeInternalScheme)) {
return &NewWebUI<NewTabUI>;
}
// We must compare hosts only since some of the Web UIs append extra stuff
// after the host name.
if (url.host() == chrome::kChromeUIBookmarksHost)
return &NewWebUI<BookmarksUI>;
if (url.host() == chrome::kChromeUIBugReportHost)
return &NewWebUI<BugReportUI>;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
if (url.host() == chrome::kChromeUICertificateViewerHost)
return &NewWebUI<CertificateViewerUI>;
#endif
#if defined(OS_WIN)
if (url.host() == chrome::kChromeUIConflictsHost)
return &NewWebUI<ConflictsUI>;
#endif
if (url.host() == chrome::kChromeUICrashesHost)
return &NewWebUI<CrashesUI>;
if (url.host() == chrome::kChromeUIDevToolsHost)
return &NewWebUI<DevToolsUI>;
if (url.host() == chrome::kChromeUIDownloadsHost)
return &NewWebUI<DownloadsUI>;
if (url.host() == chrome::kChromeUIEditSearchEngineDialogHost)
return &NewWebUI<EditSearchEngineDialogUI>;
if (url.host() == chrome::kChromeUIExtensionsFrameHost)
return &NewWebUI<ExtensionsUI>;
if (url.host() == chrome::kChromeUIFlagsHost)
return &NewWebUI<FlagsUI>;
if (url.host() == chrome::kChromeUIFlashHost)
return &NewWebUI<FlashUI>;
if (url.host() == chrome::kChromeUIGpuInternalsHost)
return &NewWebUI<GpuInternalsUI>;
if (url.host() == chrome::kChromeUIHistoryHost)
return &NewWebUI<HistoryUI>;
if (url.host() == chrome::kChromeUIHungRendererDialogHost)
return &NewWebUI<HungRendererDialogUI>;
if (url.host() == chrome::kChromeUIInputWindowDialogHost)
return &NewWebUI<InputWindowDialogUI>;
#if defined(USE_VIRTUAL_KEYBOARD)
if (url.host() == chrome::kChromeUIKeyboardHost)
return &NewWebUI<KeyboardUI>;
#endif
if (url.host() == chrome::kChromeUIMediaInternalsHost)
return &NewWebUI<MediaInternalsUI>;
if (url.host() == chrome::kChromeUINetInternalsHost)
return &NewWebUI<NetInternalsUI>;
#if defined(ENABLE_CONFIGURATION_POLICY)
if (url.host() == chrome::kChromeUIPolicyHost)
return &NewWebUI<PolicyUI>;
#endif
if (url.host() == chrome::kChromeUIPluginsHost)
return &NewWebUI<PluginsUI>;
if (url.host() == chrome::kChromeUIProfilerHost)
return &NewWebUI<ProfilerUI>;
if (url.host() == chrome::kChromeUIQuotaInternalsHost)
return &NewWebUI<QuotaInternalsUI>;
if (url.host() == chrome::kChromeUISSLClientCertificateSelectorHost)
return &NewWebUI<ConstrainedHtmlUI>;
if (url.host() == chrome::kChromeUISessionsHost)
return &NewWebUI<SessionsUI>;
if (url.host() == chrome::kChromeUISettingsHost)
return &NewWebUI<OptionsUI>;
if (url.host() == chrome::kChromeUISettingsFrameHost)
return &NewWebUI<Options2UI>;
if (url.host() == chrome::kChromeUISyncInternalsHost)
return &NewWebUI<SyncInternalsUI>;
if (url.host() == chrome::kChromeUITaskManagerHost)
return &NewWebUI<TaskManagerUI>;
if (url.host() == chrome::kChromeUITracingHost)
return &NewWebUI<TracingUI>;
if (url.host() == chrome::kChromeUIUberHost)
return &NewWebUI<UberUI>;
if (url.host() == chrome::kChromeUIWorkersHost)
return &NewWebUI<WorkersUI>;
#if defined(OS_CHROMEOS)
if (url.host() == chrome::kChromeUIActiveDownloadsHost)
return &NewWebUI<ActiveDownloadsUI>;
if (url.host() == chrome::kChromeUIChooseMobileNetworkHost)
return &NewWebUI<chromeos::ChooseMobileNetworkUI>;
if (url.host() == chrome::kChromeUIImageBurnerHost)
return &NewWebUI<ImageBurnUI>;
if (url.host() == chrome::kChromeUIKeyboardOverlayHost)
return &NewWebUI<KeyboardOverlayUI>;
if (url.host() == chrome::kChromeUIMobileSetupHost)
return &NewWebUI<MobileSetupUI>;
if (url.host() == chrome::kChromeUIOobeHost)
return &NewWebUI<chromeos::OobeUI>;
if (url.host() == chrome::kChromeUIProxySettingsHost)
return &NewWebUI<chromeos::ProxySettingsUI>;
if (url.host() == chrome::kChromeUIRegisterPageHost)
return &NewWebUI<RegisterPageUI>;
if (url.host() == chrome::kChromeUISimUnlockHost)
return &NewWebUI<chromeos::SimUnlockUI>;
if (url.host() == chrome::kChromeUISystemInfoHost)
return &NewWebUI<SystemInfoUI>;
#endif // defined(OS_CHROMEOS)
#if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA)
if (url.host() == chrome::kChromeUICollectedCookiesHost ||
url.host() == chrome::kChromeUIHttpAuthHost ||
url.host() == chrome::kChromeUITabModalConfirmDialogHost) {
return &NewWebUI<ConstrainedHtmlUI>;
}
#endif
#if defined(USE_AURA)
if (url.host() == chrome::kChromeUIAppListHost)
return &NewWebUI<AppListUI>;
#endif
if (url.host() == chrome::kChromeUIPrintHost &&
switches::IsPrintPreviewEnabled()) {
return &NewWebUI<PrintPreviewUI>;
}
if (url.spec() == chrome::kChromeUIConstrainedHTMLTestURL)
return &NewWebUI<ConstrainedHtmlUI>;
#if !defined(OS_CHROMEOS)
if (url.host() == chrome::kChromeUISyncPromoHost) {
// If the sync promo page is enabled then use the sync promo WebUI otherwise
// use the NTP WebUI. We don't want to return NULL if the sync promo page
// is disabled because the page can be disabled mid-flight (for example,
// if sync login finishes).
if (SyncPromoUI::ShouldShowSyncPromo(profile))
return &NewWebUI<SyncPromoUI>;
else
return &NewWebUI<NewTabUI>;
}
#endif
if (url.host() == chrome::kChromeUIChromeURLsHost ||
url.host() == chrome::kChromeUICreditsHost ||
url.host() == chrome::kChromeUIDNSHost ||
url.host() == chrome::kChromeUIHistogramsHost ||
url.host() == chrome::kChromeUIMemoryHost ||
url.host() == chrome::kChromeUIMemoryRedirectHost ||
url.host() == chrome::kChromeUIStatsHost ||
url.host() == chrome::kChromeUITaskManagerHost ||
url.host() == chrome::kChromeUITermsHost ||
url.host() == chrome::kChromeUIVersionHost
#if defined(USE_TCMALLOC)
|| url.host() == chrome::kChromeUITCMallocHost
#endif
#if defined(OS_LINUX) || defined(OS_OPENBSD)
|| url.host() == chrome::kChromeUILinuxProxyConfigHost
|| url.host() == chrome::kChromeUISandboxHost
#endif
#if defined(OS_CHROMEOS)
|| url.host() == chrome::kChromeUICryptohomeHost
|| url.host() == chrome::kChromeUIDiscardsHost
|| url.host() == chrome::kChromeUINetworkHost
|| url.host() == chrome::kChromeUIOSCreditsHost
#endif
) {
return &NewWebUI<AboutUI>;
}
DLOG(WARNING) << "Unknown WebUI:" << url;
return NULL;
}
// When the test-type switch is set, return a TestType object, which should be a
// subclass of Type. The logic is provided here in the traits class, rather than
// in GetInstance() so that the choice is made only once, when the Singleton is
// first instantiated, rather than every time GetInstance() is called.
template<typename Type, typename TestType>
struct PossibleTestSingletonTraits : public DefaultSingletonTraits<Type> {
static Type* New() {
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
return DefaultSingletonTraits<TestType>::New();
else
return DefaultSingletonTraits<Type>::New();
}
};
} // namespace
WebUI::TypeID ChromeWebUIFactory::GetWebUIType(
content::BrowserContext* browser_context, const GURL& url) const {
Profile* profile = Profile::FromBrowserContext(browser_context);
WebUIFactoryFunction function = GetWebUIFactoryFunction(NULL, profile, url);
return function ? reinterpret_cast<WebUI::TypeID>(function) : WebUI::kNoWebUI;
}
bool ChromeWebUIFactory::UseWebUIForURL(
content::BrowserContext* browser_context, const GURL& url) const {
return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
}
bool ChromeWebUIFactory::UseWebUIBindingsForURL(
content::BrowserContext* browser_context, const GURL& url) const {
// Extensions are rendered via WebUI in tabs, but don't actually need WebUI
// bindings (see the ExtensionWebUI constructor).
return !NeedsExtensionWebUI(NULL,
Profile::FromBrowserContext(browser_context),
url) &&
UseWebUIForURL(browser_context, url);
}
bool ChromeWebUIFactory::HasWebUIScheme(const GURL& url) const {
return url.SchemeIs(chrome::kChromeDevToolsScheme) ||
url.SchemeIs(chrome::kChromeInternalScheme) ||
url.SchemeIs(chrome::kChromeUIScheme);
}
bool ChromeWebUIFactory::IsURLAcceptableForWebUI(
content::BrowserContext* browser_context,
const GURL& url) const {
return UseWebUIForURL(browser_context, url) ||
// javacsript: URLs are allowed to run in Web UI pages
url.SchemeIs(chrome::kJavaScriptScheme) ||
// It's possible to load about:blank in a Web UI renderer.
// See http://crbug.com/42547
url.spec() == chrome::kAboutBlankURL ||
// Chrome URLs crash, kill, hang, and shorthang are allowed.
url == GURL(chrome::kChromeUICrashURL) ||
url == GURL(chrome::kChromeUIKillURL) ||
url == GURL(chrome::kChromeUIHangURL) ||
url == GURL(chrome::kChromeUIShorthangURL);
}
WebUI* ChromeWebUIFactory::CreateWebUIForURL(
TabContents* tab_contents,
const GURL& url) const {
Profile* profile =
Profile::FromBrowserContext(tab_contents->browser_context());
WebUIFactoryFunction function = GetWebUIFactoryFunction(tab_contents,
profile, url);
if (!function)
return NULL;
return (*function)(tab_contents, url);
}
void ChromeWebUIFactory::GetFaviconForURL(
Profile* profile,
FaviconService::GetFaviconRequest* request,
const GURL& page_url) const {
// All extensions but the bookmark manager get their favicon from the icons
// part of the manifest.
if (page_url.SchemeIs(chrome::kExtensionScheme) &&
page_url.host() != extension_misc::kBookmarkManagerId) {
ExtensionWebUI::GetFaviconForURL(profile, request, page_url);
} else {
history::FaviconData favicon;
favicon.image_data = scoped_refptr<RefCountedMemory>(
GetFaviconResourceBytes(page_url));
favicon.known_icon = favicon.image_data.get() != NULL &&
favicon.image_data->size() > 0;
favicon.icon_type = history::FAVICON;
request->ForwardResultAsync(request->handle(), favicon);
}
}
// static
ChromeWebUIFactory* ChromeWebUIFactory::GetInstance() {
return Singleton< ChromeWebUIFactory, PossibleTestSingletonTraits<
ChromeWebUIFactory, TestChromeWebUIFactory> >::get();
}
ChromeWebUIFactory::ChromeWebUIFactory() {
}
ChromeWebUIFactory::~ChromeWebUIFactory() {
}
RefCountedMemory* ChromeWebUIFactory::GetFaviconResourceBytes(
const GURL& page_url) const {
// The bookmark manager is a chrome extension, so we have to check for it
// before we check for extension scheme.
if (page_url.host() == extension_misc::kBookmarkManagerId)
return BookmarksUI::GetFaviconResourceBytes();
// The extension scheme is handled in GetFaviconForURL.
if (page_url.SchemeIs(chrome::kExtensionScheme)) {
NOTREACHED();
return NULL;
}
if (!HasWebUIScheme(page_url))
return NULL;
#if defined(OS_WIN)
if (page_url.host() == chrome::kChromeUIConflictsHost)
return ConflictsUI::GetFaviconResourceBytes();
#endif
if (page_url.host() == chrome::kChromeUICrashesHost)
return CrashesUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUIDownloadsHost)
return DownloadsUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUIHistoryHost)
return HistoryUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUIFlagsHost)
return FlagsUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUISessionsHost)
return SessionsUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUIFlashHost)
return FlashUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUISettingsHost)
return OptionsUI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUISettingsFrameHost)
return Options2UI::GetFaviconResourceBytes();
if (page_url.host() == chrome::kChromeUIPluginsHost)
return PluginsUI::GetFaviconResourceBytes();
return NULL;
}
|