Commit eaae49d7 authored by darin@chromium.org's avatar darin@chromium.org

Mojo: Internalize ServiceConnector<>

Authored by davemoore@chromium.org, see https://codereview.chromium.org/275363002/

R=darin@chromium.org
TBR=davemoore@chromium.org

Review URL: https://codereview.chromium.org/287143003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270945 0039d316-1c4b-4281-b951-d872f2087c98
parent 0622259a
...@@ -74,7 +74,7 @@ class DBusExternalService : public DBusExternalServiceBase { ...@@ -74,7 +74,7 @@ class DBusExternalService : public DBusExternalServiceBase {
} }
virtual void Activate(ScopedMessagePipeHandle shell_handle) OVERRIDE { virtual void Activate(ScopedMessagePipeHandle shell_handle) OVERRIDE {
app_.reset(new Application(shell_handle.Pass())); app_.reset(new Application(shell_handle.Pass()));
app_->AddServiceConnector(new ServiceConnector<ServiceImpl>()); app_->AddService<ServiceImpl>();
} }
private: private:
DBusExternalService* service_; DBusExternalService* service_;
......
...@@ -188,26 +188,27 @@ class LauncherController : public views::TextfieldController { ...@@ -188,26 +188,27 @@ class LauncherController : public views::TextfieldController {
DISALLOW_COPY_AND_ASSIGN(LauncherController); DISALLOW_COPY_AND_ASSIGN(LauncherController);
}; };
class LauncherImpl : public ServiceConnection<Launcher, LauncherImpl>, class LauncherImpl : public InterfaceImpl<Launcher>,
public URLReceiver { public URLReceiver {
public: public:
LauncherImpl() explicit LauncherImpl(Application* app)
: launcher_controller_(this), : app_(app),
launcher_controller_(this),
pending_show_(false) { pending_show_(false) {
}
void Initialize() {
screen_.reset(ScreenMojo::Create()); screen_.reset(ScreenMojo::Create());
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
NativeViewportPtr viewport; NativeViewportPtr viewport;
ConnectTo(shell(), "mojo:mojo_native_viewport_service", &viewport); app_->ConnectTo("mojo:mojo_native_viewport_service", &viewport);
window_tree_host_.reset(new WindowTreeHostMojo( window_tree_host_.reset(new WindowTreeHostMojo(
viewport.Pass(), gfx::Rect(50, 50, 450, 60), viewport.Pass(), gfx::Rect(50, 50, 450, 60),
base::Bind(&LauncherImpl::HostContextCreated, base::Unretained(this)))); base::Bind(&LauncherImpl::HostContextCreated, base::Unretained(this))));
} }
// Overridden from InterfaceImpl:
virtual void OnConnectionError() OVERRIDE {}
private: private:
// Overridden from Launcher: // Overridden from Launcher:
virtual void Show() OVERRIDE { virtual void Show() OVERRIDE {
...@@ -250,6 +251,7 @@ class LauncherImpl : public ServiceConnection<Launcher, LauncherImpl>, ...@@ -250,6 +251,7 @@ class LauncherImpl : public ServiceConnection<Launcher, LauncherImpl>,
} }
} }
Application* app_;
scoped_ptr<ScreenMojo> screen_; scoped_ptr<ScreenMojo> screen_;
scoped_ptr<LauncherWindowTreeClient> window_tree_client_; scoped_ptr<LauncherWindowTreeClient> window_tree_client_;
scoped_ptr<aura::client::FocusClient> focus_client_; scoped_ptr<aura::client::FocusClient> focus_client_;
...@@ -287,8 +289,7 @@ extern "C" LAUNCHER_EXPORT MojoResult CDECL MojoMain( ...@@ -287,8 +289,7 @@ extern "C" LAUNCHER_EXPORT MojoResult CDECL MojoMain(
aura::Env::CreateInstance(true); aura::Env::CreateInstance(true);
mojo::Application app(shell_handle); mojo::Application app(shell_handle);
app.AddServiceConnector( app.AddService<mojo::examples::LauncherImpl>(&app);
new mojo::ServiceConnector<mojo::examples::LauncherImpl>());
loop.Run(); loop.Run();
return MOJO_RESULT_OK; return MOJO_RESULT_OK;
......
...@@ -368,9 +368,10 @@ ...@@ -368,9 +368,10 @@
'type': 'static_library', 'type': 'static_library',
'sources': [ 'sources': [
'public/cpp/shell/application.h', 'public/cpp/shell/application.h',
'public/cpp/shell/service.h', 'public/cpp/shell/connect.h',
'public/cpp/shell/lib/application.cc', 'public/cpp/shell/lib/application.cc',
'public/cpp/shell/lib/service.cc', 'public/cpp/shell/lib/service_connector.cc',
'public/cpp/shell/lib/service_connector.h',
], ],
'dependencies': [ 'dependencies': [
'mojo_shell_bindings', 'mojo_shell_bindings',
......
...@@ -14,15 +14,6 @@ class ErrorHandler { ...@@ -14,15 +14,6 @@ class ErrorHandler {
virtual void OnConnectionError() = 0; 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 } // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_ERROR_HANDLER_H_ #endif // MOJO_PUBLIC_CPP_BINDINGS_ERROR_HANDLER_H_
...@@ -13,18 +13,17 @@ namespace mojo { ...@@ -13,18 +13,17 @@ namespace mojo {
// InterfaceImpl<..> is designed to be the base class of an interface // 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 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and
// BindToProxy. // 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> template <typename Interface>
class InterfaceImpl : public WithErrorHandler<Interface> { class InterfaceImpl : public internal::InterfaceImplBase<Interface> {
public: public:
typedef typename Interface::Client Client; typedef typename Interface::Client Client;
InterfaceImpl() : internal_state_(this) {} InterfaceImpl() : internal_state_(this) {}
virtual ~InterfaceImpl() {} virtual ~InterfaceImpl() {}
// Subclasses can override this to handle post connection initialization.
virtual void OnConnectionEstablished() {}
// Subclasses must handle connection errors. // Subclasses must handle connection errors.
virtual void OnConnectionError() = 0; virtual void OnConnectionError() = 0;
......
...@@ -14,12 +14,20 @@ ...@@ -14,12 +14,20 @@
namespace mojo { namespace mojo {
namespace internal { namespace internal {
template <typename Interface>
class InterfaceImplBase : public Interface {
public:
virtual ~InterfaceImplBase() {}
virtual void OnConnectionEstablished() = 0;
virtual void OnConnectionError() = 0;
};
template <typename Interface> template <typename Interface>
class InterfaceImplState : public ErrorHandler { class InterfaceImplState : public ErrorHandler {
public: public:
typedef typename Interface::Client Client; typedef typename Interface::Client Client;
explicit InterfaceImplState(WithErrorHandler<Interface>* instance) explicit InterfaceImplState(InterfaceImplBase<Interface>* instance)
: router_(NULL), : router_(NULL),
client_(NULL), client_(NULL),
proxy_(NULL) { proxy_(NULL) {
...@@ -58,7 +66,8 @@ class InterfaceImplState : public ErrorHandler { ...@@ -58,7 +66,8 @@ class InterfaceImplState : public ErrorHandler {
proxy_ = new typename Client::Proxy_(router_); proxy_ = new typename Client::Proxy_(router_);
stub_.sink()->SetClient(proxy_); instance()->SetClient(proxy_);
instance()->OnConnectionEstablished();
} }
Router* router() { return router_; } Router* router() { return router_; }
...@@ -67,9 +76,12 @@ class InterfaceImplState : public ErrorHandler { ...@@ -67,9 +76,12 @@ class InterfaceImplState : public ErrorHandler {
Client* client() { return client_; } Client* client() { return client_; }
private: private:
InterfaceImplBase<Interface>* instance() {
return static_cast<InterfaceImplBase<Interface>*>(stub_.sink());
}
virtual void OnConnectionError() MOJO_OVERRIDE { virtual void OnConnectionError() MOJO_OVERRIDE {
static_cast<WithErrorHandler<Interface>*>(stub_.sink())-> instance()->OnConnectionError();
OnConnectionError();
} }
Router* router_; Router* router_;
......
...@@ -32,7 +32,13 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { ...@@ -32,7 +32,13 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> {
public: public:
virtual ~MathCalculatorImpl() {} virtual ~MathCalculatorImpl() {}
MathCalculatorImpl() : total_(0.0) { MathCalculatorImpl()
: total_(0.0),
got_connection_(false) {
}
virtual void OnConnectionEstablished() MOJO_OVERRIDE {
got_connection_ = true;
} }
virtual void OnConnectionError() MOJO_OVERRIDE { virtual void OnConnectionError() MOJO_OVERRIDE {
...@@ -53,8 +59,13 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { ...@@ -53,8 +59,13 @@ class MathCalculatorImpl : public InterfaceImpl<math::Calculator> {
client()->Output(total_); client()->Output(total_);
} }
private: bool got_connection() const {
return got_connection_;
}
private:
double total_; double total_;
bool got_connection_;
}; };
class MathCalculatorUIImpl : public math::CalculatorUI { class MathCalculatorUIImpl : public math::CalculatorUI {
...@@ -116,7 +127,8 @@ class InterfacePtrTest : public testing::Test { ...@@ -116,7 +127,8 @@ class InterfacePtrTest : public testing::Test {
TEST_F(InterfacePtrTest, EndToEnd) { TEST_F(InterfacePtrTest, EndToEnd) {
math::CalculatorPtr calc; math::CalculatorPtr calc;
BindToProxy(new MathCalculatorImpl(), &calc); MathCalculatorImpl* impl = BindToProxy(new MathCalculatorImpl(), &calc);
EXPECT_TRUE(impl->got_connection());
// Suppose this is instantiated in a process that has pipe1_. // Suppose this is instantiated in a process that has pipe1_.
MathCalculatorUIImpl calculator_ui(calc.Pass()); MathCalculatorUIImpl calculator_ui(calc.Pass());
......
...@@ -7,24 +7,61 @@ ...@@ -7,24 +7,61 @@
#include <vector> #include <vector>
#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/shell/connect.h"
#include "mojo/public/cpp/shell/lib/service_connector.h"
#include "mojo/public/cpp/system/core.h" #include "mojo/public/cpp/system/core.h"
#include "mojo/public/interfaces/shell/shell.mojom.h" #include "mojo/public/interfaces/shell/shell.mojom.h"
namespace mojo { 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 { class Application : public internal::ServiceConnectorBase::Owner {
public: public:
explicit Application(ScopedMessagePipeHandle shell_handle); explicit Application(ScopedMessagePipeHandle shell_handle);
explicit Application(MojoHandle shell_handle); explicit Application(MojoHandle shell_handle);
virtual ~Application(); virtual ~Application();
// internal::ServiceConnectorBase::Owner methods. template <typename Impl, typename Context>
// Takes ownership of |service_connector|. void AddService(Context* context) {
virtual void AddServiceConnector( AddServiceConnector(new internal::ServiceConnector<Impl, Context>(context));
internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; }
virtual void RemoveServiceConnector(
internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; template <typename Impl>
void AddService() {
AddServiceConnector(new internal::ServiceConnector<Impl, void>(NULL));
}
template <typename Interface> template <typename Interface>
void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) { void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) {
...@@ -33,11 +70,20 @@ class Application : public internal::ServiceConnectorBase::Owner { ...@@ -33,11 +70,20 @@ class Application : public internal::ServiceConnectorBase::Owner {
protected: protected:
// ShellClient methods. // 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, virtual void AcceptConnection(const mojo::String& url,
ScopedMessagePipeHandle client_handle) ScopedMessagePipeHandle client_handle)
MOJO_OVERRIDE; MOJO_OVERRIDE;
private: 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; typedef std::vector<internal::ServiceConnectorBase*> ServiceConnectorList;
ServiceConnectorList service_connectors_; ServiceConnectorList service_connectors_;
}; };
......
// 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_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/shell/lib/service_connector.h"
namespace mojo { namespace mojo {
namespace internal { namespace internal {
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef MOJO_PUBLIC_SHELL_SERVICE_H_ #ifndef MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_
#define MOJO_PUBLIC_SHELL_SERVICE_H_ #define MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_
#include <assert.h> #include <assert.h>
...@@ -12,54 +12,53 @@ ...@@ -12,54 +12,53 @@
#include "mojo/public/cpp/bindings/allocation_scope.h" #include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/interfaces/shell/shell.mojom.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 mojo {
namespace internal { 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>();
}
};
class ServiceConnectorBase { class ServiceConnectorBase {
public: public:
class Owner : public ShellClient { class Owner : public ShellClient {
...@@ -88,7 +87,6 @@ class ServiceConnectorBase { ...@@ -88,7 +87,6 @@ class ServiceConnectorBase {
protected: protected:
Owner* owner_; Owner* owner_;
}; };
} // namespace internal
template <class ServiceImpl, typename Context=void> template <class ServiceImpl, typename Context=void>
class ServiceConnector : public internal::ServiceConnectorBase { class ServiceConnector : public internal::ServiceConnectorBase {
...@@ -107,12 +105,12 @@ class ServiceConnector : public internal::ServiceConnectorBase { ...@@ -107,12 +105,12 @@ class ServiceConnector : public internal::ServiceConnectorBase {
virtual void AcceptConnection(const std::string& url, virtual void AcceptConnection(const std::string& url,
ScopedMessagePipeHandle handle) MOJO_OVERRIDE { ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
ServiceImpl* impl = BindToPipe(new ServiceImpl(), handle.Pass()); ServiceConnection<ServiceImpl, Context>* impl =
impl->set_connector(this); ServiceConstructor<ServiceImpl, Context>::New(context_);
impl->set_service_connector(this);
BindToPipe(impl, handle.Pass());
connections_.push_back(impl); connections_.push_back(impl);
impl->Initialize();
} }
void RemoveConnection(ServiceImpl* impl) { void RemoveConnection(ServiceImpl* impl) {
...@@ -137,58 +135,7 @@ class ServiceConnector : public internal::ServiceConnectorBase { ...@@ -137,58 +135,7 @@ class ServiceConnector : public internal::ServiceConnectorBase {
Context* context_; Context* context_;
}; };
// Specialization of ServiceConnection. } // namespace internal
// 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 } // namespace mojo
#endif // MOJO_PUBLIC_SHELL_SERVICE_H_ #endif // MOJO_PUBLIC_CPP_SHELL_LIB_SERVICE_CONNECTOR_H_
...@@ -24,24 +24,28 @@ struct TestContext { ...@@ -24,24 +24,28 @@ struct TestContext {
int num_loader_deletes; int num_loader_deletes;
}; };
class TestServiceImpl : class TestServiceImpl : public InterfaceImpl<TestService> {
public ServiceConnection<TestService, TestServiceImpl, TestContext> {
public: public:
explicit TestServiceImpl(TestContext* context) : context_(context) {
++context_->num_impls;
}
virtual ~TestServiceImpl() { virtual ~TestServiceImpl() {
if (context()) --context_->num_impls;
--context()->num_impls;
} }
void Initialize() { // InterfaceImpl<TestService> implementation.
if (context()) virtual void OnConnectionError() OVERRIDE {
++context()->num_impls;
} }
// TestService implementation: // TestService implementation:
virtual void Test(const mojo::String& test_string) OVERRIDE { 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(); client()->AckTest();
} }
private:
TestContext* context_;
}; };
class TestClientImpl : public TestClient { class TestClientImpl : public TestClient {
...@@ -98,8 +102,7 @@ class TestServiceLoader : public ServiceLoader { ...@@ -98,8 +102,7 @@ class TestServiceLoader : public ServiceLoader {
ScopedMessagePipeHandle shell_handle) OVERRIDE { ScopedMessagePipeHandle shell_handle) OVERRIDE {
++num_loads_; ++num_loads_;
test_app_.reset(new Application(shell_handle.Pass())); test_app_.reset(new Application(shell_handle.Pass()));
test_app_->AddServiceConnector( test_app_->AddService<TestServiceImpl>(context_);
new ServiceConnector<TestServiceImpl, TestContext>(context_));
} }
virtual void OnServiceError(ServiceManager* manager, virtual void OnServiceError(ServiceManager* manager,
......
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
#include "mojo/services/dbus_echo/echo.mojom.h" #include "mojo/services/dbus_echo/echo.mojom.h"
namespace { namespace {
class EchoServiceImpl class EchoServiceImpl : public mojo::InterfaceImpl<mojo::EchoService> {
: public mojo::ServiceConnection<mojo::EchoService, EchoServiceImpl> {
public: public:
EchoServiceImpl() {} EchoServiceImpl() {}
virtual ~EchoServiceImpl() {} virtual ~EchoServiceImpl() {}
virtual void OnConnectionError() OVERRIDE {}
protected: protected:
virtual void Echo( virtual void Echo(
const mojo::String& in_to_echo, const mojo::String& in_to_echo,
......
...@@ -28,13 +28,12 @@ bool IsRateLimitedEventType(ui::Event* event) { ...@@ -28,13 +28,12 @@ bool IsRateLimitedEventType(ui::Event* event) {
} }
class NativeViewportImpl class NativeViewportImpl
: public ServiceConnection<mojo::NativeViewport, : public InterfaceImpl<mojo::NativeViewport>,
NativeViewportImpl,
shell::Context>,
public NativeViewportDelegate { public NativeViewportDelegate {
public: public:
NativeViewportImpl() NativeViewportImpl(shell::Context* context)
: widget_(gfx::kNullAcceleratedWidget), : context_(context),
widget_(gfx::kNullAcceleratedWidget),
waiting_for_event_ack_(false) {} waiting_for_event_ack_(false) {}
virtual ~NativeViewportImpl() { virtual ~NativeViewportImpl() {
// Destroy the NativeViewport early on as it may call us back during // Destroy the NativeViewport early on as it may call us back during
...@@ -42,9 +41,11 @@ class NativeViewportImpl ...@@ -42,9 +41,11 @@ class NativeViewportImpl
native_viewport_.reset(); native_viewport_.reset();
} }
virtual void OnConnectionError() OVERRIDE {}
virtual void Create(const Rect& bounds) OVERRIDE { virtual void Create(const Rect& bounds) OVERRIDE {
native_viewport_ = native_viewport_ =
services::NativeViewport::Create(context(), this); services::NativeViewport::Create(context_, this);
native_viewport_->Init(bounds); native_viewport_->Init(bounds);
client()->OnCreated(); client()->OnCreated();
OnBoundsChanged(bounds); OnBoundsChanged(bounds);
...@@ -174,6 +175,7 @@ class NativeViewportImpl ...@@ -174,6 +175,7 @@ class NativeViewportImpl
} }
private: private:
shell::Context* context_;
gfx::AcceleratedWidget widget_; gfx::AcceleratedWidget widget_;
scoped_ptr<services::NativeViewport> native_viewport_; scoped_ptr<services::NativeViewport> native_viewport_;
ScopedMessagePipeHandle command_buffer_handle_; ScopedMessagePipeHandle command_buffer_handle_;
...@@ -189,9 +191,7 @@ MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application* ...@@ -189,9 +191,7 @@ MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application*
CreateNativeViewportService(mojo::shell::Context* context, CreateNativeViewportService(mojo::shell::Context* context,
mojo::ScopedMessagePipeHandle shell_handle) { mojo::ScopedMessagePipeHandle shell_handle) {
mojo::Application* app = new mojo::Application(shell_handle.Pass()); mojo::Application* app = new mojo::Application(shell_handle.Pass());
app->AddServiceConnector( app->AddService<mojo::services::NativeViewportImpl>(context);
new mojo::ServiceConnector<mojo::services::NativeViewportImpl,
mojo::shell::Context>(context));
return app; return app;
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/shell/connect.h"
#include "mojo/public/interfaces/shell/shell.mojom.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_manager_private.h"
#include "mojo/services/public/cpp/view_manager/lib/view_private.h" #include "mojo/services/public/cpp/view_manager/lib/view_private.h"
......
...@@ -26,10 +26,8 @@ extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain( ...@@ -26,10 +26,8 @@ extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain(
base::MessageLoop loop; base::MessageLoop loop;
mojo::Application app(shell_handle); mojo::Application app(shell_handle);
mojo::view_manager::service::RootNodeManager root_node_manager(app.shell()); mojo::view_manager::service::RootNodeManager root_node_manager(app.shell());
app.AddServiceConnector( app.AddService<mojo::view_manager::service::ViewManagerConnection>(
new mojo::ServiceConnector< &root_node_manager);
mojo::view_manager::service::ViewManagerConnection,
mojo::view_manager::service::RootNodeManager>(&root_node_manager));
loop.Run(); loop.Run();
return MOJO_RESULT_OK; return MOJO_RESULT_OK;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "mojo/aura/screen_mojo.h" #include "mojo/aura/screen_mojo.h"
#include "mojo/aura/window_tree_host_mojo.h" #include "mojo/aura/window_tree_host_mojo.h"
#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/shell/connect.h"
#include "mojo/public/interfaces/shell/shell.mojom.h" #include "mojo/public/interfaces/shell/shell.mojom.h"
#include "mojo/services/view_manager/root_node_manager.h" #include "mojo/services/view_manager/root_node_manager.h"
#include "ui/aura/client/default_capture_client.h" #include "ui/aura/client/default_capture_client.h"
......
...@@ -59,7 +59,9 @@ void NodeToINode(Node* node, ...@@ -59,7 +59,9 @@ void NodeToINode(Node* node,
} // namespace } // namespace
ViewManagerConnection::ViewManagerConnection() : id_(0) { ViewManagerConnection::ViewManagerConnection(RootNodeManager* root_node_manager)
: root_node_manager_(root_node_manager),
id_(0) {
} }
ViewManagerConnection::~ViewManagerConnection() { ViewManagerConnection::~ViewManagerConnection() {
...@@ -78,22 +80,25 @@ ViewManagerConnection::~ViewManagerConnection() { ...@@ -78,22 +80,25 @@ ViewManagerConnection::~ViewManagerConnection() {
} }
STLDeleteContainerPairSecondPointers(node_map_.begin(), node_map_.end()); STLDeleteContainerPairSecondPointers(node_map_.begin(), node_map_.end());
context()->RemoveConnection(this); root_node_manager_->RemoveConnection(this);
} }
void ViewManagerConnection::Initialize() { void ViewManagerConnection::OnConnectionEstablished() {
DCHECK_EQ(0, id_); // Should only get Initialize() once. DCHECK_EQ(0, id_); // Should only get SetClient() once.
id_ = context()->GetAndAdvanceNextConnectionId(); id_ = root_node_manager_->GetAndAdvanceNextConnectionId();
context()->AddConnection(this); root_node_manager_->AddConnection(this);
client()->OnConnectionEstablished(id_, context()->next_server_change_id()); client()->OnConnectionEstablished(
id_, root_node_manager_->next_server_change_id());
} }
void ViewManagerConnection::OnConnectionError() {}
Node* ViewManagerConnection::GetNode(const NodeId& id) { Node* ViewManagerConnection::GetNode(const NodeId& id) {
if (id_ == id.connection_id) { if (id_ == id.connection_id) {
NodeMap::iterator i = node_map_.find(id.node_id); NodeMap::iterator i = node_map_.find(id.node_id);
return i == node_map_.end() ? NULL : i->second; return i == node_map_.end() ? NULL : i->second;
} }
return context()->GetNode(id); return root_node_manager_->GetNode(id);
} }
View* ViewManagerConnection::GetView(const ViewId& id) { View* ViewManagerConnection::GetView(const ViewId& id) {
...@@ -101,7 +106,7 @@ View* ViewManagerConnection::GetView(const ViewId& id) { ...@@ -101,7 +106,7 @@ View* ViewManagerConnection::GetView(const ViewId& id) {
ViewMap::const_iterator i = view_map_.find(id.view_id); ViewMap::const_iterator i = view_map_.find(id.view_id);
return i == view_map_.end() ? NULL : i->second; return i == view_map_.end() ? NULL : i->second;
} }
return context()->GetView(id); return root_node_manager_->GetView(id);
} }
void ViewManagerConnection::NotifyNodeHierarchyChanged( void ViewManagerConnection::NotifyNodeHierarchyChanged(
...@@ -141,7 +146,8 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source, ...@@ -141,7 +146,8 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source,
if (!node) if (!node)
return false; return false;
RootNodeManager::ScopedChange change( RootNodeManager::ScopedChange change(
source, context(), RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); source, root_node_manager_,
RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID);
if (node->GetParent()) if (node->GetParent())
node->GetParent()->Remove(node); node->GetParent()->Remove(node);
std::vector<Node*> children(node->GetChildren()); std::vector<Node*> children(node->GetChildren());
...@@ -151,7 +157,7 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source, ...@@ -151,7 +157,7 @@ bool ViewManagerConnection::DeleteNodeImpl(ViewManagerConnection* source,
node_map_.erase(node_id.node_id); node_map_.erase(node_id.node_id);
delete node; delete node;
node = NULL; node = NULL;
context()->NotifyNodeDeleted(node_id); root_node_manager_->NotifyNodeDeleted(node_id);
return true; return true;
} }
...@@ -162,7 +168,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source, ...@@ -162,7 +168,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source,
if (!view) if (!view)
return false; return false;
RootNodeManager::ScopedChange change( RootNodeManager::ScopedChange change(
source, context(), source, root_node_manager_,
RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID); RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID);
if (view->node()) if (view->node())
view->node()->SetView(NULL); view->node()->SetView(NULL);
...@@ -171,7 +177,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source, ...@@ -171,7 +177,7 @@ bool ViewManagerConnection::DeleteViewImpl(ViewManagerConnection* source,
// valid. // valid.
const ViewId view_id_copy(view_id); const ViewId view_id_copy(view_id);
delete view; delete view;
context()->NotifyViewDeleted(view_id_copy); root_node_manager_->NotifyViewDeleted(view_id_copy);
return true; return true;
} }
...@@ -184,7 +190,7 @@ bool ViewManagerConnection::SetViewImpl(const NodeId& node_id, ...@@ -184,7 +190,7 @@ bool ViewManagerConnection::SetViewImpl(const NodeId& node_id,
if (!view && view_id != ViewId()) if (!view && view_id != ViewId())
return false; return false;
RootNodeManager::ScopedChange change( RootNodeManager::ScopedChange change(
this, context(), this, root_node_manager_,
RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID); RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID);
node->SetView(view); node->SetView(view);
return true; return true;
...@@ -207,7 +213,7 @@ void ViewManagerConnection::DeleteNode( ...@@ -207,7 +213,7 @@ void ViewManagerConnection::DeleteNode(
TransportNodeId transport_node_id, TransportNodeId transport_node_id,
const Callback<void(bool)>& callback) { const Callback<void(bool)>& callback) {
const NodeId node_id(NodeIdFromTransportId(transport_node_id)); const NodeId node_id(NodeIdFromTransportId(transport_node_id));
ViewManagerConnection* connection = context()->GetConnection( ViewManagerConnection* connection = root_node_manager_->GetConnection(
node_id.connection_id); node_id.connection_id);
callback.Run(connection && callback.Run(connection &&
connection->DeleteNodeImpl(this, node_id)); connection->DeleteNodeImpl(this, node_id));
...@@ -219,14 +225,14 @@ void ViewManagerConnection::AddNode( ...@@ -219,14 +225,14 @@ void ViewManagerConnection::AddNode(
TransportChangeId server_change_id, TransportChangeId server_change_id,
const Callback<void(bool)>& callback) { const Callback<void(bool)>& callback) {
bool success = false; bool success = false;
if (server_change_id == context()->next_server_change_id()) { if (server_change_id == root_node_manager_->next_server_change_id()) {
Node* parent = GetNode(NodeIdFromTransportId(parent_id)); Node* parent = GetNode(NodeIdFromTransportId(parent_id));
Node* child = GetNode(NodeIdFromTransportId(child_id)); Node* child = GetNode(NodeIdFromTransportId(child_id));
if (parent && child && child->GetParent() != parent && if (parent && child && child->GetParent() != parent &&
!child->window()->Contains(parent->window())) { !child->window()->Contains(parent->window())) {
success = true; success = true;
RootNodeManager::ScopedChange change( RootNodeManager::ScopedChange change(
this, context(), this, root_node_manager_,
RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID);
parent->Add(child); parent->Add(child);
} }
...@@ -239,12 +245,12 @@ void ViewManagerConnection::RemoveNodeFromParent( ...@@ -239,12 +245,12 @@ void ViewManagerConnection::RemoveNodeFromParent(
TransportChangeId server_change_id, TransportChangeId server_change_id,
const Callback<void(bool)>& callback) { const Callback<void(bool)>& callback) {
bool success = false; bool success = false;
if (server_change_id == context()->next_server_change_id()) { if (server_change_id == root_node_manager_->next_server_change_id()) {
Node* node = GetNode(NodeIdFromTransportId(node_id)); Node* node = GetNode(NodeIdFromTransportId(node_id));
if (node && node->GetParent()) { if (node && node->GetParent()) {
success = true; success = true;
RootNodeManager::ScopedChange change( RootNodeManager::ScopedChange change(
this, context(), this, root_node_manager_,
RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID); RootNodeManager::CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID);
node->GetParent()->Remove(node); node->GetParent()->Remove(node);
} }
...@@ -281,7 +287,7 @@ void ViewManagerConnection::DeleteView( ...@@ -281,7 +287,7 @@ void ViewManagerConnection::DeleteView(
TransportViewId transport_view_id, TransportViewId transport_view_id,
const Callback<void(bool)>& callback) { const Callback<void(bool)>& callback) {
const ViewId view_id(ViewIdFromTransportId(transport_view_id)); const ViewId view_id(ViewIdFromTransportId(transport_view_id));
ViewManagerConnection* connection = context()->GetConnection( ViewManagerConnection* connection = root_node_manager_->GetConnection(
view_id.connection_id); view_id.connection_id);
callback.Run(connection && connection->DeleteViewImpl(this, view_id)); callback.Run(connection && connection->DeleteViewImpl(this, view_id));
} }
...@@ -316,13 +322,13 @@ void ViewManagerConnection::SetViewContents( ...@@ -316,13 +322,13 @@ void ViewManagerConnection::SetViewContents(
void ViewManagerConnection::OnNodeHierarchyChanged(const NodeId& node, void ViewManagerConnection::OnNodeHierarchyChanged(const NodeId& node,
const NodeId& new_parent, const NodeId& new_parent,
const NodeId& old_parent) { const NodeId& old_parent) {
context()->NotifyNodeHierarchyChanged(node, new_parent, old_parent); root_node_manager_->NotifyNodeHierarchyChanged(node, new_parent, old_parent);
} }
void ViewManagerConnection::OnNodeViewReplaced(const NodeId& node, void ViewManagerConnection::OnNodeViewReplaced(const NodeId& node,
const ViewId& new_view_id, const ViewId& new_view_id,
const ViewId& old_view_id) { const ViewId& old_view_id) {
context()->NotifyNodeViewReplaced(node, new_view_id, old_view_id); root_node_manager_->NotifyNodeViewReplaced(node, new_view_id, old_view_id);
} }
} // namespace service } // namespace service
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/compiler_specific.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/public/interfaces/view_manager/view_manager.mojom.h"
#include "mojo/services/view_manager/ids.h" #include "mojo/services/view_manager/ids.h"
#include "mojo/services/view_manager/node_delegate.h" #include "mojo/services/view_manager/node_delegate.h"
...@@ -32,17 +31,16 @@ class View; ...@@ -32,17 +31,16 @@ class View;
// Manages a connection from the client. // Manages a connection from the client.
class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection
: public ServiceConnection<IViewManager, ViewManagerConnection, : public InterfaceImpl<IViewManager>,
RootNodeManager>,
public NodeDelegate { public NodeDelegate {
public: public:
ViewManagerConnection(); ViewManagerConnection(RootNodeManager* root_node_manager);
virtual ~ViewManagerConnection(); virtual ~ViewManagerConnection();
TransportConnectionId id() const { return id_; } virtual void OnConnectionEstablished() MOJO_OVERRIDE;
virtual void OnConnectionError() MOJO_OVERRIDE;
// Invoked when connection is established. TransportConnectionId id() const { return id_; }
void Initialize();
// Returns the Node with the specified id. // Returns the Node with the specified id.
Node* GetNode(const NodeId& id); Node* GetNode(const NodeId& id);
...@@ -111,9 +109,10 @@ class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection ...@@ -111,9 +109,10 @@ class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection
virtual void OnNodeViewReplaced(const NodeId& node, virtual void OnNodeViewReplaced(const NodeId& node,
const ViewId& new_view_id, const ViewId& new_view_id,
const ViewId& old_view_id) OVERRIDE; const ViewId& old_view_id) OVERRIDE;
RootNodeManager* root_node_manager_;
// Id of this connection as assigned by RootNodeManager. Assigned in // Id of this connection as assigned by RootNodeManager. Assigned in
// Initialize(). // OnConnectionEstablished().
TransportConnectionId id_; TransportConnectionId id_;
NodeMap node_map_; NodeMap node_map_;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "mojo/public/cpp/bindings/allocation_scope.h" #include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/cpp/environment/environment.h" #include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/shell/service.h" #include "mojo/public/cpp/shell/connect.h"
#include "mojo/services/public/cpp/view_manager/util.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/cpp/view_manager/view_manager_types.h"
#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
......
...@@ -25,10 +25,8 @@ void ViewManagerLoader::LoadService(ServiceManager* manager, ...@@ -25,10 +25,8 @@ void ViewManagerLoader::LoadService(ServiceManager* manager,
root_node_manager_.reset( root_node_manager_.reset(
new view_manager::service::RootNodeManager(app->shell())); new view_manager::service::RootNodeManager(app->shell()));
} }
app->AddServiceConnector( app->AddService<view_manager::service::ViewManagerConnection>(
new ServiceConnector<view_manager::service::ViewManagerConnection, root_node_manager_.get());
view_manager::service::RootNodeManager>(
root_node_manager_.get()));
apps_.push_back(app.release()); apps_.push_back(app.release());
} }
......
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