admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright © 2019 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 "RegisterBoundObjectRegistry.h"
#include "..\CefSharp.Core\Internals\Messaging\Messages.h"
#include "..\CefSharp.Core\Internals\Serialization\Primitives.h"
 
using namespace System;
using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;
 
namespace CefSharp
{
    const CefString kBindObjectAsync = CefString("BindObjectAsync");
    const CefString kBindObjectAsyncCamelCase = CefString("bindObjectAsync");
 
    private class BindObjectAsyncHandler : public CefV8Handler
    {
    private:
        gcroot<RegisterBoundObjectRegistry^> _callbackRegistry;
        gcroot<Dictionary<String^, JavascriptObject^>^> _javascriptObjects;
        gcroot<CefBrowserWrapper^> _browserWrapper;
 
    public:
        BindObjectAsyncHandler(RegisterBoundObjectRegistry^ callbackRegistery, Dictionary<String^, JavascriptObject^>^ javascriptObjects, CefBrowserWrapper^ browserWrapper)
        {
            _callbackRegistry = callbackRegistery;
            _javascriptObjects = javascriptObjects;
            _browserWrapper = browserWrapper;
        }
 
        ~BindObjectAsyncHandler()
        {
            _callbackRegistry = nullptr;
            _javascriptObjects = nullptr;
            _browserWrapper = nullptr;
        }
 
        bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception) OVERRIDE
        {
            auto context = CefV8Context::GetCurrentContext();
 
            if (context.get() && context->Enter())
            {
                try
                {
                    auto params = CefListValue::Create();
 
                    auto boundObjectRequired = false;
                    auto notifyIfAlreadyBound = false;
                    auto ignoreCache = false;
                    auto cachedObjects = gcnew List<JavascriptObject^>();
                    //TODO: Create object to represent this information
                    auto objectNamesWithBoundStatus = gcnew List<Tuple<String^, bool, bool>^>();
                    auto objectCount = 0;
 
                    if (arguments.size() > 0)
                    {
                        objectCount = (int)arguments.size();
 
                        //If first argument is an object, we'll see if it contains config values
                        if (arguments[0]->IsObject())
                        {
                            //Upper and camelcase options are supported
                            notifyIfAlreadyBound = GetV8BoolValue(arguments[0], "NotifyIfAlreadyBound", "notifyIfAlreadyBound");
                            ignoreCache = GetV8BoolValue(arguments[0], "IgnoreCache", "ignoreCache");
 
                            //If we have a config object then we remove that from the count
                            objectCount = objectCount - 1;
                        }
 
                        auto global = context->GetGlobal();
 
                        //Loop through all arguments and ignore anything that's not a string
                        for (auto i = 0; i < arguments.size(); i++)
                        {
                            //Validate arg as being a string
                            if (arguments[i]->IsString())
                            {
                                auto objectName = arguments[i]->GetStringValue();
                                auto managedObjectName = StringUtils::ToClr(objectName);
                                auto alreadyBound = global->HasValue(objectName);
                                auto cached = false;
 
                                //Check if the object has already been bound
                                if (alreadyBound)
                                {
                                    cached = _javascriptObjects->ContainsKey(managedObjectName);
                                }
                                else
                                {
                                    //If no matching object found then we'll add the object name to the list
                                    boundObjectRequired = true;
                                    params->SetString(i, objectName);
 
                                    JavascriptObject^ obj;
                                    if (_javascriptObjects->TryGetValue(managedObjectName, obj))
                                    {
                                        cachedObjects->Add(obj);
 
                                        cached = true;
                                    }
                                }
 
                                objectNamesWithBoundStatus->Add(Tuple::Create(managedObjectName, alreadyBound, cached));
                            }
                        }
                    }
                    else
                    {
                        //No objects names were specified so we default to makeing the request
                        boundObjectRequired = true;
                    }
 
                    auto frame = context->GetFrame();
 
                    if (frame.get() && frame->IsValid())
                    {
                        if (boundObjectRequired || ignoreCache)
                        {
                            //If the number of cached objects matches the number of args
                            //(we have a cached copy of all requested objects)
                            //then we'll immediately bind the cached objects
                            if (cachedObjects->Count == objectCount && ignoreCache == false)
                            {
                                if (Object::ReferenceEquals(_browserWrapper, nullptr))
                                {
                                    exception = "BindObjectAsyncHandler::Execute - Browser wrapper null, unable to bind objects";
 
                                    return true;
                                }
 
                                auto browser = context->GetBrowser();
 
                                auto rootObjectWrappers = _browserWrapper->JavascriptRootObjectWrappers;
 
                                JavascriptRootObjectWrapper^ rootObject;
                                if (!rootObjectWrappers->TryGetValue(frame->GetIdentifier(), rootObject))
                                {
                                    rootObject = gcnew JavascriptRootObjectWrapper(browser->GetIdentifier(), _browserWrapper->BrowserProcess);
                                    rootObjectWrappers->TryAdd(frame->GetIdentifier(), rootObject);
                                }
 
                                //Cached objects only contains a list of objects not already bound
                                rootObject->Bind(cachedObjects, context->GetGlobal());
 
                                //Objects already bound or ignore cache
                                CefRefPtr<CefV8Value> promiseResolve;
                                CefRefPtr<CefV8Exception> promiseException;
 
                                auto promiseResolveScript = StringUtils::ToNative("Promise.resolve({Success:true, Count:" + cachedObjects->Count + ", Message:'OK'});");
 
                                if (context->Eval(promiseResolveScript, CefString(), 0, promiseResolve, promiseException))
                                {
                                    retval = promiseResolve;
                                }
                                else
                                {
                                    exception = promiseException->GetMessage();
 
                                    return true;
                                }
 
                                NotifyObjectBound(frame, objectNamesWithBoundStatus);
                            }
                            else
                            {
                                CefRefPtr<CefV8Value> promiseData;
                                CefRefPtr<CefV8Exception> promiseException;
                                //this will create a promise and give us the reject/resolve functions {p: Promise, res: resolve(), rej: reject()}
                                if (!context->Eval(CefAppUnmanagedWrapper::kPromiseCreatorScript, CefString(), 0, promiseData, promiseException))
                                {
                                    exception = promiseException->GetMessage();
 
                                    return true;
                                }
 
                                //when refreshing the browser this is sometimes null, in this case return true and log message
                                //https://github.com/cefsharp/CefSharp/pull/2446
                                if (promiseData == NULL)
                                {
                                    LOG(WARNING) << "BindObjectAsyncHandler::Execute promiseData returned NULL";
 
                                    return true;
                                }
 
                                //return the promose
                                retval = promiseData->GetValue("p");
 
                                //References to the promise resolve and reject methods
                                auto resolve = promiseData->GetValue("res");
                                auto reject = promiseData->GetValue("rej");
 
                                auto callback = gcnew JavascriptAsyncMethodCallback(context, resolve, reject);
 
                                auto request = CefProcessMessage::Create(kJavascriptRootObjectRequest);
                                auto argList = request->GetArgumentList();
 
                                //Obtain a callbackId then send off the Request for objects
                                auto callbackId = _callbackRegistry->SaveMethodCallback(callback);
 
                                SetInt64(argList, 0, callbackId);
                                argList->SetList(1, params);
 
                                frame->SendProcessMessage(CefProcessId::PID_BROWSER, request);
                            }
                        }
                        else
                        {
                            //Objects already bound or ignore cache
                            CefRefPtr<CefV8Value> promiseResolve;
                            CefRefPtr<CefV8Exception> promiseException;
 
                            auto promiseResolveScript = CefString("Promise.resolve({Success:false, Count:0, Message:'Object(s) already bound'});");
 
                            if (context->Eval(promiseResolveScript, CefString(), 0, promiseResolve, promiseException))
                            {
                                retval = promiseResolve;
 
                                if (notifyIfAlreadyBound)
                                {
                                    NotifyObjectBound(frame, objectNamesWithBoundStatus);
                                }
                            }
                            else
                            {
                                exception = promiseException->GetMessage();
                            }
                        }
                    }
                    else
                    {
                        exception = "BindObjectAsyncHandler::Execute - Frame is invalid.";
                    }
                }
                finally
                {
                    context->Exit();
                }
            }
            else
            {
                exception = "BindObjectAsyncHandler::Execute - Unable to Get or Enter Context";
            }
 
 
            return true;
        }
 
