summaryrefslogtreecommitdiff
path: root/src/inspector/worker_inspector.cc
diff options
context:
space:
mode:
authorEugene Ostroukhov <eostroukhov@google.com>2018-09-08 19:45:10 -0700
committerEugene Ostroukhov <eostroukhov@google.com>2018-09-18 09:01:33 -0700
commitf28c6f7eef58e7c3133bb2cd457d05b986194ba3 (patch)
treeb8e8583ff735a3b0721831af51c4f92d967849f1 /src/inspector/worker_inspector.cc
parentba0b4e43e442926bfb9389a42aa7393f91e6748a (diff)
downloadandroid-node-v8-f28c6f7eef58e7c3133bb2cd457d05b986194ba3.tar.gz
android-node-v8-f28c6f7eef58e7c3133bb2cd457d05b986194ba3.tar.bz2
android-node-v8-f28c6f7eef58e7c3133bb2cd457d05b986194ba3.zip
inspector: workers debugging
Introduce a NodeTarget inspector domain modelled after ChromeDevTools Target domain. It notifies inspector frontend attached to a main V8 isolate when workers are starting and allows passing messages to inspectors on their isolates. All inspector functionality is enabled on worker isolates. PR-URL: https://github.com/nodejs/node/pull/21364 Reviewed-By: Aleksei Koziatinskii <ak239spb@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/inspector/worker_inspector.cc')
-rw-r--r--src/inspector/worker_inspector.cc128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/inspector/worker_inspector.cc b/src/inspector/worker_inspector.cc
new file mode 100644
index 0000000000..52e71a562d
--- /dev/null
+++ b/src/inspector/worker_inspector.cc
@@ -0,0 +1,128 @@
+#include "worker_inspector.h"
+
+#include "main_thread_interface.h"
+
+namespace node {
+namespace inspector {
+namespace {
+
+class WorkerStartedRequest : public Request {
+ public:
+ WorkerStartedRequest(
+ int id,
+ const std::string& url,
+ std::shared_ptr<node::inspector::MainThreadHandle> worker_thread,
+ bool waiting)
+ : id_(id),
+ info_(BuildWorkerTitle(id), url, worker_thread),
+ waiting_(waiting) {}
+ void Call(MainThreadInterface* thread) override {
+ auto manager = thread->inspector_agent()->GetWorkerManager();
+ manager->WorkerStarted(id_, info_, waiting_);
+ }
+
+ private:
+ static std::string BuildWorkerTitle(int id) {
+ return "Worker " + std::to_string(id);
+ }
+
+ int id_;
+ WorkerInfo info_;
+ bool waiting_;
+};
+
+
+void Report(const std::unique_ptr<WorkerDelegate>& delegate,
+ const WorkerInfo& info, bool waiting) {
+ if (info.worker_thread)
+ delegate->WorkerCreated(info.title, info.url, waiting, info.worker_thread);
+}
+
+class WorkerFinishedRequest : public Request {
+ public:
+ explicit WorkerFinishedRequest(int worker_id) : worker_id_(worker_id) {}
+
+ void Call(MainThreadInterface* thread) override {
+ thread->inspector_agent()->GetWorkerManager()->WorkerFinished(worker_id_);
+ }
+
+ private:
+ int worker_id_;
+};
+} // namespace
+
+
+ParentInspectorHandle::ParentInspectorHandle(
+ int id, const std::string& url,
+ std::shared_ptr<MainThreadHandle> parent_thread, bool wait_for_connect)
+ : id_(id), url_(url), parent_thread_(parent_thread),
+ wait_(wait_for_connect) {}
+
+ParentInspectorHandle::~ParentInspectorHandle() {
+ parent_thread_->Post(
+ std::unique_ptr<Request>(new WorkerFinishedRequest(id_)));
+}
+
+void ParentInspectorHandle::WorkerStarted(
+ std::shared_ptr<MainThreadHandle> worker_thread, bool waiting) {
+ std::unique_ptr<Request> request(
+ new WorkerStartedRequest(id_, url_, worker_thread, waiting));
+ parent_thread_->Post(std::move(request));
+}
+
+void WorkerManager::WorkerFinished(int session_id) {
+ children_.erase(session_id);
+}
+
+void WorkerManager::WorkerStarted(int session_id,
+ const WorkerInfo& info,
+ bool waiting) {
+ if (info.worker_thread->Expired())
+ return;
+ children_.emplace(session_id, info);
+ for (const auto& delegate : delegates_) {
+ Report(delegate.second, info, waiting);
+ }
+}
+
+std::unique_ptr<ParentInspectorHandle>
+WorkerManager::NewParentHandle(int thread_id, const std::string& url) {
+ bool wait = !delegates_waiting_on_start_.empty();
+ return std::unique_ptr<ParentInspectorHandle>(
+ new ParentInspectorHandle(thread_id, url, thread_, wait));
+}
+
+void WorkerManager::RemoveAttachDelegate(int id) {
+ delegates_.erase(id);
+ delegates_waiting_on_start_.erase(id);
+}
+
+std::unique_ptr<WorkerManagerEventHandle> WorkerManager::SetAutoAttach(
+ std::unique_ptr<WorkerDelegate> attach_delegate) {
+ int id = ++next_delegate_id_;
+ delegates_[id] = std::move(attach_delegate);
+ const auto& delegate = delegates_[id];
+ for (const auto& worker : children_) {
+ // Waiting is only reported when a worker is started, same as browser
+ Report(delegate, worker.second, false);
+ }
+ return std::unique_ptr<WorkerManagerEventHandle>(
+ new WorkerManagerEventHandle(shared_from_this(), id));
+}
+
+void WorkerManager::SetWaitOnStartForDelegate(int id, bool wait) {
+ if (wait)
+ delegates_waiting_on_start_.insert(id);
+ else
+ delegates_waiting_on_start_.erase(id);
+}
+
+void WorkerManagerEventHandle::SetWaitOnStart(bool wait_on_start) {
+ manager_->SetWaitOnStartForDelegate(id_, wait_on_start);
+}
+
+WorkerManagerEventHandle::~WorkerManagerEventHandle() {
+ manager_->RemoveAttachDelegate(id_);
+}
+} // namespace inspector
+} // namespace node