messaging_util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2017 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 EXTENSIONS_RENDERER_MESSAGING_UTIL_H_
  5. #define EXTENSIONS_RENDERER_MESSAGING_UTIL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "v8/include/v8-forward.h"
  10. namespace blink {
  11. class WebLocalFrame;
  12. }
  13. namespace extensions {
  14. enum class SerializationFormat;
  15. class ScriptContext;
  16. struct Message;
  17. namespace messaging_util {
  18. // The channel names for the sendMessage and sendRequest calls.
  19. extern const char kSendMessageChannel[];
  20. extern const char kSendRequestChannel[];
  21. // Messaging-related events.
  22. extern const char kOnMessageEvent[];
  23. extern const char kOnMessageExternalEvent[];
  24. extern const char kOnRequestEvent[];
  25. extern const char kOnRequestExternalEvent[];
  26. extern const char kOnConnectEvent[];
  27. extern const char kOnConnectExternalEvent[];
  28. extern const char kOnConnectNativeEvent[];
  29. extern const int kNoFrameId;
  30. // Parses the message from a v8 value, returning null on failure. On error,
  31. // will populate |error_out|.
  32. std::unique_ptr<Message> MessageFromV8(v8::Local<v8::Context> context,
  33. v8::Local<v8::Value> value,
  34. SerializationFormat format,
  35. std::string* error);
  36. // Converts a message to a v8 value. This is expected not to fail, since it
  37. // should only be used for messages that have been validated.
  38. v8::Local<v8::Value> MessageToV8(v8::Local<v8::Context> context,
  39. const Message& message);
  40. // Extracts an integer id from |value|, including accounting for -0 (which is a
  41. // valid integer, but is stored in V8 as a number). This will DCHECK that
  42. // |value| is either an int32 or -0.
  43. int ExtractIntegerId(v8::Local<v8::Value> value);
  44. // Returns the preferred serialization format for the given `context`. Note
  45. // extension native messaging clients shouldn't call this as they should always
  46. // use JSON.
  47. SerializationFormat GetSerializationFormat(const ScriptContext& context);
  48. // Flags for ParseMessageOptions().
  49. enum ParseOptionsFlags {
  50. NO_FLAGS = 0,
  51. PARSE_CHANNEL_NAME = 1,
  52. PARSE_FRAME_ID = 1 << 1,
  53. };
  54. struct MessageOptions {
  55. std::string channel_name;
  56. int frame_id = kNoFrameId;
  57. std::string document_id;
  58. };
  59. // Parses and returns the options parameter for sendMessage or connect.
  60. // |flags| specifies additional properties to look for on the object.
  61. MessageOptions ParseMessageOptions(v8::Local<v8::Context> context,
  62. v8::Local<v8::Object> v8_options,
  63. int flags);
  64. // Parses the target from |v8_target_id|, or uses the extension associated with
  65. // the |script_context| as a default. Returns true on success, and false on
  66. // failure. In the case of failure, will populate |error_out| with an error
  67. // based on the |method_name|.
  68. bool GetTargetExtensionId(ScriptContext* script_context,
  69. v8::Local<v8::Value> v8_target_id,
  70. const char* method_name,
  71. std::string* target_out,
  72. std::string* error_out);
  73. // Massages the sendMessage() or sendRequest() arguments into the expected
  74. // schema. These arguments are ambiguous (could match multiple signatures), so
  75. // we can't just rely on the normal signature parsing. Sets |arguments| to the
  76. // result if successful; otherwise leaves |arguments| untouched. (If the massage
  77. // is unsuccessful, our normal argument parsing code should throw a reasonable
  78. // error.
  79. void MassageSendMessageArguments(
  80. v8::Isolate* isolate,
  81. bool allow_options_argument,
  82. std::vector<v8::Local<v8::Value>>* arguments_out);
  83. // Returns true if the sendRequest-related properties are disabled for the given
  84. // |script_context|.
  85. bool IsSendRequestDisabled(ScriptContext* script_context);
  86. } // namespace messaging_util
  87. } // namespace extensions
  88. #endif // EXTENSIONS_RENDERER_MESSAGING_UTIL_H_