wake_event_page.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 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_WAKE_EVENT_PAGE_H_
  5. #define EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "base/callback.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/synchronization/lock.h"
  13. #include "content/public/renderer/render_thread_observer.h"
  14. #include "ipc/ipc_sync_message_filter.h"
  15. #include "v8/include/v8-forward.h"
  16. namespace content {
  17. class RenderThread;
  18. }
  19. namespace extensions {
  20. class ScriptContext;
  21. // This class implements the wake-event-page JavaScript function, which wakes
  22. // an event page and runs a callback when done.
  23. //
  24. // Note, the function will do a round trip to the browser even if event page is
  25. // open. Any optimisation to prevent this must be at the JavaScript level.
  26. class WakeEventPage : public content::RenderThreadObserver {
  27. public:
  28. WakeEventPage();
  29. WakeEventPage(const WakeEventPage&) = delete;
  30. WakeEventPage& operator=(const WakeEventPage&) = delete;
  31. ~WakeEventPage() override;
  32. // Returns the single instance of the WakeEventPage object.
  33. //
  34. // Thread safe.
  35. static WakeEventPage* Get();
  36. // Initializes the WakeEventPage.
  37. //
  38. // This must be called before any bindings are installed, and must be called
  39. // on the render thread.
  40. void Init(content::RenderThread* render_thread);
  41. // Returns the wake-event-page function bound to a given context. The
  42. // function will be cached as a hidden value in the context's global object.
  43. //
  44. // To mix C++ and JavaScript, example usage might be:
  45. //
  46. // WakeEventPage::Get().GetForContext(context)(function() {
  47. // ...
  48. // });
  49. //
  50. // Thread safe.
  51. v8::Local<v8::Function> GetForContext(ScriptContext* context);
  52. private:
  53. class WakeEventPageNativeHandler;
  54. // The response from an ExtensionHostMsg_WakeEvent call, passed true if the
  55. // call was successful, false on failure.
  56. using OnResponseCallback = base::OnceCallback<void(bool)>;
  57. // Makes an ExtensionHostMsg_WakeEvent request for an extension ID. The
  58. // second argument is a callback to run when the request has completed.
  59. using MakeRequestCallback =
  60. base::RepeatingCallback<void(const std::string&, OnResponseCallback)>;
  61. // For |requests_|.
  62. struct RequestData {
  63. RequestData(int thread_id, OnResponseCallback on_response);
  64. ~RequestData();
  65. // The thread ID the request was made on. |on_response| must be called on
  66. // that thread.
  67. int thread_id;
  68. // Callback to run when the response to the request arrives.
  69. OnResponseCallback on_response;
  70. };
  71. // Runs |on_response|, passing it |success|.
  72. static void RunOnResponseWithResult(OnResponseCallback on_response,
  73. bool success);
  74. // Sends the ExtensionHostMsg_WakeEvent IPC for |extension_id|, and
  75. // updates |requests_| bookkeeping.
  76. void MakeRequest(const std::string& extension_id,
  77. OnResponseCallback on_response);
  78. // content::RenderThreadObserver:
  79. bool OnControlMessageReceived(const IPC::Message& message) override;
  80. // OnControlMessageReceived handlers:
  81. void OnWakeEventPageResponse(int request_id, bool success);
  82. // IPC sender. Belongs to the render thread, but thread safe.
  83. scoped_refptr<IPC::SyncMessageFilter> message_filter_;
  84. // All in-flight requests, keyed by request ID. Used on multiple threads, so
  85. // must be guarded by |requests_lock_|.
  86. std::unordered_map<int, std::unique_ptr<RequestData>> requests_;
  87. // Lock for |requests_|.
  88. base::Lock requests_lock_;
  89. };
  90. } // namespace extensions
  91. #endif // EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_