Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / webui / web_ui_controller_factory_registry.cc
bloba80f52b8760983fff92d4bd1fefa84991f074c8c
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/browser/frame_host/debug_urls.h"
9 #include "content/public/common/url_constants.h"
10 #include "url/gurl.h"
12 namespace content {
14 base::LazyInstance<std::vector<WebUIControllerFactory*> > g_factories =
15 LAZY_INSTANCE_INITIALIZER;
17 void WebUIControllerFactory::RegisterFactory(WebUIControllerFactory* factory) {
18 g_factories.Pointer()->push_back(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;
30 NOTREACHED() << "Tried to unregister a factory but it wasn't found";
33 WebUIControllerFactoryRegistry* WebUIControllerFactoryRegistry::GetInstance() {
34 return Singleton<WebUIControllerFactoryRegistry>::get();
37 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;
46 return NULL;
49 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;
57 return WebUI::kNoWebUI;
60 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;
67 return false;
70 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;
77 return false;
80 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);
91 WebUIControllerFactoryRegistry::WebUIControllerFactoryRegistry() {
94 WebUIControllerFactoryRegistry::~WebUIControllerFactoryRegistry() {
97 } // namespace content