kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 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 | |
| 5 | #ifndef EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_ |
| 6 | #define EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_ |
| 7 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 8 | #include <memory> |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 9 | #include <string> |
avi | 7368a3e | 2016-12-29 01:26:28 | [diff] [blame] | 10 | #include <unordered_map> |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 11 | |
| 12 | #include "base/callback.h" |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/ref_counted.h" |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
kalman | 6f984ae | 2015-09-18 17:21:58 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
tyoshino | 832a58a | 2016-04-18 08:14:08 | [diff] [blame] | 17 | #include "content/public/renderer/render_thread_observer.h" |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 18 | #include "ipc/ipc_sync_message_filter.h" |
| 19 | #include "v8/include/v8.h" |
| 20 | |
| 21 | namespace content { |
| 22 | class RenderThread; |
| 23 | } |
| 24 | |
| 25 | namespace extensions { |
| 26 | class ScriptContext; |
| 27 | |
| 28 | // This class implements the wake-event-page JavaScript function, which wakes |
| 29 | // an event page and runs a callback when done. |
| 30 | // |
| 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. |
tyoshino | 832a58a | 2016-04-18 08:14:08 | [diff] [blame] | 33 | class WakeEventPage : public content::RenderThreadObserver { |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 34 | public: |
| 35 | WakeEventPage(); |
| 36 | ~WakeEventPage() override; |
| 37 | |
| 38 | // Returns the single instance of the WakeEventPage object. |
| 39 | // |
| 40 | // Thread safe. |
| 41 | static WakeEventPage* Get(); |
| 42 | |
| 43 | // Initializes the WakeEventPage. |
| 44 | // |
| 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); |
| 48 | |
| 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. |
| 51 | // |
| 52 | // To mix C++ and JavaScript, example usage might be: |
| 53 | // |
| 54 | // WakeEventPage::Get().GetForContext(context)(function() { |
| 55 | // ... |
| 56 | // }); |
| 57 | // |
| 58 | // Thread safe. |
| 59 | v8::Local<v8::Function> GetForContext(ScriptContext* context); |
| 60 | |
| 61 | private: |
| 62 | class WakeEventPageNativeHandler; |
| 63 | |
| 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)>; |
| 67 | |
| 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&)>; |
| 72 | |
| 73 | // For |requests_|. |
| 74 | struct RequestData { |
kalman | 6f984ae | 2015-09-18 17:21:58 | [diff] [blame] | 75 | RequestData(int thread_id, const OnResponseCallback& on_response); |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 76 | ~RequestData(); |
kalman | 6f984ae | 2015-09-18 17:21:58 | [diff] [blame] | 77 | |
| 78 | // The thread ID the request was made on. |on_response| must be called on |
| 79 | // that thread. |
| 80 | int thread_id; |
| 81 | |
| 82 | // Callback to run when the response to the request arrives. |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 83 | OnResponseCallback on_response; |
| 84 | }; |
| 85 | |
| 86 | // Runs |on_response|, passing it |success|. |
| 87 | static void RunOnResponseWithResult(const OnResponseCallback& on_response, |
| 88 | bool success); |
| 89 | |
| 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); |
| 94 | |
tyoshino | 832a58a | 2016-04-18 08:14:08 | [diff] [blame] | 95 | // content::RenderThreadObserver: |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 96 | bool OnControlMessageReceived(const IPC::Message& message) override; |
| 97 | |
| 98 | // OnControlMessageReceived handlers: |
| 99 | void OnWakeEventPageResponse(int request_id, bool success); |
| 100 | |
| 101 | // IPC sender. Belongs to the render thread, but thread safe. |
| 102 | scoped_refptr<IPC::SyncMessageFilter> message_filter_; |
| 103 | |
kalman | 6f984ae | 2015-09-18 17:21:58 | [diff] [blame] | 104 | // All in-flight requests, keyed by request ID. Used on multiple threads, so |
| 105 | // must be guarded by |requests_lock_|. |
avi | 7368a3e | 2016-12-29 01:26:28 | [diff] [blame] | 106 | std::unordered_map<int, std::unique_ptr<RequestData>> requests_; |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 107 | |
kalman | 6f984ae | 2015-09-18 17:21:58 | [diff] [blame] | 108 | // Lock for |requests_|. |
| 109 | base::Lock requests_lock_; |
kalman | 12033f12 | 2015-08-21 19:30:13 | [diff] [blame] | 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(WakeEventPage); |
| 112 | }; |
| 113 | |
| 114 | } // namespace extensions |
| 115 | |
| 116 | #endif // EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_ |