0

Sync Cast Core gRPC APis to the latest revision.

gRPC APIs submitted in
https://chromium-review.googlesource.com/c/cast_core/public/+/5933643.

Bug: none
Change-Id: Ic95f67c1ae845e391466f1ce8252259d637c662a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5955550
Reviewed-by: Antonio Rivera <antoniori@google.com>
Commit-Queue: Vigen Issahhanjan <vigeni@google.com>
Reviewed-by: Mark Foltz <mfoltz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1373547}
This commit is contained in:
Vigen Issahhanjan
2024-10-24 20:29:43 +00:00
committed by Chromium LUCI CQ
parent 6843764ff6
commit 22d55e2f16
16 changed files with 4 additions and 736 deletions

2
DEPS

@ -1687,7 +1687,7 @@ deps = {
Var('chromium_git') + '/breakpad/breakpad.git' + '@' + '6b0c5b7ee1988a14a4af94564e8ae8bba8a94374',
'src/third_party/cast_core/public/src':
Var('chromium_git') + '/cast_core/public' + '@' + 'deadbce86e6d6c2c650df9e7fd9431696ca7ae47',
Var('chromium_git') + '/cast_core/public' + '@' + 'fbc5e98031e1271a0a566fcd4d9092b2d3275d05',
'src/third_party/catapult':
Var('chromium_git') + '/catapult.git' + '@' + Var('catapult_revision'),

@ -30,35 +30,6 @@ cast_source_set("metrics_recorder") {
]
}
cast_source_set("grpc_webui") {
sources = [
"grpc_resource_data_source.cc",
"grpc_resource_data_source.h",
"grpc_webui_controller.cc",
"grpc_webui_controller.h",
"grpc_webui_controller_factory.cc",
"grpc_webui_controller_factory.h",
]
deps = [
"//base",
"//chromecast:chromecast_buildflags",
"//chromecast/base",
"//chromecast/base:chromecast_switches",
"//chromecast/base:default_create_sys_info",
"//chromecast/browser",
"//net",
"//third_party/blink/public/common:headers",
"//third_party/grpc:grpc++",
]
public_deps = [
"//chromecast/browser:public",
"//third_party/abseil-cpp:absl",
"//third_party/cast_core/public/src/proto/v2:core_application_service_proto_castcore",
]
}
cast_source_set("runtime_service_impl") {
public = [ "runtime_service_impl.h" ]
sources = [
@ -78,7 +49,6 @@ cast_source_set("runtime_service_impl") {
public_deps = [ "//third_party/cast_core/public/src/proto/runtime:runtime_service_proto_castcore" ]
deps = [
":grpc_webui",
":metrics_recorder",
"//base",
"//chromecast:chromecast_buildflags",
@ -181,11 +151,9 @@ cast_source_set("unit_tests") {
"cast_runtime_metrics_test_helpers.cc",
"cast_runtime_metrics_test_helpers.h",
"core_streaming_config_manager_unittest.cc",
"grpc_resource_data_source_unittest.cc",
]
deps = [
":grpc_webui",
":metrics_recorder",
":runtime_service_impl",
"//base",

@ -9,7 +9,6 @@
#include "base/memory/raw_ref.h"
#include "chromecast/browser/cast_content_browser_client.h"
#include "chromecast/browser/service/cast_service_simple.h"
#include "chromecast/browser/webui/constants.h"
#include "chromecast/cast_core/cast_core_switches.h"
#include "chromecast/cast_core/runtime/browser/runtime_service_impl.h"
#include "chromecast/common/cors_exempt_headers.h"
@ -106,8 +105,8 @@ void CastRuntimeContentBrowserClient::AppendExtraCommandLineSwitches(
}
bool CastRuntimeContentBrowserClient::IsWebUIAllowedToMakeNetworkRequests(
const url::Origin& origin) {
return origin.host() == kCastWebUIHomeHost;
const url::Origin&) {
return false;
}
bool CastRuntimeContentBrowserClient::IsBufferingEnabled() {

@ -1,202 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/cast_core/runtime/browser/grpc_resource_data_source.h"
#include <string_view>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "base/task/bind_post_task.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chromecast/base/cast_constants.h"
#include "net/base/mime_util.h"
namespace chromecast {
namespace {
// File extension types
constexpr const char kExtensionTypeCss[] = ".css";
constexpr const char kExtensionTypeJs[] = ".js";
constexpr const char kExtensionTypeJson[] = ".json";
constexpr const char kExtensionTypePdf[] = ".pdf";
constexpr const char kExtensionTypeSvg[] = ".svg";
constexpr const char kExtensionTypePng[] = ".png";
constexpr const char kExtensionTypeJpeg[] = ".jpeg";
constexpr const char kExtensionTypeHtml[] = ".html";
// Mime types of the resource requested by the Cast app's WebUI.
constexpr const char kMimeTypeHtml[] = "text/html";
constexpr const char kMimeTypeCss[] = "text/css";
constexpr const char kMimeTypeJavascript[] = "application/javascript";
constexpr const char kMimeTypeJson[] = "application/json";
constexpr const char kMimeTypePdf[] = "application/pdf";
constexpr const char kMimeTypeSvgXml[] = "image/svg+xml";
constexpr const char kMimeTypeJpeg[] = "image/jpeg";
constexpr const char kMimeTypePng[] = "image/png";
constexpr const char kAllowedOriginPrefix[] = "chrome://";
} // namespace
GrpcResourceDataSource::GrpcResourceDataSource(
const std::string host,
bool for_webui,
cast::v2::CoreApplicationServiceStub* core_app_service_stub)
: task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
host_(host),
for_webui_(for_webui),
core_app_service_stub_(core_app_service_stub) {
DCHECK(!host.empty());
}
GrpcResourceDataSource::~GrpcResourceDataSource() = default;
std::string GrpcResourceDataSource::GetSource() {
return host_;
}
void GrpcResourceDataSource::StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) {
DVLOG(1) << "Starting Data request for " << url;
auto call = core_app_service_stub_->CreateCall<
cast::v2::CoreApplicationServiceStub::GetWebUIResource>();
call.request().set_resource_id(content::URLDataSource::URLToRequestPath(url));
std::move(call).InvokeAsync(base::BindPostTask(
task_runner_,
base::BindOnce(&GrpcResourceDataSource::OnWebUiResourceReceived,
weak_factory_.GetWeakPtr(), std::move(callback))));
}
void GrpcResourceDataSource::OnWebUiResourceReceived(
content::URLDataSource::GotDataCallback callback,
cast::utils::GrpcStatusOr<cast::v2::GetWebUIResourceResponse> response_or) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
if (!response_or.ok()) {
LOG(ERROR) << "Failed to receive resource path response: status="
<< response_or.ToString();
std::move(callback).Run(nullptr);
return;
}
DVLOG(1) << "Got resource path: " << response_or->resource_path();
ReadResourceFile(response_or->resource_path(), std::move(callback));
}
void GrpcResourceDataSource::ReadResourceFile(
std::string_view resource_file_path,
content::URLDataSource::GotDataCallback callback) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
base::FilePath path(resource_file_path);
if (!base::PathExists(path)) {
LOG(ERROR) << "Resource " << resource_file_path << " does not exist";
std::move(callback).Run(nullptr);
return;
}
std::string text;
base::ReadFileToString(base::FilePath(resource_file_path), &text);
std::move(callback).Run(
base::MakeRefCounted<base::RefCountedString>(std::move(text)));
}
// The Path can either be a filename or a remote url string starting with "?".
// Examples - "?remote_url=https://google.com", "fonts.css".
std::string GrpcResourceDataSource::GetMimeType(const GURL& url) {
const std::string path = content::URLDataSource::URLToRequestPath(url);
if (!for_webui_) {
std::string mime_type;
base::FilePath::StringType file_ext =
base::FilePath().AppendASCII(path).Extension();
if (!file_ext.empty())
net::GetWellKnownMimeTypeFromExtension(file_ext.substr(1), &mime_type);
return mime_type;
}
// If the path starts with ? or if the path does not contain an extension,
// return default MimeType.
auto extension_index = path.find_last_of(".");
if (path.find("?") != std::string::npos ||
extension_index == std::string::npos) {
return kMimeTypeHtml;
}
base::FilePath file_path(path);
auto extension = file_path.Extension();
if (extension.empty()) {
return kMimeTypeHtml;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeCss)) {
return kMimeTypeCss;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeJs)) {
return kMimeTypeJavascript;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeJson)) {
return kMimeTypeJson;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypePdf)) {
return kMimeTypePdf;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeSvg)) {
return kMimeTypeSvgXml;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeJpeg)) {
return kMimeTypeJpeg;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypePng)) {
return kMimeTypePng;
}
if (base::EqualsCaseInsensitiveASCII(extension, kExtensionTypeHtml)) {
return kMimeTypeHtml;
}
NOTREACHED() << "Unknown Mime type of file " << path;
}
bool GrpcResourceDataSource::ShouldServiceRequest(
const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) {
if (url.SchemeIs(kChromeResourceScheme)) {
return true;
}
return URLDataSource::ShouldServiceRequest(url, browser_context,
render_process_id);
}
std::string GrpcResourceDataSource::GetAccessControlAllowOriginForOrigin(
const std::string& origin) {
if (!base::StartsWith(origin, kAllowedOriginPrefix,
base::CompareCase::SENSITIVE)) {
return "";
}
return origin;
}
void GrpcResourceDataSource::OverrideContentSecurityPolicyChildSrc(
const std::string& data) {
frame_src_ = data;
}
void GrpcResourceDataSource::DisableDenyXFrameOptions() {
deny_xframe_options_ = false;
}
bool GrpcResourceDataSource::ShouldDenyXFrameOptions() {
return deny_xframe_options_;
}
} // namespace chromecast

