1
1
#include " tracing_agent.h"
2
+ #include " main_thread_interface.h"
2
3
#include " node_internals.h"
3
4
4
5
#include " env-inl.h"
@@ -14,10 +15,76 @@ namespace protocol {
14
15
namespace {
15
16
using v8::platform::tracing::TraceWriter;
16
17
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
+
17
83
class InspectorTraceWriter : public node ::tracing::AsyncTraceWriter {
18
84
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) {}
21
88
22
89
void AppendTraceEvent (
23
90
v8::platform::tracing::TraceObject* trace_event) override {
@@ -35,27 +102,35 @@ class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
35
102
std::ostringstream::ate);
36
103
result << stream_.str ();
37
104
result << " }" ;
38
- frontend_->sendRawNotification (result.str ());
105
+ main_thread_->Post (std::make_unique<SendMessageRequest>(frontend_object_id_,
106
+ result.str ()));
39
107
stream_.str (" " );
40
108
}
41
109
42
110
private:
43
111
std::unique_ptr<TraceWriter> json_writer_;
44
112
std::ostringstream stream_;
45
- NodeTracing::Frontend* frontend_;
113
+ int frontend_object_id_;
114
+ std::shared_ptr<MainThreadHandle> main_thread_;
46
115
};
47
116
} // namespace
48
117
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) { }
52
121
53
122
TracingAgent::~TracingAgent () {
54
123
trace_writer_.reset ();
124
+ main_thread_->Post (
125
+ std::make_unique<DestroyFrontendWrapperRequest>(frontend_object_id_));
55
126
}
56
127
57
128
void 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_));
59
134
NodeTracing::Dispatcher::wire (dispatcher, this );
60
135
}
61
136
@@ -81,11 +156,11 @@ DispatchResponse TracingAgent::start(
81
156
82
157
tracing::AgentWriterHandle* writer = GetTracingAgentWriter ();
83
158
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 );
89
164
}
90
165
return DispatchResponse::OK ();
91
166
}
0 commit comments