Commit 7eb54c85 authored by Xiyuan Xia's avatar Xiyuan Xia Committed by Commit Bot

arc: Move MojoChannel into its own file

Move MojoChannel code out of ArcBridgeHostImpl into its own file
to be shared.

Bug: 768439
Change-Id: I730ae1f498893455f3aa2e768f47893409109768
Reviewed-on: https://chromium-review.googlesource.com/1012136Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550646}
parent 100b5c73
......@@ -153,6 +153,7 @@ static_library("arc_base") {
"connection_notifier.cc",
"connection_notifier.h",
"connection_observer.h",
"mojo_channel.h",
]
deps = [
......
......@@ -10,63 +10,10 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "components/arc/arc_bridge_service.h"
#include "components/arc/mojo_channel.h"
namespace arc {
// Thin interface to wrap InterfacePtr<T> with type erasure.
class ArcBridgeHostImpl::MojoChannel {
public:
virtual ~MojoChannel() = default;
protected:
MojoChannel() = default;
private:
DISALLOW_COPY_AND_ASSIGN(MojoChannel);
};
namespace {
// The thin wrapper for InterfacePtr<T>, where T is one of ARC mojo Instance
// class.
template <typename InstanceType, typename HostType>
class MojoChannelImpl : public ArcBridgeHostImpl::MojoChannel {
public:
MojoChannelImpl(ConnectionHolder<InstanceType, HostType>* holder,
mojo::InterfacePtr<InstanceType> ptr)
: holder_(holder), ptr_(std::move(ptr)) {
// Delay registration to the ConnectionHolder until the version is ready.
}
~MojoChannelImpl() override { holder_->CloseInstance(ptr_.get()); }
void set_connection_error_handler(base::OnceClosure error_handler) {
ptr_.set_connection_error_handler(std::move(error_handler));
}
void QueryVersion() {
// Note: the callback will not be called if |ptr_| is destroyed.
ptr_.QueryVersion(
base::Bind(&MojoChannelImpl::OnVersionReady, base::Unretained(this)));
}
private:
void OnVersionReady(uint32_t unused_version) {
holder_->SetInstance(ptr_.get(), ptr_.version());
}
// Owned by ArcBridgeService.
ConnectionHolder<InstanceType, HostType>* const holder_;
// Put as a last member to ensure that any callback tied to the |ptr_|
// is not invoked.
mojo::InterfacePtr<InstanceType> ptr_;
DISALLOW_COPY_AND_ASSIGN(MojoChannelImpl);
};
} // namespace
ArcBridgeHostImpl::ArcBridgeHostImpl(ArcBridgeService* arc_bridge_service,
mojom::ArcBridgeInstancePtr instance)
: arc_bridge_service_(arc_bridge_service),
......@@ -320,7 +267,7 @@ void ArcBridgeHostImpl::OnInstanceReady(
// closed on ArcBridgeHost/Instance closing or the ArcBridgeHostImpl's
// destruction.
auto* channel =
new MojoChannelImpl<InstanceType, HostType>(holder, std::move(ptr));
new MojoChannel<InstanceType, HostType>(holder, std::move(ptr));
mojo_channels_.emplace_back(channel);
// Since |channel| is managed by |mojo_channels_|, its lifetime is shorter
......@@ -334,11 +281,11 @@ void ArcBridgeHostImpl::OnInstanceReady(
channel->QueryVersion();
}
void ArcBridgeHostImpl::OnChannelClosed(MojoChannel* channel) {
void ArcBridgeHostImpl::OnChannelClosed(MojoChannelBase* channel) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
mojo_channels_.erase(
std::find_if(mojo_channels_.begin(), mojo_channels_.end(),
[channel](std::unique_ptr<MojoChannel>& ptr) {
[channel](std::unique_ptr<MojoChannelBase>& ptr) {
return ptr.get() == channel;
}));
}
......
......@@ -18,6 +18,7 @@
namespace arc {
class ArcBridgeService;
class MojoChannelBase;
// Implementation of the ArcBridgeHost.
// The lifetime of ArcBridgeHost and ArcBridgeInstance mojo channels are tied
......@@ -30,9 +31,6 @@ class ArcBridgeService;
// Note that ArcBridgeService must be alive while ArcBridgeHostImpl is alive.
class ArcBridgeHostImpl : public mojom::ArcBridgeHost {
public:
// Interface to keep the Mojo channel InterfacePtr.
class MojoChannel;
ArcBridgeHostImpl(ArcBridgeService* arc_bridge_service,
mojom::ArcBridgeInstancePtr instance);
~ArcBridgeHostImpl() override;
......@@ -114,7 +112,7 @@ class ArcBridgeHostImpl : public mojom::ArcBridgeHost {
mojo::InterfacePtr<InstanceType> ptr);
// Called if one of the established channels is closed.
void OnChannelClosed(MojoChannel* channel);
void OnChannelClosed(MojoChannelBase* channel);
THREAD_CHECKER(thread_checker_);
......@@ -126,7 +124,7 @@ class ArcBridgeHostImpl : public mojom::ArcBridgeHost {
// Put as a last member to ensure that any callback tied to the elements
// is not invoked.
std::vector<std::unique_ptr<MojoChannel>> mojo_channels_;
std::vector<std::unique_ptr<MojoChannelBase>> mojo_channels_;
DISALLOW_COPY_AND_ASSIGN(ArcBridgeHostImpl);
};
......
// Copyright 2018 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 COMPONENTS_ARC_MOJO_CHANNEL_H_
#define COMPONENTS_ARC_MOJO_CHANNEL_H_
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "components/arc/connection_holder.h"
namespace arc {
// Thin interface to wrap InterfacePtr<T> with type erasure.
class MojoChannelBase {
public:
virtual ~MojoChannelBase() = default;
protected:
MojoChannelBase() = default;
private:
DISALLOW_COPY_AND_ASSIGN(MojoChannelBase);
};
// Thin wrapper for InterfacePtr<T>, where T is one of ARC mojo Instance class.
template <typename InstanceType, typename HostType>
class MojoChannel : public MojoChannelBase {
public:
MojoChannel(ConnectionHolder<InstanceType, HostType>* holder,
mojo::InterfacePtr<InstanceType> ptr)
: holder_(holder), ptr_(std::move(ptr)) {
// Delay registration to the ConnectionHolder until the version is ready.
}
~MojoChannel() override { holder_->CloseInstance(ptr_.get()); }
void set_connection_error_handler(base::OnceClosure error_handler) {
ptr_.set_connection_error_handler(std::move(error_handler));
}
void QueryVersion() {
// Note: the callback will not be called if |ptr_| is destroyed.
ptr_.QueryVersion(
base::Bind(&MojoChannel::OnVersionReady, base::Unretained(this)));
}
private:
void OnVersionReady(uint32_t unused_version) {
holder_->SetInstance(ptr_.get(), ptr_.version());
}
// Externally owned ConnectionHolder instance.
ConnectionHolder<InstanceType, HostType>* const holder_;
// Put as a last member to ensure that any callback tied to the |ptr_|
// is not invoked.
mojo::InterfacePtr<InstanceType> ptr_;
DISALLOW_COPY_AND_ASSIGN(MojoChannel);
};
} // namespace arc
#endif // COMPONENTS_ARC_MOJO_CHANNEL_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