summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/wake_event_page.h
blob: 48bfd4bedb4c80246f786d7f3ead5ed1fa8b543a (plain)
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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 <string>

#include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "content/public/renderer/render_process_observer.h"
#include "ipc/ipc_sync_message_filter.h"
#include "v8/include/v8.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 : public content::RenderProcessObserver {
 public:
  WakeEventPage();
  ~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);

  // 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::Get().GetForContext(context)(function() {
  //   ...
  // });
  //
  // Thread safe.
  v8::Local<v8::Function> GetForContext(ScriptContext* context);

 private:
  class WakeEventPageNativeHandler;

  // The response from an ExtensionHostMsg_WakeEvent call, passed true if the
  // call was successful, false on failure.
  using OnResponseCallback = base::Callback<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::Callback<void(const std::string&, const OnResponseCallback&)>;

  // For |requests_|.
  struct RequestData {
    RequestData(int thread_id, const 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(const OnResponseCallback& on_response,
                                      bool success);

  // Sends the ExtensionHostMsg_WakeEvent IPC for |extension_id|, and
  // updates |requests_| bookkeeping.
  void MakeRequest(const std::string& extension_id,
                   const OnResponseCallback& on_response);

  // content::RenderProcessObserver:
  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::ScopedPtrHashMap<int, scoped_ptr<RequestData>> requests_;

  // Lock for |requests_|.
  base::Lock requests_lock_;

  DISALLOW_COPY_AND_ASSIGN(WakeEventPage);
};

}  //  namespace extensions

#endif  // EXTENSIONS_RENDERER_WAKE_EVENT_PAGE_H_