diff options
21 files changed, 209 insertions, 245 deletions
diff --git a/mojo/dbus/dbus_external_service.h b/mojo/dbus/dbus_external_service.h index ea72222..c3e20e1 100644 --- a/mojo/dbus/dbus_external_service.h +++ b/mojo/dbus/dbus_external_service.h @@ -74,7 +74,7 @@ class DBusExternalService : public DBusExternalServiceBase { } virtual void Activate(ScopedMessagePipeHandle shell_handle) OVERRIDE { app_.reset(new Application(shell_handle.Pass())); - app_->AddService<ServiceImpl>(); + app_->AddServiceConnector(new ServiceConnector<ServiceImpl>()); } private: DBusExternalService* service_; diff --git a/mojo/examples/launcher/launcher.cc b/mojo/examples/launcher/launcher.cc index f71294b..e39b3b3 100644 --- a/mojo/examples/launcher/launcher.cc +++ b/mojo/examples/launcher/launcher.cc @@ -188,18 +188,20 @@ class LauncherController : public views::TextfieldController { DISALLOW_COPY_AND_ASSIGN(LauncherController); }; -class LauncherImpl : public InterfaceImpl<Launcher>, +class LauncherImpl : public ServiceConnection<Launcher, LauncherImpl>, public URLReceiver { public: - explicit LauncherImpl(Application* app) - : app_(app), - launcher_controller_(this), + LauncherImpl() + : launcher_controller_(this), pending_show_(false) { + } + + void Initialize() { screen_.reset(ScreenMojo::Create()); gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); NativeViewportPtr viewport; - app_->ConnectTo("mojo:mojo_native_viewport_service", &viewport); + ConnectTo(shell(), "mojo:mojo_native_viewport_service", &viewport); window_tree_host_.reset(new WindowTreeHostMojo( viewport.Pass(), gfx::Rect(50, 50, 450, 60), @@ -248,7 +250,6 @@ class LauncherImpl : public InterfaceImpl<Launcher>, } } - Application* app_; scoped_ptr<ScreenMojo> screen_; scoped_ptr<LauncherWindowTreeClient> window_tree_client_; scoped_ptr<aura::client::FocusClient> focus_client_; @@ -286,7 +287,8 @@ extern "C" LAUNCHER_EXPORT MojoResult CDECL MojoMain( aura::Env::CreateInstance(true); mojo::Application app(shell_handle); - app.AddService<mojo::examples::LauncherImpl>(&app); + app.AddServiceConnector( + new mojo::ServiceConnector<mojo::examples::LauncherImpl>()); loop.Run(); return MOJO_RESULT_OK; diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi index e103472..11fbcac 100644 --- a/mojo/mojo_public.gypi +++ b/mojo/mojo_public.gypi @@ -368,10 +368,9 @@ 'type': 'static_library', 'sources': [ 'public/cpp/shell/application.h', - 'public/cpp/shell/connect.h', + 'public/cpp/shell/service.h', 'public/cpp/shell/lib/application.cc', - 'public/cpp/shell/lib/service_connector.cc', - 'public/cpp/shell/lib/service_connector.h', + 'public/cpp/shell/lib/service.cc', ], 'dependencies': [ 'mojo_shell_bindings', diff --git a/mojo/public/cpp/bindings/error_handler.h b/mojo/public/cpp/bindings/error_handler.h index 8ce1af2..a6f0a4a 100644 --- a/mojo/public/cpp/bindings/error_handler.h +++ b/mojo/public/cpp/bindings/error_handler.h @@ -14,6 +14,15 @@ class ErrorHandler { virtual void OnConnectionError() = 0; }; +// Used when you'd like to extend a base class with the same method signature +// as ErrorHandler. +template <typename Base> +class WithErrorHandler : public Base { + public: + virtual ~WithErrorHandler() {} + virtual void OnConnectionError() = 0; +}; + } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_ERROR_HANDLER_H_ diff --git a/mojo/public/cpp/bindings/interface_impl.h b/mojo/public/cpp/bindings/interface_impl.h index 15dc980..a9d0947 100644 --- a/mojo/public/cpp/bindings/interface_impl.h +++ b/mojo/public/cpp/bindings/interface_impl.h @@ -13,17 +13,18 @@ namespace mojo { // InterfaceImpl<..> is designed to be the base class of an interface // implementation. It may be bound to a pipe or a proxy, see BindToPipe and // BindToProxy. +// +// NOTE: A base class of WithErrorHandler<Interface> is used to avoid multiple +// inheritance. This base class inserts the signature of ErrorHandler into the +// inheritance chain. template <typename Interface> -class InterfaceImpl : public internal::InterfaceImplBase<Interface> { +class InterfaceImpl : public WithErrorHandler<Interface> { public: typedef typename Interface::Client Client; InterfaceImpl() : internal_state_(this) {} virtual ~InterfaceImpl() {} - // Subclasses can override this to handle post connection initialization. - virtual void OnConnectionEstablished() {} - // Subclasses must handle connection errors. virtual void OnConnectionError() = 0; diff --git a/mojo/public/cpp/bindings/lib/interface_impl_internal.h b/mojo/public/cpp/bindings/lib/interface_impl_internal.h index e661290..26f2443 100644 --- a/mojo/public/cpp/bindings/lib/interface_impl_internal.h +++ b/mojo/public/cpp/bindings/lib/interface_impl_internal.h @@ -15,19 +15,11 @@ namespace mojo { namespace internal { template <typename Interface> -class InterfaceImplBase : public Interface { - public: - virtual ~InterfaceImplBase() {} - virtual void OnConnectionEstablished() = 0; - virtual void OnConnectionError() = 0; -}; - -template <typename Interface> class InterfaceImplState : public ErrorHandler { public: typedef typename Interface::Client Client; - explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) + explicit InterfaceImplState(WithErrorHandler<Interface>* instance) : router_(NULL), client_(NULL), proxy_(NULL) { @@ -66,8 +58,7 @@ class InterfaceImplState : public ErrorHandler { proxy_ = new typename Client::Proxy_(router_); - instance()->SetClient(proxy_); - instance()->OnConnectionEstablished(); + stub_.sink()->SetClient(proxy_); } Router* router() { return router_; } @@ -76,12 +67,9 @@ class InterfaceImplState : public ErrorHandler { Client* client() { return client_; } private: - InterfaceImplBase<Interface>* instance() { - return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); - } - virtual void OnConnectionError() MOJO_OVERRIDE { - instance()->OnConnectionError(); + static_cast<WithErrorHandler<Interface>*>(stub_.sink())-> + OnConnectionError(); } Router* router_; diff --git a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc index 541aba1..4410e6f 100644 --- a/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc +++ b/mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc @@ -32,13 +32,7 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { public: virtual ~MathCalculatorImpl() {} - MathCalculatorImpl() - : total_(0.0), - got_connection_(false) { - } - - virtual void OnConnectionEstablished() MOJO_OVERRIDE { - got_connection_ = true; + MathCalculatorImpl() : total_(0.0) { } virtual void OnConnectionError() MOJO_OVERRIDE { @@ -59,13 +53,8 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { client()->Output(total_); } - bool got_connection() const { - return got_connection_; - } - -private: + private: double total_; - bool got_connection_; }; class MathCalculatorUIImpl : public math::CalculatorUI { @@ -127,8 +116,7 @@ class InterfacePtrTest : public testing::Test { TEST_F(InterfacePtrTest, EndToEnd) { math::CalculatorPtr calc; - MathCalculatorImpl* impl = BindToProxy(new MathCalculatorImpl(), &calc); - EXPECT_TRUE(impl->got_connection()); + BindToProxy(new MathCalculatorImpl(), &calc); // Suppose this is instantiated in a process that has pipe1_. MathCalculatorUIImpl calculator_ui(calc.Pass()); diff --git a/mojo/public/cpp/shell/application.h b/mojo/public/cpp/shell/application.h index 3e016d1..933077b 100644 --- a/mojo/public/cpp/shell/application.h +++ b/mojo/public/cpp/shell/application.h @@ -7,61 +7,24 @@ #include <vector> -#include "mojo/public/cpp/shell/connect.h" -#include "mojo/public/cpp/shell/lib/service_connector.h" +#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/system/core.h" #include "mojo/public/interfaces/shell/shell.mojom.h" namespace mojo { -// Utility class for creating ShellClients that vend service instances. -// To use define a class that implements your specific server api, e.g. FooImpl -// to implement a service named Foo. -// That class must subclass an InterfaceImpl specialization. -// -// If there is context that is to be shared amongst all instances, define a -// constructor with that class as its only argument, otherwise define an empty -// constructor. -// -// class FooImpl : public InterfaceImpl<Foo> { -// public: -// FooImpl() {} -// }; -// -// or -// -// class BarImpl : public InterfaceImpl<Bar> { -// public: -// // context will remain valid for the lifetime of BarImpl. -// BarImpl(BarContext* context) : context_(context) {} -// private: -// BarContext* context; -// }; -// -// Create an Application instance that collects any service implementations. -// -// Application app(shell_handle); -// app.AddService<FooImpl>(); -// -// BarContext context; -// app.AddService<BarImpl>(&context); -// -// class Application : public internal::ServiceConnectorBase::Owner { public: explicit Application(ScopedMessagePipeHandle shell_handle); explicit Application(MojoHandle shell_handle); virtual ~Application(); - template <typename Impl, typename Context> - void AddService(Context* context) { - AddServiceConnector(new internal::ServiceConnector<Impl, Context>(context)); - } - - template <typename Impl> - void AddService() { - AddServiceConnector(new internal::ServiceConnector<Impl, void>(NULL)); - } + // internal::ServiceConnectorBase::Owner methods. + // Takes ownership of |service_connector|. + virtual void AddServiceConnector( + internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; + virtual void RemoveServiceConnector( + internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; template <typename Interface> void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) { @@ -70,20 +33,11 @@ class Application : public internal::ServiceConnectorBase::Owner { protected: // ShellClient methods. - // Override this to dispatch to correct service when there's more than one. - // TODO(davemoore): Augment this with name registration. virtual void AcceptConnection(const mojo::String& url, ScopedMessagePipeHandle client_handle) MOJO_OVERRIDE; private: - // internal::ServiceConnectorBase::Owner methods. - // Takes ownership of |service_connector|. - virtual void AddServiceConnector( - internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; - virtual void RemoveServiceConnector( - internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; - typedef std::vector<internal::ServiceConnectorBase*> ServiceConnectorList; ServiceConnectorList service_connectors_; }; diff --git a/mojo/public/cpp/shell/connect.h b/mojo/public/cpp/shell/connect.h deleted file mode 100644 index caee589..0000000 --- a/mojo/public/cpp/shell/connect.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2014 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 MOJO_PUBLIC_CPP_SHELL_CONNECT_H_ -#define MOJO_PUBLIC_CPP_SHELL_CONNECT_H_ - -#include "mojo/public/cpp/bindings/allocation_scope.h" -#include "mojo/public/interfaces/shell/shell.mojom.h" - -namespace mojo { - -template <typename Interface> -inline void ConnectTo(Shell* shell, const std::string& url, - InterfacePtr<Interface>* ptr) { - MessagePipe pipe; - ptr->Bind(pipe.handle0.Pass()); - - AllocationScope scope; - shell->Connect(url, pipe.handle1.Pass()); -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SHELL_CONNECT_H_ diff --git a/mojo/public/cpp/shell/lib/service_connector.cc b/mojo/public/cpp/shell/lib/service.cc index 18f34b6..2f2e542 100644 --- a/mojo/public/cpp/shell/lib/service_connector.cc +++ b/mojo/public/cpp/shell/lib/service.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/cpp/shell/lib/service_connector.h" +#include "mojo/public/cpp/shell/service.h" namespace mojo { namespace internal { diff --git a/mojo/public/cpp/shell/lib/service_connector.h b/mojo/public/cpp/shell/service.h index 421cb72..c546289 100644 --- a/mojo/public/cpp/shell/lib/service_connector.h +++ b/mojo/public/cpp/shell/service.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_ -#define MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_ +#ifndef MOJO_PUBLIC_SHELL_SERVICE_H_ +#define MOJO_PUBLIC_SHELL_SERVICE_H_ #include <assert.h> @@ -12,53 +12,54 @@ #include "mojo/public/cpp/bindings/allocation_scope.h" #include "mojo/public/interfaces/shell/shell.mojom.h" +// Utility classes for creating ShellClients that vend service instances. +// To use define a class that implements your specific server api, e.g. FooImpl +// to implement a service named Foo. That class must define an empty constructor +// and the Initialize() method. +// class FooImpl : public Foo { +// public: +// FooImpl(); +// void Initialize(); +// private: +// ServiceConnector<FooImpl>* service_connector_; +// }; +// +// +// To simplify further FooImpl can use the ServiceConnection<> template. +// class FooImpl : public ServiceConnection<Foo, FooImpl> { +// public: +// FooImpl(); +// ... +// <Foo implementation> +// }; +// +// Instances of FooImpl will be created by a specialized ServiceConnector +// +// ServiceConnector<FooImpl> +// +// Optionally the classes can be specializeed with a shared context +// class ServiceConnector<FooImpl, MyContext> +// and +// class FooImpl : public ServiceConnection<Foo, FooImpl, MyContext> +// +// foo_connector = new ServiceConnector<FooImpl, MyContext>(my_context); +// instances of FooImpl can call context() and retrieve the value of my_context. +// +// Lastly create an Application instance that collects all the +// ServiceConnectors. +// +// Application app(shell_handle); +// app.AddServiceConnector(new ServiceConnector<FooImpl>); +// +// +// Specialization of ServiceConnector. +// ServiceImpl: Implementation of Service interface. +// Context: Optional type of shared context.v +// +// namespace mojo { -namespace internal { - -template <class ServiceImpl, typename Context> -class ServiceConnector; - -// Specialization of ServiceConnection. -// ServiceImpl: Subclass of InterfaceImpl<...>. -// Context: Type of shared context. -template <class ServiceImpl, typename Context> -class ServiceConnection : public ServiceImpl { - public: - ServiceConnection() : ServiceImpl() {} - ServiceConnection(Context* context) : ServiceImpl(context) {} - - virtual void OnConnectionError() MOJO_OVERRIDE { - service_connector_->RemoveConnection(static_cast<ServiceImpl*>(this)); - ServiceImpl::OnConnectionError(); - } - -private: - friend class ServiceConnector<ServiceImpl, Context>; - - // Called shortly after this class is instantiated. - void set_service_connector( - ServiceConnector<ServiceImpl, Context>* connector) { - service_connector_ = connector; - } - - ServiceConnector<ServiceImpl, Context>* service_connector_; -}; - -template <typename ServiceImpl, typename Context> -struct ServiceConstructor { - static ServiceConnection<ServiceImpl, Context>* New(Context* context) { - return new ServiceConnection<ServiceImpl, Context>(context); - } -}; - -template <typename ServiceImpl> -struct ServiceConstructor<ServiceImpl, void> { - public: - static ServiceConnection<ServiceImpl, void>* New(void* context) { - return new ServiceConnection<ServiceImpl, void>(); - } -}; +namespace internal { class ServiceConnectorBase { public: class Owner : public ShellClient { @@ -87,6 +88,7 @@ class ServiceConnectorBase { protected: Owner* owner_; }; +} // namespace internal template <class ServiceImpl, typename Context=void> class ServiceConnector : public internal::ServiceConnectorBase { @@ -105,12 +107,12 @@ class ServiceConnector : public internal::ServiceConnectorBase { virtual void AcceptConnection(const std::string& url, ScopedMessagePipeHandle handle) MOJO_OVERRIDE { - ServiceConnection<ServiceImpl, Context>* impl = - ServiceConstructor<ServiceImpl, Context>::New(context_); - impl->set_service_connector(this); - BindToPipe(impl, handle.Pass()); + ServiceImpl* impl = BindToPipe(new ServiceImpl(), handle.Pass()); + impl->set_connector(this); connections_.push_back(impl); + + impl->Initialize(); } void RemoveConnection(ServiceImpl* impl) { @@ -135,7 +137,58 @@ class ServiceConnector : public internal::ServiceConnectorBase { Context* context_; }; -} // namespace internal +// Specialization of ServiceConnection. +// ServiceInterface: Service interface. +// ServiceImpl: Subclass of ServiceConnection<...>. +// Context: Optional type of shared context. +template <class ServiceInterface, class ServiceImpl, typename Context=void> +class ServiceConnection : public InterfaceImpl<ServiceInterface> { + protected: + // NOTE: shell() and context() are not available at construction time. + // Initialize() will be called once those are available. + ServiceConnection() : service_connector_(NULL) {} + + virtual ~ServiceConnection() {} + + virtual void OnConnectionError() MOJO_OVERRIDE { + service_connector_->RemoveConnection(static_cast<ServiceImpl*>(this)); + } + + // Shadow this method in ServiceImpl to perform one-time initialization. + // At the time this is called, shell() and context() will be available. + // NOTE: No need to call the base class Initialize from your subclass. It + // will always be a no-op. + void Initialize() {} + + Shell* shell() { + return service_connector_->shell(); + } + + Context* context() const { + return service_connector_->context(); + } + + private: + friend class ServiceConnector<ServiceImpl, Context>; + + // Called shortly after this class is instantiated. + void set_connector(ServiceConnector<ServiceImpl, Context>* connector) { + service_connector_ = connector; + } + + ServiceConnector<ServiceImpl, Context>* service_connector_; +}; + +template <typename Interface> +inline void ConnectTo(Shell* shell, const std::string& url, + InterfacePtr<Interface>* ptr) { + MessagePipe pipe; + ptr->Bind(pipe.handle0.Pass()); + + AllocationScope scope; + shell->Connect(url, pipe.handle1.Pass()); +} + } // namespace mojo -#endif // MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_ +#endif // MOJO_PUBLIC_SHELL_SERVICE_H_ diff --git a/mojo/service_manager/service_manager_unittest.cc b/mojo/service_manager/service_manager_unittest.cc index 3d21dc8..0e6b069 100644 --- a/mojo/service_manager/service_manager_unittest.cc +++ b/mojo/service_manager/service_manager_unittest.cc @@ -24,28 +24,24 @@ struct TestContext { int num_loader_deletes; }; -class TestServiceImpl : public InterfaceImpl<TestService> { +class TestServiceImpl : + public ServiceConnection<TestService, TestServiceImpl, TestContext> { public: - explicit TestServiceImpl(TestContext* context) : context_(context) { - ++context_->num_impls; - } - virtual ~TestServiceImpl() { - --context_->num_impls; + if (context()) + --context()->num_impls; } - // InterfaceImpl<TestService> implementation. - virtual void OnConnectionError() OVERRIDE { + void Initialize() { + if (context()) + ++context()->num_impls; } // TestService implementation: virtual void Test(const mojo::String& test_string) OVERRIDE { - context_->last_test_string = test_string.To<std::string>(); + context()->last_test_string = test_string.To<std::string>(); client()->AckTest(); } - - private: - TestContext* context_; }; class TestClientImpl : public TestClient { @@ -102,7 +98,8 @@ class TestServiceLoader : public ServiceLoader { ScopedMessagePipeHandle shell_handle) OVERRIDE { ++num_loads_; test_app_.reset(new Application(shell_handle.Pass())); - test_app_->AddService<TestServiceImpl>(context_); + test_app_->AddServiceConnector( + new ServiceConnector<TestServiceImpl, TestContext>(context_)); } virtual void OnServiceError(ServiceManager* manager, diff --git a/mojo/services/dbus_echo/dbus_echo_service.cc b/mojo/services/dbus_echo/dbus_echo_service.cc index 0678339..f1f5ced 100644 --- a/mojo/services/dbus_echo/dbus_echo_service.cc +++ b/mojo/services/dbus_echo/dbus_echo_service.cc @@ -16,13 +16,12 @@ #include "mojo/services/dbus_echo/echo.mojom.h" namespace { -class EchoServiceImpl : public mojo::InterfaceImpl<mojo::EchoService> { +class EchoServiceImpl + : public mojo::ServiceConnection<mojo::EchoService, EchoServiceImpl> { public: EchoServiceImpl() {} virtual ~EchoServiceImpl() {} - virtual void OnConnectionError() OVERRIDE {} - protected: virtual void Echo( const mojo::String& in_to_echo, diff --git a/mojo/services/native_viewport/native_viewport_service.cc b/mojo/services/native_viewport/native_viewport_service.cc index 1b82cf9..0118208 100644 --- a/mojo/services/native_viewport/native_viewport_service.cc +++ b/mojo/services/native_viewport/native_viewport_service.cc @@ -28,12 +28,13 @@ bool IsRateLimitedEventType(ui::Event* event) { } class NativeViewportImpl - : public InterfaceImpl<mojo::NativeViewport>, + : public ServiceConnection<mojo::NativeViewport, + NativeViewportImpl, + shell::Context>, public NativeViewportDelegate { public: - NativeViewportImpl(shell::Context* context) - : context_(context), - widget_(gfx::kNullAcceleratedWidget), + NativeViewportImpl() + : widget_(gfx::kNullAcceleratedWidget), waiting_for_event_ack_(false) {} virtual ~NativeViewportImpl() { // Destroy the NativeViewport early on as it may call us back during @@ -41,11 +42,9 @@ class NativeViewportImpl native_viewport_.reset(); } - virtual void OnConnectionError() OVERRIDE {} - virtual void Create(const Rect& bounds) OVERRIDE { native_viewport_ = - services::NativeViewport::Create(context_, this); + services::NativeViewport::Create(context(), this); native_viewport_->Init(bounds); client()->OnCreated(); OnBoundsChanged(bounds); @@ -175,7 +174,6 @@ class NativeViewportImpl } private: - shell::Context* context_; gfx::AcceleratedWidget widget_; scoped_ptr<services::NativeViewport> native_viewport_; ScopedMessagePipeHandle command_buffer_handle_; @@ -191,7 +189,9 @@ MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application* CreateNativeViewportService(mojo::shell::Context* context, mojo::ScopedMessagePipeHandle shell_handle) { mojo::Application* app = new mojo::Application(shell_handle.Pass()); - app->AddService<mojo::services::NativeViewportImpl>(context); + app->AddServiceConnector( + new mojo::ServiceConnector<mojo::services::NativeViewportImpl, + mojo::shell::Context>(context)); return app; } diff --git a/mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.cc b/mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.cc index 434fa28..cb9a8ef 100644 --- a/mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.cc +++ b/mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.cc @@ -6,7 +6,7 @@ #include "base/bind.h" #include "base/run_loop.h" -#include "mojo/public/cpp/shell/connect.h" +#include "mojo/public/cpp/shell/service.h" #include "mojo/public/interfaces/shell/shell.mojom.h" #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" #include "mojo/services/public/cpp/view_manager/lib/view_private.h" diff --git a/mojo/services/view_manager/main.cc b/mojo/services/view_manager/main.cc index 60a850b..2835d7c 100644 --- a/mojo/services/view_manager/main.cc +++ b/mojo/services/view_manager/main.cc @@ -26,8 +26,10 @@ extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain( base::MessageLoop loop; mojo::Application app(shell_handle); mojo::view_manager::service::RootNodeManager root_node_manager(app.shell()); - app.AddService<mojo::view_manager::service::ViewManagerConnection>( - &root_node_manager); + app.AddServiceConnector( + new mojo::ServiceConnector< + mojo::view_manager::service::ViewManagerConnection, + mojo::view_manager::service::RootNodeManager>(&root_node_manager)); loop.Run(); return MOJO_RESULT_OK; diff --git a/mojo/services/view_manager/root_view_manager.cc b/mojo/services/view_manager/root_view_manager.cc index 6302bed..d0f62a6 100644 --- a/mojo/services/view_manager/root_view_manager.cc +++ b/mojo/services/view_manager/root_view_manager.cc @@ -7,7 +7,7 @@ #include "base/auto_reset.h" #include "mojo/aura/screen_mojo.h" #include "mojo/aura/window_tree_host_mojo.h" -#include "mojo/public/cpp/shell/connect.h" +#include "mojo/public/cpp/shell/service.h" #include "mojo/public/interfaces/shell/shell.mojom.h" #include "mojo/services/view_manager/root_node_manager.h" #include "ui/aura/client/default_capture_client.h" diff --git a/mojo/services/view_manager/view_manager_connection.cc b/mojo/services/view_manager/view_manager_connection.cc index 342bf9e..749f25c 100644 --- a/mojo/services/view_manager/view_manager_connection.cc +++ b/mojo/services/view_manager/view_manager_connection.cc @@ -59,9 +59,7 @@ void NodeToINode(Node* node, } // namespace -ViewManagerConnection::ViewManagerConnection(RootNodeManager* root_node_manager) - : root_node_manager_(root_node_manager), - id_(0) { +ViewManagerConnection::ViewManagerConnection() : id_(0) { } ViewManagerConnection::~ViewManagerConnection() { @@ -80,25 +78,22 @@ ViewManagerConnection::~ViewManagerConnection() { } STLDeleteContainerPairSecondPointers(node_map_.begin(), node_map_.end()); - root_node_manager_->RemoveConnection(this); + context()->RemoveConnection(this); } -void ViewManagerConnection::OnConnectionEstablished() { - DCHECK_EQ(0, id_); // Should only get SetClient() once. - id_ = root_node_manager_->GetAndAdvanceNextConnectionId(); - root_node_manager_->AddConnection(this); - client()->OnConnectionEstablished( - id_, root_node_manager_->next_server_change_id()); +void ViewManagerConnection::Initialize() { + DCHECK_EQ(0, id_); // Should only get Initialize() once. + id_ = context()->GetAndAdvanceNextConnectionId(); + context()->AddConnection(this); + client()->OnConnectionEstablished(id_, context()->next_server_change_id()); } -void ViewManagerConnection::OnConnectionError() {} - Node* ViewManagerConnection::GetNode(const NodeId& id) { if (id_ == id.connection_id) { NodeMap::iterator i = node_map_.find(id.node_id); return i == node_map_.end() ? NULL : i->second; } - return root_node_manager_->GetNode(id); + return context()->GetNode(id); } View* ViewManagerConnection::GetView(const ViewId& id) { @@ -106,7 +101,7 @@ View* ViewManagerConnection::GetView(const ViewId& id) { ViewMap::const_iterator i = view_map_.find(id.view_id); return i == view_map_.end() ? NULL : i->second; } - return root_node_manager_->GetView(id); + return context()->GetView(id); } void ViewManagerConnection::NotifyNodeHierarchyChanged( @@ -146,8 +141,7 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source, if (!node) return false; RootNodeManager::ScopedChange change( - source, root_node_manager_, - RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); + source, context(), RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); if (node->GetParent()) node->GetParent()->Remove(node); std::vector<Node*> children(node->GetChildren()); @@ -157,7 +151,7 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source, node_map_.erase(node_id.node_id); delete node; node = NULL; - root_node_manager_->NotifyNodeDeleted(node_id); + context()->NotifyNodeDeleted(node_id); return true; } @@ -168,7 +162,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source, if (!view) return false; RootNodeManager::ScopedChange change( - source, root_node_manager_, + source, context(), RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID); if (view->node()) view->node()->SetView(NULL); @@ -177,7 +171,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source, // valid. const ViewId view_id_copy(view_id); delete view; - root_node_manager_->NotifyViewDeleted(view_id_copy); + context()->NotifyViewDeleted(view_id_copy); return true; } @@ -190,7 +184,7 @@ bool ViewManagerConnection::SetViewImpl(const NodeId& node_id, if (!view && view_id != ViewId()) return false; RootNodeManager::ScopedChange change( - this, root_node_manager_, + this, context(), RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID); node->SetView(view); return true; @@ -213,7 +207,7 @@ void ViewManagerConnection::DeleteNode( TransportNodeId transport_node_id, const Callback<void(bool)>& callback) { const NodeId node_id(NodeIdFromTransportId(transport_node_id)); - ViewManagerConnection* connection = root_node_manager_->GetConnection( + ViewManagerConnection* connection = context()->GetConnection( node_id.connection_id); callback.Run(connection && connection->DeleteNodeImpl(this, node_id)); @@ -225,14 +219,14 @@ void ViewManagerConnection::AddNode( TransportChangeId server_change_id, const Callback<void(bool)>& callback) { bool success = false; - if (server_change_id == root_node_manager_->next_server_change_id()) { + if (server_change_id == context()->next_server_change_id()) { Node* parent = GetNode(NodeIdFromTransportId(parent_id)); Node* child = GetNode(NodeIdFromTransportId(child_id)); if (parent && child && child->GetParent() != parent && !child->window()->Contains(parent->window())) { success = true; RootNodeManager::ScopedChange change( - this, root_node_manager_, + this, context(), RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); parent->Add(child); } @@ -245,12 +239,12 @@ void ViewManagerConnection::RemoveNodeFromParent( TransportChangeId server_change_id, const Callback<void(bool)>& callback) { bool success = false; - if (server_change_id == root_node_manager_->next_server_change_id()) { + if (server_change_id == context()->next_server_change_id()) { Node* node = GetNode(NodeIdFromTransportId(node_id)); if (node && node->GetParent()) { success = true; RootNodeManager::ScopedChange change( - this, root_node_manager_, + this, context(), RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); node->GetParent()->Remove(node); } @@ -287,7 +281,7 @@ void ViewManagerConnection::DeleteView( TransportViewId transport_view_id, const Callback<void(bool)>& callback) { const ViewId view_id(ViewIdFromTransportId(transport_view_id)); - ViewManagerConnection* connection = root_node_manager_->GetConnection( + ViewManagerConnection* connection = context()->GetConnection( view_id.connection_id); callback.Run(connection && connection->DeleteViewImpl(this, view_id)); } @@ -322,13 +316,13 @@ void ViewManagerConnection::SetViewContents( void ViewManagerConnection::OnNodeHierarchyChanged(const NodeId& node, const NodeId& new_parent, const NodeId& old_parent) { - root_node_manager_->NotifyNodeHierarchyChanged(node, new_parent, old_parent); + context()->NotifyNodeHierarchyChanged(node, new_parent, old_parent); } void ViewManagerConnection::OnNodeViewReplaced(const NodeId& node, const ViewId& new_view_id, const ViewId& old_view_id) { - root_node_manager_->NotifyNodeViewReplaced(node, new_view_id, old_view_id); + context()->NotifyNodeViewReplaced(node, new_view_id, old_view_id); } } // namespace service diff --git a/mojo/services/view_manager/view_manager_connection.h b/mojo/services/view_manager/view_manager_connection.h index 853012b..87997d1 100644 --- a/mojo/services/view_manager/view_manager_connection.h +++ b/mojo/services/view_manager/view_manager_connection.h @@ -9,6 +9,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" +#include "mojo/public/cpp/shell/service.h" #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" #include "mojo/services/view_manager/ids.h" #include "mojo/services/view_manager/node_delegate.h" @@ -31,17 +32,18 @@ class View; // Manages a connection from the client. class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection - : public InterfaceImpl<IViewManager>, + : public ServiceConnection<IViewManager, ViewManagerConnection, + RootNodeManager>, public NodeDelegate { public: - ViewManagerConnection(RootNodeManager* root_node_manager); + ViewManagerConnection(); virtual ~ViewManagerConnection(); - virtual void OnConnectionEstablished() MOJO_OVERRIDE; - virtual void OnConnectionError() MOJO_OVERRIDE; - TransportConnectionId id() const { return id_; } + // Invoked when connection is established. + void Initialize(); + // Returns the Node with the specified id. Node* GetNode(const NodeId& id); @@ -109,10 +111,9 @@ class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection virtual void OnNodeViewReplaced(const NodeId& node, const ViewId& new_view_id, const ViewId& old_view_id) OVERRIDE; - RootNodeManager* root_node_manager_; // Id of this connection as assigned by RootNodeManager. Assigned in - // OnConnectionEstablished(). + // Initialize(). TransportConnectionId id_; NodeMap node_map_; diff --git a/mojo/services/view_manager/view_manager_connection_unittest.cc b/mojo/services/view_manager/view_manager_connection_unittest.cc index 57ab93e..c1f4d33 100644 --- a/mojo/services/view_manager/view_manager_connection_unittest.cc +++ b/mojo/services/view_manager/view_manager_connection_unittest.cc @@ -12,7 +12,7 @@ #include "base/strings/stringprintf.h" #include "mojo/public/cpp/bindings/allocation_scope.h" #include "mojo/public/cpp/environment/environment.h" -#include "mojo/public/cpp/shell/connect.h" +#include "mojo/public/cpp/shell/service.h" #include "mojo/services/public/cpp/view_manager/util.h" #include "mojo/services/public/cpp/view_manager/view_manager_types.h" #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" diff --git a/mojo/shell/view_manager_loader.cc b/mojo/shell/view_manager_loader.cc index c590dd7..cd24638 100644 --- a/mojo/shell/view_manager_loader.cc +++ b/mojo/shell/view_manager_loader.cc @@ -25,8 +25,10 @@ void ViewManagerLoader::LoadService(ServiceManager* manager, root_node_manager_.reset( new view_manager::service::RootNodeManager(app->shell())); } - app->AddService<view_manager::service::ViewManagerConnection>( - root_node_manager_.get()); + app->AddServiceConnector( + new ServiceConnector<view_manager::service::ViewManagerConnection, + view_manager::service::RootNodeManager>( + root_node_manager_.get())); apps_.push_back(app.release()); } |