File: test_chrome_web_ui_controller_factory.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 3,402 bytes parent folder | download | duplicates (6)
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
// 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.host());
  return found != factory_overrides_.end() ? found->second : nullptr;
}

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

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