@ -1,80 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_RESOURCE_DATA_SOURCE_H_
#define CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_RESOURCE_DATA_SOURCE_H_
#include <optional>
#include <string_view>
#include "base/task/sequenced_task_runner.h"
#include "content/public/browser/url_data_source.h"
#include "third_party/cast_core/public/src/proto/v2/core_application_service.castcore.pb.h"
namespace chromecast {
// This class is responsible for making data requests for resources required by
// Cast WebUI. This implementation uses gRPC for requesting resources from
// CastCore.
class GrpcResourceDataSource : public content::URLDataSource {
public:
GrpcResourceDataSource(
const std::string host,
bool for_webui,
cast::v2::CoreApplicationServiceStub* core_app_service_stub);
~GrpcResourceDataSource() override;
void OverrideContentSecurityPolicyChildSrc(const std::string& data);
void DisableDenyXFrameOptions();
private:
friend class GrpcResourceDataSourceTest;
// content::URLDataSource implementation.
std::string GetSource() override;
// Starts a gRPC request to fetch resources.
void StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) override;
std::string GetMimeType(const GURL& url) override;
// Determines whether the Url request is allowed.
bool ShouldServiceRequest(const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) override;
// Checks origin of the data request url.
std::string GetAccessControlAllowOriginForOrigin(
const std::string& origin) override;
bool ShouldDenyXFrameOptions() override;
// Helper methods.
void OnWebUiResourceReceived(
content::URLDataSource::GotDataCallback callback,
cast::utils::GrpcStatusOr<cast::v2::GetWebUIResourceResponse>
response_or);
void ReadResourceFile(std::string_view path,
content::URLDataSource::GotDataCallback callback);
scoped_refptr<base::SequencedTaskRunner> task_runner_;
const std::string host_;
// This is set to true when GrpcResourceDataSource is initialized by
// GrpcWebUIController and false otherwise. In practice, for all the
// chrome://home/* urls this is set to true and false for chrome-resource://*
const bool for_webui_;
cast::v2::CoreApplicationServiceStub* const core_app_service_stub_;
std::optional<std::string> frame_src_;
bool deny_xframe_options_ = true;
base::WeakPtrFactory<GrpcResourceDataSource> weak_factory_{this};
};
} // namespace chromecast
#endif // CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_RESOURCE_DATA_SOURCE_H_

