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.
4
5 #include "content/browser/webui/web_ui_controller_factory_registry.h"
6
7 #include "base/lazy_instance.h"
8 #include "content/public/common/url_constants.h"
9 #include "url/gurl.h"
10
11 namespace content {
12
13 base::LazyInstance<std::vector<WebUIControllerFactory*> > g_factories =
14 LAZY_INSTANCE_INITIALIZER;
15
RegisterFactory(WebUIControllerFactory * factory)16 void WebUIControllerFactory::RegisterFactory(WebUIControllerFactory* factory) {
17 g_factories.Pointer()->push_back(factory);
18 }
19
UnregisterFactoryForTesting(WebUIControllerFactory * 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);
26 return;
27 }
28 }
29 NOTREACHED() << "Tried to unregister a factory but it wasn't found";
30 }
31
GetInstance()32 WebUIControllerFactoryRegistry* WebUIControllerFactoryRegistry::GetInstance() {
33 return Singleton<WebUIControllerFactoryRegistry>::get();
34 }
35
CreateWebUIControllerForURL(WebUI * web_ui,const GURL & url) const36 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(
41 web_ui, url);
42 if (controller)
43 return controller;
44 }
45 return NULL;
46 }
47
GetWebUIType(BrowserContext * browser_context,const GURL & url) const48 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)
54 return type;
55 }
56 return WebUI::kNoWebUI;
57 }
58
UseWebUIForURL(BrowserContext * browser_context,const GURL & url) const59 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))
64 return true;
65 }
66 return false;
67 }
68
UseWebUIBindingsForURL(BrowserContext * browser_context,const GURL & url) const69 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))
74 return true;
75 }
76 return false;
77 }
78
IsURLAcceptableForWebUI(BrowserContext * browser_context,const GURL & url,bool data_urls_allowed) const79 bool WebUIControllerFactoryRegistry::IsURLAcceptableForWebUI(
80 BrowserContext* browser_context,
81 const GURL& url,
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));
98 }
99
WebUIControllerFactoryRegistry()100 WebUIControllerFactoryRegistry::WebUIControllerFactoryRegistry() {
101 }
102
~WebUIControllerFactoryRegistry()103 WebUIControllerFactoryRegistry::~WebUIControllerFactoryRegistry() {
104 }
105
106 } // namespace content
107