|
12 | 12 | #include "libplatform/libplatform.h"
|
13 | 13 |
|
14 | 14 | #include <string.h>
|
| 15 | +#include <unordered_map> |
15 | 16 | #include <vector>
|
16 | 17 |
|
17 | 18 | #ifdef __POSIX__
|
@@ -39,6 +40,7 @@ using v8::Value;
|
39 | 40 | using v8_inspector::StringBuffer;
|
40 | 41 | using v8_inspector::StringView;
|
41 | 42 | using v8_inspector::V8Inspector;
|
| 43 | +using v8_inspector::V8InspectorClient; |
42 | 44 |
|
43 | 45 | static uv_sem_t start_io_thread_semaphore;
|
44 | 46 | static uv_async_t start_io_thread_async;
|
@@ -429,9 +431,66 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
|
429 | 431 | std::unique_ptr<v8_inspector::V8InspectorSession> session_;
|
430 | 432 | };
|
431 | 433 |
|
| 434 | +class InspectorTimer { |
| 435 | + public: |
| 436 | + InspectorTimer(uv_loop_t* loop, |
| 437 | + double interval_s, |
| 438 | + V8InspectorClient::TimerCallback callback, |
| 439 | + void* data) : timer_(), |
| 440 | + callback_(callback), |
| 441 | + data_(data) { |
| 442 | + uv_timer_init(loop, &timer_); |
| 443 | + int64_t interval_ms = 1000 * interval_s; |
| 444 | + uv_timer_start(&timer_, OnTimer, interval_ms, interval_ms); |
| 445 | + } |
| 446 | + |
| 447 | + InspectorTimer(const InspectorTimer&) = delete; |
| 448 | + |
| 449 | + void Stop() { |
| 450 | + uv_timer_stop(&timer_); |
| 451 | + uv_close(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb); |
| 452 | + } |
| 453 | + |
| 454 | + private: |
| 455 | + static void OnTimer(uv_timer_t* uvtimer) { |
| 456 | + InspectorTimer* timer = node::ContainerOf(&InspectorTimer::timer_, uvtimer); |
| 457 | + timer->callback_(timer->data_); |
| 458 | + } |
| 459 | + |
| 460 | + static void TimerClosedCb(uv_handle_t* uvtimer) { |
| 461 | + InspectorTimer* timer = |
| 462 | + node::ContainerOf(&InspectorTimer::timer_, |
| 463 | + reinterpret_cast<uv_timer_t*>(uvtimer)); |
| 464 | + delete timer; |
| 465 | + } |
| 466 | + |
| 467 | + ~InspectorTimer() {} |
| 468 | + |
| 469 | + uv_timer_t timer_; |
| 470 | + V8InspectorClient::TimerCallback callback_; |
| 471 | + void* data_; |
| 472 | +}; |
| 473 | + |
| 474 | +class InspectorTimerHandle { |
| 475 | + public: |
| 476 | + InspectorTimerHandle(uv_loop_t* loop, double interval_s, |
| 477 | + V8InspectorClient::TimerCallback callback, void* data) { |
| 478 | + timer_ = new InspectorTimer(loop, interval_s, callback, data); |
| 479 | + } |
| 480 | + |
| 481 | + InspectorTimerHandle(const InspectorTimerHandle&) = delete; |
| 482 | + |
| 483 | + ~InspectorTimerHandle() { |
| 484 | + CHECK_NE(timer_, nullptr); |
| 485 | + timer_->Stop(); |
| 486 | + timer_ = nullptr; |
| 487 | + } |
| 488 | + private: |
| 489 | + InspectorTimer* timer_; |
| 490 | +}; |
432 | 491 | } // namespace
|
433 | 492 |
|
434 |
| -class NodeInspectorClient : public v8_inspector::V8InspectorClient { |
| 493 | +class NodeInspectorClient : public V8InspectorClient { |
435 | 494 | public:
|
436 | 495 | NodeInspectorClient(node::Environment* env,
|
437 | 496 | v8::Platform* platform) : env_(env),
|
@@ -527,13 +586,26 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
|
527 | 586 | return channel_.get();
|
528 | 587 | }
|
529 | 588 |
|
| 589 | + void startRepeatingTimer(double interval_s, |
| 590 | + TimerCallback callback, |
| 591 | + void* data) override { |
| 592 | + timers_.emplace(std::piecewise_construct, std::make_tuple(data), |
| 593 | + std::make_tuple(env_->event_loop(), interval_s, callback, |
| 594 | + data)); |
| 595 | + } |
| 596 | + |
| 597 | + void cancelTimer(void* data) override { |
| 598 | + timers_.erase(data); |
| 599 | + } |
| 600 | + |
530 | 601 | private:
|
531 | 602 | node::Environment* env_;
|
532 | 603 | v8::Platform* platform_;
|
533 | 604 | bool terminated_;
|
534 | 605 | bool running_nested_loop_;
|
535 | 606 | std::unique_ptr<V8Inspector> client_;
|
536 | 607 | std::unique_ptr<ChannelImpl> channel_;
|
| 608 | + std::unordered_map<void*, InspectorTimerHandle> timers_; |
537 | 609 | };
|
538 | 610 |
|
539 | 611 | Agent::Agent(Environment* env) : parent_env_(env),
|
|
0 commit comments