mojo_facade.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef IOS_WEB_WEBUI_MOJO_FACADE_H_
  5. #define IOS_WEB_WEBUI_MOJO_FACADE_H_
  6. #include <objc/objc.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/callback.h"
  11. #include "base/values.h"
  12. #include "mojo/public/cpp/system/message_pipe.h"
  13. #include "mojo/public/cpp/system/simple_watcher.h"
  14. namespace web {
  15. class WebState;
  16. // Facade class for Mojo. All inputs and outputs are optimized for communication
  17. // with WebUI pages and hence use JSON format. Must be created used and
  18. // destroyed on UI thread.
  19. class MojoFacade {
  20. public:
  21. // Constructs MojoFacade. The calling code must retain ownership of
  22. // |web_state|, which cannot be null.
  23. explicit MojoFacade(WebState* web_state);
  24. ~MojoFacade();
  25. // Handles Mojo message received from WebUI page. Returns a valid JSON string
  26. // on success or empty string if supplied JSON does not have required
  27. // structure. Every message must have "name" and "args" keys, where "name" is
  28. // a string representing the name of Mojo message and "args" is a dictionary
  29. // with arguments specific for each message name.
  30. // Supported message names with their handler methods in parenthesis:
  31. // Mojo.bindInterface (HandleMojoBindInterface)
  32. // MojoHandle.close (HandleMojoHandleClose)
  33. // Mojo.createMessagePipe (HandleMojoCreateMessagePipe)
  34. // MojoHandle.writeMessage (HandleMojoHandleWriteMessage)
  35. // MojoHandle.readMessage (HandleMojoHandleReadMessage)
  36. // MojoHandle.watch (HandleMojoHandleWatch)
  37. // MojoWatcher.cancel (HandleMojoWatcherCancel)
  38. std::string HandleMojoMessage(const std::string& mojo_message_as_json);
  39. private:
  40. // Value returned by GetMessageNameAndArguments.
  41. struct MessageNameAndArguments {
  42. std::string name;
  43. base::Value args;
  44. };
  45. // Extracts message name and arguments from the given JSON string obtained
  46. // from WebUI page. This method either succeeds or crashes the app (this
  47. // matches other platforms where Mojo API is strict on malformed input).
  48. MessageNameAndArguments GetMessageNameAndArguments(
  49. const std::string& mojo_message_as_json);
  50. // Connects to specified Mojo interface. |args| is a dictionary with the
  51. // following keys:
  52. // - "interfaceName" (a string representing an interface name);
  53. // - "requestHandle" (a number representing MojoHandle of the interface
  54. // request).
  55. void HandleMojoBindInterface(base::Value args);
  56. // Closes the given handle. |args| is a dictionary which must contain "handle"
  57. // key, which is a number representing a MojoHandle.
  58. void HandleMojoHandleClose(base::Value args);
  59. // Creates a Mojo message pipe. |args| is unused.
  60. // Returns a dictionary with the following keys:
  61. // - "result" (a number representing MojoResult);
  62. // - "handle0" and "handle1" (the numbers representing two endpoints of the
  63. // message pipe).
  64. base::Value HandleMojoCreateMessagePipe(base::Value args);
  65. // Writes a message to the message pipe endpoint given by handle. |args| is a
  66. // dictionary which must contain the following keys:
  67. // - "handle" (a number representing MojoHandle, the endpoint to write to);
  68. // - "buffer" (a dictionary representing the message data; may be empty);
  69. // - "handles" (an array representing any handles to attach; handles are
  70. // transferred and will no longer be valid; may be empty);
  71. // Returns MojoResult as a number.
  72. base::Value HandleMojoHandleWriteMessage(base::Value args);
  73. // Reads a message from the message pipe endpoint given by handle. |args| is
  74. // a dictionary which must contain the keys "handle" (a number representing
  75. // MojoHandle, the endpoint to read from).
  76. // Returns a dictionary with the following keys:
  77. // - "result" (a number representing MojoResult);
  78. // - "buffer" (an array representing message data; non-empty only on
  79. // success);
  80. // - "handles" (an array representing MojoHandles received, if any);
  81. base::Value HandleMojoHandleReadMessage(base::Value args);
  82. // Begins watching a handle for signals to be satisfied or unsatisfiable.
  83. // |args| is a dictionary which must contain the following keys:
  84. // - "handle" (a number representing a MojoHandle);
  85. // - "signals" (a number representing MojoHandleSignals to watch);
  86. // - "callbackId" (a number representing the id which should be passed to
  87. // Mojo.internal.signalWatch call).
  88. // Returns watch id as a number.
  89. base::Value HandleMojoHandleWatch(base::Value args);
  90. // Cancels a handle watch initiated by "MojoHandle.watch". |args| is a
  91. // dictionary which must contain "watchId" key (a number representing id
  92. // returned from "MojoHandle.watch").
  93. void HandleMojoWatcherCancel(base::Value args);
  94. // Runs JavaScript on WebUI page.
  95. WebState* web_state_ = nil;
  96. // Id of the last created watch.
  97. int last_watch_id_ = 0;
  98. // Currently active watches created through this facade.
  99. std::map<int, std::unique_ptr<mojo::SimpleWatcher>> watchers_;
  100. };
  101. } // web
  102. #endif // IOS_WEB_WEBUI_MOJO_FACADE_H_