|
15 | 15 | #include "node_process.h" |
16 | 16 | #include "node_url.h" |
17 | 17 | #include "util-inl.h" |
| 18 | +#include "timer_wrap.h" |
18 | 19 | #include "v8-inspector.h" |
19 | 20 | #include "v8-platform.h" |
20 | 21 |
|
@@ -326,86 +327,6 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel, |
326 | 327 | bool retaining_context_; |
327 | 328 | }; |
328 | 329 |
|
329 | | -class InspectorTimer { |
330 | | - public: |
331 | | - InspectorTimer(Environment* env, |
332 | | - double interval_s, |
333 | | - V8InspectorClient::TimerCallback callback, |
334 | | - void* data) : env_(env), |
335 | | - callback_(callback), |
336 | | - data_(data) { |
337 | | - uv_timer_init(env->event_loop(), &timer_); |
338 | | - int64_t interval_ms = 1000 * interval_s; |
339 | | - uv_timer_start(&timer_, OnTimer, interval_ms, interval_ms); |
340 | | - timer_.data = this; |
341 | | - } |
342 | | - |
343 | | - InspectorTimer(const InspectorTimer&) = delete; |
344 | | - |
345 | | - void Stop() { |
346 | | - if (timer_.data == nullptr) return; |
347 | | - |
348 | | - timer_.data = nullptr; |
349 | | - uv_timer_stop(&timer_); |
350 | | - env_->CloseHandle(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb); |
351 | | - } |
352 | | - |
353 | | - inline Environment* env() const { return env_; } |
354 | | - |
355 | | - private: |
356 | | - static void OnTimer(uv_timer_t* uvtimer) { |
357 | | - InspectorTimer* timer = node::ContainerOf(&InspectorTimer::timer_, uvtimer); |
358 | | - timer->callback_(timer->data_); |
359 | | - } |
360 | | - |
361 | | - static void TimerClosedCb(uv_handle_t* uvtimer) { |
362 | | - std::unique_ptr<InspectorTimer> timer( |
363 | | - node::ContainerOf(&InspectorTimer::timer_, |
364 | | - reinterpret_cast<uv_timer_t*>(uvtimer))); |
365 | | - // Unique_ptr goes out of scope here and pointer is deleted. |
366 | | - } |
367 | | - |
368 | | - ~InspectorTimer() = default; |
369 | | - |
370 | | - Environment* env_; |
371 | | - uv_timer_t timer_; |
372 | | - V8InspectorClient::TimerCallback callback_; |
373 | | - void* data_; |
374 | | - |
375 | | - friend std::unique_ptr<InspectorTimer>::deleter_type; |
376 | | -}; |
377 | | - |
378 | | -class InspectorTimerHandle { |
379 | | - public: |
380 | | - InspectorTimerHandle(Environment* env, double interval_s, |
381 | | - V8InspectorClient::TimerCallback callback, void* data) { |
382 | | - timer_ = new InspectorTimer(env, interval_s, callback, data); |
383 | | - |
384 | | - env->AddCleanupHook(CleanupHook, this); |
385 | | - } |
386 | | - |
387 | | - InspectorTimerHandle(const InspectorTimerHandle&) = delete; |
388 | | - |
389 | | - ~InspectorTimerHandle() { |
390 | | - Stop(); |
391 | | - } |
392 | | - |
393 | | - private: |
394 | | - void Stop() { |
395 | | - if (timer_ != nullptr) { |
396 | | - timer_->env()->RemoveCleanupHook(CleanupHook, this); |
397 | | - timer_->Stop(); |
398 | | - } |
399 | | - timer_ = nullptr; |
400 | | - } |
401 | | - |
402 | | - static void CleanupHook(void* data) { |
403 | | - static_cast<InspectorTimerHandle*>(data)->Stop(); |
404 | | - } |
405 | | - |
406 | | - InspectorTimer* timer_; |
407 | | -}; |
408 | | - |
409 | 330 | class SameThreadInspectorSession : public InspectorSession { |
410 | 331 | public: |
411 | 332 | SameThreadInspectorSession( |
@@ -602,9 +523,12 @@ class NodeInspectorClient : public V8InspectorClient { |
602 | 523 | void startRepeatingTimer(double interval_s, |
603 | 524 | TimerCallback callback, |
604 | 525 | void* data) override { |
605 | | - timers_.emplace(std::piecewise_construct, std::make_tuple(data), |
606 | | - std::make_tuple(env_, interval_s, callback, |
607 | | - data)); |
| 526 | + auto result = |
| 527 | + timers_.emplace(std::piecewise_construct, std::make_tuple(data), |
| 528 | + std::make_tuple(env_, callback, data)); |
| 529 | + CHECK(result.second); |
| 530 | + uint64_t interval = 1000 * interval_s; |
| 531 | + result.first->second.Update(interval, interval); |
608 | 532 | } |
609 | 533 |
|
610 | 534 | void cancelTimer(void* data) override { |
@@ -724,7 +648,7 @@ class NodeInspectorClient : public V8InspectorClient { |
724 | 648 | bool running_nested_loop_ = false; |
725 | 649 | std::unique_ptr<V8Inspector> client_; |
726 | 650 | // Note: ~ChannelImpl may access timers_ so timers_ has to come first. |
727 | | - std::unordered_map<void*, InspectorTimerHandle> timers_; |
| 651 | + std::unordered_map<void*, TimerWrapHandle> timers_; |
728 | 652 | std::unordered_map<int, std::unique_ptr<ChannelImpl>> channels_; |
729 | 653 | int next_session_id_ = 1; |
730 | 654 | bool waiting_for_resume_ = false; |
|
0 commit comments