File: test_chrome_web_ui_controller_factory.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,418 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/test_chrome_web_ui_controller_factory.h"

#include "base/functional/callback_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/web_ui_test_data_source.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller.h"

using content::WebContents;
using content::WebUI;
using content::WebUIController;

TestChromeWebUIControllerFactory::WebUIProvider::~WebUIProvider() = default;

TestChromeWebUIControllerFactory::TestChromeWebUIControllerFactory() = default;

TestChromeWebUIControllerFactory::~TestChromeWebUIControllerFactory() = default;

void TestChromeWebUIControllerFactory::set_webui_host(
    const std::string& webui_host) {
  webui_host_ = webui_host;
}

void TestChromeWebUIControllerFactory::AddFactoryOverride(
    const std::string& host, WebUIProvider* provider) {
  DCHECK_EQ(0U, factory_overrides_.count(host));
  factory_overrides_[host] = provider;
}

void TestChromeWebUIControllerFactory::RemoveFactoryOverride(
    const std::string& host) {
  DCHECK_EQ(1U, factory_overrides_.count(host));
  factory_overrides_.erase(host);
}

WebUI::TypeID TestChromeWebUIControllerFactory::GetWebUIType(
    content::BrowserContext* browser_context,
    const GURL& url) {
  Profile* profile = Profile::FromBrowserContext(browser_context);
  const GURL& webui_url = TestURLToWebUIURL(url);
  WebUIProvider* provider = GetWebUIProvider(profile, webui_url);
  return provider
             ? reinterpret_cast<WebUI::TypeID>(provider)
             : ChromeWebUIControllerFactory::GetWebUIType(profile, webui_url);
}

std::unique_ptr<WebUIController>
TestChromeWebUIControllerFactory::CreateWebUIControllerForURL(
    content::WebUI* web_ui,
    const GURL& url) {
  Profile* profile = Profile::FromWebUI(web_ui);
  const GURL& webui_url = TestURLToWebUIURL(url);
  WebUIProvider* provider = GetWebUIProvider(profile, webui_url);
  auto controller =
      provider ? provider->NewWebUI(web_ui, webui_url)
               : ChromeWebUIControllerFactory::CreateWebUIControllerForURL(
                     web_ui, webui_url);

  // Add an empty callback since managed-footnote always sends this message.
  web_ui->RegisterMessageCallback("observeManagedUI", base::DoNothing());
  content::WebUIDataSource* source =
      webui::CreateAndAddWebUITestDataSource(profile);
  if (provider)
    provider->DataSourceOverrides(source);

  return controller;
}

TestChromeWebUIControllerFactory::WebUIProvider*
    TestChromeWebUIControllerFactory::GetWebUIProvider(
        Profile* profile, const GURL& url) const {
  const GURL& webui_url = TestURLToWebUIURL(url);
  auto found = factory_overrides_.find(webui_url.GetHost());
  return found != factory_overrides_.end() ? found->second : nullptr;
}

GURL TestChromeWebUIControllerFactory::TestURLToWebUIURL(
    const GURL& url) const {
  if ((url.GetHost() != "test" &&
       url.GetHost() != chrome::kChromeUIWebUITestHost) ||
      webui_host_.empty()) {
    return url;
  }

  GURL webui_url(url);
  GURL::Replacements replacements;
  replacements.SetHostStr(webui_host_);
  return webui_url.ReplaceComponents(replacements);
}