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_agent.cc Lines: 72 73 98.6 %
Date: 2019-02-23 22:23:05 Branches: 11 22 50.0 %

Line Branch Exec Source
1
#include "worker_agent.h"
2
3
#include "main_thread_interface.h"
4
#include "worker_inspector.h"
5
#include "util-inl.h"
6
7
namespace node {
8
namespace inspector {
9
namespace protocol {
10
11
4238
class NodeWorkers
12
    : public std::enable_shared_from_this<NodeWorkers> {
13
 public:
14
4607
  explicit NodeWorkers(std::weak_ptr<NodeWorker::Frontend> frontend,
15
                      std::shared_ptr<MainThreadHandle> thread)
16
4607
                      : frontend_(frontend), thread_(thread) {}
17
  void WorkerCreated(const std::string& title,
18
                     const std::string& url,
19
                     bool waiting,
20
                     std::shared_ptr<MainThreadHandle> target);
21
  void Receive(const std::string& id, const std::string& message);
22
  void Send(const std::string& id, const std::string& message);
23
  void Detached(const std::string& id);
24
25
 private:
26
  std::weak_ptr<NodeWorker::Frontend> frontend_;
27
  std::shared_ptr<MainThreadHandle> thread_;
28
  std::unordered_map<std::string, std::unique_ptr<InspectorSession>> sessions_;
29
  int next_target_id_ = 0;
30
};
31
32
namespace {
33
6
class AgentWorkerInspectorDelegate : public WorkerDelegate {
34
 public:
35
3
  explicit AgentWorkerInspectorDelegate(std::shared_ptr<NodeWorkers> workers)
36
3
                                        : workers_(workers) {}
37
38
4
  void WorkerCreated(const std::string& title,
39
                     const std::string& url,
40
                     bool waiting,
41
                     std::shared_ptr<MainThreadHandle> target) override {
42
4
    workers_->WorkerCreated(title, url, waiting, target);
43
4
  }
44
45
 private:
46
  std::shared_ptr<NodeWorkers> workers_;
47
};
48
49
class ParentInspectorSessionDelegate : public InspectorSessionDelegate {
50
 public:
51
4
  ParentInspectorSessionDelegate(const std::string& id,
52
                                 std::shared_ptr<NodeWorkers> workers)
53
4
                                 : id_(id), workers_(workers) {}
54
55
12
  ~ParentInspectorSessionDelegate() override {
56
4
    workers_->Detached(id_);
57
8
  }
58
59
92
  void SendMessageToFrontend(const v8_inspector::StringView& msg) override {
60
92
    std::string message = protocol::StringUtil::StringViewToUtf8(msg);
61
92
    workers_->Send(id_, message);
62
92
  }
63
64
 private:
65
  std::string id_;
66
  std::shared_ptr<NodeWorkers> workers_;
67
};
68
69
4
std::unique_ptr<NodeWorker::WorkerInfo> WorkerInfo(const std::string& id,
70
                                                   const std::string& title,
71
                                                   const std::string& url) {
72
  return NodeWorker::WorkerInfo::create()
73
8
      .setWorkerId(id)
74
4
      .setTitle(title)
75
4
      .setUrl(url)
76
12
      .setType("worker").build();
77
}
78
}  // namespace
79
80
4607
WorkerAgent::WorkerAgent(std::weak_ptr<WorkerManager> manager)
81
4607
                         : manager_(manager) {}
82
83
84
4607
void WorkerAgent::Wire(UberDispatcher* dispatcher) {
85
4607
  frontend_.reset(new NodeWorker::Frontend(dispatcher->channel()));
86
4607
  NodeWorker::Dispatcher::wire(dispatcher, this);
87
4607
  auto manager = manager_.lock();
88
4607
  CHECK_NOT_NULL(manager);
89
9214
  workers_ =
90
9214
      std::make_shared<NodeWorkers>(frontend_, manager->MainThread());
91
4607
}
92
93
6
DispatchResponse WorkerAgent::sendMessageToWorker(const String& message,
94
                                                  const String& sessionId) {
95
6
  workers_->Receive(sessionId, message);
96
6
  return DispatchResponse::OK();
97
}
98
99
3
DispatchResponse WorkerAgent::enable(bool waitForDebuggerOnStart) {
100
3
  auto manager = manager_.lock();
101
3
  if (!manager) {
102
    return DispatchResponse::OK();
103
  }
104
3
  if (!event_handle_) {
105
    std::unique_ptr<AgentWorkerInspectorDelegate> delegate(
106
3
            new AgentWorkerInspectorDelegate(workers_));
107
3
    event_handle_ = manager->SetAutoAttach(std::move(delegate));
108
  }
109
3
  event_handle_->SetWaitOnStart(waitForDebuggerOnStart);
110
3
  return DispatchResponse::OK();
111
}
112
113
4240
DispatchResponse WorkerAgent::disable() {
114
4240
  event_handle_.reset();
115
4240
  return DispatchResponse::OK();
116
}
117
118
4
void NodeWorkers::WorkerCreated(const std::string& title,
119
                                const std::string& url,
120
                                bool waiting,
121
                                std::shared_ptr<MainThreadHandle> target) {
122
4
  auto frontend = frontend_.lock();
123
4
  if (!frontend)
124
4
    return;
125
8
  std::string id = std::to_string(++next_target_id_);
126
  auto delegate = thread_->MakeDelegateThreadSafe(
127
      std::unique_ptr<InspectorSessionDelegate>(
128
8
          new ParentInspectorSessionDelegate(id, shared_from_this())));
129
4
  sessions_[id] = target->Connect(std::move(delegate), true);
130
8
  frontend->attachedToWorker(id, WorkerInfo(id, title, url), waiting);
131
}
132
133
92
void NodeWorkers::Send(const std::string& id, const std::string& message) {
134
92
  auto frontend = frontend_.lock();
135
92
  if (frontend)
136
92
    frontend->receivedMessageFromWorker(id, message);
137
92
}
138
139
6
void NodeWorkers::Receive(const std::string& id, const std::string& message) {
140
6
  auto it = sessions_.find(id);
141
6
  if (it != sessions_.end())
142
6
    it->second->Dispatch(Utf8ToStringView(message)->string());
143
6
}
144
145
4
void NodeWorkers::Detached(const std::string& id) {
146
4
  if (sessions_.erase(id) == 0)
147
4
    return;
148
4
  auto frontend = frontend_.lock();
149
4
  if (frontend) {
150
4
    frontend->detachedFromWorker(id);
151
4
  }
152
}
153
}  // namespace protocol
154
}  // namespace inspector
155
}  // namespace node