| 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 IOS_WEB_WEBUI_MOJO_FACADE_H_ |
| 6 #define IOS_WEB_WEBUI_MOJO_FACADE_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #import "base/ios/weak_nsobject.h" |
| 12 #import "mojo/public/cpp/system/watcher.h" |
| 13 |
| 14 @protocol CRWJSInjectionEvaluator; |
| 15 |
| 16 namespace base { |
| 17 class DictionaryValue; |
| 18 class Value; |
| 19 } // base |
| 20 |
| 21 namespace shell { |
| 22 namespace mojom { |
| 23 class InterfaceProvider; |
| 24 } // mojom |
| 25 } // shell |
| 26 |
| 27 namespace web { |
| 28 |
| 29 // Facade class for Mojo. All inputs and outputs are optimized for communication |
| 30 // with WebUI pages and hence use JSON format. Must be created used and |
| 31 // destroyed on UI thread. |
| 32 class MojoFacade { |
| 33 public: |
| 34 // Constructs MojoFacade. The calling code must retain the ownership of |
| 35 // |interface_provider| and |script_evaluator|, both can not be null. |
| 36 MojoFacade(shell::mojom::InterfaceProvider* interface_provider, |
| 37 id<CRWJSInjectionEvaluator> script_evaluator); |
| 38 ~MojoFacade(); |
| 39 |
| 40 // Handles Mojo message received from WebUI page. Returns a valid JSON string |
| 41 // on success or empty string if supplied JSON does not have required |
| 42 // structure. Every message must have "name" and "args" keys, where "name" is |
| 43 // a string representing the name of Mojo message and "args" is a dictionary |
| 44 // with arguments specific for each message name. |
| 45 // Supported message names with their handler methods in parenthesis: |
| 46 // service_provider.connectToService (HandleServiceProviderConnectToService) |
| 47 // core.close (HandleCoreClose) |
| 48 // core.createMessagePipe (HandleCoreCreateMessagePipe) |
| 49 // core.writeMessage (HandleCoreWriteMessage) |
| 50 // core.readMessage (HandleCoreReadMessage) |
| 51 // support.watch (HandleSupportWatch) |
| 52 // support.cancelWatch (HandleSupportCancelWatch) |
| 53 std::string HandleMojoMessage(const std::string& mojo_message_as_json); |
| 54 |
| 55 private: |
| 56 // Extracts message name and arguments from the given JSON string obtained |
| 57 // from WebUI page. This method either succeeds or crashes the app (this |
| 58 // matches other platforms where Mojo API is strict on malformed input). |
| 59 void GetMessageNameAndArguments( |
| 60 const std::string& mojo_message_as_json, |
| 61 std::string* out_name, |
| 62 std::unique_ptr<base::DictionaryValue>* out_args); |
| 63 |
| 64 // Connects to specified Mojo interface. |args| is a dictionary which must |
| 65 // contain "serviceName" key, which is a string representing a service name. |
| 66 // Returns MojoHandle as a number. |
| 67 std::unique_ptr<base::Value> HandleServiceProviderConnectToService( |
| 68 const base::DictionaryValue* args); |
| 69 |
| 70 // Closes the given handle. |args| is a dictionary which must contain "handle" |
| 71 // key, which is a number representing a MojoHandle. |
| 72 // Returns MojoResult as a number. |
| 73 std::unique_ptr<base::Value> HandleCoreClose( |
| 74 const base::DictionaryValue* args); |
| 75 |
| 76 // Creates a Mojo message pipe. |args| is a dictionary which must contain |
| 77 // "optionsDict" key. optionsDict is a dictionary with the following keys: |
| 78 // - "struct_size" (a number representing the size of this struct; used to |
| 79 // allow for future extensions); |
| 80 // - "flags" (a number representing MojoCreateMessagePipeOptionsFlags; used |
| 81 // to specify different modes of operation); |
| 82 // Returns a dictionary with "handle0" and "handle1" keys (the numbers |
| 83 // representing two ports for the message pipe). |
| 84 std::unique_ptr<base::Value> HandleCoreCreateMessagePipe( |
| 85 base::DictionaryValue* args); |
| 86 |
| 87 // Writes a message to the message pipe endpoint given by handle. |args| is a |
| 88 // dictionary which must contain the following keys: |
| 89 // - "handle" (a number representing MojoHandle, the endpoint to write to); |
| 90 // - "buffer" (a dictionary representing the message data; may be empty); |
| 91 // - "handles" (an array representing any handles to attach; handles are |
| 92 // transferred on success and will no longer be valid; may be empty); |
| 93 // - "flags" (a number representing MojoWriteMessageFlags); |
| 94 // Returns MojoResult as a number. |
| 95 std::unique_ptr<base::Value> HandleCoreWriteMessage( |
| 96 base::DictionaryValue* args); |
| 97 |
| 98 // Reads a message from the message pipe endpoint given by handle. |args| is |
| 99 // a dictionary which must contain the following keys: |
| 100 // - "handle" (a number representing MojoHandle, the endpoint to read from); |
| 101 // - "flags" (a number representing MojoWriteMessageFlags); |
| 102 // Returns a dictionary with the following keys: |
| 103 // - "result" (a number representing MojoResult); |
| 104 // - "buffer" (an array representing message data; non-empty only on |
| 105 // success); |
| 106 // - "handles" (an array representing MojoHandles transferred, if any); |
| 107 std::unique_ptr<base::Value> HandleCoreReadMessage( |
| 108 const base::DictionaryValue* args); |
| 109 |
| 110 // Begins watching a handle for signals to be satisfied or unsatisfiable. |
| 111 // |args| is a dictionary which must contain the following keys: |
| 112 // - "handle" (a number representing a MojoHandle), "signals" (a number |
| 113 // representing MojoHandleSignals to watch); |
| 114 // - "callbackId" (a number representing the id which should be passed to |
| 115 // __crWeb.mojo.mojoWatchSignal call); |
| 116 // Returns watch id as a number. |
| 117 std::unique_ptr<base::Value> HandleSupportWatch( |
| 118 const base::DictionaryValue* args); |
| 119 |
| 120 // Cancels a handle watch initiated by "support.watch". |args| is a dictionary |
| 121 // which must contain "watchId" key (a number representing id returned from |
| 122 // "support.watch"). |
| 123 // Returns null. |
| 124 std::unique_ptr<base::Value> HandleSupportCancelWatch( |
| 125 const base::DictionaryValue* args); |
| 126 |
| 127 // Provides service interfaces. |
| 128 shell::mojom::InterfaceProvider* interface_provider_; |
| 129 // Runs JavaScript on WebUI page. |
| 130 base::WeakNSProtocol<id<CRWJSInjectionEvaluator>> script_evaluator_; |
| 131 // Id of the last created watch. |
| 132 int last_watch_id_; |
| 133 // Currently active watches created through this facade. |
| 134 std::map<int, mojo::Watcher> watchers_; |
| 135 }; |
| 136 |
| 137 } // web |
| 138 |
| 139 #endif // IOS_WEB_WEBUI_MOJO_FACADE_H_ |
| OLD | NEW |