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: 61 61 100.0 %
Date: 2019-07-27 22:37:30 Branches: 10 14 71.4 %

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