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