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-02-13 22:28:58 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
9804
class Request {
27
 public:
28
  virtual void Call(MainThreadInterface*) = 0;
29
9801
  virtual ~Request() {}
30
};
31
32
4639
class Deletable {
33
 public:
34
4629
  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
4415
  explicit MainThreadHandle(MainThreadInterface* main_thread)
45
4415
                            : main_thread_(main_thread) {
46
4415
  }
47
8094
  ~MainThreadHandle() {
48
4047
    Mutex::ScopedLock scoped_lock(block_lock_);
49
4047
    CHECK_NULL(main_thread_);  // main_thread_ should have called Reset
50
4047
  }
51
  std::unique_ptr<InspectorSession> Connect(
52
      std::unique_ptr<InspectorSessionDelegate> delegate,
53
      bool prevent_shutdown);
54
4639
  int newObjectId() {
55
4639
    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
323
  Agent* inspector_agent() {
84
323
    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
  using AsyncAndInterface = std::pair<uv_async_t, MainThreadInterface*>;
93
94
  static void DispatchMessagesAsyncCallback(uv_async_t* async);
95
  static void CloseAsync(AsyncAndInterface*);
96
97
  MessageQueue requests_;
98
  Mutex requests_lock_;   // requests_ live across threads
99
  // This queue is to maintain the order of the messages for the cases
100
  // when we reenter the DispatchMessages function.
101
  MessageQueue dispatching_message_queue_;
102
  bool dispatching_messages_ = false;
103
  ConditionVariable incoming_message_cond_;
104
  // Used from any thread
105
  Agent* const agent_;
106
  v8::Isolate* const isolate_;
107
  v8::Platform* const platform_;
108
  DeleteFnPtr<AsyncAndInterface, CloseAsync> main_thread_request_;
109
  std::shared_ptr<MainThreadHandle> handle_;
110
  std::unordered_map<int, std::unique_ptr<Deletable>> managed_objects_;
111
};
112
113
}  // namespace inspector
114
}  // namespace node
115
#endif  // SRC_INSPECTOR_MAIN_THREAD_INTERFACE_H_