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