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 IOS_WEB_WEBUI_MOJO_FACADE_H_ | 5 #ifndef IOS_WEB_WEBUI_MOJO_FACADE_H_ |
6 #define IOS_WEB_WEBUI_MOJO_FACADE_H_ | 6 #define IOS_WEB_WEBUI_MOJO_FACADE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 26 matching lines...) Expand all Loading... |
37 MojoFacade(service_manager::mojom::InterfaceProvider* interface_provider, | 37 MojoFacade(service_manager::mojom::InterfaceProvider* interface_provider, |
38 id<CRWJSInjectionEvaluator> script_evaluator); | 38 id<CRWJSInjectionEvaluator> script_evaluator); |
39 ~MojoFacade(); | 39 ~MojoFacade(); |
40 | 40 |
41 // Handles Mojo message received from WebUI page. Returns a valid JSON string | 41 // Handles Mojo message received from WebUI page. Returns a valid JSON string |
42 // on success or empty string if supplied JSON does not have required | 42 // on success or empty string if supplied JSON does not have required |
43 // structure. Every message must have "name" and "args" keys, where "name" is | 43 // structure. Every message must have "name" and "args" keys, where "name" is |
44 // a string representing the name of Mojo message and "args" is a dictionary | 44 // a string representing the name of Mojo message and "args" is a dictionary |
45 // with arguments specific for each message name. | 45 // with arguments specific for each message name. |
46 // Supported message names with their handler methods in parenthesis: | 46 // Supported message names with their handler methods in parenthesis: |
47 // interface_provider.getInterface (HandleInterfaceProviderGetInterface) | 47 // Mojo.bindInterface (HandleMojoBindInterface) |
48 // core.close (HandleCoreClose) | 48 // MojoHandle.close (HandleMojoHandleClose) |
49 // core.createMessagePipe (HandleCoreCreateMessagePipe) | 49 // Mojo.createMessagePipe (HandleMojoCreateMessagePipe) |
50 // core.writeMessage (HandleCoreWriteMessage) | 50 // MojoHandle.writeMessage (HandleMojoHandleWriteMessage) |
51 // core.readMessage (HandleCoreReadMessage) | 51 // MojoHandle.readMessage (HandleMojoHandleReadMessage) |
52 // support.watch (HandleSupportWatch) | 52 // MojoHandle.watch (HandleMojoHandleWatch) |
53 // support.cancelWatch (HandleSupportCancelWatch) | 53 // MojoWatcher.cancel (HandleMojoWatcherCancel) |
54 std::string HandleMojoMessage(const std::string& mojo_message_as_json); | 54 std::string HandleMojoMessage(const std::string& mojo_message_as_json); |
55 | 55 |
56 private: | 56 private: |
57 // Extracts message name and arguments from the given JSON string obtained | 57 // Extracts message name and arguments from the given JSON string obtained |
58 // from WebUI page. This method either succeeds or crashes the app (this | 58 // from WebUI page. This method either succeeds or crashes the app (this |
59 // matches other platforms where Mojo API is strict on malformed input). | 59 // matches other platforms where Mojo API is strict on malformed input). |
60 void GetMessageNameAndArguments( | 60 void GetMessageNameAndArguments( |
61 const std::string& mojo_message_as_json, | 61 const std::string& mojo_message_as_json, |
62 std::string* out_name, | 62 std::string* out_name, |
63 std::unique_ptr<base::DictionaryValue>* out_args); | 63 std::unique_ptr<base::DictionaryValue>* out_args); |
64 | 64 |
65 // Connects to specified Mojo interface. |args| is a dictionary which must | 65 // Connects to specified Mojo interface. |args| is a dictionary with the |
66 // contain "interfaceName" key, which is a string representing a interface | 66 // following keys: |
67 // name. | 67 // - "interfaceName" (a string representing an interface name); |
68 // Returns MojoHandle as a number. | 68 // - "requestHandle" (a number representing MojoHandle of the interface |
69 std::unique_ptr<base::Value> HandleInterfaceProviderGetInterface( | 69 // request). |
| 70 // Always returns null. |
| 71 std::unique_ptr<base::Value> HandleMojoBindInterface( |
70 const base::DictionaryValue* args); | 72 const base::DictionaryValue* args); |
71 | 73 |
72 // Closes the given handle. |args| is a dictionary which must contain "handle" | 74 // Closes the given handle. |args| is a dictionary which must contain "handle" |
73 // key, which is a number representing a MojoHandle. | 75 // key, which is a number representing a MojoHandle. |
74 // Returns MojoResult as a number. | 76 // Always returns null. |
75 std::unique_ptr<base::Value> HandleCoreClose( | 77 std::unique_ptr<base::Value> HandleMojoHandleClose( |
76 const base::DictionaryValue* args); | 78 const base::DictionaryValue* args); |
77 | 79 |
78 // Creates a Mojo message pipe. |args| is a dictionary which must contain | 80 // Creates a Mojo message pipe. |args| is unused. |
79 // "optionsDict" key. optionsDict is a dictionary with the following keys: | 81 // Returns a dictionary with the following keys: |
80 // - "struct_size" (a number representing the size of this struct; used to | 82 // - "result" (a number representing MojoResult); |
81 // allow for future extensions); | 83 // - "handle0" and "handle1" (the numbers representing two endpoints of the |
82 // - "flags" (a number representing MojoCreateMessagePipeOptionsFlags; used | 84 // message pipe). |
83 // to specify different modes of operation); | 85 std::unique_ptr<base::Value> HandleMojoCreateMessagePipe( |
84 // Returns a dictionary with "handle0" and "handle1" keys (the numbers | |
85 // representing two ports for the message pipe). | |
86 std::unique_ptr<base::Value> HandleCoreCreateMessagePipe( | |
87 base::DictionaryValue* args); | 86 base::DictionaryValue* args); |
88 | 87 |
89 // Writes a message to the message pipe endpoint given by handle. |args| is a | 88 // Writes a message to the message pipe endpoint given by handle. |args| is a |
90 // dictionary which must contain the following keys: | 89 // dictionary which must contain the following keys: |
91 // - "handle" (a number representing MojoHandle, the endpoint to write to); | 90 // - "handle" (a number representing MojoHandle, the endpoint to write to); |
92 // - "buffer" (a dictionary representing the message data; may be empty); | 91 // - "buffer" (a dictionary representing the message data; may be empty); |
93 // - "handles" (an array representing any handles to attach; handles are | 92 // - "handles" (an array representing any handles to attach; handles are |
94 // transferred on success and will no longer be valid; may be empty); | 93 // transferred and will no longer be valid; may be empty); |
95 // - "flags" (a number representing MojoWriteMessageFlags); | |
96 // Returns MojoResult as a number. | 94 // Returns MojoResult as a number. |
97 std::unique_ptr<base::Value> HandleCoreWriteMessage( | 95 std::unique_ptr<base::Value> HandleMojoHandleWriteMessage( |
98 base::DictionaryValue* args); | 96 base::DictionaryValue* args); |
99 | 97 |
100 // Reads a message from the message pipe endpoint given by handle. |args| is | 98 // Reads a message from the message pipe endpoint given by handle. |args| is |
101 // a dictionary which must contain the following keys: | 99 // a dictionary which must contain the keys "handle" (a number representing |
102 // - "handle" (a number representing MojoHandle, the endpoint to read from); | 100 // MojoHandle, the endpoint to read from). |
103 // - "flags" (a number representing MojoWriteMessageFlags); | |
104 // Returns a dictionary with the following keys: | 101 // Returns a dictionary with the following keys: |
105 // - "result" (a number representing MojoResult); | 102 // - "result" (a number representing MojoResult); |
106 // - "buffer" (an array representing message data; non-empty only on | 103 // - "buffer" (an array representing message data; non-empty only on |
107 // success); | 104 // success); |
108 // - "handles" (an array representing MojoHandles transferred, if any); | 105 // - "handles" (an array representing MojoHandles received, if any); |
109 std::unique_ptr<base::Value> HandleCoreReadMessage( | 106 std::unique_ptr<base::Value> HandleMojoHandleReadMessage( |
110 const base::DictionaryValue* args); | 107 const base::DictionaryValue* args); |
111 | 108 |
112 // Begins watching a handle for signals to be satisfied or unsatisfiable. | 109 // Begins watching a handle for signals to be satisfied or unsatisfiable. |
113 // |args| is a dictionary which must contain the following keys: | 110 // |args| is a dictionary which must contain the following keys: |
114 // - "handle" (a number representing a MojoHandle), "signals" (a number | 111 // - "handle" (a number representing a MojoHandle); |
115 // representing MojoHandleSignals to watch); | 112 // - "signals" (a number representing MojoHandleSignals to watch); |
116 // - "callbackId" (a number representing the id which should be passed to | 113 // - "callbackId" (a number representing the id which should be passed to |
117 // __crWeb.mojo.mojoWatchSignal call); | 114 // Mojo.internal.signalWatch call). |
118 // Returns watch id as a number. | 115 // Returns watch id as a number. |
119 std::unique_ptr<base::Value> HandleSupportWatch( | 116 std::unique_ptr<base::Value> HandleMojoHandleWatch( |
120 const base::DictionaryValue* args); | 117 const base::DictionaryValue* args); |
121 | 118 |
122 // Cancels a handle watch initiated by "support.watch". |args| is a dictionary | 119 // Cancels a handle watch initiated by "MojoHandle.watch". |args| is a |
123 // which must contain "watchId" key (a number representing id returned from | 120 // dictionary which must contain "watchId" key (a number representing id |
124 // "support.watch"). | 121 // returned from "MojoHandle.watch"). |
125 // Returns null. | 122 // Returns null. |
126 std::unique_ptr<base::Value> HandleSupportCancelWatch( | 123 std::unique_ptr<base::Value> HandleMojoWatcherCancel( |
127 const base::DictionaryValue* args); | 124 const base::DictionaryValue* args); |
128 | 125 |
129 // Provides interfaces. | 126 // Provides interfaces. |
130 service_manager::mojom::InterfaceProvider* interface_provider_; | 127 service_manager::mojom::InterfaceProvider* interface_provider_; |
131 // Runs JavaScript on WebUI page. | 128 // Runs JavaScript on WebUI page. |
132 base::WeakNSProtocol<id<CRWJSInjectionEvaluator>> script_evaluator_; | 129 base::WeakNSProtocol<id<CRWJSInjectionEvaluator>> script_evaluator_; |
133 // Id of the last created watch. | 130 // Id of the last created watch. |
134 int last_watch_id_; | 131 int last_watch_id_; |
135 // Currently active watches created through this facade. | 132 // Currently active watches created through this facade. |
136 std::map<int, std::unique_ptr<mojo::SimpleWatcher>> watchers_; | 133 std::map<int, std::unique_ptr<mojo::SimpleWatcher>> watchers_; |
137 }; | 134 }; |
138 | 135 |
139 } // web | 136 } // web |
140 | 137 |
141 #endif // IOS_WEB_WEBUI_MOJO_FACADE_H_ | 138 #endif // IOS_WEB_WEBUI_MOJO_FACADE_H_ |
OLD | NEW |