post_message_receiver.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 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 PDF_POST_MESSAGE_RECEIVER_H_
  5. #define PDF_POST_MESSAGE_RECEIVER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/values.h"
  11. #include "gin/interceptor.h"
  12. #include "gin/public/wrapper_info.h"
  13. #include "gin/wrappable.h"
  14. #include "v8/include/v8.h"
  15. namespace base {
  16. class SequencedTaskRunner;
  17. } // namespace base
  18. namespace gin {
  19. class ObjectTemplateBuilder;
  20. } // namespace gin
  21. namespace chrome_pdf {
  22. class V8ValueConverter;
  23. // Implements the `postMessage()` API exposed to the plugin embedder. The
  24. // received messages are converted and forwarded to the `Client`.
  25. // `PostMessageReceiver`'s lifetime is managed by the V8 garbage collector,
  26. // meaning it can outlive the `Client`. Messages are dropped if the `Client` is
  27. // destroyed.
  28. class PostMessageReceiver final : public gin::Wrappable<PostMessageReceiver>,
  29. public gin::NamedPropertyInterceptor {
  30. public:
  31. // The interface for a plugin client that handles messages from its embedder.
  32. class Client {
  33. public:
  34. // Handles converted messages from the embedder.
  35. virtual void OnMessage(const base::Value::Dict& message) = 0;
  36. protected:
  37. Client() = default;
  38. ~Client() = default;
  39. };
  40. static gin::WrapperInfo kWrapperInfo;
  41. // Creates a scriptable object with an implemented `postMessage()` method.
  42. // Messages are posted asynchronously to `client` using `client_task_runner`.
  43. static v8::Local<v8::Object> Create(
  44. v8::Isolate* isolate,
  45. base::WeakPtr<V8ValueConverter> v8_value_converter,
  46. base::WeakPtr<Client> client,
  47. scoped_refptr<base::SequencedTaskRunner> client_task_runner);
  48. PostMessageReceiver(const PostMessageReceiver&) = delete;
  49. PostMessageReceiver& operator=(const PostMessageReceiver&) = delete;
  50. protected:
  51. ~PostMessageReceiver() override;
  52. private:
  53. PostMessageReceiver(
  54. v8::Isolate* isolate,
  55. base::WeakPtr<V8ValueConverter> v8_value_converter,
  56. base::WeakPtr<Client> client,
  57. scoped_refptr<base::SequencedTaskRunner> client_task_runner);
  58. // gin::Wrappable:
  59. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  60. v8::Isolate* isolate) override;
  61. const char* GetTypeName() override;
  62. // gin::NamedPropertyInterceptor:
  63. v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
  64. const std::string& property) override;
  65. std::vector<std::string> EnumerateNamedProperties(
  66. v8::Isolate* isolate) override;
  67. // Lazily creates and retrieves `function_template_`.
  68. v8::Local<v8::FunctionTemplate> GetFunctionTemplate();
  69. // Implements the `postMessage()` method called by the embedder.
  70. void PostMessage(v8::Local<v8::Value> message);
  71. base::WeakPtr<V8ValueConverter> v8_value_converter_;
  72. v8::Persistent<v8::FunctionTemplate> function_template_;
  73. raw_ptr<v8::Isolate> isolate_;
  74. base::WeakPtr<Client> client_;
  75. scoped_refptr<base::SequencedTaskRunner> client_task_runner_;
  76. base::WeakPtrFactory<PostMessageReceiver> weak_factory_{this};
  77. };
  78. } // namespace chrome_pdf
  79. #endif // PDF_POST_MESSAGE_RECEIVER_H_