Commit 86fab625 authored by Shawn Gallea's avatar Shawn Gallea Committed by Commit Bot

Add PlatformViewsService

This service allows control of webviews and cast apps via gRPC.
Webview functionality will be added in a future change.

Merge-With: eureka-internal/333992

Bug: Internal b/143945774
Bug: Internal b/143307560
Test: Build cast_shell
Change-Id: I05d36a2bd8900afeee3a175c97d773428d482311
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1907108
Commit-Queue: Shawn Gallea <sagallea@google.com>
Reviewed-by: default avatarDaniel Nicoara <dnicoara@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714229}
parent 82a19f3a
...@@ -389,8 +389,14 @@ cast_source_set("browser") { ...@@ -389,8 +389,14 @@ cast_source_set("browser") {
"exo/cast_wm_helper.h", "exo/cast_wm_helper.h",
"exo/wayland_server_controller.cc", "exo/wayland_server_controller.cc",
"exo/wayland_server_controller.h", "exo/wayland_server_controller.h",
"webview/cast_app_controller.cc",
"webview/cast_app_controller.h",
"webview/cast_app_rpc_instance.cc",
"webview/cast_app_rpc_instance.h",
"webview/js_channel_service.cc", "webview/js_channel_service.cc",
"webview/js_channel_service.h", "webview/js_channel_service.h",
"webview/platform_views_grpc_service.cc",
"webview/platform_views_grpc_service.h",
"webview/platform_views_rpc_instance.cc", "webview/platform_views_rpc_instance.cc",
"webview/platform_views_rpc_instance.h", "webview/platform_views_rpc_instance.h",
"webview/web_content_controller.cc", "webview/web_content_controller.cc",
...@@ -410,6 +416,7 @@ cast_source_set("browser") { ...@@ -410,6 +416,7 @@ cast_source_set("browser") {
] ]
configs += [ "//third_party/grpc:grpc_config" ] configs += [ "//third_party/grpc:grpc_config" ]
deps += [ deps += [
":web_contents_provider",
"//chromecast/browser/webview/proto", "//chromecast/browser/webview/proto",
"//components/exo", "//components/exo",
"//components/exo/wayland", "//components/exo/wayland",
...@@ -577,3 +584,13 @@ if (is_android) { ...@@ -577,3 +584,13 @@ if (is_android) {
] ]
} }
} }
cast_source_set("web_contents_provider") {
sources = [
"webview/web_contents_provider.h",
]
deps = [
"//content/public/browser",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/browser/webview/cast_app_controller.h"
namespace chromecast {
CastAppController::CastAppController(Client* client,
content::WebContents* contents)
: WebContentController(client), contents_(contents) {
std::unique_ptr<webview::WebviewResponse> response =
std::make_unique<webview::WebviewResponse>();
client->EnqueueSend(std::move(response));
}
CastAppController::~CastAppController() {}
void CastAppController::Destroy() {
client_ = nullptr;
delete this;
}
content::WebContents* CastAppController::GetWebContents() {
return contents_;
}
} // namespace chromecast
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_BROWSER_WEBVIEW_CAST_APP_CONTROLLER_H_
#define CHROMECAST_BROWSER_WEBVIEW_CAST_APP_CONTROLLER_H_
#include "chromecast/browser/webview/web_content_controller.h"
#include "base/macros.h"
namespace chromecast {
class CastAppController : public WebContentController {
public:
CastAppController(Client* client, content::WebContents* contents);
~CastAppController() override;
void Destroy() override;
protected:
content::WebContents* GetWebContents() override;
private:
content::WebContents* contents_;
DISALLOW_COPY_AND_ASSIGN(CastAppController);
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_WEBVIEW_CAST_APP_CONTROLLER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/browser/webview/cast_app_rpc_instance.h"
#include "base/bind.h"
#include "base/callback.h"
#include "chromecast/browser/webview/cast_app_controller.h"
#include "chromecast/browser/webview/platform_views_rpc_instance.h"
#include "chromecast/browser/webview/web_contents_provider.h"
#include "third_party/grpc/src/include/grpcpp/grpcpp.h"
#include "third_party/grpc/src/include/grpcpp/security/server_credentials.h"
#include "third_party/grpc/src/include/grpcpp/server.h"
#include "third_party/grpc/src/include/grpcpp/server_builder.h"
namespace chromecast {
CastAppRpcInstance::CastAppRpcInstance(
webview::PlatformViewsService::AsyncService* service,
grpc::ServerCompletionQueue* cq,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
WebviewWindowManager* window_manager,
WebContentsProvider* web_contents_provider)
: PlatformViewsRpcInstance(cq, task_runner, window_manager),
service_(service),
web_contents_provider_(web_contents_provider) {
service_->RequestCreateCastAppWindowLink(&ctx_, &io_, cq_, cq_,
&init_callback_);
}
CastAppRpcInstance::~CastAppRpcInstance() {}
void CastAppRpcInstance::CreateNewInstance() {
new CastAppRpcInstance(service_, cq_, task_runner_, window_manager_,
web_contents_provider_);
}
bool CastAppRpcInstance::Initialize() {
if (request_->type_case() != webview::WebviewRequest::kAssociate)
return false;
// This needs to be done on a valid thread.
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&CastAppRpcInstance::CreateCastAppWindowLink,
base::Unretained(this),
request_->associate().platform_view_id(),
request_->associate().app_window_id()));
return true;
}
void CastAppRpcInstance::CreateCastAppWindowLink(int platform_view_id,
int app_window_id) {
app_id_ = platform_view_id;
content::WebContents* web_contents =
web_contents_provider_->GetWebContents(app_window_id);
Observe(web_contents);
controller_ = std::make_unique<CastAppController>(this, web_contents);
window_manager_->AddObserver(this);
// Begin reading again.
io_.Read(request_.get(), &read_callback_);
}
void CastAppRpcInstance::WebContentsDestroyed() {
controller_.reset();
}
} // namespace chromecast
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_BROWSER_WEBVIEW_CAST_APP_RPC_INSTANCE_H_
#define CHROMECAST_BROWSER_WEBVIEW_CAST_APP_RPC_INSTANCE_H_
#include "chromecast/browser/webview/platform_views_rpc_instance.h"
#include "content/public/browser/web_contents_observer.h"
namespace chromecast {
class WebContentsProvider;
class CastAppRpcInstance : public PlatformViewsRpcInstance,
public content::WebContentsObserver {
public:
CastAppRpcInstance(webview::PlatformViewsService::AsyncService* service,
grpc::ServerCompletionQueue* cq,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
WebviewWindowManager* window_manager,
WebContentsProvider* web_contents_provider);
~CastAppRpcInstance() override;
protected:
void CreateNewInstance() override;
bool Initialize() override;
private:
void CreateCastAppWindowLink(int platform_view_id, int app_window_id);
void WebContentsDestroyed() override;
webview::PlatformViewsService::AsyncService* service_;
WebContentsProvider* web_contents_provider_;
DISALLOW_COPY_AND_ASSIGN(CastAppRpcInstance);
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_WEBVIEW_CAST_APP_RPC_INSTANCE_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/browser/webview/platform_views_grpc_service.h"
#include "base/callback.h"
#include "chromecast/browser/webview/cast_app_rpc_instance.h"
#include "chromecast/browser/webview/web_contents_provider.h"
#include "third_party/grpc/src/include/grpcpp/grpcpp.h"
#include "third_party/grpc/src/include/grpcpp/security/server_credentials.h"
#include "third_party/grpc/src/include/grpcpp/server_builder.h"
namespace chromecast {
PlatformViewsAsyncService::PlatformViewsAsyncService(
std::unique_ptr<webview::PlatformViewsService::AsyncService> service,
std::unique_ptr<grpc::ServerCompletionQueue> cq,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
WebContentsProvider* web_contents_provider)
: ui_task_runner_(std::move(ui_task_runner)),
cq_(std::move(cq)),
service_(std::move(service)),
window_manager_(nullptr),
web_contents_provider_(web_contents_provider) {
base::PlatformThread::Create(0, this, &rpc_thread_);
}
PlatformViewsAsyncService::~PlatformViewsAsyncService() {
base::PlatformThread::Join(rpc_thread_);
}
void PlatformViewsAsyncService::ThreadMain() {
base::PlatformThread::SetName("CastPlatformViewsGrpcMessagePump");
void* tag;
bool ok;
// This self-deletes.
new CastAppRpcInstance(service_.get(), cq_.get(), ui_task_runner_,
&window_manager_, web_contents_provider_);
// This thread is joined when this service is destroyed.
while (cq_->Next(&tag, &ok)) {
reinterpret_cast<GrpcCallback*>(tag)->Run(ok);
}
}
} // namespace chromecast
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_BROWSER_WEBVIEW_PLATFORM_VIEWS_GRPC_SERVICE_H_
#define CHROMECAST_BROWSER_WEBVIEW_PLATFORM_VIEWS_GRPC_SERVICE_H_
#include <string>
#include "base/files/file_path.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromecast/browser/webview/proto/webview.grpc.pb.h"
#include "chromecast/browser/webview/webview_window_manager.h"
#include "third_party/grpc/src/include/grpcpp/server.h"
namespace chromecast {
class WebContentsProvider;
// This is a service that provides a GRPC interface to create and control
// webviews, as well as control cast apps. See the proto file for commands.
class PlatformViewsAsyncService : public base::PlatformThread::Delegate {
public:
PlatformViewsAsyncService(
std::unique_ptr<webview::PlatformViewsService::AsyncService> service,
std::unique_ptr<grpc::ServerCompletionQueue> cq,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
WebContentsProvider* web_contents_provider);
~PlatformViewsAsyncService() override;
private:
void ThreadMain() override;
// Separate thread to run the gRPC completion queue on.
base::PlatformThreadHandle rpc_thread_;
// Requests need to be posted back to the browser main UI thread to manage
// Webview state.
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
std::unique_ptr<grpc::ServerCompletionQueue> cq_;
std::unique_ptr<webview::PlatformViewsService::AsyncService> service_;
WebviewWindowManager window_manager_;
WebContentsProvider* web_contents_provider_;
DISALLOW_COPY_AND_ASSIGN(PlatformViewsAsyncService);
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_WEBVIEW_PLATFORM_VIEWS_GRPC_SERVICE_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMECAST_BROWSER_WEBVIEW_WEB_CONTENTS_PROVIDER_H_
#define CHROMECAST_BROWSER_WEBVIEW_WEB_CONTENTS_PROVIDER_H_
#include "content/public/browser/web_contents.h"
namespace chromecast {
// Class that provides WebContents when given a window ID.
class WebContentsProvider {
public:
virtual content::WebContents* GetWebContents(int window_id) = 0;
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_WEBVIEW_WEB_CONTENTS_PROVIDER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment