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