activity_log_converter_strategy.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 The Chromium 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. #include "extensions/renderer/activity_log_converter_strategy.h"
  5. #include <memory>
  6. #include "base/values.h"
  7. #include "v8/include/v8-container.h"
  8. #include "v8/include/v8-exception.h"
  9. #include "v8/include/v8-function.h"
  10. #include "v8/include/v8-isolate.h"
  11. #include "v8/include/v8-object.h"
  12. #include "v8/include/v8-primitive.h"
  13. #include "v8/include/v8-value.h"
  14. namespace extensions {
  15. namespace {
  16. // Summarize a V8 value. This performs a shallow conversion in all cases, and
  17. // returns only a string with a description of the value (e.g.,
  18. // "[HTMLElement]").
  19. std::unique_ptr<base::Value> SummarizeV8Value(v8::Isolate* isolate,
  20. v8::Local<v8::Object> object) {
  21. v8::TryCatch try_catch(isolate);
  22. v8::Isolate::DisallowJavascriptExecutionScope scope(
  23. isolate, v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
  24. v8::Local<v8::String> name = v8::String::NewFromUtf8Literal(isolate, "[");
  25. if (object->IsFunction()) {
  26. name = v8::String::Concat(
  27. isolate, name, v8::String::NewFromUtf8Literal(isolate, "Function"));
  28. v8::Local<v8::Value> fname =
  29. v8::Local<v8::Function>::Cast(object)->GetName();
  30. if (fname->IsString() && v8::Local<v8::String>::Cast(fname)->Length()) {
  31. name = v8::String::Concat(isolate, name,
  32. v8::String::NewFromUtf8Literal(isolate, " "));
  33. name =
  34. v8::String::Concat(isolate, name, v8::Local<v8::String>::Cast(fname));
  35. name = v8::String::Concat(isolate, name,
  36. v8::String::NewFromUtf8Literal(isolate, "()"));
  37. }
  38. } else {
  39. name = v8::String::Concat(isolate, name, object->GetConstructorName());
  40. }
  41. name = v8::String::Concat(isolate, name,
  42. v8::String::NewFromUtf8Literal(isolate, "]"));
  43. if (try_catch.HasCaught()) {
  44. return std::make_unique<base::Value>("[JS Execution Exception]");
  45. }
  46. return std::make_unique<base::Value>(
  47. std::string(*v8::String::Utf8Value(isolate, name)));
  48. }
  49. } // namespace
  50. ActivityLogConverterStrategy::ActivityLogConverterStrategy() {}
  51. ActivityLogConverterStrategy::~ActivityLogConverterStrategy() {}
  52. bool ActivityLogConverterStrategy::FromV8Object(
  53. v8::Local<v8::Object> value,
  54. std::unique_ptr<base::Value>* out,
  55. v8::Isolate* isolate) {
  56. return FromV8Internal(value, out, isolate);
  57. }
  58. bool ActivityLogConverterStrategy::FromV8Array(
  59. v8::Local<v8::Array> value,
  60. std::unique_ptr<base::Value>* out,
  61. v8::Isolate* isolate) {
  62. return FromV8Internal(value, out, isolate);
  63. }
  64. bool ActivityLogConverterStrategy::FromV8Internal(
  65. v8::Local<v8::Object> value,
  66. std::unique_ptr<base::Value>* out,
  67. v8::Isolate* isolate) const {
  68. *out = SummarizeV8Value(isolate, value);
  69. return true;
  70. }
  71. } // namespace extensions