| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef MOJO_SHELL_PUBLIC_CPP_SHELL_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_SHELL_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_SHELL_H_ | 6 #define MOJO_SHELL_PUBLIC_CPP_SHELL_H_ |
| 7 | 7 |
| 8 #include "mojo/shell/public/cpp/connection.h" | 8 #include "mojo/shell/public/cpp/connection.h" |
| 9 #include "mojo/shell/public/cpp/connector.h" |
| 9 #include "mojo/shell/public/interfaces/shell.mojom.h" | 10 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 10 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 | 14 |
| 15 shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter(); | |
| 16 | |
| 17 using ShellClientRequest = InterfaceRequest<shell::mojom::ShellClient>; | |
| 18 | |
| 19 // An interface implementation can keep this object as a member variable to | 15 // An interface implementation can keep this object as a member variable to |
| 20 // hold a reference to the ShellConnection, keeping it alive as long as the | 16 // hold a reference to the ShellConnection, keeping it alive as long as the |
| 21 // bound implementation exists. | 17 // bound implementation exists. |
| 22 // Since interface implementations can be bound on different threads than the | 18 // Since interface implementations can be bound on different threads than the |
| 23 // ShellConnection, this class is safe to use on any thread. However, each | 19 // ShellConnection, this class is safe to use on any thread. However, each |
| 24 // instance should only be used on one thread at a time (otherwise there'll be | 20 // instance should only be used on one thread at a time (otherwise there'll be |
| 25 // races between the AddRef resulting from cloning and destruction). | 21 // races between the AddRef resulting from cloning and destruction). |
| 26 class AppRefCount { | 22 class AppRefCount { |
| 27 public: | 23 public: |
| 28 virtual ~AppRefCount() {} | 24 virtual ~AppRefCount() {} |
| 29 | 25 |
| 30 virtual scoped_ptr<AppRefCount> Clone() = 0; | 26 virtual scoped_ptr<AppRefCount> Clone() = 0; |
| 31 }; | 27 }; |
| 32 | 28 |
| 33 // An interface that encapsulates the Mojo Shell's broker interface by which | 29 // An interface that encapsulates the Mojo Shell's broker interface by which |
| 34 // connections between applications are established. Implemented by | 30 // connections between applications are established. Implemented by |
| 35 // ShellConnection, this is the primary interface exposed to clients. | 31 // ShellConnection, this is the primary interface exposed to clients. |
| 36 class Shell { | 32 class Shell { |
| 37 public: | 33 public: |
| 38 class ConnectParams { | |
| 39 public: | |
| 40 explicit ConnectParams(const std::string& url); | |
| 41 ~ConnectParams(); | |
| 42 | |
| 43 const GURL& url() { return url_; } | |
| 44 shell::mojom::CapabilityFilterPtr TakeFilter() { | |
| 45 return std::move(filter_); | |
| 46 } | |
| 47 void set_filter(shell::mojom::CapabilityFilterPtr filter) { | |
| 48 filter_ = std::move(filter); | |
| 49 } | |
| 50 void set_user_id(uint32_t user_id) { user_id_ = user_id; } | |
| 51 uint32_t user_id() const { return user_id_; } | |
| 52 | |
| 53 private: | |
| 54 GURL url_; | |
| 55 shell::mojom::CapabilityFilterPtr filter_; | |
| 56 uint32_t user_id_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(ConnectParams); | |
| 59 }; | |
| 60 | |
| 61 // Requests a new connection to an application. Returns a pointer to the | 34 // Requests a new connection to an application. Returns a pointer to the |
| 62 // connection if the connection is permitted by this application's delegate, | 35 // connection if the connection is permitted by this application's delegate, |
| 63 // or nullptr otherwise. Caller takes ownership. | 36 // or nullptr otherwise. Caller takes ownership. |
| 64 virtual scoped_ptr<Connection> Connect(const std::string& url) = 0; | 37 virtual scoped_ptr<Connection> Connect(const std::string& url) = 0; |
| 65 virtual scoped_ptr<Connection> Connect(ConnectParams* params) = 0; | 38 virtual scoped_ptr<Connection> Connect(Connector::ConnectParams* params) = 0; |
| 66 | 39 |
| 67 // Connect to application identified by |request->url| and connect to the | 40 // Connect to application identified by |request->url| and connect to the |
| 68 // service implementation of the interface identified by |Interface|. | 41 // service implementation of the interface identified by |Interface|. |
| 69 template <typename Interface> | 42 template <typename Interface> |
| 70 void ConnectToInterface(ConnectParams* params, InterfacePtr<Interface>* ptr) { | 43 void ConnectToInterface(Connector::ConnectParams* params, |
| 44 InterfacePtr<Interface>* ptr) { |
| 71 scoped_ptr<Connection> connection = Connect(params); | 45 scoped_ptr<Connection> connection = Connect(params); |
| 72 if (connection) | 46 if (connection) |
| 73 connection->GetInterface(ptr); | 47 connection->GetInterface(ptr); |
| 74 } | 48 } |
| 75 template <typename Interface> | 49 template <typename Interface> |
| 76 void ConnectToInterface(const std::string& url, | 50 void ConnectToInterface(const std::string& url, |
| 77 InterfacePtr<Interface>* ptr) { | 51 InterfacePtr<Interface>* ptr) { |
| 78 ConnectParams params(url); | 52 Connector::ConnectParams params(url); |
| 79 params.set_filter(CreatePermissiveCapabilityFilter()); | 53 params.set_filter(CreatePermissiveCapabilityFilter()); |
| 80 return ConnectToInterface(¶ms, ptr); | 54 return ConnectToInterface(¶ms, ptr); |
| 81 } | 55 } |
| 82 | 56 |
| 57 virtual scoped_ptr<Connector> CloneConnector() const = 0; |
| 58 |
| 83 // Initiate shutdown of this application. This may involve a round trip to the | 59 // Initiate shutdown of this application. This may involve a round trip to the |
| 84 // Shell to ensure there are no inbound service requests. | 60 // Shell to ensure there are no inbound service requests. |
| 85 virtual void Quit() = 0; | 61 virtual void Quit() = 0; |
| 86 | 62 |
| 87 // Create an object that can be used to refcount the lifetime of the | 63 // Create an object that can be used to refcount the lifetime of the |
| 88 // application. The returned object may be cloned, and when the refcount falls | 64 // application. The returned object may be cloned, and when the refcount falls |
| 89 // to zero Quit() is called. | 65 // to zero Quit() is called. |
| 90 virtual scoped_ptr<AppRefCount> CreateAppRefCount() = 0; | 66 virtual scoped_ptr<AppRefCount> CreateAppRefCount() = 0; |
| 91 }; | 67 }; |
| 92 | 68 |
| 93 } // namespace mojo | 69 } // namespace mojo |
| 94 | 70 |
| 95 #endif // MOJO_SHELL_PUBLIC_CPP_SHELL_H_ | 71 #endif // MOJO_SHELL_PUBLIC_CPP_SHELL_H_ |
| OLD | NEW |