Chromium Code Reviews| Index: test/inspector-protocol/inspector-impl.cc |
| diff --git a/test/inspector-protocol/inspector-impl.cc b/test/inspector-protocol/inspector-impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..02410fd552edad4708e840d35095ec8d330e7e91 |
| --- /dev/null |
| +++ b/test/inspector-protocol/inspector-impl.cc |
| @@ -0,0 +1,169 @@ |
| +// Copyright 2016 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "test/inspector-protocol/inspector-impl.h" |
| + |
| +#include "include/v8.h" |
| +#include "src/base/platform/platform.h" |
| +#include "src/vector.h" |
| + |
| +namespace { |
| + |
| +const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1; |
| + |
| +InspectorClientImpl* FromContext(v8::Local<v8::Context> context) { |
| + InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>( |
| + context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); |
| + CHECK(inspector_client); |
| + return inspector_client; |
| +} |
| + |
| +} // namespace |
| + |
| +void ChannelImpl::SendMessageToFrontend( |
| + const v8_inspector::StringView& message) { |
| + CHECK(!message.is8Bit()); |
| + |
| + v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| + v8::HandleScope handle_scope(isolate); |
| + v8::Local<v8::String> message_string = |
| + v8::String::NewFromTwoByte(isolate, message.characters16(), |
| + v8::String::kNormalString, message.length()); |
| + |
| + std::string message_utf8 = *v8::String::Utf8Value(message_string); |
| + |
| + const char* format_string = "InspectorTest.dispatchMessage(%s)"; |
| + v8::internal::Vector<char> source = v8::internal::Vector<char>::New( |
| + message_utf8.length() + strlen(format_string)); |
| + int written = v8::base::OS::SNPrintF(source.start(), source.length(), |
| + format_string, message_utf8.c_str()); |
| + CHECK(written != -1); |
| + frontend_task_runner_->Append( |
| + new ExecuteStringTask(std::string(source.start(), written))); |
| +} |
| + |
| +InspectorClientImpl::InspectorClientImpl() : isolate_(nullptr) {} |
| + |
| +InspectorClientImpl::~InspectorClientImpl() {} |
| + |
| +void InspectorClientImpl::connect(ChannelImpl* channel, |
| + v8::Local<v8::Context> context) { |
| + isolate_ = context->GetIsolate(); |
| + |
| + inspector_ = v8_inspector::V8Inspector::create(isolate_, this); |
| + session_ = inspector_->connect(1, channel, v8_inspector::StringView()); |
| + |
| + context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this); |
| + inspector_->contextCreated( |
| + v8_inspector::V8ContextInfo(context, 1, v8_inspector::StringView())); |
| + context_.Reset(isolate_, context); |
| +} |
| + |
| +v8::Local<v8::Context> InspectorClientImpl::ensureDefaultContextInGroup(int) { |
| + CHECK(isolate_); |
| + return context_.Get(isolate_); |
| +} |
| + |
| +double InspectorClientImpl::currentTimeMS() { |
| + return v8::base::OS::TimeCurrentMillis(); |
| +} |
| + |
| +void InspectorClientImpl::runMessageLoopOnPause(int) { |
| + TaskRunner* task_runner = TaskRunner::FromContext(context_.Get(isolate_)); |
| + CHECK(task_runner); |
| + task_runner->RunMessageLoop(true); |
| +} |
| + |
| +void InspectorClientImpl::quitMessageLoopOnPause() { |
| + TaskRunner* task_runner = TaskRunner::FromContext(context_.Get(isolate_)); |
| + CHECK(task_runner); |
| + task_runner->QuitMessageLoop(); |
| +} |
| + |
| +v8_inspector::V8Inspector* InspectorClientImpl::InspectorFromContext( |
| + v8::Local<v8::Context> context) { |
| + return FromContext(context)->inspector_.get(); |
| +} |
| + |
| +v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( |
| + v8::Local<v8::Context> context) { |
| + return FromContext(context)->session_.get(); |
| +} |
| + |
| +ConnectTask::ConnectTask(InspectorClientImpl* client, ChannelImpl* channel, |
| + v8::base::Semaphore* ready_semaphore) |
| + : client_(client), channel_(channel), ready_semaphore_(ready_semaphore) {} |
| + |
| +void ConnectTask::Run(v8::Isolate* isolate, |
| + const v8::Global<v8::Context>& global_context) { |
| + v8::HandleScope handle_scope(isolate); |
| + v8::Local<v8::Context> context = global_context.Get(isolate); |
| + client_->connect(channel_, context); |
| + if (ready_semaphore_) ready_semaphore_->Signal(); |
| +} |
| + |
| +TaskRunner* BackendExtension::frontend_task_runner_ = nullptr; |
| + |
| +v8::Local<v8::FunctionTemplate> BackendExtension::GetNativeFunctionTemplate( |
| + v8::Isolate* isolate, v8::Local<v8::String> name) { |
| + return v8::FunctionTemplate::New(isolate, |
| + BackendExtension::EvaluateInFrontend); |
| +} |
| + |
| +void BackendExtension::EvaluateInFrontend( |
|
dgozman
2016/09/27 16:30:36
Let's remove this.
kozy
2016/09/27 17:00:53
Done.
|
| + const v8::FunctionCallbackInfo<v8::Value>& args) { |
| + CHECK(frontend_task_runner_); |
| + CHECK(args.Length() == 1 && args[0]->IsString()); |
| + |
| + std::string expression = |
| + *v8::String::Utf8Value(v8::Local<v8::String>::Cast(args[0])); |
| + frontend_task_runner_->Append(new ExecuteStringTask(expression)); |
| +} |
| + |
| +namespace { |
| + |
| +class ProcessMessageOnBackend : public TaskRunner::Task { |
| + public: |
| + ProcessMessageOnBackend(const std::string& message) : message_(message) {} |
| + |
| + bool is_protocol_task() final { return true; } |
| + |
| + void Run(v8::Isolate* isolate, |
| + const v8::Global<v8::Context>& global_context) override { |
| + v8_inspector::V8InspectorSession* session = nullptr; |
| + { |
| + v8::HandleScope handle_scope(isolate); |
| + v8::Local<v8::Context> context = global_context.Get(isolate); |
| + session = InspectorClientImpl::SessionFromContext(context); |
| + CHECK(session); |
| + } |
| + v8_inspector::StringView message_view( |
| + reinterpret_cast<const uint8_t*>(message_.data()), message_.length()); |
| + v8::MicrotasksScope microtasks_scope(isolate, |
|
dgozman
2016/09/27 16:30:36
Let's remove this. Why is it needed?
kozy
2016/09/27 17:00:53
Done.
|
| + v8::MicrotasksScope::kRunMicrotasks); |
| + session->dispatchProtocolMessage(message_view); |
| + } |
| + |
| + private: |
| + std::string message_; |
| +}; |
| + |
| +} // namespace |
| + |
| +TaskRunner* FrontendExtension::backend_task_runner_ = nullptr; |
| + |
| +v8::Local<v8::FunctionTemplate> FrontendExtension::GetNativeFunctionTemplate( |
| + v8::Isolate* isolate, v8::Local<v8::String> name) { |
| + return v8::FunctionTemplate::New(isolate, |
| + FrontendExtension::SendMessageToBackend); |
| +} |
| + |
| +void FrontendExtension::SendMessageToBackend( |
| + const v8::FunctionCallbackInfo<v8::Value>& args) { |
| + CHECK(backend_task_runner_); |
| + CHECK(args.Length() == 1 && args[0]->IsString()); |
| + std::string message = |
| + *v8::String::Utf8Value(v8::Local<v8::String>::Cast(args[0])); |
| + backend_task_runner_->Append(new ProcessMessageOnBackend(message)); |
| +} |