Chromium Code Reviews| 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/base/platform/platform.h" | |
| 9 #include "src/vector.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1; | |
| 14 | |
| 15 InspectorClientImpl* FromContext(v8::Local<v8::Context> context) { | |
| 16 InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>( | |
| 17 context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); | |
| 18 CHECK(inspector_client); | |
| 19 return inspector_client; | |
| 20 } | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 void ChannelImpl::SendMessageToFrontend( | |
| 25 const v8_inspector::StringView& message) { | |
| 26 CHECK(!message.is8Bit()); | |
| 27 | |
| 28 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 29 v8::HandleScope handle_scope(isolate); | |
| 30 v8::Local<v8::String> message_string = | |
| 31 v8::String::NewFromTwoByte(isolate, message.characters16(), | |
| 32 v8::String::kNormalString, message.length()); | |
| 33 | |
| 34 std::string message_utf8 = *v8::String::Utf8Value(message_string); | |
| 35 | |
| 36 const char* format_string = "InspectorTest.dispatchMessage(%s)"; | |
| 37 v8::internal::Vector<char> source = v8::internal::Vector<char>::New( | |
| 38 message_utf8.length() + strlen(format_string)); | |
| 39 int written = v8::base::OS::SNPrintF(source.start(), source.length(), | |
| 40 format_string, message_utf8.c_str()); | |
| 41 CHECK(written != -1); | |
| 42 frontend_task_runner_->Append( | |
| 43 new ExecuteStringTask(std::string(source.start(), written))); | |
| 44 } | |
| 45 | |
| 46 InspectorClientImpl::InspectorClientImpl() : isolate_(nullptr) {} | |
| 47 | |
| 48 InspectorClientImpl::~InspectorClientImpl() {} | |
| 49 | |
| 50 void InspectorClientImpl::connect(ChannelImpl* channel, | |
| 51 v8::Local<v8::Context> context) { | |
| 52 isolate_ = context->GetIsolate(); | |
| 53 | |
| 54 inspector_ = v8_inspector::V8Inspector::create(isolate_, this); | |
| 55 session_ = inspector_->connect(1, channel, v8_inspector::StringView()); | |
| 56 | |
| 57 context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this); | |
| 58 inspector_->contextCreated( | |
| 59 v8_inspector::V8ContextInfo(context, 1, v8_inspector::StringView())); | |
| 60 context_.Reset(isolate_, context); | |
| 61 } | |
| 62 | |
| 63 v8::Local<v8::Context> InspectorClientImpl::ensureDefaultContextInGroup(int) { | |
| 64 CHECK(isolate_); | |
| 65 return context_.Get(isolate_); | |
| 66 } | |
| 67 | |
| 68 double InspectorClientImpl::currentTimeMS() { | |
| 69 return v8::base::OS::TimeCurrentMillis(); | |
| 70 } | |
| 71 | |
| 72 void InspectorClientImpl::runMessageLoopOnPause(int) { | |
| 73 TaskRunner* task_runner = TaskRunner::FromContext(context_.Get(isolate_)); | |
| 74 CHECK(task_runner); | |
| 75 task_runner->RunMessageLoop(true); | |
| 76 } | |
| 77 | |
| 78 void InspectorClientImpl::quitMessageLoopOnPause() { | |
| 79 TaskRunner* task_runner = TaskRunner::FromContext(context_.Get(isolate_)); | |
| 80 CHECK(task_runner); | |
| 81 task_runner->QuitMessageLoop(); | |
| 82 } | |
| 83 | |
| 84 v8_inspector::V8Inspector* InspectorClientImpl::InspectorFromContext( | |
| 85 v8::Local<v8::Context> context) { | |
| 86 return FromContext(context)->inspector_.get(); | |
| 87 } | |
| 88 | |
| 89 v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( | |
| 90 v8::Local<v8::Context> context) { | |
| 91 return FromContext(context)->session_.get(); | |
| 92 } | |
| 93 | |
| 94 ConnectTask::ConnectTask(InspectorClientImpl* client, ChannelImpl* channel, | |
| 95 v8::base::Semaphore* ready_semaphore) | |
| 96 : client_(client), channel_(channel), ready_semaphore_(ready_semaphore) {} | |
| 97 | |
| 98 void ConnectTask::Run(v8::Isolate* isolate, | |
| 99 const v8::Global<v8::Context>& global_context) { | |
| 100 v8::HandleScope handle_scope(isolate); | |
| 101 v8::Local<v8::Context> context = global_context.Get(isolate); | |
| 102 client_->connect(channel_, context); | |
| 103 if (ready_semaphore_) ready_semaphore_->Signal(); | |
| 104 } | |
| 105 | |
| 106 TaskRunner* BackendExtension::frontend_task_runner_ = nullptr; | |
| 107 | |
| 108 v8::Local<v8::FunctionTemplate> BackendExtension::GetNativeFunctionTemplate( | |
| 109 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
| 110 return v8::FunctionTemplate::New(isolate, | |
| 111 BackendExtension::EvaluateInFrontend); | |
| 112 } | |
| 113 | |
| 114 void BackendExtension::EvaluateInFrontend( | |
|
dgozman
2016/09/27 16:30:36
Let's remove this.
kozy
2016/09/27 17:00:53
Done.
| |
| 115 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 116 CHECK(frontend_task_runner_); | |
| 117 CHECK(args.Length() == 1 && args[0]->IsString()); | |
| 118 | |
| 119 std::string expression = | |
| 120 *v8::String::Utf8Value(v8::Local<v8::String>::Cast(args[0])); | |
| 121 frontend_task_runner_->Append(new ExecuteStringTask(expression)); | |
| 122 } | |
| 123 | |
| 124 namespace { | |
| 125 | |
| 126 class ProcessMessageOnBackend : public TaskRunner::Task { | |
| 127 public: | |
| 128 ProcessMessageOnBackend(const std::string& message) : message_(message) {} | |
| 129 | |
| 130 bool is_protocol_task() final { return true; } | |
| 131 | |
| 132 void Run(v8::Isolate* isolate, | |
| 133 const v8::Global<v8::Context>& global_context) override { | |
| 134 v8_inspector::V8InspectorSession* session = nullptr; | |
| 135 { | |
| 136 v8::HandleScope handle_scope(isolate); | |
| 137 v8::Local<v8::Context> context = global_context.Get(isolate); | |
| 138 session = InspectorClientImpl::SessionFromContext(context); | |
| 139 CHECK(session); | |
| 140 } | |
| 141 v8_inspector::StringView message_view( | |
| 142 reinterpret_cast<const uint8_t*>(message_.data()), message_.length()); | |
| 143 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.
| |
| 144 v8::MicrotasksScope::kRunMicrotasks); | |
| 145 session->dispatchProtocolMessage(message_view); | |
| 146 } | |
| 147 | |
| 148 private: | |
| 149 std::string message_; | |
| 150 }; | |
| 151 | |
| 152 } // namespace | |
| 153 | |
| 154 TaskRunner* FrontendExtension::backend_task_runner_ = nullptr; | |
| 155 | |
| 156 v8::Local<v8::FunctionTemplate> FrontendExtension::GetNativeFunctionTemplate( | |
| 157 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
| 158 return v8::FunctionTemplate::New(isolate, | |
| 159 FrontendExtension::SendMessageToBackend); | |
| 160 } | |
| 161 | |
| 162 void FrontendExtension::SendMessageToBackend( | |
| 163 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 164 CHECK(backend_task_runner_); | |
| 165 CHECK(args.Length() == 1 && args[0]->IsString()); | |
| 166 std::string message = | |
| 167 *v8::String::Utf8Value(v8::Local<v8::String>::Cast(args[0])); | |
| 168 backend_task_runner_->Append(new ProcessMessageOnBackend(message)); | |
| 169 } | |
| OLD | NEW |