-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavascriptCallbackProxy.cpp
146 lines (122 loc) · 4.87 KB
/
JavascriptCallbackProxy.cpp
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
// Copyright © 2015 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.
#include "Stdafx.h"
#include "JavascriptCallbackProxy.h"
#include "Messaging/Messages.h"
#include "Serialization/Primitives.h"
#include "Serialization/V8Serialization.h"
using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;
namespace CefSharp
{
namespace Internals
{
Task<JavascriptResponse^>^ JavascriptCallbackProxy::ExecuteAsync(cli::array<Object^>^ parameters)
{
return ExecuteWithTimeoutAsync(Nullable<TimeSpan>(), parameters);
}
Task<JavascriptResponse^>^ JavascriptCallbackProxy::ExecuteWithTimeoutAsync(Nullable<TimeSpan> timeout, cli::array<Object^>^ parameters)
{
DisposedGuard();
auto browser = GetBrowser();
if (browser == nullptr)
{
throw gcnew InvalidOperationException("Browser instance is null. Check CanExecute before calling this method.");
}
auto browserWrapper = static_cast<CefBrowserWrapper^>(browser);
auto javascriptNameConverter = GetJavascriptNameConverter();
auto doneCallback = _pendingTasks->CreateJavascriptCallbackPendingTask(_callback->Id, timeout);
auto callbackMessage = CefProcessMessage::Create(kJavascriptCallbackRequest);
auto argList = callbackMessage->GetArgumentList();
SetInt64(argList, 0, doneCallback.Key);
SetInt64(argList, 1, _callback->Id);
auto paramList = CefListValue::Create();
for (int i = 0; i < parameters->Length; i++)
{
auto param = parameters[i];
SerializeV8Object(paramList, i, param, javascriptNameConverter);
}
argList->SetList(2, paramList);
auto frame = browserWrapper->Browser->GetFrameByIdentifier(StringUtils::ToNative(_callback->FrameId));
if (frame.get() && frame->IsValid())
{
frame->SendProcessMessage(CefProcessId::PID_RENDERER, callbackMessage);
return doneCallback.Value->Task;
}
auto invalidFrameResponse = gcnew JavascriptResponse();
invalidFrameResponse->Success = false;
invalidFrameResponse->Message = "Frame with Id:" + _callback->FrameId + " is no longer valid.";
return Task::FromResult(invalidFrameResponse);
}
CefRefPtr<CefProcessMessage> JavascriptCallbackProxy::CreateDestroyMessage()
{
auto result = CefProcessMessage::Create(kJavascriptCallbackDestroyRequest);
auto argList = result->GetArgumentList();
SetInt64(argList, 0, _callback->Id);
return result;
}
//TODO: Reduce code duplication
IBrowser^ JavascriptCallbackProxy::GetBrowser()
{
IBrowser^ result = nullptr;
IBrowserAdapter^ browserAdapter;
if (_browserAdapter->TryGetTarget(browserAdapter))
{
if (!browserAdapter->IsDisposed)
{
result = browserAdapter->GetBrowser(_callback->BrowserId);
}
}
return result;
}
IJavascriptNameConverter^ JavascriptCallbackProxy::GetJavascriptNameConverter()
{
IJavascriptNameConverter^ result = nullptr;
IBrowserAdapter^ browserAdapter;
if (_browserAdapter->TryGetTarget(browserAdapter))
{
if (!browserAdapter->IsDisposed && browserAdapter->JavascriptObjectRepository != nullptr)
{
result = browserAdapter->JavascriptObjectRepository->NameConverter;
}
}
return result;
}
Int64 JavascriptCallbackProxy::Id::get()
{
DisposedGuard();
return _callback->Id;
}
bool JavascriptCallbackProxy::IsDisposed::get()
{
return _disposed;
}
bool JavascriptCallbackProxy::CanExecute::get()
{
if (_disposed)
{
return false;
}
auto browser = GetBrowser();
if (browser == nullptr)
{
return false;
}
//If the frame Id is still valid then we can attemp to execute the callback
auto frame = browser->GetFrameByIdentifier(_callback->FrameId);
if (frame == nullptr)
{
return false;
}
return frame->IsValid;
}
void JavascriptCallbackProxy::DisposedGuard()
{
if (_disposed)
{
throw gcnew ObjectDisposedException("JavascriptCallbackProxy");
}
}
}
}