V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
injected-script.cc
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "src/inspector/injected-script.h"
32 
33 #include <cmath>
34 #include <unordered_set>
35 
36 #include "src/inspector/custom-preview.h"
37 #include "src/inspector/inspected-context.h"
38 #include "src/inspector/protocol/Protocol.h"
39 #include "src/inspector/remote-object-id.h"
40 #include "src/inspector/string-util.h"
41 #include "src/inspector/v8-console.h"
42 #include "src/inspector/v8-inspector-impl.h"
43 #include "src/inspector/v8-inspector-session-impl.h"
44 #include "src/inspector/v8-stack-trace-impl.h"
45 #include "src/inspector/v8-value-utils.h"
46 #include "src/inspector/value-mirror.h"
47 
48 #include "include/v8-inspector.h"
49 
50 namespace v8_inspector {
51 
52 namespace {
53 static const char kGlobalHandleLabel[] = "DevTools console";
54 static bool isResolvableNumberLike(String16 query) {
55  return query == "Infinity" || query == "-Infinity" || query == "NaN";
56 }
57 } // namespace
58 
59 using protocol::Array;
60 using protocol::Runtime::PropertyDescriptor;
61 using protocol::Runtime::InternalPropertyDescriptor;
62 using protocol::Runtime::RemoteObject;
63 using protocol::Maybe;
64 
66  public:
67  static bool add(V8InspectorSessionImpl* session,
69  int executionContextId, const String16& objectGroup,
70  WrapMode wrapMode, EvaluateCallback* callback) {
72  if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) {
73  callback->sendFailure(Response::InternalError());
74  return false;
75  }
76  if (!resolver->Resolve(context, value).FromMaybe(false)) {
77  callback->sendFailure(Response::InternalError());
78  return false;
79  }
80 
81  v8::Local<v8::Promise> promise = resolver->GetPromise();
82  V8InspectorImpl* inspector = session->inspector();
84  session, executionContextId, objectGroup, wrapMode, callback);
85  v8::Local<v8::Value> wrapper = handler->m_wrapper.Get(inspector->isolate());
86  v8::Local<v8::Function> thenCallbackFunction =
87  v8::Function::New(context, thenCallback, wrapper, 0,
88  v8::ConstructorBehavior::kThrow)
89  .ToLocalChecked();
90  if (promise->Then(context, thenCallbackFunction).IsEmpty()) {
91  callback->sendFailure(Response::InternalError());
92  return false;
93  }
94  v8::Local<v8::Function> catchCallbackFunction =
95  v8::Function::New(context, catchCallback, wrapper, 0,
96  v8::ConstructorBehavior::kThrow)
97  .ToLocalChecked();
98  if (promise->Catch(context, catchCallbackFunction).IsEmpty()) {
99  callback->sendFailure(Response::InternalError());
100  return false;
101  }
102  return true;
103  }
104 
105  private:
106  static void thenCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
107  ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(
108  info.Data().As<v8::External>()->Value());
109  DCHECK(handler);
110  v8::Local<v8::Value> value =
111  info.Length() > 0
112  ? info[0]
113  : v8::Local<v8::Value>::Cast(v8::Undefined(info.GetIsolate()));
114  handler->thenCallback(value);
115  delete handler;
116  }
117 
118  static void catchCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
119  ProtocolPromiseHandler* handler = static_cast<ProtocolPromiseHandler*>(
120  info.Data().As<v8::External>()->Value());
121  DCHECK(handler);
122  v8::Local<v8::Value> value =
123  info.Length() > 0
124  ? info[0]
125  : v8::Local<v8::Value>::Cast(v8::Undefined(info.GetIsolate()));
126  handler->catchCallback(value);
127  delete handler;
128  }
129 
131  int executionContextId, const String16& objectGroup,
132  WrapMode wrapMode, EvaluateCallback* callback)
133  : m_inspector(session->inspector()),
134  m_sessionId(session->sessionId()),
135  m_contextGroupId(session->contextGroupId()),
136  m_executionContextId(executionContextId),
137  m_objectGroup(objectGroup),
138  m_wrapMode(wrapMode),
139  m_callback(std::move(callback)),
140  m_wrapper(m_inspector->isolate(),
141  v8::External::New(m_inspector->isolate(), this)) {
142  m_wrapper.SetWeak(this, cleanup, v8::WeakCallbackType::kParameter);
143  }
144 
145  static void cleanup(
147  if (!data.GetParameter()->m_wrapper.IsEmpty()) {
148  data.GetParameter()->m_wrapper.Reset();
149  data.SetSecondPassCallback(cleanup);
150  } else {
151  data.GetParameter()->sendPromiseCollected();
152  delete data.GetParameter();
153  }
154  }
155 
156  void thenCallback(v8::Local<v8::Value> result) {
157  V8InspectorSessionImpl* session =
158  m_inspector->sessionById(m_contextGroupId, m_sessionId);
159  if (!session) return;
160  InjectedScript::ContextScope scope(session, m_executionContextId);
161  Response response = scope.initialize();
162  if (!response.isSuccess()) return;
163  if (m_objectGroup == "console") {
164  scope.injectedScript()->setLastEvaluationResult(result);
165  }
166  std::unique_ptr<EvaluateCallback> callback =
167  scope.injectedScript()->takeEvaluateCallback(m_callback);
168  if (!callback) return;
169  std::unique_ptr<protocol::Runtime::RemoteObject> wrappedValue;
170  response = scope.injectedScript()->wrapObject(result, m_objectGroup,
171  m_wrapMode, &wrappedValue);
172  if (!response.isSuccess()) {
173  callback->sendFailure(response);
174  return;
175  }
176  callback->sendSuccess(std::move(wrappedValue),
177  Maybe<protocol::Runtime::ExceptionDetails>());
178  }
179 
180  void catchCallback(v8::Local<v8::Value> result) {
181  V8InspectorSessionImpl* session =
182  m_inspector->sessionById(m_contextGroupId, m_sessionId);
183  if (!session) return;
184  InjectedScript::ContextScope scope(session, m_executionContextId);
185  Response response = scope.initialize();
186  if (!response.isSuccess()) return;
187  std::unique_ptr<EvaluateCallback> callback =
188  scope.injectedScript()->takeEvaluateCallback(m_callback);
189  if (!callback) return;
190  std::unique_ptr<protocol::Runtime::RemoteObject> wrappedValue;
191  response = scope.injectedScript()->wrapObject(result, m_objectGroup,
192  m_wrapMode, &wrappedValue);
193  if (!response.isSuccess()) {
194  callback->sendFailure(response);
195  return;
196  }
197  String16 message;
198  std::unique_ptr<V8StackTraceImpl> stack;
199  v8::Isolate* isolate = session->inspector()->isolate();
200  if (result->IsNativeError()) {
201  message = " " + toProtocolString(
202  isolate,
203  result->ToDetailString(isolate->GetCurrentContext())
204  .ToLocalChecked());
205  v8::Local<v8::StackTrace> stackTrace = v8::debug::GetDetailedStackTrace(
206  isolate, v8::Local<v8::Object>::Cast(result));
207  if (!stackTrace.IsEmpty()) {
208  stack = m_inspector->debugger()->createStackTrace(stackTrace);
209  }
210  }
211  if (!stack) {
212  stack = m_inspector->debugger()->captureStackTrace(true);
213  }
214  std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails =
215  protocol::Runtime::ExceptionDetails::create()
216  .setExceptionId(m_inspector->nextExceptionId())
217  .setText("Uncaught (in promise)" + message)
218  .setLineNumber(stack && !stack->isEmpty() ? stack->topLineNumber()
219  : 0)
220  .setColumnNumber(
221  stack && !stack->isEmpty() ? stack->topColumnNumber() : 0)
222  .setException(wrappedValue->clone())
223  .build();
224  if (stack)
225  exceptionDetails->setStackTrace(
226  stack->buildInspectorObjectImpl(m_inspector->debugger()));
227  if (stack && !stack->isEmpty())
228  exceptionDetails->setScriptId(toString16(stack->topScriptId()));
229  callback->sendSuccess(std::move(wrappedValue), std::move(exceptionDetails));
230  }
231 
232  void sendPromiseCollected() {
233  V8InspectorSessionImpl* session =
234  m_inspector->sessionById(m_contextGroupId, m_sessionId);
235  if (!session) return;
236  InjectedScript::ContextScope scope(session, m_executionContextId);
237  Response response = scope.initialize();
238  if (!response.isSuccess()) return;
239  std::unique_ptr<EvaluateCallback> callback =
240  scope.injectedScript()->takeEvaluateCallback(m_callback);
241  if (!callback) return;
242  callback->sendFailure(Response::Error("Promise was collected"));
243  }
244 
245  V8InspectorImpl* m_inspector;
246  int m_sessionId;
247  int m_contextGroupId;
248  int m_executionContextId;
249  String16 m_objectGroup;
250  WrapMode m_wrapMode;
251  EvaluateCallback* m_callback;
252  v8::Global<v8::External> m_wrapper;
253 };
254 
255 InjectedScript::InjectedScript(InspectedContext* context, int sessionId)
256  : m_context(context), m_sessionId(sessionId) {}
257 
258 InjectedScript::~InjectedScript() { discardEvaluateCallbacks(); }
259 
260 namespace {
261 class PropertyAccumulator : public ValueMirror::PropertyAccumulator {
262  public:
263  explicit PropertyAccumulator(std::vector<PropertyMirror>* mirrors)
264  : m_mirrors(mirrors) {}
265  bool Add(PropertyMirror mirror) override {
266  m_mirrors->push_back(std::move(mirror));
267  return true;
268  }
269 
270  private:
271  std::vector<PropertyMirror>* m_mirrors;
272 };
273 } // anonymous namespace
274 
275 Response InjectedScript::getProperties(
276  v8::Local<v8::Object> object, const String16& groupName, bool ownProperties,
277  bool accessorPropertiesOnly, WrapMode wrapMode,
278  std::unique_ptr<Array<PropertyDescriptor>>* properties,
279  Maybe<protocol::Runtime::ExceptionDetails>* exceptionDetails) {
280  v8::HandleScope handles(m_context->isolate());
281  v8::Local<v8::Context> context = m_context->context();
282  v8::Isolate* isolate = m_context->isolate();
283  int sessionId = m_sessionId;
284  v8::TryCatch tryCatch(isolate);
285 
286  *properties = Array<PropertyDescriptor>::create();
287  std::vector<PropertyMirror> mirrors;
288  PropertyAccumulator accumulator(&mirrors);
289  if (!ValueMirror::getProperties(context, object, ownProperties,
290  accessorPropertiesOnly, &accumulator)) {
291  return createExceptionDetails(tryCatch, groupName, wrapMode,
292  exceptionDetails);
293  }
294  for (const PropertyMirror& mirror : mirrors) {
295  std::unique_ptr<PropertyDescriptor> descriptor =
296  PropertyDescriptor::create()
297  .setName(mirror.name)
298  .setConfigurable(mirror.configurable)
299  .setEnumerable(mirror.enumerable)
300  .setIsOwn(mirror.isOwn)
301  .build();
302  Response response;
303  std::unique_ptr<RemoteObject> remoteObject;
304  if (mirror.value) {
305  response =
306  mirror.value->buildRemoteObject(context, wrapMode, &remoteObject);
307  if (!response.isSuccess()) return response;
308  response =
309  bindRemoteObjectIfNeeded(sessionId, context, mirror.value->v8Value(),
310  groupName, remoteObject.get());
311  if (!response.isSuccess()) return response;
312  descriptor->setValue(std::move(remoteObject));
313  descriptor->setWritable(mirror.writable);
314  }
315  if (mirror.getter) {
316  response =
317  mirror.getter->buildRemoteObject(context, wrapMode, &remoteObject);
318  if (!response.isSuccess()) return response;
319  response =
320  bindRemoteObjectIfNeeded(sessionId, context, mirror.getter->v8Value(),
321  groupName, remoteObject.get());
322  if (!response.isSuccess()) return response;
323  descriptor->setGet(std::move(remoteObject));
324  }
325  if (mirror.setter) {
326  response =
327  mirror.setter->buildRemoteObject(context, wrapMode, &remoteObject);
328  if (!response.isSuccess()) return response;
329  response =
330  bindRemoteObjectIfNeeded(sessionId, context, mirror.setter->v8Value(),
331  groupName, remoteObject.get());
332  if (!response.isSuccess()) return response;
333  descriptor->setSet(std::move(remoteObject));
334  }
335  if (mirror.symbol) {
336  response =
337  mirror.symbol->buildRemoteObject(context, wrapMode, &remoteObject);
338  if (!response.isSuccess()) return response;
339  response =
340  bindRemoteObjectIfNeeded(sessionId, context, mirror.symbol->v8Value(),
341  groupName, remoteObject.get());
342  if (!response.isSuccess()) return response;
343  descriptor->setSymbol(std::move(remoteObject));
344  }
345  if (mirror.exception) {
346  response =
347  mirror.exception->buildRemoteObject(context, wrapMode, &remoteObject);
348  if (!response.isSuccess()) return response;
349  response = bindRemoteObjectIfNeeded(sessionId, context,
350  mirror.exception->v8Value(),
351  groupName, remoteObject.get());
352  if (!response.isSuccess()) return response;
353  descriptor->setValue(std::move(remoteObject));
354  descriptor->setWasThrown(true);
355  }
356  (*properties)->addItem(std::move(descriptor));
357  }
358  return Response::OK();
359 }
360 
361 Response InjectedScript::getInternalProperties(
362  v8::Local<v8::Value> value, const String16& groupName,
363  std::unique_ptr<protocol::Array<InternalPropertyDescriptor>>* result) {
364  *result = protocol::Array<InternalPropertyDescriptor>::create();
365  v8::Local<v8::Context> context = m_context->context();
366  int sessionId = m_sessionId;
367  std::vector<InternalPropertyMirror> wrappers;
368  if (value->IsObject()) {
369  ValueMirror::getInternalProperties(m_context->context(),
370  value.As<v8::Object>(), &wrappers);
371  }
372  for (size_t i = 0; i < wrappers.size(); ++i) {
373  std::unique_ptr<RemoteObject> remoteObject;
374  Response response = wrappers[i].value->buildRemoteObject(
375  m_context->context(), WrapMode::kNoPreview, &remoteObject);
376  if (!response.isSuccess()) return response;
377  response = bindRemoteObjectIfNeeded(sessionId, context,
378  wrappers[i].value->v8Value(), groupName,
379  remoteObject.get());
380  if (!response.isSuccess()) return response;
381  (*result)->addItem(InternalPropertyDescriptor::create()
382  .setName(wrappers[i].name)
383  .setValue(std::move(remoteObject))
384  .build());
385  }
386  return Response::OK();
387 }
388 
389 void InjectedScript::releaseObject(const String16& objectId) {
390  std::unique_ptr<protocol::Value> parsedObjectId =
391  protocol::StringUtil::parseJSON(objectId);
392  if (!parsedObjectId) return;
393  protocol::DictionaryValue* object =
394  protocol::DictionaryValue::cast(parsedObjectId.get());
395  if (!object) return;
396  int boundId = 0;
397  if (!object->getInteger("id", &boundId)) return;
398  unbindObject(boundId);
399 }
400 
401 Response InjectedScript::wrapObject(
402  v8::Local<v8::Value> value, const String16& groupName, WrapMode wrapMode,
403  std::unique_ptr<protocol::Runtime::RemoteObject>* result) {
404  return wrapObject(value, groupName, wrapMode, v8::MaybeLocal<v8::Value>(),
405  kMaxCustomPreviewDepth, result);
406 }
407 
408 Response InjectedScript::wrapObject(
409  v8::Local<v8::Value> value, const String16& groupName, WrapMode wrapMode,
410  v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
411  std::unique_ptr<protocol::Runtime::RemoteObject>* result) {
412  v8::Local<v8::Context> context = m_context->context();
413  v8::Context::Scope contextScope(context);
414  int customPreviewEnabled = m_customPreviewEnabled;
415  int sessionId = m_sessionId;
416  auto obj = ValueMirror::create(m_context->context(), value);
417  if (!obj) return Response::InternalError();
418  Response response = obj->buildRemoteObject(context, wrapMode, result);
419  if (!response.isSuccess()) return response;
420  response = bindRemoteObjectIfNeeded(sessionId, context, value, groupName,
421  result->get());
422  if (!response.isSuccess()) return response;
423  if (customPreviewEnabled && value->IsObject()) {
424  std::unique_ptr<protocol::Runtime::CustomPreview> customPreview;
425  generateCustomPreview(sessionId, groupName, context, value.As<v8::Object>(),
426  customPreviewConfig, maxCustomPreviewDepth,
427  &customPreview);
428  if (customPreview) (*result)->setCustomPreview(std::move(customPreview));
429  }
430  return Response::OK();
431 }
432 
433 std::unique_ptr<protocol::Runtime::RemoteObject> InjectedScript::wrapTable(
434  v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> maybeColumns) {
435  using protocol::Runtime::RemoteObject;
436  using protocol::Runtime::ObjectPreview;
437  using protocol::Runtime::PropertyPreview;
438  using protocol::Array;
439 
440  v8::Isolate* isolate = m_context->isolate();
441  v8::HandleScope handles(isolate);
442  v8::Local<v8::Context> context = m_context->context();
443 
444  std::unique_ptr<RemoteObject> remoteObject;
445  Response response =
446  wrapObject(table, "console", WrapMode::kNoPreview, &remoteObject);
447  if (!remoteObject || !response.isSuccess()) return nullptr;
448 
449  auto mirror = ValueMirror::create(context, table);
450  std::unique_ptr<ObjectPreview> preview;
451  int limit = 1000;
452  mirror->buildObjectPreview(context, true /* generatePreviewForTable */,
453  &limit, &limit, &preview);
454  if (!preview) return nullptr;
455 
456  Array<PropertyPreview>* columns = preview->getProperties();
457  std::unordered_set<String16> selectedColumns;
458  v8::Local<v8::Array> v8Columns;
459  if (maybeColumns.ToLocal(&v8Columns)) {
460  for (uint32_t i = 0; i < v8Columns->Length(); ++i) {
461  v8::Local<v8::Value> column;
462  if (v8Columns->Get(context, i).ToLocal(&column) && column->IsString()) {
463  selectedColumns.insert(
464  toProtocolString(isolate, column.As<v8::String>()));
465  }
466  }
467  }
468  if (!selectedColumns.empty()) {
469  for (size_t i = 0; i < columns->length(); ++i) {
470  ObjectPreview* columnPreview = columns->get(i)->getValuePreview(nullptr);
471  if (!columnPreview) continue;
472 
473  std::unique_ptr<Array<PropertyPreview>> filtered =
474  Array<PropertyPreview>::create();
475  Array<PropertyPreview>* columns = columnPreview->getProperties();
476  for (size_t j = 0; j < columns->length(); ++j) {
477  PropertyPreview* property = columns->get(j);
478  if (selectedColumns.find(property->getName()) !=
479  selectedColumns.end()) {
480  filtered->addItem(property->clone());
481  }
482  }
483  columnPreview->setProperties(std::move(filtered));
484  }
485  }
486  remoteObject->setPreview(std::move(preview));
487  return remoteObject;
488 }
489 
490 void InjectedScript::addPromiseCallback(
491  V8InspectorSessionImpl* session, v8::MaybeLocal<v8::Value> value,
492  const String16& objectGroup, WrapMode wrapMode,
493  std::unique_ptr<EvaluateCallback> callback) {
494  if (value.IsEmpty()) {
495  callback->sendFailure(Response::InternalError());
496  return;
497  }
498  v8::MicrotasksScope microtasksScope(m_context->isolate(),
499  v8::MicrotasksScope::kRunMicrotasks);
500  if (ProtocolPromiseHandler::add(
501  session, m_context->context(), value.ToLocalChecked(),
502  m_context->contextId(), objectGroup, wrapMode, callback.get())) {
503  m_evaluateCallbacks.insert(callback.release());
504  }
505 }
506 
507 void InjectedScript::discardEvaluateCallbacks() {
508  for (auto& callback : m_evaluateCallbacks) {
509  callback->sendFailure(Response::Error("Execution context was destroyed."));
510  delete callback;
511  }
512  m_evaluateCallbacks.clear();
513 }
514 
515 std::unique_ptr<EvaluateCallback> InjectedScript::takeEvaluateCallback(
516  EvaluateCallback* callback) {
517  auto it = m_evaluateCallbacks.find(callback);
518  if (it == m_evaluateCallbacks.end()) return nullptr;
519  std::unique_ptr<EvaluateCallback> value(*it);
520  m_evaluateCallbacks.erase(it);
521  return value;
522 }
523 
524 Response InjectedScript::findObject(const RemoteObjectId& objectId,
525  v8::Local<v8::Value>* outObject) const {
526  auto it = m_idToWrappedObject.find(objectId.id());
527  if (it == m_idToWrappedObject.end())
528  return Response::Error("Could not find object with given id");
529  *outObject = it->second.Get(m_context->isolate());
530  return Response::OK();
531 }
532 
533 String16 InjectedScript::objectGroupName(const RemoteObjectId& objectId) const {
534  if (objectId.id() <= 0) return String16();
535  auto it = m_idToObjectGroupName.find(objectId.id());
536  return it != m_idToObjectGroupName.end() ? it->second : String16();
537 }
538 
539 void InjectedScript::releaseObjectGroup(const String16& objectGroup) {
540  if (objectGroup == "console") m_lastEvaluationResult.Reset();
541  if (objectGroup.isEmpty()) return;
542  auto it = m_nameToObjectGroup.find(objectGroup);
543  if (it == m_nameToObjectGroup.end()) return;
544  for (int id : it->second) unbindObject(id);
545  m_nameToObjectGroup.erase(it);
546 }
547 
548 void InjectedScript::setCustomObjectFormatterEnabled(bool enabled) {
549  m_customPreviewEnabled = enabled;
550 }
551 
552 v8::Local<v8::Value> InjectedScript::lastEvaluationResult() const {
553  if (m_lastEvaluationResult.IsEmpty())
554  return v8::Undefined(m_context->isolate());
555  return m_lastEvaluationResult.Get(m_context->isolate());
556 }
557 
558 void InjectedScript::setLastEvaluationResult(v8::Local<v8::Value> result) {
559  m_lastEvaluationResult.Reset(m_context->isolate(), result);
560  m_lastEvaluationResult.AnnotateStrongRetainer(kGlobalHandleLabel);
561 }
562 
563 Response InjectedScript::resolveCallArgument(
564  protocol::Runtime::CallArgument* callArgument,
565  v8::Local<v8::Value>* result) {
566  if (callArgument->hasObjectId()) {
567  std::unique_ptr<RemoteObjectId> remoteObjectId;
568  Response response =
569  RemoteObjectId::parse(callArgument->getObjectId(""), &remoteObjectId);
570  if (!response.isSuccess()) return response;
571  if (remoteObjectId->contextId() != m_context->contextId())
572  return Response::Error(
573  "Argument should belong to the same JavaScript world as target "
574  "object");
575  return findObject(*remoteObjectId, result);
576  }
577  if (callArgument->hasValue() || callArgument->hasUnserializableValue()) {
578  String16 value;
579  if (callArgument->hasValue()) {
580  value = "(" + callArgument->getValue(nullptr)->serialize() + ")";
581  } else {
582  String16 unserializableValue = callArgument->getUnserializableValue("");
583  // Protect against potential identifier resolution for NaN and Infinity.
584  if (isResolvableNumberLike(unserializableValue))
585  value = "Number(\"" + unserializableValue + "\")";
586  else
587  value = unserializableValue;
588  }
589  if (!m_context->inspector()
590  ->compileAndRunInternalScript(
591  m_context->context(), toV8String(m_context->isolate(), value))
592  .ToLocal(result)) {
593  return Response::Error("Couldn't parse value object in call argument");
594  }
595  return Response::OK();
596  }
597  *result = v8::Undefined(m_context->isolate());
598  return Response::OK();
599 }
600 
601 Response InjectedScript::createExceptionDetails(
602  const v8::TryCatch& tryCatch, const String16& objectGroup,
603  WrapMode wrapMode, Maybe<protocol::Runtime::ExceptionDetails>* result) {
604  if (!tryCatch.HasCaught()) return Response::InternalError();
605  v8::Local<v8::Message> message = tryCatch.Message();
606  v8::Local<v8::Value> exception = tryCatch.Exception();
607  String16 messageText =
608  message.IsEmpty()
609  ? String16()
610  : toProtocolString(m_context->isolate(), message->Get());
611  std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails =
612  protocol::Runtime::ExceptionDetails::create()
613  .setExceptionId(m_context->inspector()->nextExceptionId())
614  .setText(exception.IsEmpty() ? messageText : String16("Uncaught"))
615  .setLineNumber(
616  message.IsEmpty()
617  ? 0
618  : message->GetLineNumber(m_context->context()).FromMaybe(1) -
619  1)
620  .setColumnNumber(
621  message.IsEmpty()
622  ? 0
623  : message->GetStartColumn(m_context->context()).FromMaybe(0))
624  .build();
625  if (!message.IsEmpty()) {
626  exceptionDetails->setScriptId(String16::fromInteger(
627  static_cast<int>(message->GetScriptOrigin().ScriptID()->Value())));
628  v8::Local<v8::StackTrace> stackTrace = message->GetStackTrace();
629  if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0)
630  exceptionDetails->setStackTrace(
631  m_context->inspector()
632  ->debugger()
633  ->createStackTrace(stackTrace)
634  ->buildInspectorObjectImpl(m_context->inspector()->debugger()));
635  }
636  if (!exception.IsEmpty()) {
637  std::unique_ptr<protocol::Runtime::RemoteObject> wrapped;
638  Response response =
639  wrapObject(exception, objectGroup,
640  exception->IsNativeError() ? WrapMode::kNoPreview
641  : WrapMode::kWithPreview,
642  &wrapped);
643  if (!response.isSuccess()) return response;
644  exceptionDetails->setException(std::move(wrapped));
645  }
646  *result = std::move(exceptionDetails);
647  return Response::OK();
648 }
649 
650 Response InjectedScript::wrapEvaluateResult(
651  v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch& tryCatch,
652  const String16& objectGroup, WrapMode wrapMode,
653  std::unique_ptr<protocol::Runtime::RemoteObject>* result,
654  Maybe<protocol::Runtime::ExceptionDetails>* exceptionDetails) {
655  v8::Local<v8::Value> resultValue;
656  if (!tryCatch.HasCaught()) {
657  if (!maybeResultValue.ToLocal(&resultValue))
658  return Response::InternalError();
659  Response response = wrapObject(resultValue, objectGroup, wrapMode, result);
660  if (!response.isSuccess()) return response;
661  if (objectGroup == "console") {
662  m_lastEvaluationResult.Reset(m_context->isolate(), resultValue);
663  m_lastEvaluationResult.AnnotateStrongRetainer(kGlobalHandleLabel);
664  }
665  } else {
666  if (tryCatch.HasTerminated() || !tryCatch.CanContinue()) {
667  return Response::Error("Execution was terminated");
668  }
669  v8::Local<v8::Value> exception = tryCatch.Exception();
670  Response response =
671  wrapObject(exception, objectGroup,
672  exception->IsNativeError() ? WrapMode::kNoPreview
673  : WrapMode::kWithPreview,
674  result);
675  if (!response.isSuccess()) return response;
676  // We send exception in result for compatibility reasons, even though it's
677  // accessible through exceptionDetails.exception.
678  response = createExceptionDetails(tryCatch, objectGroup, wrapMode,
679  exceptionDetails);
680  if (!response.isSuccess()) return response;
681  }
682  return Response::OK();
683 }
684 
685 v8::Local<v8::Object> InjectedScript::commandLineAPI() {
686  if (m_commandLineAPI.IsEmpty()) {
687  m_commandLineAPI.Reset(
688  m_context->isolate(),
689  m_context->inspector()->console()->createCommandLineAPI(
690  m_context->context(), m_sessionId));
691  m_commandLineAPI.AnnotateStrongRetainer(kGlobalHandleLabel);
692  }
693  return m_commandLineAPI.Get(m_context->isolate());
694 }
695 
696 InjectedScript::Scope::Scope(V8InspectorSessionImpl* session)
697  : m_inspector(session->inspector()),
698  m_injectedScript(nullptr),
699  m_handleScope(m_inspector->isolate()),
700  m_tryCatch(m_inspector->isolate()),
701  m_ignoreExceptionsAndMuteConsole(false),
702  m_previousPauseOnExceptionsState(v8::debug::NoBreakOnException),
703  m_userGesture(false),
704  m_allowEval(false),
705  m_contextGroupId(session->contextGroupId()),
706  m_sessionId(session->sessionId()) {}
707 
708 Response InjectedScript::Scope::initialize() {
709  cleanup();
710  V8InspectorSessionImpl* session =
711  m_inspector->sessionById(m_contextGroupId, m_sessionId);
712  if (!session) return Response::InternalError();
713  Response response = findInjectedScript(session);
714  if (!response.isSuccess()) return response;
715  m_context = m_injectedScript->context()->context();
716  m_context->Enter();
717  if (m_allowEval) m_context->AllowCodeGenerationFromStrings(true);
718  return Response::OK();
719 }
720 
721 void InjectedScript::Scope::installCommandLineAPI() {
722  DCHECK(m_injectedScript && !m_context.IsEmpty() &&
723  !m_commandLineAPIScope.get());
724  m_commandLineAPIScope.reset(new V8Console::CommandLineAPIScope(
725  m_context, m_injectedScript->commandLineAPI(), m_context->Global()));
726 }
727 
728 void InjectedScript::Scope::ignoreExceptionsAndMuteConsole() {
729  DCHECK(!m_ignoreExceptionsAndMuteConsole);
730  m_ignoreExceptionsAndMuteConsole = true;
731  m_inspector->client()->muteMetrics(m_contextGroupId);
732  m_inspector->muteExceptions(m_contextGroupId);
733  m_previousPauseOnExceptionsState =
734  setPauseOnExceptionsState(v8::debug::NoBreakOnException);
735 }
736 
737 v8::debug::ExceptionBreakState InjectedScript::Scope::setPauseOnExceptionsState(
738  v8::debug::ExceptionBreakState newState) {
739  if (!m_inspector->debugger()->enabled()) return newState;
740  v8::debug::ExceptionBreakState presentState =
741  m_inspector->debugger()->getPauseOnExceptionsState();
742  if (presentState != newState)
743  m_inspector->debugger()->setPauseOnExceptionsState(newState);
744  return presentState;
745 }
746 
747 void InjectedScript::Scope::pretendUserGesture() {
748  DCHECK(!m_userGesture);
749  m_userGesture = true;
750  m_inspector->client()->beginUserGesture();
751 }
752 
753 void InjectedScript::Scope::allowCodeGenerationFromStrings() {
754  DCHECK(!m_allowEval);
755  if (m_context->IsCodeGenerationFromStringsAllowed()) return;
756  m_allowEval = true;
757  m_context->AllowCodeGenerationFromStrings(true);
758 }
759 
760 void InjectedScript::Scope::cleanup() {
761  m_commandLineAPIScope.reset();
762  if (!m_context.IsEmpty()) {
763  if (m_allowEval) m_context->AllowCodeGenerationFromStrings(false);
764  m_context->Exit();
765  m_context.Clear();
766  }
767 }
768 
769 InjectedScript::Scope::~Scope() {
770  if (m_ignoreExceptionsAndMuteConsole) {
771  setPauseOnExceptionsState(m_previousPauseOnExceptionsState);
772  m_inspector->client()->unmuteMetrics(m_contextGroupId);
773  m_inspector->unmuteExceptions(m_contextGroupId);
774  }
775  if (m_userGesture) m_inspector->client()->endUserGesture();
776  cleanup();
777 }
778 
779 InjectedScript::ContextScope::ContextScope(V8InspectorSessionImpl* session,
780  int executionContextId)
781  : InjectedScript::Scope(session),
782  m_executionContextId(executionContextId) {}
783 
784 InjectedScript::ContextScope::~ContextScope() = default;
785 
786 Response InjectedScript::ContextScope::findInjectedScript(
787  V8InspectorSessionImpl* session) {
788  return session->findInjectedScript(m_executionContextId, m_injectedScript);
789 }
790 
791 InjectedScript::ObjectScope::ObjectScope(V8InspectorSessionImpl* session,
792  const String16& remoteObjectId)
793  : InjectedScript::Scope(session), m_remoteObjectId(remoteObjectId) {}
794 
795 InjectedScript::ObjectScope::~ObjectScope() = default;
796 
797 Response InjectedScript::ObjectScope::findInjectedScript(
798  V8InspectorSessionImpl* session) {
799  std::unique_ptr<RemoteObjectId> remoteId;
800  Response response = RemoteObjectId::parse(m_remoteObjectId, &remoteId);
801  if (!response.isSuccess()) return response;
802  InjectedScript* injectedScript = nullptr;
803  response = session->findInjectedScript(remoteId.get(), injectedScript);
804  if (!response.isSuccess()) return response;
805  m_objectGroupName = injectedScript->objectGroupName(*remoteId);
806  response = injectedScript->findObject(*remoteId, &m_object);
807  if (!response.isSuccess()) return response;
808  m_injectedScript = injectedScript;
809  return Response::OK();
810 }
811 
812 InjectedScript::CallFrameScope::CallFrameScope(V8InspectorSessionImpl* session,
813  const String16& remoteObjectId)
814  : InjectedScript::Scope(session), m_remoteCallFrameId(remoteObjectId) {}
815 
816 InjectedScript::CallFrameScope::~CallFrameScope() = default;
817 
818 Response InjectedScript::CallFrameScope::findInjectedScript(
819  V8InspectorSessionImpl* session) {
820  std::unique_ptr<RemoteCallFrameId> remoteId;
821  Response response = RemoteCallFrameId::parse(m_remoteCallFrameId, &remoteId);
822  if (!response.isSuccess()) return response;
823  m_frameOrdinal = static_cast<size_t>(remoteId->frameOrdinal());
824  return session->findInjectedScript(remoteId.get(), m_injectedScript);
825 }
826 
827 String16 InjectedScript::bindObject(v8::Local<v8::Value> value,
828  const String16& groupName) {
829  if (m_lastBoundObjectId <= 0) m_lastBoundObjectId = 1;
830  int id = m_lastBoundObjectId++;
831  m_idToWrappedObject[id].Reset(m_context->isolate(), value);
832  m_idToWrappedObject[id].AnnotateStrongRetainer(kGlobalHandleLabel);
833  if (!groupName.isEmpty() && id > 0) {
834  m_idToObjectGroupName[id] = groupName;
835  m_nameToObjectGroup[groupName].push_back(id);
836  }
837  // TODO(dgozman): get rid of "injectedScript" notion.
838  return String16::concat(
839  "{\"injectedScriptId\":", String16::fromInteger(m_context->contextId()),
840  ",\"id\":", String16::fromInteger(id), "}");
841 }
842 
843 // static
844 Response InjectedScript::bindRemoteObjectIfNeeded(
845  int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value> value,
846  const String16& groupName, protocol::Runtime::RemoteObject* remoteObject) {
847  if (!remoteObject) return Response::OK();
848  if (remoteObject->hasValue()) return Response::OK();
849  if (remoteObject->hasUnserializableValue()) return Response::OK();
850  if (remoteObject->getType() != RemoteObject::TypeEnum::Undefined) {
851  v8::Isolate* isolate = context->GetIsolate();
852  V8InspectorImpl* inspector =
853  static_cast<V8InspectorImpl*>(v8::debug::GetInspector(isolate));
854  InspectedContext* inspectedContext =
855  inspector->getContext(InspectedContext::contextId(context));
856  InjectedScript* injectedScript =
857  inspectedContext ? inspectedContext->getInjectedScript(sessionId)
858  : nullptr;
859  if (!injectedScript) {
860  return Response::Error("Cannot find context with specified id");
861  }
862  remoteObject->setObjectId(injectedScript->bindObject(value, groupName));
863  }
864  return Response::OK();
865 }
866 
867 void InjectedScript::unbindObject(int id) {
868  m_idToWrappedObject.erase(id);
869  m_idToObjectGroupName.erase(id);
870 }
871 
872 } // namespace v8_inspector
static V8_WARN_UNUSED_RESULT MaybeLocal< Resolver > New(Local< Context > context)
Definition: api.cc:7240
V8_WARN_UNUSED_RESULT Maybe< bool > Resolve(Local< Context > context, Local< Value > value)
Definition: api.cc:7256
bool IsNativeError() const
Definition: api.cc:3460
V8_WARN_UNUSED_RESULT Maybe< int > GetLineNumber(Local< Context > context) const
Definition: api.cc:2808
V8_INLINE bool IsString() const
Definition: v8.h:10016
V8_INLINE int Length() const
Definition: v8.h:9751
static V8_INLINE Local< T > Cast(Local< S > that)
Definition: v8.h:251
Local< StackTrace > GetStackTrace() const
Definition: api.cc:2796
V8_WARN_UNUSED_RESULT MaybeLocal< Promise > Catch(Local< Context > context, Local< Function > handler)
Definition: api.cc:7294
V8_INLINE bool IsEmpty() const
Definition: v8.h:195
static MaybeLocal< Function > New(Local< Context > context, FunctionCallback callback, Local< Value > data=Local< Value >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect)
Definition: api.cc:4919
V8_INLINE Local< S > As() const
Definition: v8.h:266
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local< S > *out) const
Definition: v8.h:347
int GetFrameCount() const
Definition: api.cc:2910
int GetStartColumn() const
Definition: api.cc:2834
Definition: libplatform.h:13
V8_INLINE Local< Value > Data() const
Definition: v8.h:9727
Local< Promise > GetPromise()
Definition: api.cc:7250
ScriptOrigin GetScriptOrigin() const
Definition: api.cc:2782
V8_INLINE void SetWeak(P *parameter, typename WeakCallbackInfo< P >::Callback callback, WeakCallbackType type)
Definition: v8.h:9499
V8_INLINE void AnnotateStrongRetainer(const char *label)
Definition: v8.h:9520
V8_INLINE void Reset()
Definition: v8.h:9469
V8_INLINE Isolate * GetIsolate() const
Definition: v8.h:9733