11#include " tracing_agent.h"
2+ #include " main_thread_interface.h"
23#include " node_internals.h"
34
45#include " env-inl.h"
@@ -14,10 +15,76 @@ namespace protocol {
1415namespace {
1516using v8::platform::tracing::TraceWriter;
1617
18+ class DeletableFrontendWrapper : public Deletable {
19+ public:
20+ explicit DeletableFrontendWrapper (
21+ std::weak_ptr<NodeTracing::Frontend> frontend)
22+ : frontend_(frontend) {}
23+
24+ // This should only be called from the main thread, meaning frontend should
25+ // not be destroyed concurrently.
26+ NodeTracing::Frontend* get () { return frontend_.lock ().get (); }
27+
28+ private:
29+ std::weak_ptr<NodeTracing::Frontend> frontend_;
30+ };
31+
32+ class CreateFrontendWrapperRequest : public Request {
33+ public:
34+ CreateFrontendWrapperRequest (int object_id,
35+ std::weak_ptr<NodeTracing::Frontend> frontend)
36+ : object_id_(object_id) {
37+ frontend_wrapper_ = std::make_unique<DeletableFrontendWrapper>(frontend);
38+ }
39+
40+ void Call (MainThreadInterface* thread) override {
41+ thread->AddObject (object_id_, std::move (frontend_wrapper_));
42+ }
43+
44+ private:
45+ int object_id_;
46+ std::unique_ptr<DeletableFrontendWrapper> frontend_wrapper_;
47+ };
48+
49+ class DestroyFrontendWrapperRequest : public Request {
50+ public:
51+ explicit DestroyFrontendWrapperRequest (int object_id)
52+ : object_id_(object_id) {}
53+
54+ void Call (MainThreadInterface* thread) override {
55+ thread->RemoveObject (object_id_);
56+ }
57+
58+ private:
59+ int object_id_;
60+ };
61+
62+ class SendMessageRequest : public Request {
63+ public:
64+ explicit SendMessageRequest (int object_id, const std::string& message)
65+ : object_id_(object_id), message_(message) {}
66+
67+ void Call (MainThreadInterface* thread) override {
68+ DeletableFrontendWrapper* frontend_wrapper =
69+ static_cast <DeletableFrontendWrapper*>(
70+ thread->GetObjectIfExists (object_id_));
71+ if (frontend_wrapper == nullptr ) return ;
72+ auto frontend = frontend_wrapper->get ();
73+ if (frontend != nullptr ) {
74+ frontend->sendRawNotification (message_);
75+ }
76+ }
77+
78+ private:
79+ int object_id_;
80+ std::string message_;
81+ };
82+
1783class InspectorTraceWriter : public node ::tracing::AsyncTraceWriter {
1884 public:
19- explicit InspectorTraceWriter (NodeTracing::Frontend* frontend)
20- : frontend_(frontend) {}
85+ explicit InspectorTraceWriter (int frontend_object_id,
86+ std::shared_ptr<MainThreadHandle> main_thread)
87+ : frontend_object_id_(frontend_object_id), main_thread_(main_thread) {}
2188
2289 void AppendTraceEvent (
2390 v8::platform::tracing::TraceObject* trace_event) override {
@@ -35,27 +102,35 @@ class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
35102 std::ostringstream::ate);
36103 result << stream_.str ();
37104 result << " }" ;
38- frontend_->sendRawNotification (result.str ());
105+ main_thread_->Post (std::make_unique<SendMessageRequest>(frontend_object_id_,
106+ result.str ()));
39107 stream_.str (" " );
40108 }
41109
42110 private:
43111 std::unique_ptr<TraceWriter> json_writer_;
44112 std::ostringstream stream_;
45- NodeTracing::Frontend* frontend_;
113+ int frontend_object_id_;
114+ std::shared_ptr<MainThreadHandle> main_thread_;
46115};
47116} // namespace
48117
49- TracingAgent::TracingAgent (Environment* env)
50- : env_(env) {
51- }
118+ TracingAgent::TracingAgent (Environment* env,
119+ std::shared_ptr<MainThreadHandle> main_thread)
120+ : env_(env), main_thread_(main_thread) { }
52121
53122TracingAgent::~TracingAgent () {
54123 trace_writer_.reset ();
124+ main_thread_->Post (
125+ std::make_unique<DestroyFrontendWrapperRequest>(frontend_object_id_));
55126}
56127
57128void TracingAgent::Wire (UberDispatcher* dispatcher) {
58- frontend_.reset (new NodeTracing::Frontend (dispatcher->channel ()));
129+ // Note that frontend is still owned by TracingAgent
130+ frontend_ = std::make_shared<NodeTracing::Frontend>(dispatcher->channel ());
131+ frontend_object_id_ = main_thread_->newObjectId ();
132+ main_thread_->Post (std::make_unique<CreateFrontendWrapperRequest>(
133+ frontend_object_id_, frontend_));
59134 NodeTracing::Dispatcher::wire (dispatcher, this );
60135}
61136
@@ -81,11 +156,11 @@ DispatchResponse TracingAgent::start(
81156
82157 tracing::AgentWriterHandle* writer = GetTracingAgentWriter ();
83158 if (writer != nullptr ) {
84- trace_writer_ = writer-> agent ()-> AddClient (
85- categories_set,
86- std::unique_ptr <InspectorTraceWriter>(
87- new InspectorTraceWriter (frontend_. get ()) ),
88- tracing::Agent::kIgnoreDefaultCategories );
159+ trace_writer_ =
160+ writer-> agent ()-> AddClient ( categories_set,
161+ std::make_unique <InspectorTraceWriter>(
162+ frontend_object_id_, main_thread_ ),
163+ tracing::Agent::kIgnoreDefaultCategories );
89164 }
90165 return DispatchResponse::OK ();
91166}
0 commit comments