user_gestures_native_handler.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/user_gestures_native_handler.h"
  5. #include "base/bind.h"
  6. #include "extensions/renderer/extension_interaction_provider.h"
  7. #include "extensions/renderer/script_context.h"
  8. #include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.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-function.h"
  12. #include "v8/include/v8-primitive.h"
  13. namespace extensions {
  14. UserGesturesNativeHandler::UserGesturesNativeHandler(ScriptContext* context)
  15. : ObjectBackedNativeHandler(context) {}
  16. void UserGesturesNativeHandler::AddRoutes() {
  17. RouteHandlerFunction(
  18. "IsProcessingUserGesture", "test",
  19. base::BindRepeating(&UserGesturesNativeHandler::IsProcessingUserGesture,
  20. base::Unretained(this)));
  21. RouteHandlerFunction(
  22. "RunWithUserGesture", "test",
  23. base::BindRepeating(
  24. &UserGesturesNativeHandler::RunWithUserActivationForTest,
  25. base::Unretained(this)));
  26. }
  27. void UserGesturesNativeHandler::IsProcessingUserGesture(
  28. const v8::FunctionCallbackInfo<v8::Value>& args) {
  29. args.GetReturnValue().Set(v8::Boolean::New(
  30. args.GetIsolate(),
  31. ExtensionInteractionProvider::HasActiveExtensionInteraction(
  32. context()->v8_context())));
  33. }
  34. void UserGesturesNativeHandler::RunWithUserActivationForTest(
  35. const v8::FunctionCallbackInfo<v8::Value>& args) {
  36. CHECK_EQ(args.Length(), 1);
  37. CHECK(args[0]->IsFunction());
  38. if (context()->web_frame()) {
  39. context()->web_frame()->NotifyUserActivation(
  40. blink::mojom::UserActivationNotificationType::kTest);
  41. context()->SafeCallFunction(v8::Local<v8::Function>::Cast(args[0]), 0,
  42. nullptr);
  43. } else if (context()->IsForServiceWorker()) {
  44. // Note |scoped_extension_interaction| requires a HandleScope.
  45. const v8::HandleScope handle_scope(context()->isolate());
  46. const std::unique_ptr<InteractionProvider::Scope> scoped_interaction =
  47. ExtensionInteractionProvider::Scope::ForWorker(context()->v8_context());
  48. context()->SafeCallFunction(v8::Local<v8::Function>::Cast(args[0]), 0,
  49. nullptr);
  50. }
  51. }
  52. } // namespace extensions