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-05-05 22:32:45 Branches: 10 14 71.4 %

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