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-02-26 22:23:30 Branches: 10 14 71.4 %

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