| 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/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() = default; | |
| 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() = default; | |
| 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 SendMessageToBackendTask : public TaskRunner::Task { | |
| 108 public: | |
| 109 explicit SendMessageToBackendTask(const v8_inspector::String16& message) | |
| 110 : message_(message) {} | |
| 111 | |
| 112 bool is_inspector_task() final { return true; } | |
| 113 | |
| 114 void Run(v8::Isolate* isolate, | |
| 115 const v8::Global<v8::Context>& global_context) override { | |
| 116 v8_inspector::V8InspectorSession* session = nullptr; | |
| 117 { | |
| 118 v8::HandleScope handle_scope(isolate); | |
| 119 v8::Local<v8::Context> context = global_context.Get(isolate); | |
| 120 session = InspectorClientImpl::SessionFromContext(context); | |
| 121 CHECK(session); | |
| 122 } | |
| 123 v8_inspector::StringView message_view( | |
| 124 reinterpret_cast<const uint16_t*>(message_.characters16()), | |
| 125 message_.length()); | |
| 126 session->dispatchProtocolMessage(message_view); | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 v8_inspector::String16 message_; | |
| 131 }; | |
| 132 | |
| 133 TaskRunner* SendMessageToBackendExtension::backend_task_runner_ = nullptr; | |
| 134 | |
| 135 v8::Local<v8::FunctionTemplate> | |
| 136 SendMessageToBackendExtension::GetNativeFunctionTemplate( | |
| 137 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
| 138 return v8::FunctionTemplate::New( | |
| 139 isolate, SendMessageToBackendExtension::SendMessageToBackend); | |
| 140 } | |
| 141 | |
| 142 void SendMessageToBackendExtension::SendMessageToBackend( | |
| 143 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 144 CHECK(backend_task_runner_); | |
| 145 CHECK(args.Length() == 1 && args[0]->IsString()); | |
| 146 v8::Local<v8::String> message = args[0].As<v8::String>(); | |
| 147 std::unique_ptr<uint16_t[]> buffer(new uint16_t[message->Length()]); | |
| 148 message.As<v8::String>()->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, | |
| 149 message->Length()); | |
| 150 v8_inspector::String16 message_string(buffer.get(), message->Length()); | |
| 151 backend_task_runner_->Append(new SendMessageToBackendTask(message_string)); | |
| 152 } | |
| OLD | NEW |