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/browser/frame_host/debug_urls.h"
9 #include "content/public/common/url_constants.h"
10 #include "url/gurl.h"
11
12 namespace content {
13
14 base::LazyInstance<std::vector<WebUIControllerFactory*> > g_factories =
15 LAZY_INSTANCE_INITIALIZER;
16
RegisterFactory(WebUIControllerFactory * factory)17 void WebUIControllerFactory::RegisterFactory(WebUIControllerFactory* factory) {
18 g_factories.Pointer()->push_back(factory);
19 }
20
UnregisterFactoryForTesting(WebUIControllerFactory * factory)21 void WebUIControllerFactory::UnregisterFactoryForTesting(
22 WebUIControllerFactory* factory) {
23 std::vector<WebUIControllerFactory*>* factories = g_factories.Pointer();
24 for (size_t i = 0; i < factories->size(); ++i) {
25 if ((*factories)[i] == factory) {
26 factories->erase(factories->begin() + i);
27 return;
28 }
29 }
30 NOTREACHED() << "Tried to unregister a factory but it wasn't found";
31 }
32
GetInstance()33 WebUIControllerFactoryRegistry* WebUIControllerFactoryRegistry::GetInstance() {
34 return Singleton<WebUIControllerFactoryRegistry>::get();
35 }
36
CreateWebUIControllerForURL(WebUI * web_ui,const GURL & url) const37 WebUIController* WebUIControllerFactoryRegistry::CreateWebUIControllerForURL(
38 WebUI* web_ui, const GURL& url) const {
39 std::vector<WebUIControllerFactory*>* factories = g_factories.Pointer();
40 for (size_t i = 0; i < factories->size(); ++i) {
41 WebUIController* controller = (*factories)[i]->CreateWebUIControllerForURL(
42 web_ui, url);
43 if (controller)
44 return controller;
45 }
46 return NULL;
47 }
48
GetWebUIType(BrowserContext * browser_context,const GURL & url) const49 WebUI::TypeID WebUIControllerFactoryRegistry::GetWebUIType(
50 BrowserContext* browser_context, const GURL& url) const {
51 std::vector<WebUIControllerFactory*>* factories = g_factories.Pointer();
52 for (size_t i = 0; i < factories->size(); ++i) {
53 WebUI::TypeID type = (*factories)[i]->GetWebUIType(browser_context, url);
54 if (type != WebUI::kNoWebUI)
55 return type;
56 }
57 return WebUI::kNoWebUI;
58 }
59
UseWebUIForURL(BrowserContext * browser_context,const GURL & url) const60 bool WebUIControllerFactoryRegistry::UseWebUIForURL(
61 BrowserContext* browser_context, const GURL& url) const {
62 std::vector<WebUIControllerFactory*>* factories = g_factories.Pointer();
63 for (size_t i = 0; i < factories->size(); ++i) {
64 if ((*factories)[i]->UseWebUIForURL(browser_context, url))
65 return true;
66 }
67 return false;
68 }
69
UseWebUIBindingsForURL(BrowserContext * browser_context,const GURL & url) const70 bool WebUIControllerFactoryRegistry::UseWebUIBindingsForURL(
71 BrowserContext* browser_context, const GURL& url) const {
72 std::vector<WebUIControllerFactory*>* factories = g_factories.Pointer();
73 for (size_t i = 0; i < factories->size(); ++i) {
74 if ((*factories)[i]->UseWebUIBindingsForURL(browser_context, url))
75 return true;
76 }
77 return false;
78 }
79
IsURLAcceptableForWebUI(BrowserContext * browser_context,const GURL & url) const80 bool WebUIControllerFactoryRegistry::IsURLAcceptableForWebUI(
81 BrowserContext* browser_context,
82 const GURL& url) const {
83 return UseWebUIForURL(browser_context, url) ||
84 // It's possible to load about:blank in a Web UI renderer.
85 // See http://crbug.com/42547
86 url.spec() == url::kAboutBlankURL ||
87 // javascript: and debug URLs like chrome://kill are allowed.
88 IsRendererDebugURL(url);
89 }
90
WebUIControllerFactoryRegistry()91 WebUIControllerFactoryRegistry::WebUIControllerFactoryRegistry() {
92 }
93
~WebUIControllerFactoryRegistry()94 WebUIControllerFactoryRegistry::~WebUIControllerFactoryRegistry() {
95 }
96
97 } // namespace content
98