Commit 3c78cf97 authored by Daniel Nicoara's avatar Daniel Nicoara Committed by Commit Bot

Change the Webview gRPC service handler to take in the service and CQ

Rather than start a new service, allow the caller to re-use an existing
connection. In this model, the caller is responsible for initializing
the service and a completion queue the async Webview service would be
using to execute tasks.

BUG=b/132811925
TEST=Compiled

Change-Id: I7e7cdffdccc6f9e7282a1b952e2e5d8013a93be3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1804556
Commit-Queue: Daniel Nicoara <dnicoara@chromium.org>
Reviewed-by: default avatarAlex Sakhartchouk <alexst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696890}
parent a2a0d278
...@@ -233,28 +233,17 @@ void WebviewRpcInstance::OnError(const std::string& error_message) { ...@@ -233,28 +233,17 @@ void WebviewRpcInstance::OnError(const std::string& error_message) {
} // namespace } // namespace
WebviewAsyncService::WebviewAsyncService( WebviewAsyncService::WebviewAsyncService(
scoped_refptr<base::SingleThreadTaskRunner> webview_task_runner) std::unique_ptr<webview::WebviewService::AsyncService> service,
: webview_task_runner_(std::move(webview_task_runner)) {} std::unique_ptr<grpc::ServerCompletionQueue> cq,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
WebviewAsyncService::~WebviewAsyncService() { : ui_task_runner_(std::move(ui_task_runner)),
if (server_) { cq_(std::move(cq)),
server_->Shutdown(); service_(std::move(service)) {
cq_->Shutdown(); base::PlatformThread::Create(0, this, &rpc_thread_);
base::PlatformThread::Join(rpc_thread_);
}
} }
void WebviewAsyncService::StartWithSocket(const base::FilePath& socket_path) { WebviewAsyncService::~WebviewAsyncService() {
DCHECK(!server_.get()); base::PlatformThread::Join(rpc_thread_);
grpc::ServerBuilder builder;
builder.AddListeningPort("unix:" + socket_path.value(),
grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
cq_ = builder.AddCompletionQueue();
server_ = builder.BuildAndStart();
base::PlatformThread::Create(0, this, &rpc_thread_);
} }
void WebviewAsyncService::ThreadMain() { void WebviewAsyncService::ThreadMain() {
...@@ -263,7 +252,7 @@ void WebviewAsyncService::ThreadMain() { ...@@ -263,7 +252,7 @@ void WebviewAsyncService::ThreadMain() {
void* tag; void* tag;
bool ok; bool ok;
// This self-deletes. // This self-deletes.
new WebviewRpcInstance(&service_, cq_.get(), webview_task_runner_); new WebviewRpcInstance(service_.get(), cq_.get(), ui_task_runner_);
// This thread is joined when this service is destroyed. // This thread is joined when this service is destroyed.
while (cq_->Next(&tag, &ok)) { while (cq_->Next(&tag, &ok)) {
reinterpret_cast<GrpcCallback*>(tag)->Run(ok); reinterpret_cast<GrpcCallback*>(tag)->Run(ok);
......
...@@ -19,21 +19,24 @@ namespace chromecast { ...@@ -19,21 +19,24 @@ namespace chromecast {
// webviews. See the proto file for commands. // webviews. See the proto file for commands.
class WebviewAsyncService : public base::PlatformThread::Delegate { class WebviewAsyncService : public base::PlatformThread::Delegate {
public: public:
explicit WebviewAsyncService( WebviewAsyncService(
scoped_refptr<base::SingleThreadTaskRunner> webview_task_runner); std::unique_ptr<webview::WebviewService::AsyncService> service,
std::unique_ptr<grpc::ServerCompletionQueue> cq,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
~WebviewAsyncService() override; ~WebviewAsyncService() override;
// Start the server listening on an address, eg "localhost:12345".
void StartWithSocket(const base::FilePath& socket_path);
private: private:
void ThreadMain() override; void ThreadMain() override;
// Separate thread to run the gRPC completion queue on.
base::PlatformThreadHandle rpc_thread_; base::PlatformThreadHandle rpc_thread_;
scoped_refptr<base::SingleThreadTaskRunner> webview_task_runner_;
// 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<grpc::ServerCompletionQueue> cq_;
webview::WebviewService::AsyncService service_; std::unique_ptr<webview::WebviewService::AsyncService> service_;
std::unique_ptr<grpc::Server> server_;
DISALLOW_COPY_AND_ASSIGN(WebviewAsyncService); DISALLOW_COPY_AND_ASSIGN(WebviewAsyncService);
}; };
......
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