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