1 // Copyright (c) 2011 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 "chrome/test/base/test_chrome_web_ui_controller_factory.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/web_contents.h"
9
10 using content::WebContents;
11 using content::WebUI;
12 using content::WebUIController;
13
~WebUIProvider()14 TestChromeWebUIControllerFactory::WebUIProvider::~WebUIProvider() {
15 }
16
TestChromeWebUIControllerFactory()17 TestChromeWebUIControllerFactory::TestChromeWebUIControllerFactory() {
18 }
19
~TestChromeWebUIControllerFactory()20 TestChromeWebUIControllerFactory::~TestChromeWebUIControllerFactory() {
21 }
22
AddFactoryOverride(const std::string & host,WebUIProvider * provider)23 void TestChromeWebUIControllerFactory::AddFactoryOverride(
24 const std::string& host, WebUIProvider* provider) {
25 DCHECK_EQ(0U, factory_overrides_.count(host));
26 factory_overrides_[host] = provider;
27 }
28
RemoveFactoryOverride(const std::string & host)29 void TestChromeWebUIControllerFactory::RemoveFactoryOverride(
30 const std::string& host) {
31 DCHECK_EQ(1U, factory_overrides_.count(host));
32 factory_overrides_.erase(host);
33 }
34
GetWebUIType(content::BrowserContext * browser_context,const GURL & url) const35 WebUI::TypeID TestChromeWebUIControllerFactory::GetWebUIType(
36 content::BrowserContext* browser_context, const GURL& url) const {
37 Profile* profile = Profile::FromBrowserContext(browser_context);
38 WebUIProvider* provider = GetWebUIProvider(profile, url);
39 return provider ? reinterpret_cast<WebUI::TypeID>(provider) :
40 ChromeWebUIControllerFactory::GetWebUIType(profile, url);
41 }
42
CreateWebUIControllerForURL(content::WebUI * web_ui,const GURL & url) const43 WebUIController* TestChromeWebUIControllerFactory::CreateWebUIControllerForURL(
44 content::WebUI* web_ui, const GURL& url) const {
45 Profile* profile = Profile::FromWebUI(web_ui);
46 WebUIProvider* provider = GetWebUIProvider(profile, url);
47 return provider ? provider->NewWebUI(web_ui, url) :
48 ChromeWebUIControllerFactory::CreateWebUIControllerForURL(web_ui, url);
49 }
50
51 TestChromeWebUIControllerFactory::WebUIProvider*
GetWebUIProvider(Profile * profile,const GURL & url) const52 TestChromeWebUIControllerFactory::GetWebUIProvider(
53 Profile* profile, const GURL& url) const {
54 FactoryOverridesMap::const_iterator found =
55 factory_overrides_.find(url.host());
56 return (found == factory_overrides_.end()) ? NULL : found->second;
57 }
58