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