@ -1,62 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/cast_core/runtime/browser/grpc_resource_data_source.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::IsEmpty;
namespace chromecast {
class GrpcResourceDataSourceTest : public ::testing::Test {
public:
GrpcResourceDataSourceTest()
: grpc_resource_data_source_("chrome", true, nullptr) {}
protected:
std::string GetMimeType(const std::string& path) {
const GURL url("chrome://chrome/" + path);
return grpc_resource_data_source_.GetMimeType(url);
}
std::string GetAccessControlAllowOriginForOrigin(const std::string& origin) {
return grpc_resource_data_source_.GetAccessControlAllowOriginForOrigin(
origin);
}
base::test::TaskEnvironment task_environment_;
std::string core_application_service_address_ = "fake-address";
GrpcResourceDataSource grpc_resource_data_source_;
};
TEST_F(GrpcResourceDataSourceTest, GetMimeTypeEmptyParam) {
EXPECT_EQ(GetMimeType(""), "text/html");
}
TEST_F(GrpcResourceDataSourceTest, GetMimeTypeRemoteUrl) {
EXPECT_EQ(GetMimeType("?resource=http://google.com"), "text/html");
}
TEST_F(GrpcResourceDataSourceTest, GetMimeTypeNoExtension) {
EXPECT_EQ(GetMimeType("fontscss"), "text/html");
}
TEST_F(GrpcResourceDataSourceTest, GetMimeTypeSuccess) {
EXPECT_EQ(GetMimeType("fonts.css"), "text/css");
}
TEST_F(GrpcResourceDataSourceTest,
GetAccessControlAllowOriginForOriginAllowedOrigin) {
EXPECT_EQ(GetAccessControlAllowOriginForOrigin("chrome://"), "chrome://");
}
TEST_F(GrpcResourceDataSourceTest,
GetAccessControlAllowOriginForOriginDisallowedOrigin) {
EXPECT_THAT(GetAccessControlAllowOriginForOrigin("chrome-resources://"),
IsEmpty());
}
} // namespace chromecast

