GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector/worker_inspector.h Lines: 17 17 100.0 %
Date: 2019-09-15 22:29:17 Branches: 1 2 50.0 %

Line Branch Exec Source
1
#ifndef SRC_INSPECTOR_WORKER_INSPECTOR_H_
2
#define SRC_INSPECTOR_WORKER_INSPECTOR_H_
3
4
#if !HAVE_INSPECTOR
5
#error("This header can only be used when inspector is enabled")
6
#endif
7
8
#include <memory>
9
#include <string>
10
#include <unordered_map>
11
#include <unordered_set>
12
13
namespace node {
14
namespace inspector {
15
class MainThreadHandle;
16
class WorkerManager;
17
18
6
class WorkerDelegate {
19
 public:
20
  virtual void WorkerCreated(const std::string& title,
21
                             const std::string& url,
22
                             bool waiting,
23
                             std::shared_ptr<MainThreadHandle> worker) = 0;
24
6
  virtual ~WorkerDelegate() = default;
25
};
26
27
class WorkerManagerEventHandle {
28
 public:
29
6
  explicit WorkerManagerEventHandle(std::shared_ptr<WorkerManager> manager,
30
                                    int id)
31
6
                                    : manager_(manager), id_(id) {}
32
  void SetWaitOnStart(bool wait_on_start);
33
  ~WorkerManagerEventHandle();
34
35
 private:
36
  std::shared_ptr<WorkerManager> manager_;
37
  int id_;
38
};
39
40
627
struct WorkerInfo {
41
209
  WorkerInfo(const std::string& target_title,
42
             const std::string& target_url,
43
             std::shared_ptr<MainThreadHandle> worker_thread)
44
             : title(target_title),
45
               url(target_url),
46
209
               worker_thread(worker_thread) {}
47
  std::string title;
48
  std::string url;
49
  std::shared_ptr<MainThreadHandle> worker_thread;
50
};
51
52
class ParentInspectorHandle {
53
 public:
54
  ParentInspectorHandle(int id, const std::string& url,
55
                        std::shared_ptr<MainThreadHandle> parent_thread,
56
                        bool wait_for_connect);
57
  ~ParentInspectorHandle();
58
3
  std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
59
      int thread_id, const std::string& url) {
60
    return std::make_unique<ParentInspectorHandle>(thread_id,
61
                                                   url,
62
                                                   parent_thread_,
63
3
                                                   wait_);
64
  }
65
  void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread,
66
                     bool waiting);
67
209
  bool WaitForConnect() {
68
209
    return wait_;
69
  }
70
208
  const std::string& url() const { return url_; }
71
72
 private:
73
  int id_;
74
  std::string url_;
75
  std::shared_ptr<MainThreadHandle> parent_thread_;
76
  bool wait_;
77
};
78
79
4564
class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
80
 public:
81
4969
  explicit WorkerManager(std::shared_ptr<MainThreadHandle> thread)
82
4969
                         : thread_(thread) {}
83
84
  std::unique_ptr<ParentInspectorHandle> NewParentHandle(
85
      int thread_id, const std::string& url);
86
  void WorkerStarted(int session_id, const WorkerInfo& info, bool waiting);
87
  void WorkerFinished(int session_id);
88
  std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach(
89
      std::unique_ptr<WorkerDelegate> attach_delegate);
90
  void SetWaitOnStartForDelegate(int id, bool wait);
91
  void RemoveAttachDelegate(int id);
92
5210
  std::shared_ptr<MainThreadHandle> MainThread() {
93
5210
    return thread_;
94
  }
95
96
 private:
97
  std::shared_ptr<MainThreadHandle> thread_;
98
  std::unordered_map<int, WorkerInfo> children_;
99
  std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_;
100
  // If any one needs it, workers stop for all
101
  std::unordered_set<int> delegates_waiting_on_start_;
102
  int next_delegate_id_ = 0;
103
};
104
}  // namespace inspector
105
}  // namespace node
106
107
#endif  // SRC_INSPECTOR_WORKER_INSPECTOR_H_