况洋洋
2025-07-04 0d247bd2a17e0f99f3609774a1ce54ae00857997
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
// Copyright © 2022 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"
 
using namespace System;
 
namespace CefSharp
{
    namespace BrowserSubprocess
    {
        private class JavascriptPromiseResolverCatch : public CefV8Handler
        {
            int64_t _callbackId;
            bool _isJsCallback;
 
        public:
            JavascriptPromiseResolverCatch(int64_t callbackId, bool isJsCallback) : _callbackId(callbackId), _isJsCallback(isJsCallback)
            {
 
            }
 
            virtual bool Execute(const CefString& name,
                CefRefPtr<CefV8Value> object,
                const CefV8ValueList& arguments,
                CefRefPtr<CefV8Value>& retval,
                CefString& exception)
            {
                auto context = CefV8Context::GetCurrentContext();
 
                auto reason = arguments[0];
                CefString reasonString;
 
                if (reason->IsString())
                {
                    reasonString = reason->GetStringValue();
                }
                else
                {
                    //Convert value to String
                    auto strFunc = context->GetGlobal()->GetValue("String");
                    CefV8ValueList args;
                    args.push_back(reason);
                    auto strVal = strFunc->ExecuteFunction(nullptr, args);                    
 
                    reasonString = strVal->GetStringValue();
                }
 
                auto response = CefProcessMessage::Create(_isJsCallback ? kJavascriptCallbackResponse : kEvaluateJavascriptResponse);
                auto responseArgList = response->GetArgumentList();
 
                //Success
                responseArgList->SetBool(0, false);
                //Callback Id
                SetInt64(responseArgList, 1, _callbackId);
                responseArgList->SetString(2, reasonString);
 
                auto frame = context->GetFrame();
 
                frame->SendProcessMessage(CefProcessId::PID_BROWSER, response);
 
                return true;
            }
 
            IMPLEMENT_REFCOUNTINGM(JavascriptPromiseResolverCatch);
        };
    }
}