Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / extensions / renderer / wake_event_page.h
blob38ceda4f0cfcda6fb56d18354dd073b73e318373
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/lazy_instance.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.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 explicit RequestData(const OnResponseCallback& on_response);
76 ~RequestData();
77 OnResponseCallback on_response;
80 // Runs |on_response|, passing it |success|.
81 static void RunOnResponseWithResult(const OnResponseCallback& on_response,
82 bool success);
84 // Sends the ExtensionHostMsg_WakeEvent IPC for |extension_id|, and
85 // updates |requests_| bookkeeping.
86 void MakeRequest(const std::string& extension_id,
87 const OnResponseCallback& on_response);
89 // content::RenderProcessObserver:
90 bool OnControlMessageReceived(const IPC::Message& message) override;
92 // OnControlMessageReceived handlers:
93 void OnWakeEventPageResponse(int request_id, bool success);
95 // IPC sender. Belongs to the render thread, but thread safe.
96 scoped_refptr<IPC::SyncMessageFilter> message_filter_;
98 // All in-flight requests, keyed by request ID.
99 base::ScopedPtrHashMap<int, scoped_ptr<RequestData>> requests_;
101 base::WeakPtrFactory<WakeEventPage> weak_ptr_factory_;
103 DISALLOW_COPY_AND_ASSIGN(WakeEventPage);
106 } // namespace extensions
108 #endif // EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_