| Index: mojo/public/cpp/bindings/lib/interface_ptr_state.h
|
| diff --git a/mojo/public/cpp/bindings/lib/interface_ptr_state.h b/mojo/public/cpp/bindings/lib/interface_ptr_state.h
|
| index 584933ebc91b417c96c1cbdaf3e532819f8067ca..3df3aeb7352d4f45af91dbfeb28a317330cc84ef 100644
|
| --- a/mojo/public/cpp/bindings/lib/interface_ptr_state.h
|
| +++ b/mojo/public/cpp/bindings/lib/interface_ptr_state.h
|
| @@ -35,21 +35,85 @@ namespace internal {
|
| template <typename Interface, bool use_multiplex_router>
|
| class InterfacePtrState;
|
|
|
| +// Non-templated base class to reduce the amount of code generated for every
|
| +// mojom file.
|
| +class SimpleInterfacePtrState {
|
| + public:
|
| + SimpleInterfacePtrState();
|
| +
|
| + ~SimpleInterfacePtrState();
|
| +
|
| + uint32_t version() const { return version_; }
|
| +
|
| + void QueryVersion(const base::Callback<void(uint32_t)>& callback);
|
| +
|
| + void RequireVersion(uint32_t version);
|
| +
|
| + bool HasAssociatedInterfaces() const;
|
| +
|
| + bool is_bound() const { return handle_.is_valid() || router_; }
|
| +
|
| + bool encountered_error() const {
|
| + return router_ ? router_->encountered_error() : false;
|
| + }
|
| +
|
| + void set_connection_error_handler(const base::Closure& error_handler) {
|
| + ConfigureProxyIfNecessary();
|
| +
|
| + DCHECK(router_);
|
| + router_->set_connection_error_handler(error_handler);
|
| + }
|
| +
|
| + // Returns true if bound and awaiting a response to a message.
|
| + bool has_pending_callbacks() const {
|
| + return router_ && router_->has_pending_responders();
|
| + }
|
| +
|
| + AssociatedGroup* associated_group() { return nullptr; }
|
| +
|
| + void EnableTestingMode();
|
| +
|
| + protected:
|
| + void BindInternal(InterfacePtrInfoBase* info,
|
| + scoped_refptr<base::SingleThreadTaskRunner> runner);
|
| +
|
| + virtual void ConfigureProxyIfNecessary() = 0;
|
| +
|
| + bool ConfigureProxyIfNecessaryInternal(const char* interface_name);
|
| +
|
| + virtual MessageFilter* CreateRequestValidator() = 0;
|
| +
|
| + virtual ControlMessageProxy* proxy() = 0;
|
| +
|
| + Router* router_;
|
| +
|
| + // |proxy_| and |router_| are not initialized until read/write with the
|
| + // message pipe handle is needed. |handle_| is valid between the Bind() call
|
| + // and the initialization of |proxy_| and |router_|.
|
| + ScopedMessagePipeHandle handle_;
|
| + scoped_refptr<base::SingleThreadTaskRunner> runner_;
|
| +
|
| + uint32_t version_;
|
| +
|
| + private:
|
| + void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
|
| + uint32_t version);
|
| +};
|
| +
|
| // Uses a single-threaded, dedicated router. If |Interface| doesn't have any
|
| // methods to pass associated interface pointers or requests, there won't be
|
| // multiple interfaces running on the underlying message pipe. In that case, we
|
| // can use this specialization to reduce cost.
|
| template <typename Interface>
|
| -class InterfacePtrState<Interface, false> {
|
| +class InterfacePtrState<Interface, false> : public SimpleInterfacePtrState {
|
| public:
|
| - InterfacePtrState() : proxy_(nullptr), router_(nullptr), version_(0u) {}
|
| + InterfacePtrState() : proxy_(nullptr) {}
|
|
|
| ~InterfacePtrState() {
|
| // Destruction order matters here. We delete |proxy_| first, even though
|
| // |router_| may have a reference to it, so that destructors for any request
|
| // callbacks still pending can interact with the InterfacePtr.
|
| delete proxy_;
|
| - delete router_;
|
| }
|
|
|
| Interface* instance() {
|
| @@ -59,31 +123,6 @@ class InterfacePtrState<Interface, false> {
|
| return proxy_;
|
| }
|
|
|
| - uint32_t version() const { return version_; }
|
| -
|
| - void QueryVersion(const base::Callback<void(uint32_t)>& callback) {
|
| - ConfigureProxyIfNecessary();
|
| -
|
| - // Do a static cast in case the interface contains methods with the same
|
| - // name. It is safe to capture |this| because the callback won't be run
|
| - // after this object goes away.
|
| - static_cast<ControlMessageProxy*>(proxy_)->QueryVersion(
|
| - base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this),
|
| - callback));
|
| - }
|
| -
|
| - void RequireVersion(uint32_t version) {
|
| - ConfigureProxyIfNecessary();
|
| -
|
| - if (version <= version_)
|
| - return;
|
| -
|
| - version_ = version;
|
| - // Do a static cast in case the interface contains methods with the same
|
| - // name.
|
| - static_cast<ControlMessageProxy*>(proxy_)->RequireVersion(version);
|
| - }
|
| -
|
| void Swap(InterfacePtrState* other) {
|
| using std::swap;
|
| swap(other->proxy_, proxy_);
|
| @@ -96,18 +135,9 @@ class InterfacePtrState<Interface, false> {
|
| void Bind(InterfacePtrInfo<Interface> info,
|
| scoped_refptr<base::SingleThreadTaskRunner> runner) {
|
| DCHECK(!proxy_);
|
| - DCHECK(!router_);
|
| - DCHECK(!handle_.is_valid());
|
| - DCHECK_EQ(0u, version_);
|
| - DCHECK(info.is_valid());
|
| -
|
| - handle_ = info.PassHandle();
|
| - version_ = info.version();
|
| - runner_ = std::move(runner);
|
| + BindInternal(&info, runner);
|
| }
|
|
|
| - bool HasAssociatedInterfaces() const { return false; }
|
| -
|
| // After this method is called, the object is in an invalid state and
|
| // shouldn't be reused.
|
| InterfacePtrInfo<Interface> PassInterface() {
|
| @@ -115,70 +145,29 @@ class InterfacePtrState<Interface, false> {
|
| router_ ? router_->PassMessagePipe() : std::move(handle_), version_);
|
| }
|
|
|
| - bool is_bound() const { return handle_.is_valid() || router_; }
|
| -
|
| - bool encountered_error() const {
|
| - return router_ ? router_->encountered_error() : false;
|
| - }
|
| -
|
| - void set_connection_error_handler(const base::Closure& error_handler) {
|
| - ConfigureProxyIfNecessary();
|
| -
|
| - DCHECK(router_);
|
| - router_->set_connection_error_handler(error_handler);
|
| - }
|
| -
|
| - // Returns true if bound and awaiting a response to a message.
|
| - bool has_pending_callbacks() const {
|
| - return router_ && router_->has_pending_responders();
|
| - }
|
| -
|
| - AssociatedGroup* associated_group() { return nullptr; }
|
| -
|
| - void EnableTestingMode() {
|
| - ConfigureProxyIfNecessary();
|
| - router_->EnableTestingMode();
|
| - }
|
| -
|
| private:
|
| using Proxy = typename Interface::Proxy_;
|
|
|
| - void ConfigureProxyIfNecessary() {
|
| + void ConfigureProxyIfNecessary() override {
|
| // The proxy has been configured.
|
| if (proxy_) {
|
| DCHECK(router_);
|
| return;
|
| }
|
| - // The object hasn't been bound.
|
| - if (!handle_.is_valid())
|
| + if (!SimpleInterfacePtrState::ConfigureProxyIfNecessaryInternal(
|
| + Interface::Name_)) {
|
| return;
|
| -
|
| - FilterChain filters;
|
| - filters.Append<MessageHeaderValidator>(Interface::Name_);
|
| - filters.Append<typename Interface::ResponseValidator_>();
|
| -
|
| - router_ = new Router(std::move(handle_), std::move(filters), false,
|
| - std::move(runner_));
|
| -
|
| + }
|
| proxy_ = new Proxy(router_);
|
| }
|
|
|
| - void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
|
| - uint32_t version) {
|
| - version_ = version;
|
| - callback.Run(version);
|
| + MessageFilter* CreateRequestValidator() override {
|
| + return new typename Interface::ResponseValidator_();
|
| }
|
|
|
| - Proxy* proxy_;
|
| - Router* router_;
|
| + ControlMessageProxy* proxy() override { return proxy_; }
|
|
|
| - // |proxy_| and |router_| are not initialized until read/write with the
|
| - // message pipe handle is needed. |handle_| is valid between the Bind() call
|
| - // and the initialization of |proxy_| and |router_|.
|
| - ScopedMessagePipeHandle handle_;
|
| - scoped_refptr<base::SingleThreadTaskRunner> runner_;
|
| -
|
| - uint32_t version_;
|
| + Proxy* proxy_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
|
| };
|
|
|