OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_SHELL_PUBLIC_CPP_CONNECTOR_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_CONNECTOR_H_ |
| 7 |
| 8 #include "mojo/shell/public/cpp/connection.h" |
| 9 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 10 #include "url/gurl.h" |
| 11 |
| 12 namespace mojo { |
| 13 |
| 14 shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter(); |
| 15 |
| 16 // An interface that encapsulates the Mojo Shell's broker interface by which |
| 17 // connections between applications are established. Once Connect() is called, |
| 18 // this class is bound to the thread the call was made on and it cannot be |
| 19 // passed to another thread without calling Clone(). |
| 20 // An instance of this class is created internally by ShellConnection for use |
| 21 // on the thread ShellConnection is instantiated on, and this interface is |
| 22 // wrapped by the Shell interface. |
| 23 // To use this interface on other threads, call Shell::CloneConnector() and |
| 24 // pass the result to another thread. To pass to subsequent threads, call |
| 25 // Clone() on instances of this object. |
| 26 // While instances of this object are owned by the caller, the underlying |
| 27 // connection with the shell is bound to the lifetime of the instance that |
| 28 // created it, i.e. when the application is terminated the Connector pipe is |
| 29 // closed. |
| 30 class Connector { |
| 31 public: |
| 32 class ConnectParams { |
| 33 public: |
| 34 explicit ConnectParams(const std::string& url); |
| 35 ~ConnectParams(); |
| 36 |
| 37 const GURL& url() { return url_; } |
| 38 shell::mojom::CapabilityFilterPtr TakeFilter() { |
| 39 return std::move(filter_); |
| 40 } |
| 41 void set_filter(shell::mojom::CapabilityFilterPtr filter) { |
| 42 filter_ = std::move(filter); |
| 43 } |
| 44 void set_user_id(uint32_t user_id) { user_id_ = user_id; } |
| 45 uint32_t user_id() const { return user_id_; } |
| 46 |
| 47 private: |
| 48 GURL url_; |
| 49 shell::mojom::CapabilityFilterPtr filter_; |
| 50 uint32_t user_id_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(ConnectParams); |
| 53 }; |
| 54 |
| 55 // Requests a new connection to an application. Returns a pointer to the |
| 56 // connection if the connection is permitted by this application's delegate, |
| 57 // or nullptr otherwise. Caller takes ownership. |
| 58 // Once this method is called, this object is bound to the thread on which the |
| 59 // call took place. To pass to another thread, call Clone() and pass the |
| 60 // result. |
| 61 virtual scoped_ptr<Connection> Connect(const std::string& url) = 0; |
| 62 virtual scoped_ptr<Connection> Connect(ConnectParams* params) = 0; |
| 63 |
| 64 // Connect to application identified by |request->url| and connect to the |
| 65 // service implementation of the interface identified by |Interface|. |
| 66 template <typename Interface> |
| 67 void ConnectToInterface(ConnectParams* params, InterfacePtr<Interface>* ptr) { |
| 68 scoped_ptr<Connection> connection = Connect(params); |
| 69 if (connection) |
| 70 connection->GetInterface(ptr); |
| 71 } |
| 72 template <typename Interface> |
| 73 void ConnectToInterface(const std::string& url, |
| 74 InterfacePtr<Interface>* ptr) { |
| 75 ConnectParams params(url); |
| 76 params.set_filter(CreatePermissiveCapabilityFilter()); |
| 77 return ConnectToInterface(¶ms, ptr); |
| 78 } |
| 79 |
| 80 // Creates a new instance of this class which may be passed to another thread. |
| 81 // The returned object may be passed multiple times until Connect() is called, |
| 82 // at which point this method must be called again to pass again. |
| 83 virtual scoped_ptr<Connector> Clone() = 0; |
| 84 }; |
| 85 |
| 86 } // namespace mojo |
| 87 |
| 88 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTOR_H_ |
OLD | NEW |