v8_context_native_handler.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/v8_context_native_handler.h"
  5. #include "base/bind.h"
  6. #include "extensions/common/features/feature.h"
  7. #include "extensions/renderer/script_context.h"
  8. #include "extensions/renderer/script_context_set.h"
  9. #include "third_party/blink/public/web/web_local_frame.h"
  10. #include "v8/include/v8-function-callback.h"
  11. #include "v8/include/v8-object.h"
  12. #include "v8/include/v8-primitive.h"
  13. namespace extensions {
  14. V8ContextNativeHandler::V8ContextNativeHandler(ScriptContext* context)
  15. : ObjectBackedNativeHandler(context), context_(context) {}
  16. void V8ContextNativeHandler::AddRoutes() {
  17. RouteHandlerFunction(
  18. "GetAvailability",
  19. base::BindRepeating(&V8ContextNativeHandler::GetAvailability,
  20. base::Unretained(this)));
  21. RouteHandlerFunction(
  22. "GetModuleSystem",
  23. base::BindRepeating(&V8ContextNativeHandler::GetModuleSystem,
  24. base::Unretained(this)));
  25. }
  26. void V8ContextNativeHandler::GetAvailability(
  27. const v8::FunctionCallbackInfo<v8::Value>& args) {
  28. CHECK_EQ(args.Length(), 1);
  29. v8::Isolate* isolate = args.GetIsolate();
  30. std::string api_name = *v8::String::Utf8Value(isolate, args[0]);
  31. Feature::Availability availability = context_->GetAvailability(api_name);
  32. v8::Local<v8::Context> context = context_->v8_context();
  33. v8::Local<v8::Object> ret = v8::Object::New(isolate);
  34. v8::Maybe<bool> maybe = ret->SetPrototype(context, v8::Null(isolate));
  35. CHECK(maybe.IsJust() && maybe.FromJust());
  36. ret->Set(context,
  37. v8::String::NewFromUtf8(isolate, "is_available",
  38. v8::NewStringType::kInternalized)
  39. .ToLocalChecked(),
  40. v8::Boolean::New(isolate, availability.is_available()))
  41. .ToChecked();
  42. ret->Set(context,
  43. v8::String::NewFromUtf8(isolate, "message",
  44. v8::NewStringType::kInternalized)
  45. .ToLocalChecked(),
  46. v8::String::NewFromUtf8(isolate, availability.message().c_str(),
  47. v8::NewStringType::kNormal)
  48. .ToLocalChecked())
  49. .ToChecked();
  50. ret->Set(context,
  51. v8::String::NewFromUtf8(isolate, "result",
  52. v8::NewStringType::kInternalized)
  53. .ToLocalChecked(),
  54. v8::Integer::New(isolate, availability.result()))
  55. .ToChecked();
  56. args.GetReturnValue().Set(ret);
  57. }
  58. void V8ContextNativeHandler::GetModuleSystem(
  59. const v8::FunctionCallbackInfo<v8::Value>& args) {
  60. CHECK_EQ(args.Length(), 1);
  61. CHECK(args[0]->IsObject());
  62. ScriptContext* context = ScriptContextSet::GetContextByObject(
  63. v8::Local<v8::Object>::Cast(args[0]));
  64. if (context && blink::WebFrame::ScriptCanAccess(context->web_frame()))
  65. args.GetReturnValue().Set(context->module_system()->NewInstance());
  66. }
  67. } // namespace extensions