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