No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavascriptPromiseHandler.h 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright © 2020 The CefSharp Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  4. #pragma once
  5. #include "include/cef_v8.h"
  6. #include "..\CefSharp.Core.Runtime\Internals\Messaging\Messages.h"
  7. #include "..\CefSharp.Core.Runtime\Internals\Serialization\Primitives.h"
  8. #include "Serialization\V8Serialization.h"
  9. using namespace System;
  10. using namespace CefSharp::Internals::Messaging;
  11. using namespace CefSharp::BrowserSubprocess::Serialization;
  12. namespace CefSharp
  13. {
  14. namespace BrowserSubprocess
  15. {
  16. const CefString kSendEvalScriptResponse = CefString("SendEvalScriptResponse");
  17. const CefString kSendEvalScriptResponseCamelCase = CefString("sendEvalScriptResponse");
  18. private class JavascriptPromiseHandler : public CefV8Handler
  19. {
  20. public:
  21. virtual bool Execute(const CefString& name,
  22. CefRefPtr<CefV8Value> object,
  23. const CefV8ValueList& arguments,
  24. CefRefPtr<CefV8Value>& retval,
  25. CefString& exception)
  26. {
  27. if (arguments.size() < 2)
  28. {
  29. //TODO: Improve error message
  30. exception = "Must specify a callback Id and then/catch.";
  31. return true;
  32. }
  33. auto callbackId = arguments[0]->GetIntValue();
  34. if (callbackId == 0)
  35. {
  36. exception = "Invalid callback Id";
  37. return true;
  38. }
  39. auto success = arguments[1]->GetBoolValue();
  40. auto javascriptCallback = arguments.size() < 4 ? false : arguments[3]->GetBoolValue();
  41. auto response = CefProcessMessage::Create(javascriptCallback ? kJavascriptCallbackResponse : kEvaluateJavascriptResponse);
  42. auto responseArgList = response->GetArgumentList();
  43. //Success
  44. responseArgList->SetBool(0, success);
  45. //Callback Id
  46. SetInt64(responseArgList, 1, callbackId);
  47. if (exception == "")
  48. {
  49. if (success)
  50. {
  51. SerializeV8Object(arguments[2], responseArgList, 2, nullptr);
  52. }
  53. else
  54. {
  55. auto reason = arguments[2];
  56. responseArgList->SetString(2, reason->GetStringValue());
  57. }
  58. }
  59. else
  60. {
  61. responseArgList->SetString(2, exception);
  62. }
  63. auto context = CefV8Context::GetCurrentContext();
  64. auto frame = context->GetFrame();
  65. frame->SendProcessMessage(CefProcessId::PID_BROWSER, response);
  66. return false;
  67. }
  68. IMPLEMENT_REFCOUNTINGM(JavascriptPromiseHandler);
  69. };
  70. }
  71. }