GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector/main_thread_interface.h Lines: 15 15 100.0 %
Date: 2019-10-05 22:32:21 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
11178
class Request {
27
 public:
28
  virtual void Call(MainThreadInterface*) = 0;
29
11180
  virtual ~Request() = default;
30
};
31
32
5384
class Deletable {
33
 public:
34
4944
  virtual ~Deletable() = default;
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
5093
  explicit MainThreadHandle(MainThreadInterface* main_thread)
45
5093
                            : main_thread_(main_thread) {
46
5093
  }
47
9306
  ~MainThreadHandle() {
48
4653
    Mutex::ScopedLock scoped_lock(block_lock_);
49
4653
    CHECK_NULL(main_thread_);  // main_thread_ should have called Reset
50
4653
  }
51
  std::unique_ptr<InspectorSession> Connect(
52
      std::unique_ptr<InspectorSessionDelegate> delegate,
53
      bool prevent_shutdown);
54
5384
  int newObjectId() {
55
5384
    return ++next_object_id_;
56
  }
57
  bool Post(std::unique_ptr<Request> request);
58
  std::unique_ptr<InspectorSessionDelegate> MakeDelegateThreadSafe(
59
      std::unique_ptr<InspectorSessionDelegate> delegate);
60
  bool Expired();
61
62
 private:
63
  void Reset();
64
65
  MainThreadInterface* main_thread_;
66
  Mutex block_lock_;
67
  int next_session_id_ = 0;
68
  std::atomic_int next_object_id_ = {1};
69
70
  friend class MainThreadInterface;
71
};
72
73
class MainThreadInterface {
74
 public:
75
  MainThreadInterface(Agent* agent, uv_loop_t*, v8::Isolate* isolate,
76
                      v8::Platform* platform);
77
  ~MainThreadInterface();
78
79
  void DispatchMessages();
80
  void Post(std::unique_ptr<Request> request);
81
  bool WaitForFrontendEvent();
82
  std::shared_ptr<MainThreadHandle> GetHandle();
83
451
  Agent* inspector_agent() {
84
451
    return agent_;
85
  }
86
  void AddObject(int handle, std::unique_ptr<Deletable> object);
87
  Deletable* GetObject(int id);
88
  Deletable* GetObjectIfExists(int id);
89
  void RemoveObject(int handle);
90
91
 private:
92
  MessageQueue requests_;
93
  Mutex requests_lock_;   // requests_ live across threads
94
  // This queue is to maintain the order of the messages for the cases
95
  // when we reenter the DispatchMessages function.
96
  MessageQueue dispatching_message_queue_;
97
  bool dispatching_messages_ = false;
98
  ConditionVariable incoming_message_cond_;
99
  // Used from any thread
100
  Agent* const agent_;
101
  v8::Isolate* const isolate_;
102
  v8::Platform* const platform_;
103
  std::shared_ptr<MainThreadHandle> handle_;
104
  std::unordered_map<int, std::unique_ptr<Deletable>> managed_objects_;
105
};
106
107
}  // namespace inspector
108
}  // namespace node
109
#endif  // SRC_INSPECTOR_MAIN_THREAD_INTERFACE_H_