| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "dbus/bus.h" | 6 #include "dbus/bus.h" |
| 7 #include "dbus/exported_object.h" | 7 #include "dbus/exported_object.h" |
| 8 #include "dbus/message.h" | 8 #include "dbus/message.h" |
| 9 #include "dbus/object_path.h" | 9 #include "dbus/object_path.h" |
| 10 #include "mojo/common/channel_init.h" | 10 #include "mojo/common/channel_init.h" |
| 11 #include "mojo/public/cpp/bindings/interface.h" | 11 #include "mojo/public/cpp/bindings/interface.h" |
| 12 #include "mojo/public/cpp/bindings/remote_ptr.h" | |
| 13 #include "mojo/public/cpp/shell/application.h" | 12 #include "mojo/public/cpp/shell/application.h" |
| 14 #include "mojo/public/interfaces/shell/shell.mojom.h" | 13 #include "mojo/public/interfaces/shell/shell.mojom.h" |
| 15 #include "mojo/shell/external_service.mojom.h" | 14 #include "mojo/shell/external_service.mojom.h" |
| 16 | 15 |
| 17 namespace mojo { | 16 namespace mojo { |
| 18 const char kMojoDBusImplPath[] = "/org/chromium/MojoImpl"; | 17 const char kMojoDBusImplPath[] = "/org/chromium/MojoImpl"; |
| 19 const char kMojoDBusInterface[] = "org.chromium.Mojo"; | 18 const char kMojoDBusInterface[] = "org.chromium.Mojo"; |
| 20 const char kMojoDBusConnectMethod[] = "ConnectChannel"; | 19 const char kMojoDBusConnectMethod[] = "ConnectChannel"; |
| 21 | 20 |
| 22 class DBusExternalServiceBase : public mojo::ErrorHandler { | 21 class DBusExternalServiceBase : public ErrorHandler { |
| 23 public: | 22 public: |
| 24 explicit DBusExternalServiceBase(const std::string& service_name); | 23 explicit DBusExternalServiceBase(const std::string& service_name); |
| 25 virtual ~DBusExternalServiceBase(); | 24 virtual ~DBusExternalServiceBase(); |
| 26 | 25 |
| 27 void Start(); | 26 void Start(); |
| 28 | 27 |
| 29 protected: | 28 protected: |
| 30 // TODO(cmasone): Enable multiple peers to connect/disconnect | 29 // TODO(cmasone): Enable multiple peers to connect/disconnect |
| 31 virtual void Connect(ScopedExternalServiceHostHandle client_handle) = 0; | 30 virtual void Connect(ScopedMessagePipeHandle client_handle) = 0; |
| 32 virtual void Disconnect() = 0; | 31 virtual void Disconnect() = 0; |
| 33 | 32 |
| 34 private: | 33 private: |
| 35 virtual void OnError() OVERRIDE; | 34 virtual void OnError() OVERRIDE; |
| 36 | 35 |
| 37 // Implementation of org.chromium.Mojo.ConnectChannel, exported over DBus. | 36 // Implementation of org.chromium.Mojo.ConnectChannel, exported over DBus. |
| 38 // Takes a file descriptor and uses it to create a MessagePipe that is then | 37 // Takes a file descriptor and uses it to create a MessagePipe that is then |
| 39 // hooked to a RemotePtr<mojo::ExternalServiceHost>. | 38 // hooked to a RemotePtr<ExternalServiceHost>. |
| 40 void ConnectChannel(dbus::MethodCall* method_call, | 39 void ConnectChannel(dbus::MethodCall* method_call, |
| 41 dbus::ExportedObject::ResponseSender sender); | 40 dbus::ExportedObject::ResponseSender sender); |
| 42 | 41 |
| 43 void ExportMethods(); | 42 void ExportMethods(); |
| 44 void InitializeDBus(); | 43 void InitializeDBus(); |
| 45 void TakeDBusServiceOwnership(); | 44 void TakeDBusServiceOwnership(); |
| 46 | 45 |
| 47 const std::string service_name_; | 46 const std::string service_name_; |
| 48 scoped_refptr<dbus::Bus> bus_; | 47 scoped_refptr<dbus::Bus> bus_; |
| 49 dbus::ExportedObject* exported_object_; // Owned by bus_; | 48 dbus::ExportedObject* exported_object_; // Owned by bus_; |
| 50 scoped_ptr<mojo::common::ChannelInit> channel_init_; | 49 scoped_ptr<common::ChannelInit> channel_init_; |
| 51 DISALLOW_COPY_AND_ASSIGN(DBusExternalServiceBase); | 50 DISALLOW_COPY_AND_ASSIGN(DBusExternalServiceBase); |
| 52 }; | 51 }; |
| 53 | 52 |
| 54 template <class ServiceImpl> | 53 template <class ServiceImpl> |
| 55 class DBusExternalService : public DBusExternalServiceBase, | 54 class DBusExternalService : public DBusExternalServiceBase, |
| 56 public mojo::ExternalService { | 55 public ExternalService { |
| 57 public: | 56 public: |
| 58 explicit DBusExternalService(const std::string& service_name) | 57 explicit DBusExternalService(const std::string& service_name) |
| 59 : DBusExternalServiceBase(service_name) { | 58 : DBusExternalServiceBase(service_name) { |
| 60 } | 59 } |
| 61 virtual ~DBusExternalService() {} | 60 virtual ~DBusExternalService() {} |
| 62 | 61 |
| 63 protected: | 62 protected: |
| 64 virtual void Connect(ScopedExternalServiceHostHandle client_handle) OVERRIDE { | 63 virtual void Connect(ScopedMessagePipeHandle client_handle) OVERRIDE { |
| 65 external_service_host_.reset(client_handle.Pass(), this, this); | 64 external_service_.Bind(new Impl()); |
| 65 external_service_.ConfigureStub(client_handle); |
| 66 external_service_.set_error_handler(this); |
| 66 } | 67 } |
| 67 | 68 |
| 68 virtual void Disconnect() OVERRIDE { | 69 virtual void Disconnect() OVERRIDE { |
| 69 app_.reset(); | 70 external_service_.reset(); |
| 70 external_service_host_.reset(); | |
| 71 } | 71 } |
| 72 | 72 |
| 73 private: | 73 private: |
| 74 virtual void Activate(mojo::ScopedMessagePipeHandle client_handle) OVERRIDE { | 74 class Impl : public ExternalService { |
| 75 mojo::ScopedShellHandle shell_handle( | 75 public: |
| 76 mojo::ShellHandle(client_handle.release().value())); | 76 virtual void Activate(ScopedMessagePipeHandle shell_handle) OVERRIDE { |
| 77 app_.reset(new mojo::Application(shell_handle.Pass())); | 77 app_.reset(new Application(shell_handle.Pass())); |
| 78 app_->AddServiceConnector(new mojo::ServiceConnector<ServiceImpl>()); | 78 app_->AddServiceConnector(new ServiceConnector<ServiceImpl>()); |
| 79 } | 79 } |
| 80 private: |
| 81 scoped_ptr<Application> app_; |
| 82 }; |
| 80 | 83 |
| 81 mojo::RemotePtr<mojo::ExternalServiceHost> external_service_host_; | 84 ExternalServicePtr external_service_; |
| 82 scoped_ptr<mojo::Application> app_; | |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 } // namespace mojo | 87 } // namespace mojo |
| OLD | NEW |