@ -1,134 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/cast_core/runtime/browser/grpc_webui_controller.h"
#include <string_view>
#include "base/command_line.h"
#include "chromecast/base/chromecast_switches.h"
#include "chromecast/browser/webui/cast_webui_message_handler.h"
#include "chromecast/browser/webui/constants.h"
#include "chromecast/cast_core/runtime/browser/grpc_resource_data_source.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/bindings_policy.h"
namespace {
// Javascript callbacks from renderer.
constexpr const char kFuncStartPingNotify[] = "startPingNotify";
constexpr const char kFuncStopPingNotify[] = "stopPingNotify";
constexpr const char kFuncSetOobeFinished[] = "setOobeFinished";
constexpr const char kFuncRecordAction[] = "recordAction";
constexpr const char kFuncLaunchTutorial[] = "launchTutorial";
constexpr const char kFuncGetQRCode[] = "getQRCode";
// Javascript functions called by callbacks.
constexpr const char kJSPingNotifyCallback[] =
"home_web_ui_.pingNotifyCallback";
constexpr const char kJSEurekaInfoChangedCallback[] =
"home_web_ui_.eurekaInfoChangedCallback";
// ContentSecurityOverride Prefix.
constexpr const char kContentSecurityPolicyOverride[] =
"frame-src https://*.google.com,";
} // namespace
namespace chromecast {
// static
std::unique_ptr<GrpcWebUIController> GrpcWebUIController::Create(
content::WebUI* webui,
const std::string host,
cast::v2::CoreApplicationServiceStub* core_app_service_stub) {
return std::make_unique<GrpcWebUIController>(webui, host, core_app_service_stub);
}
GrpcWebUIController::GrpcWebUIController(
content::WebUI* webui,
const std::string host,
cast::v2::CoreApplicationServiceStub* core_app_service_stub)
: WebUIController(webui),
web_contents_(webui->GetWebContents()),
browser_context_(web_contents_->GetBrowserContext()) {
DCHECK(web_contents_);
DCHECK(browser_context_);
webui->SetBindings(
content::BindingsPolicySet({content::BindingsPolicyValue::kWebUi}));
auto cast_resources = std::make_unique<GrpcResourceDataSource>(
host, true /* for_webui */, core_app_service_stub);
if (host == kCastWebUIHomeHost) {
cast_resources->OverrideContentSecurityPolicyChildSrc(
kContentSecurityPolicyOverride);
cast_resources->DisableDenyXFrameOptions();
}
content::URLDataSource::Add(browser_context_, std::move(cast_resources));
auto message_handler = std::make_unique<CastWebUIMessageHandler>();
message_handler_ = message_handler.get();
webui->AddMessageHandler(std::move(message_handler));
RegisterMessageCallbacks();
}
GrpcWebUIController::~GrpcWebUIController() = default;
content::WebContents* GrpcWebUIController::web_contents() const {
return web_contents_;
}
content::BrowserContext* GrpcWebUIController::browser_context() const {
return browser_context_;
}
void GrpcWebUIController::StartPingNotify(const base::Value::List& args) {
CallJavascriptFunction(kJSPingNotifyCallback, {});
}
void GrpcWebUIController::StopPingNotify(const base::Value::List& args) {
CallJavascriptFunction(kJSEurekaInfoChangedCallback, {});
}
void GrpcWebUIController::SetOobeFinished(const base::Value::List& args) {}
void GrpcWebUIController::RecordAction(const base::Value::List& args) {}
void GrpcWebUIController::LaunchTutorial(const base::Value::List& args) {}
void GrpcWebUIController::GetQRCode(const base::Value::List& args) {}
void GrpcWebUIController::RegisterMessageCallbacks() {
web_ui()->RegisterMessageCallback(
kFuncStartPingNotify,
base::BindRepeating(&GrpcWebUIController::StartPingNotify,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kFuncStopPingNotify,
base::BindRepeating(&GrpcWebUIController::StopPingNotify,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kFuncSetOobeFinished,
base::BindRepeating(&GrpcWebUIController::SetOobeFinished,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kFuncRecordAction, base::BindRepeating(&GrpcWebUIController::RecordAction,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kFuncLaunchTutorial,
base::BindRepeating(&GrpcWebUIController::LaunchTutorial,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
kFuncGetQRCode, base::BindRepeating(&GrpcWebUIController::GetQRCode,
base::Unretained(this)));
}
void GrpcWebUIController::CallJavascriptFunction(
std::string_view function,
base::span<const base::ValueView> args) {
message_handler_->CallJavascriptFunction(function, std::move(args));
}
} // namespace chromecast

@ -1,75 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_H_
#define CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_H_
#include <memory>
#include <string_view>
#include "base/containers/flat_map.h"
#include "base/containers/span.h"
#include "base/values.h"
#include "content/public/browser/web_ui_controller.h"
#include "third_party/cast_core/public/src/proto/v2/core_application_service.castcore.pb.h"
namespace content {
class BrowserContext;
class WebContents;
class WebUI;
} // namespace content
namespace chromecast {
class CastWebUIMessageHandler;
// WebUI is a part of the Chrome Web Technologies stack used to load UIs for
// chrome: URLs. GrpcWebUIController is used for rendering Backdrop (IdleScreen)
// on devices running CastCore receiver + Web runtime.
class GrpcWebUIController : public content::WebUIController {
public:
// |webui| stays alive for the lifetime of the Backdrop app. Every time the
// screen switches from casting to idle screen (Backdrop), a new WebUI
// instance is created.
GrpcWebUIController(
content::WebUI* webui,
const std::string host,
cast::v2::CoreApplicationServiceStub* core_app_service_stub);
~GrpcWebUIController() override;
// Creates an instance of GrpcWebUIController.
// Implementation is inside GrpcExtensionWebUI.
static std::unique_ptr<GrpcWebUIController> Create(
content::WebUI* webui,
const std::string host,
cast::v2::CoreApplicationServiceStub* core_app_service_stub);
protected:
content::WebContents* web_contents() const;
content::BrowserContext* browser_context() const;
private:
content::WebContents* const web_contents_;
content::BrowserContext* const browser_context_;
void RegisterMessageCallbacks();
// Callbacks from javascript
void StartPingNotify(const base::Value::List& args);
void StopPingNotify(const base::Value::List& args);
void SetOobeFinished(const base::Value::List& args);
void RecordAction(const base::Value::List& args);
void LaunchTutorial(const base::Value::List& args);
void GetQRCode(const base::Value::List& args);
void CallJavascriptFunction(std::string_view function,
base::span<const base::ValueView> args);
// Pointer to the generic message handler owned by the ctor provided|webui|.
// The message handler is guaranteed to outlive GrpcWebUIController since
// |this| is the first member to be deleted in the Web UI.
CastWebUIMessageHandler* message_handler_;
};
} // namespace chromecast
#endif // CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_H_

@ -1,55 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/cast_core/runtime/browser/grpc_webui_controller_factory.h"
#include "base/containers/contains.h"
#include "chromecast/browser/webui/constants.h"
#include "chromecast/cast_core/runtime/browser/grpc_resource_data_source.h"
#include "chromecast/cast_core/runtime/browser/grpc_webui_controller.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "url/gurl.h"
namespace chromecast {
GrpcWebUiControllerFactory::GrpcWebUiControllerFactory(
const std::vector<std::string> hosts,
cast::v2::CoreApplicationServiceStub* core_app_service_stub)
: hosts_(std::move(hosts)), core_app_service_stub_(core_app_service_stub) {}
GrpcWebUiControllerFactory::~GrpcWebUiControllerFactory() = default;
content::WebUI::TypeID GrpcWebUiControllerFactory::GetWebUIType(
content::BrowserContext* browser_context,
const GURL& url) {
if (base::Contains(hosts_, url.host())) {
return const_cast<GrpcWebUiControllerFactory*>(this);
}
return content::WebUI::kNoWebUI;
}
bool GrpcWebUiControllerFactory::UseWebUIForURL(
content::BrowserContext* browser_context,
const GURL& url) {
return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI;
}
std::unique_ptr<content::WebUIController>
GrpcWebUiControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
const GURL& url) {
static std::once_flag flag;
std::call_once(flag, [this, web_ui] {
auto cast_resources = std::make_unique<GrpcResourceDataSource>(
kCastWebUIResourceHost, false /* for_webui */,
this->core_app_service_stub_);
content::URLDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
std::move(cast_resources));
});
return GrpcWebUIController::Create(web_ui, url.host(),
core_app_service_stub_);
}
} // namespace chromecast

@ -1,53 +0,0 @@
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_FACTORY_H_
#define CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_FACTORY_H_
#include "content/public/browser/web_ui_controller_factory.h"
#include "third_party/cast_core/public/src/proto/v2/core_application_service.castcore.pb.h"
namespace content {
class BrowserContext;
class WebUI;
class WebUIController;
} // namespace content
namespace chromecast {
// WebUIController Factory that uses gRPC for IPC. It determines which WebUIType
// to use based on the URL and creates an instance of GrpcWebUI for Urls of the
// form chrome://*.
class GrpcWebUiControllerFactory : public content::WebUIControllerFactory {
public:
GrpcWebUiControllerFactory(
const std::vector<std::string> hosts,
cast::v2::CoreApplicationServiceStub* core_app_service_stub);
GrpcWebUiControllerFactory(const GrpcWebUiControllerFactory&) = delete;
GrpcWebUiControllerFactory& operator=(const GrpcWebUiControllerFactory&) =
delete;
~GrpcWebUiControllerFactory() override;
// content::WebUIControllerFactory implementation:
content::WebUI::TypeID GetWebUIType(content::BrowserContext* browser_context,
const GURL& url) override;
// Gets WebUI type for the url.
bool UseWebUIForURL(content::BrowserContext* browser_context,
const GURL& url) override;
// For CastCore, this function only creates an instance of GrpcWebUI
// and does not create a WebUIController as it does not exist on core side.
std::unique_ptr<content::WebUIController> CreateWebUIControllerForURL(
content::WebUI* web_ui,
const GURL& url) override;
private:
const std::vector<std::string> hosts_;
cast::v2::CoreApplicationServiceStub* const core_app_service_stub_;
};
} // namespace chromecast
#endif // CHROMECAST_CAST_CORE_RUNTIME_BROWSER_GRPC_WEBUI_CONTROLLER_FACTORY_H_

@ -17,7 +17,6 @@
#include "chromecast/browser/cast_web_view.h"
#include "chromecast/cast_core/grpc/grpc_status_or.h"
#include "chromecast/cast_core/runtime/browser/core_streaming_config_manager.h"
#include "chromecast/cast_core/runtime/browser/grpc_webui_controller_factory.h"
#include "chromecast/cast_core/runtime/browser/message_port_service_grpc.h"
#include "chromecast/cast_core/runtime/browser/url_rewrite/url_request_rewrite_type_converters.h"
#include "chromecast/common/feature_constants.h"
@ -576,15 +575,6 @@ RuntimeApplicationServiceImpl::GetMessagePortService() {
return GetMessagePortServiceGrpc();
}
std::unique_ptr<content::WebUIControllerFactory>
RuntimeApplicationServiceImpl::CreateWebUIControllerFactory(
std::vector<std::string> hosts) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(core_app_stub_);
return std::make_unique<GrpcWebUiControllerFactory>(std::move(hosts),
&core_app_stub_.value());
}
content::WebContents* RuntimeApplicationServiceImpl::GetWebContents() {
if (!cast_web_view_) {
return nullptr;

@ -33,7 +33,6 @@ class StreamingConfigManager;
namespace content {
class WebContents;
class WebUIControllerFactory;
} // namespace content
namespace chromecast {
@ -69,8 +68,6 @@ class RuntimeApplicationServiceImpl : public cast_receiver::EmbedderApplication,
void NotifyMediaPlaybackChanged(bool playing) override;
void GetAllBindings(GetAllBindingsCallback callback) override;
cast_receiver::MessagePortService* GetMessagePortService() override;
std::unique_ptr<content::WebUIControllerFactory> CreateWebUIControllerFactory(
std::vector<std::string> hosts) override;
content::WebContents* GetWebContents() override;
cast_receiver::ContentWindowControls* GetContentWindowControls() override;
#if !BUILDFLAG(IS_CAST_DESKTOP_BUILD)

@ -69,12 +69,6 @@ StreamingConfigManager* EmbedderApplication::GetStreamingConfigManager() {
return ptr;
}
std::unique_ptr<content::WebUIControllerFactory>
EmbedderApplication::CreateWebUIControllerFactory(
std::vector<std::string> hosts) {
return nullptr;
}
void EmbedderApplication::NavigateToPage(const GURL& gurl) {
content::WebContents* web_contents = GetWebContents();
DCHECK(web_contents);

@ -15,7 +15,6 @@
namespace content {
class WebContents;
class WebUIControllerFactory;
} // namespace content
namespace cast_receiver {
@ -78,13 +77,6 @@ class EmbedderApplication {
// Chromium state.
virtual StreamingConfigManager* GetStreamingConfigManager();
// Creates a new platform-specific WebUIControllerFactory, or nullptr if
// this feature is not to be supported. Returns nullptr by default.
// |hosts| is the set of hosts for which the custom WebUIController associated
// with the returned factory should be used.
virtual std::unique_ptr<content::WebUIControllerFactory>
CreateWebUIControllerFactory(std::vector<std::string> hosts);
// Loads |url| in the associated WebContents.
virtual void NavigateToPage(const GURL& url);
};

@ -10,7 +10,6 @@
#include "components/url_rewrite/browser/url_request_rewrite_rules_manager.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "net/base/net_errors.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
@ -40,16 +39,6 @@ void WebRuntimeApplication::Launch(StatusCallback callback) {
SetContentPermissions(*embedder_application().GetWebContents());
// Register GrpcWebUI for handling Cast apps with URLs in the form
// chrome*://* that use WebUIs.
auto web_ui_controller_factory =
embedder_application().CreateWebUIControllerFactory(
{"home", "error", "cast_resources"});
if (web_ui_controller_factory) {
content::WebUIControllerFactory::RegisterFactory(
web_ui_controller_factory.release());
}
embedder_application().GetAllBindings(base::BindPostTask(
task_runner(),
base::BindOnce(&WebRuntimeApplication::OnAllBindingsReceived,