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.cc Lines: 66 67 98.5 %
Date: 2021-06-06 04:11:53 Branches: 8 10 80.0 %

Line Branch Exec Source
1
#include "worker_inspector.h"
2
#include "main_thread_interface.h"
3
#include "util-inl.h"
4
5
#include <memory>
6
7
namespace node {
8
namespace inspector {
9
namespace {
10
11
828
class WorkerStartedRequest : public Request {
12
 public:
13
414
  WorkerStartedRequest(
14
      uint64_t id,
15
      const std::string& url,
16
      std::shared_ptr<node::inspector::MainThreadHandle> worker_thread,
17
      bool waiting)
18
414
      : id_(id),
19
828
        info_(BuildWorkerTitle(id), url, worker_thread),
20
1241
        waiting_(waiting) {}
21
414
  void Call(MainThreadInterface* thread) override {
22
828
    auto manager = thread->inspector_agent()->GetWorkerManager();
23
414
    manager->WorkerStarted(id_, info_, waiting_);
24
414
  }
25
26
 private:
27
414
  static std::string BuildWorkerTitle(int id) {
28
414
    return "Worker " + std::to_string(id);
29
  }
30
31
  uint64_t id_;
32
  WorkerInfo info_;
33
  bool waiting_;
34
};
35
36
37
13
void Report(const std::unique_ptr<WorkerDelegate>& delegate,
38
            const WorkerInfo& info, bool waiting) {
39
13
  if (info.worker_thread)
40
13
    delegate->WorkerCreated(info.title, info.url, waiting, info.worker_thread);
41
13
}
42
43
1268
class WorkerFinishedRequest : public Request {
44
 public:
45
656
  explicit WorkerFinishedRequest(uint64_t worker_id) : worker_id_(worker_id) {}
46
47
634
  void Call(MainThreadInterface* thread) override {
48
634
    thread->inspector_agent()->GetWorkerManager()->WorkerFinished(worker_id_);
49
634
  }
50
51
 private:
52
  uint64_t worker_id_;
53
};
54
}  // namespace
55
56
656
ParentInspectorHandle::ParentInspectorHandle(
57
    uint64_t id,
58
    const std::string& url,
59
    std::shared_ptr<MainThreadHandle> parent_thread,
60
656
    bool wait_for_connect)
61
    : id_(id),
62
      url_(url),
63
      parent_thread_(parent_thread),
64
656
      wait_(wait_for_connect) {}
65
66
1312
ParentInspectorHandle::~ParentInspectorHandle() {
67
656
  parent_thread_->Post(
68
1312
      std::unique_ptr<Request>(new WorkerFinishedRequest(id_)));
69
656
}
70
71
413
void ParentInspectorHandle::WorkerStarted(
72
    std::shared_ptr<MainThreadHandle> worker_thread, bool waiting) {
73
  std::unique_ptr<Request> request(
74
827
      new WorkerStartedRequest(id_, url_, worker_thread, waiting));
75
414
  parent_thread_->Post(std::move(request));
76
414
}
77
78
2
std::unique_ptr<inspector::InspectorSession> ParentInspectorHandle::Connect(
79
    std::unique_ptr<inspector::InspectorSessionDelegate> delegate,
80
    bool prevent_shutdown) {
81
2
  return parent_thread_->Connect(std::move(delegate), prevent_shutdown);
82
}
83
84
634
void WorkerManager::WorkerFinished(uint64_t session_id) {
85
634
  children_.erase(session_id);
86
634
}
87
88
414
void WorkerManager::WorkerStarted(uint64_t session_id,
89
                                  const WorkerInfo& info,
90
                                  bool waiting) {
91
414
  if (info.worker_thread->Expired())
92
    return;
93
414
  children_.emplace(session_id, info);
94
421
  for (const auto& delegate : delegates_) {
95
7
    Report(delegate.second, info, waiting);
96
  }
97
}
98
99
646
std::unique_ptr<ParentInspectorHandle> WorkerManager::NewParentHandle(
100
    uint64_t thread_id, const std::string& url) {
101
646
  bool wait = !delegates_waiting_on_start_.empty();
102
646
  return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait);
103
}
104
105
8
void WorkerManager::RemoveAttachDelegate(int id) {
106
8
  delegates_.erase(id);
107
8
  delegates_waiting_on_start_.erase(id);
108
8
}
109
110
8
std::unique_ptr<WorkerManagerEventHandle> WorkerManager::SetAutoAttach(
111
    std::unique_ptr<WorkerDelegate> attach_delegate) {
112
8
  int id = ++next_delegate_id_;
113
8
  delegates_[id] = std::move(attach_delegate);
114
8
  const auto& delegate = delegates_[id];
115
14
  for (const auto& worker : children_) {
116
    // Waiting is only reported when a worker is started, same as browser
117
6
    Report(delegate, worker.second, false);
118
  }
119
8
  return std::make_unique<WorkerManagerEventHandle>(shared_from_this(), id);
120
}
121
122
9
void WorkerManager::SetWaitOnStartForDelegate(int id, bool wait) {
123
9
  if (wait)
124
5
    delegates_waiting_on_start_.insert(id);
125
  else
126
4
    delegates_waiting_on_start_.erase(id);
127
9
}
128
129
9
void WorkerManagerEventHandle::SetWaitOnStart(bool wait_on_start) {
130
9
    manager_->SetWaitOnStartForDelegate(id_, wait_on_start);
131
9
}
132
133
16
WorkerManagerEventHandle::~WorkerManagerEventHandle() {
134
8
  manager_->RemoveAttachDelegate(id_);
135
8
}
136
}  // namespace inspector
137
}  // namespace node