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 "base/callback_helpers.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/webui/test_data_source.h"
10 #include "content/public/browser/url_data_source.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_ui_controller.h"
13
14 using content::WebContents;
15 using content::WebUI;
16 using content::WebUIController;
17
~WebUIProvider()18 TestChromeWebUIControllerFactory::WebUIProvider::~WebUIProvider() {
19 }
20
TestChromeWebUIControllerFactory()21 TestChromeWebUIControllerFactory::TestChromeWebUIControllerFactory() {
22 }
23
~TestChromeWebUIControllerFactory()24 TestChromeWebUIControllerFactory::~TestChromeWebUIControllerFactory() {
25 }
26
set_webui_host(const std::string & webui_host)27 void TestChromeWebUIControllerFactory::set_webui_host(
28 const std::string& webui_host) {
29 webui_host_ = webui_host;
30 }
31
AddFactoryOverride(const std::string & host,WebUIProvider * provider)32 void TestChromeWebUIControllerFactory::AddFactoryOverride(
33 const std::string& host, WebUIProvider* provider) {
34 DCHECK_EQ(0U, factory_overrides_.count(host));
35 factory_overrides_[host] = provider;
36 }
37
RemoveFactoryOverride(const std::string & host)38 void TestChromeWebUIControllerFactory::RemoveFactoryOverride(
39 const std::string& host) {
40 DCHECK_EQ(1U, factory_overrides_.count(host));
41 factory_overrides_.erase(host);
42 }
43
GetWebUIType(content::BrowserContext * browser_context,const GURL & url)44 WebUI::TypeID TestChromeWebUIControllerFactory::GetWebUIType(
45 content::BrowserContext* browser_context,
46 const GURL& url) {
47 Profile* profile = Profile::FromBrowserContext(browser_context);
48 const GURL& webui_url = TestURLToWebUIURL(url);
49 WebUIProvider* provider = GetWebUIProvider(profile, webui_url);
50 return provider
51 ? reinterpret_cast<WebUI::TypeID>(provider)
52 : ChromeWebUIControllerFactory::GetWebUIType(profile, webui_url);
53 }
54
55 std::unique_ptr<WebUIController>
CreateWebUIControllerForURL(content::WebUI * web_ui,const GURL & url)56 TestChromeWebUIControllerFactory::CreateWebUIControllerForURL(
57 content::WebUI* web_ui,
58 const GURL& url) {
59 Profile* profile = Profile::FromWebUI(web_ui);
60 const GURL& webui_url = TestURLToWebUIURL(url);
61 WebUIProvider* provider = GetWebUIProvider(profile, webui_url);
62 auto controller =
63 provider ? provider->NewWebUI(web_ui, webui_url)
64 : ChromeWebUIControllerFactory::CreateWebUIControllerForURL(
65 web_ui, webui_url);
66 // Add an empty callback since managed-footnote always sends this message.
67 web_ui->RegisterMessageCallback("observeManagedUI", base::DoNothing());
68 content::URLDataSource::Add(profile,
69 std::make_unique<TestDataSource>("webui"));
70 return controller;
71 }
72
73 TestChromeWebUIControllerFactory::WebUIProvider*
GetWebUIProvider(Profile * profile,const GURL & url) const74 TestChromeWebUIControllerFactory::GetWebUIProvider(
75 Profile* profile, const GURL& url) const {
76 const GURL& webui_url = TestURLToWebUIURL(url);
77 auto found = factory_overrides_.find(webui_url.host());
78 return found != factory_overrides_.end() ? found->second : nullptr;
79 }
80
TestURLToWebUIURL(const GURL & url) const81 GURL TestChromeWebUIControllerFactory::TestURLToWebUIURL(
82 const GURL& url) const {
83 if (url.host() != "test" || webui_host_.empty())
84 return url;
85
86 GURL webui_url(url);
87 GURL::Replacements replacements;
88 replacements.SetHostStr(webui_host_);
89 return webui_url.ReplaceComponents(replacements);
90 }
91