OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "test/inspector-protocol/inspector-impl.h" | |
6 | |
7 #include "include/v8.h" | |
8 #include "src/inspector/string-16.h" | |
9 | |
10 namespace { | |
11 | |
12 const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1; | |
13 | |
14 class ChannelImpl final : public v8_inspector::V8Inspector::Channel { | |
15 public: | |
16 explicit ChannelImpl(InspectorClientImpl::FrontendChannel* frontend_channel) | |
17 : frontend_channel_(frontend_channel) {} | |
18 virtual ~ChannelImpl() {} | |
19 | |
20 private: | |
21 void sendProtocolResponse(int callId, | |
22 const v8_inspector::StringView& message) override { | |
23 frontend_channel_->SendMessageToFrontend(message); | |
24 } | |
25 void sendProtocolNotification( | |
26 const v8_inspector::StringView& message) override { | |
27 frontend_channel_->SendMessageToFrontend(message); | |
28 } | |
29 void flushProtocolNotifications() override {} | |
30 | |
31 InspectorClientImpl::FrontendChannel* frontend_channel_; | |
32 DISALLOW_COPY_AND_ASSIGN(ChannelImpl); | |
33 }; | |
34 | |
35 } // namespace | |
36 | |
37 class ConnectTask : public TaskRunner::Task { | |
38 public: | |
39 ConnectTask(InspectorClientImpl* client, v8::base::Semaphore* ready_semaphore) | |
40 : client_(client), ready_semaphore_(ready_semaphore) {} | |
41 virtual ~ConnectTask() {} | |
42 | |
43 bool is_inspector_task() final { return true; } | |
44 | |
45 void Run(v8::Isolate* isolate, | |
46 const v8::Global<v8::Context>& global_context) { | |
47 v8::HandleScope handle_scope(isolate); | |
48 v8::Local<v8::Context> context = global_context.Get(isolate); | |
49 client_->connect(context); | |
50 if (ready_semaphore_) ready_semaphore_->Signal(); | |
51 } | |
52 | |
53 private: | |
54 InspectorClientImpl* client_; | |
55 v8::base::Semaphore* ready_semaphore_; | |
56 }; | |
57 | |
58 InspectorClientImpl::InspectorClientImpl(TaskRunner* task_runner, | |
59 FrontendChannel* frontend_channel, | |
60 v8::base::Semaphore* ready_semaphore) | |
61 : isolate_(nullptr), | |
62 task_runner_(task_runner), | |
63 frontend_channel_(frontend_channel) { | |
64 task_runner_->Append(new ConnectTask(this, ready_semaphore)); | |
65 } | |
66 | |
67 InspectorClientImpl::~InspectorClientImpl() {} | |
68 | |
69 void InspectorClientImpl::connect(v8::Local<v8::Context> context) { | |
70 isolate_ = context->GetIsolate(); | |
71 channel_.reset(new ChannelImpl(frontend_channel_)); | |
72 | |
73 inspector_ = v8_inspector::V8Inspector::create(isolate_, this); | |
74 session_ = inspector_->connect(1, channel_.get(), v8_inspector::StringView()); | |
75 | |
76 context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this); | |
77 inspector_->contextCreated( | |
78 v8_inspector::V8ContextInfo(context, 1, v8_inspector::StringView())); | |
79 context_.Reset(isolate_, context); | |
80 } | |
81 | |
82 v8::Local<v8::Context> InspectorClientImpl::ensureDefaultContextInGroup(int) { | |
83 CHECK(isolate_); | |
84 return context_.Get(isolate_); | |
85 } | |
86 | |
87 double InspectorClientImpl::currentTimeMS() { | |
88 return v8::base::OS::TimeCurrentMillis(); | |
89 } | |
90 | |
91 void InspectorClientImpl::runMessageLoopOnPause(int) { | |
92 task_runner_->RunMessageLoop(true); | |
93 } | |
94 | |
95 void InspectorClientImpl::quitMessageLoopOnPause() { | |
96 task_runner_->QuitMessageLoop(); | |
97 } | |
98 | |
99 v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( | |
100 v8::Local<v8::Context> context) { | |
101 InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>( | |
102 context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); | |
103 CHECK(inspector_client); | |
104 return inspector_client->session_.get(); | |
105 } | |
106 | |
107 class ProcessMessageOnBackend : public TaskRunner::Task { | |
108 public: | |
109 ProcessMessageOnBackend(const std::string& message) : message_(message) {} | |
110 | |
111 bool is_inspector_task() final { return true; } | |
112 | |
113 void Run(v8::Isolate* isolate, | |
114 const v8::Global<v8::Context>& global_context) override { | |
115 v8_inspector::V8InspectorSession* session = nullptr; | |
116 { | |
117 v8::HandleScope handle_scope(isolate); | |
118 v8::Local<v8::Context> context = global_context.Get(isolate); | |
119 session = InspectorClientImpl::SessionFromContext(context); | |
120 CHECK(session); | |
121 } | |
122 v8_inspector::StringView message_view( | |
123 reinterpret_cast<const uint8_t*>(message_.data()), message_.length()); | |
124 session->dispatchProtocolMessage(message_view); | |
125 } | |
126 | |
127 private: | |
128 std::string message_; | |
dgozman
2016/09/28 16:10:57
String16?
kozy
2016/09/28 17:03:03
Done.
| |
129 }; | |
130 | |
131 TaskRunner* FrontendExtension::backend_task_runner_ = nullptr; | |
132 | |
133 v8::Local<v8::FunctionTemplate> FrontendExtension::GetNativeFunctionTemplate( | |
134 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
135 return v8::FunctionTemplate::New(isolate, | |
136 FrontendExtension::SendMessageToBackend); | |
137 } | |
138 | |
139 void FrontendExtension::SendMessageToBackend( | |
140 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
141 CHECK(backend_task_runner_); | |
142 CHECK(args.Length() == 1 && args[0]->IsString()); | |
143 std::string message = | |
144 *v8::String::Utf8Value(v8::Local<v8::String>::Cast(args[0])); | |
145 backend_task_runner_->Append(new ProcessMessageOnBackend(message)); | |
146 } | |
OLD | NEW |