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_
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"
25 namespace extensions
{
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
{
36 ~WakeEventPage() override
;
38 // Returns the single instance of the WakeEventPage object.
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() {
59 v8::Local
<v8::Function
> GetForContext(ScriptContext
* context
);
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
&)>;
75 RequestData(int thread_id
, const OnResponseCallback
& on_response
);
78 // The thread ID the request was made on. |on_response| must be called on
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
,
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_