GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/inspector/main_thread_interface.h Lines: 14 14 100.0 %
Date: 2019-01-07 12:15:22 Branches: 3 6 50.0 %

Line Branch Exec Source
1
#ifndef SRC_INSPECTOR_MAIN_THREAD_INTERFACE_H_
2
#define SRC_INSPECTOR_MAIN_THREAD_INTERFACE_H_
3
4
#if !HAVE_INSPECTOR
5
#error("This header can only be used when inspector is enabled")
6
#endif
7
8
#include "env.h"
9
#include "inspector_agent.h"
10
#include "node_mutex.h"
11
12
#include <atomic>
13
#include <deque>
14
#include <memory>
15
#include <unordered_map>
16
17
namespace v8_inspector {
18
class StringBuffer;
19
class StringView;
20
}  // namespace v8_inspector
21
22
namespace node {
23
namespace inspector {
24
class MainThreadInterface;
25
26
583
class Request {
27
 public:
28
  virtual void Call(MainThreadInterface*) = 0;
29
583
  virtual ~Request() {}
30
};
31
32
22
class Deletable {
33
 public:
34
22
  virtual ~Deletable() {}
35
};
36
37
std::unique_ptr<v8_inspector::StringBuffer> Utf8ToStringView(
38
    const std::string& message);
39
40
using MessageQueue = std::deque<std::unique_ptr<Request>>;
41
42
class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> {
43
 public:
44
289
  explicit MainThreadHandle(MainThreadInterface* main_thread)
45
289
                            : main_thread_(main_thread) {
46
289
  }
47
498
  ~MainThreadHandle() {
48
249
    CHECK_NULL(main_thread_);  // main_thread_ should have called Reset
49
249
  }
50
  std::unique_ptr<InspectorSession> Connect(
51
      std::unique_ptr<InspectorSessionDelegate> delegate,
52
      bool prevent_shutdown);
53
22
  int newObjectId() {
54
22
    return ++next_object_id_;
55
  }
56
  bool Post(std::unique_ptr<Request> request);
57
  std::unique_ptr<InspectorSessionDelegate> MakeDelegateThreadSafe(
58
      std::unique_ptr<InspectorSessionDelegate> delegate);
59
  bool Expired();
60
61
 private:
62
  void Reset();
63
64
  MainThreadInterface* main_thread_;
65
  Mutex block_lock_;
66
  int next_session_id_ = 0;
67
  std::atomic_int next_object_id_ = {1};
68
69
  friend class MainThreadInterface;
70
};
71
72
class MainThreadInterface {
73
 public:
74
  MainThreadInterface(Agent* agent, uv_loop_t*, v8::Isolate* isolate,
75
                      v8::Platform* platform);
76
  ~MainThreadInterface();
77
78
  void DispatchMessages();
79
  void Post(std::unique_ptr<Request> request);
80
  bool WaitForFrontendEvent();
81
  std::shared_ptr<MainThreadHandle> GetHandle();
82
255
  Agent* inspector_agent() {
83
255
    return agent_;
84
  }
85
  void AddObject(int handle, std::unique_ptr<Deletable> object);
86
  Deletable* GetObject(int id);
87
  void RemoveObject(int handle);
88
89
 private:
90
  using AsyncAndInterface = std::pair<uv_async_t, MainThreadInterface*>;
91
92
  static void DispatchMessagesAsyncCallback(uv_async_t* async);
93
  static void CloseAsync(AsyncAndInterface*);
94
95
  MessageQueue requests_;
96
  Mutex requests_lock_;   // requests_ live across threads
97
  // This queue is to maintain the order of the messages for the cases
98
  // when we reenter the DispatchMessages function.
99
  MessageQueue dispatching_message_queue_;
100
  bool dispatching_messages_ = false;
101
  ConditionVariable incoming_message_cond_;
102
  // Used from any thread
103
  Agent* const agent_;
104
  v8::Isolate* const isolate_;
105
  v8::Platform* const platform_;
106
  DeleteFnPtr<AsyncAndInterface, CloseAsync> main_thread_request_;
107
  std::shared_ptr<MainThreadHandle> handle_;
108
  std::unordered_map<int, std::unique_ptr<Deletable>> managed_objects_;
109
};
110
111
}  // namespace inspector
112
}  // namespace node
113
#endif  // SRC_INSPECTOR_MAIN_THREAD_INTERFACE_H_