1 // Copyright (c) 2013 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 "content/browser/webui/web_ui_controller_factory_registry.h"
7 #include "base/lazy_instance.h"
8 #include "content/public/common/url_constants.h"
13 base::LazyInstance
<std::vector
<WebUIControllerFactory
*> > g_factories
=
14 LAZY_INSTANCE_INITIALIZER
;
16 void WebUIControllerFactory::RegisterFactory(WebUIControllerFactory
* factory
) {
17 g_factories
.Pointer()->push_back(factory
);
20 void WebUIControllerFactory::UnregisterFactoryForTesting(
21 WebUIControllerFactory
* factory
) {
22 std::vector
<WebUIControllerFactory
*>* factories
= g_factories
.Pointer();
23 for (size_t i
= 0; i
< factories
->size(); ++i
) {
24 if ((*factories
)[i
] == factory
) {
25 factories
->erase(factories
->begin() + i
);
29 NOTREACHED() << "Tried to unregister a factory but it wasn't found";
32 WebUIControllerFactoryRegistry
* WebUIControllerFactoryRegistry::GetInstance() {
33 return Singleton
<WebUIControllerFactoryRegistry
>::get();
36 WebUIController
* WebUIControllerFactoryRegistry::CreateWebUIControllerForURL(
37 WebUI
* web_ui
, const GURL
& url
) const {
38 std::vector
<WebUIControllerFactory
*>* factories
= g_factories
.Pointer();
39 for (size_t i
= 0; i
< factories
->size(); ++i
) {
40 WebUIController
* controller
= (*factories
)[i
]->CreateWebUIControllerForURL(
48 WebUI::TypeID
WebUIControllerFactoryRegistry::GetWebUIType(
49 BrowserContext
* browser_context
, const GURL
& url
) const {
50 std::vector
<WebUIControllerFactory
*>* factories
= g_factories
.Pointer();
51 for (size_t i
= 0; i
< factories
->size(); ++i
) {
52 WebUI::TypeID type
= (*factories
)[i
]->GetWebUIType(browser_context
, url
);
53 if (type
!= WebUI::kNoWebUI
)
56 return WebUI::kNoWebUI
;
59 bool WebUIControllerFactoryRegistry::UseWebUIForURL(
60 BrowserContext
* browser_context
, const GURL
& url
) const {
61 std::vector
<WebUIControllerFactory
*>* factories
= g_factories
.Pointer();
62 for (size_t i
= 0; i
< factories
->size(); ++i
) {
63 if ((*factories
)[i
]->UseWebUIForURL(browser_context
, url
))
69 bool WebUIControllerFactoryRegistry::UseWebUIBindingsForURL(
70 BrowserContext
* browser_context
, const GURL
& url
) const {
71 std::vector
<WebUIControllerFactory
*>* factories
= g_factories
.Pointer();
72 for (size_t i
= 0; i
< factories
->size(); ++i
) {
73 if ((*factories
)[i
]->UseWebUIBindingsForURL(browser_context
, url
))
79 bool WebUIControllerFactoryRegistry::IsURLAcceptableForWebUI(
80 BrowserContext
* browser_context
,
82 bool data_urls_allowed
) const {
83 return UseWebUIForURL(browser_context
, url
) ||
84 // javascript: URLs are allowed to run in Web UI pages.
85 url
.SchemeIs(kJavaScriptScheme
) ||
86 // It's possible to load about:blank in a Web UI renderer.
87 // See http://crbug.com/42547
88 url
.spec() == kAboutBlankURL
||
89 // Chrome URLs crash, kill, hang, and shorthang are allowed.
90 url
== GURL(kChromeUICrashURL
) ||
91 url
== GURL(kChromeUIKillURL
) ||
92 url
== GURL(kChromeUIHangURL
) ||
93 url
== GURL(kChromeUIShorthangURL
) ||
94 // Data URLs are usually not allowed in WebUI for security reasons.
95 // BalloonHosts are one exception needed by ChromeOS, and are safe because
96 // they cannot be scripted by other pages.
97 (data_urls_allowed
&& url
.SchemeIs(chrome::kDataScheme
));
100 WebUIControllerFactoryRegistry::WebUIControllerFactoryRegistry() {
103 WebUIControllerFactoryRegistry::~WebUIControllerFactoryRegistry() {
106 } // namespace content