    private:
        void NotifyObjectBound(const CefRefPtr<CefFrame> frame, List<Tuple<String^, bool, bool>^>^ objectNamesWithBoundStatus)
        {
            //Send message notifying Browser Process of which objects were bound
            //We do this after the objects have been created in the V8Context to gurantee
            //they are accessible.
            auto msg = CefProcessMessage::Create(kJavascriptObjectsBoundInJavascript);
            auto args = msg->GetArgumentList();
 
            auto boundObjects = CefListValue::Create();
            auto index = 0;
 
            for each(auto obj in objectNamesWithBoundStatus)
            {
                auto dict = CefDictionaryValue::Create();
 
                auto name = obj->Item1;
                auto alreadyBound = obj->Item2;
                auto isCached = obj->Item3;
                dict->SetString("Name", StringUtils::ToNative(name));
                dict->SetBool("IsCached", isCached);
                dict->SetBool("AlreadyBound", alreadyBound);
 
                boundObjects->SetDictionary(index++, dict);
            }
 
            args->SetList(0, boundObjects);
 
            frame->SendProcessMessage(CefProcessId::PID_BROWSER, msg);
        }
 
        bool GetV8BoolValue(const CefRefPtr<CefV8Value> val, const CefString key, const CefString camelCaseKey)
        {
            if (val->HasValue(key))
            {
                auto obj = val->GetValue(key);
                if (obj->IsBool())
                {
                    return obj->GetBoolValue();
                }
            }
 
            if (val->HasValue(camelCaseKey))
            {
                auto obj = val->GetValue(camelCaseKey);
                if (obj->IsBool())
                {
                    return obj->GetBoolValue();
                }
            }
 
            return false;
        }
 
 
        IMPLEMENT_REFCOUNTING(BindObjectAsyncHandler);
    };
}