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.
5 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/web_contents.h"
10 using content::WebContents
;
12 using content::WebUIController
;
14 TestChromeWebUIControllerFactory::WebUIProvider::~WebUIProvider() {
17 TestChromeWebUIControllerFactory::TestChromeWebUIControllerFactory() {
20 TestChromeWebUIControllerFactory::~TestChromeWebUIControllerFactory() {
23 void TestChromeWebUIControllerFactory::AddFactoryOverride(
24 const std::string
& host
, WebUIProvider
* provider
) {
25 DCHECK_EQ(0U, factory_overrides_
.count(host
));
26 factory_overrides_
[host
] = provider
;
29 void TestChromeWebUIControllerFactory::RemoveFactoryOverride(
30 const std::string
& host
) {
31 DCHECK_EQ(1U, factory_overrides_
.count(host
));
32 factory_overrides_
.erase(host
);
35 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
);
43 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
);
51 TestChromeWebUIControllerFactory::WebUIProvider
*
52 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
;