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: 2020-02-19 22:14:06 Branches: 1 2 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 "inspector_agent.h"
9
#include "node_mutex.h"
10
11
#include <atomic>
12
#include <deque>
13
#include <memory>
14
#include <unordered_map>
15
16
namespace v8_inspector {
17
class StringBuffer;
18
class StringView;
19
}  // namespace v8_inspector
20
21
namespace node {
22
namespace inspector {
23
class MainThreadInterface;
24
25
9936
class Request {
26
 public:
27
  virtual void Call(MainThreadInterface*) = 0;
28
9939
  virtual ~Request() = default;
29
};
30
31
4702
class Deletable {
32
 public:
33
4297
  virtual ~Deletable() = default;
34
};
35
36
std::unique_ptr<v8_inspector::StringBuffer> Utf8ToStringView(
37
    const std::string& message);
38
39
using MessageQueue = std::deque<std::unique_ptr<Request>>;
40
41
class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> {
42
 public:
43
4348
  explicit MainThreadHandle(MainThreadInterface* main_thread)
44
4348
                            : main_thread_(main_thread) {
45
4348
  }
46
7887
  ~MainThreadHandle() {
47
7887
    Mutex::ScopedLock scoped_lock(block_lock_);
48
3944
    CHECK_NULL(main_thread_);  // main_thread_ should have called Reset
49
3944
  }
50
  std::unique_ptr<InspectorSession> Connect(
51
      std::unique_ptr<InspectorSessionDelegate> delegate,
52
      bool prevent_shutdown);
53
4702
  int newObjectId() {
54
4702
    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 std::enable_shared_from_this<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
489
  Agent* inspector_agent() {
84
489
    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_