Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / wake_event_page.h
blob48bfd4bedb4c80246f786d7f3ead5ed1fa8b543a
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.
5 #ifndef EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_
6 #define EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "content/public/renderer/render_process_observer.h"
18 #include "ipc/ipc_sync_message_filter.h"
19 #include "v8/include/v8.h"
21 namespace content {
22 class RenderThread;
25 namespace extensions {
26 class ScriptContext;
28 // This class implements the wake-event-page JavaScript function, which wakes
29 // an event page and runs a callback when done.
31 // Note, the function will do a round trip to the browser even if event page is
32 // open. Any optimisation to prevent this must be at the JavaScript level.
33 class WakeEventPage : public content::RenderProcessObserver {
34 public:
35 WakeEventPage();
36 ~WakeEventPage() override;
38 // Returns the single instance of the WakeEventPage object.
40 // Thread safe.
41 static WakeEventPage* Get();
43 // Initializes the WakeEventPage.
45 // This must be called before any bindings are installed, and must be called
46 // on the render thread.
47 void Init(content::RenderThread* render_thread);
49 // Returns the wake-event-page function bound to a given context. The
50 // function will be cached as a hidden value in the context's global object.
52 // To mix C++ and JavaScript, example usage might be:
54 // WakeEventPage::Get().GetForContext(context)(function() {
55 // ...
56 // });
58 // Thread safe.
59 v8::Local<v8::Function> GetForContext(ScriptContext* context);
61 private:
62 class WakeEventPageNativeHandler;
64 // The response from an ExtensionHostMsg_WakeEvent call, passed true if the
65 // call was successful, false on failure.
66 using OnResponseCallback = base::Callback<void(bool)>;
68 // Makes an ExtensionHostMsg_WakeEvent request for an extension ID. The
69 // second argument is a callback to run when the request has completed.
70 using MakeRequestCallback =
71 base::Callback<void(const std::string&, const OnResponseCallback&)>;
73 // For |requests_|.
74 struct RequestData {
75 RequestData(int thread_id, const OnResponseCallback& on_response);
76 ~RequestData();
78 // The thread ID the request was made on. |on_response| must be called on
79 // that thread.
80 int thread_id;
82 // Callback to run when the response to the request arrives.
83 OnResponseCallback on_response;
86 // Runs |on_response|, passing it |success|.
87 static void RunOnResponseWithResult(const OnResponseCallback& on_response,
88 bool success);
90 // Sends the ExtensionHostMsg_WakeEvent IPC for |extension_id|, and
91 // updates |requests_| bookkeeping.
92 void MakeRequest(const std::string& extension_id,
93 const OnResponseCallback& on_response);
95 // content::RenderProcessObserver:
96 bool OnControlMessageReceived(const IPC::Message& message) override;
98 // OnControlMessageReceived handlers:
99 void OnWakeEventPageResponse(int request_id, bool success);
101 // IPC sender. Belongs to the render thread, but thread safe.
102 scoped_refptr<IPC::SyncMessageFilter> message_filter_;
104 // All in-flight requests, keyed by request ID. Used on multiple threads, so
105 // must be guarded by |requests_lock_|.
106 base::ScopedPtrHashMap<int, scoped_ptr<RequestData>> requests_;
108 // Lock for |requests_|.
109 base::Lock requests_lock_;
111 DISALLOW_COPY_AND_ASSIGN(WakeEventPage);
114 } // namespace extensions
116 #endif // EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_