|
23 | 23 | #include "Wrapper\Browser.h"
|
24 | 24 | #include "..\CefSharp.Core.Runtime\Internals\Messaging\Messages.h"
|
25 | 25 | #include "..\CefSharp.Core.Runtime\Internals\Serialization\Primitives.h"
|
| 26 | +#include <include/cef_parser.h> |
26 | 27 |
|
27 | 28 | using namespace System;
|
28 | 29 | using namespace System::Diagnostics;
|
@@ -100,6 +101,16 @@ namespace CefSharp
|
100 | 101 | }
|
101 | 102 |
|
102 | 103 | _jsBindingApiEnabled = extraInfo->GetBool("JavascriptBindingApiEnabled");
|
| 104 | + _jsBindingApiHasAllowOrigins = extraInfo->GetBool("JavascriptBindingApiHasAllowOrigins"); |
| 105 | + |
| 106 | + if (_jsBindingApiHasAllowOrigins) |
| 107 | + { |
| 108 | + auto allowOrigins = extraInfo->GetList("JavascriptBindingApiAllowOrigins"); |
| 109 | + if (allowOrigins.get() && allowOrigins->IsValid()) |
| 110 | + { |
| 111 | + _jsBindingApiAllowOrigins = allowOrigins->Copy(); |
| 112 | + } |
| 113 | + } |
103 | 114 |
|
104 | 115 | if (extraInfo->HasKey("JsBindingPropertyName") || extraInfo->HasKey("JsBindingPropertyNameCamelCase"))
|
105 | 116 | {
|
@@ -149,50 +160,83 @@ namespace CefSharp
|
149 | 160 |
|
150 | 161 | if (_jsBindingApiEnabled)
|
151 | 162 | {
|
152 |
| - //TODO: Look at adding some sort of javascript mapping layer to reduce the code duplication |
153 |
| - auto global = context->GetGlobal(); |
154 |
| - auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier()); |
155 |
| - auto processId = System::Diagnostics::Process::GetCurrentProcess()->Id; |
156 |
| - |
157 |
| - //TODO: JSB: Split functions into their own classes |
158 |
| - //Browser wrapper is only used for BindObjectAsync |
159 |
| - auto bindObjAsyncFunction = CefV8Value::CreateFunction(kBindObjectAsync, new BindObjectAsyncHandler(_registerBoundObjectRegistry, _javascriptObjects, browserWrapper)); |
160 |
| - auto unBindObjFunction = CefV8Value::CreateFunction(kDeleteBoundObject, new RegisterBoundObjectHandler(_javascriptObjects)); |
161 |
| - auto removeObjectFromCacheFunction = CefV8Value::CreateFunction(kRemoveObjectFromCache, new RegisterBoundObjectHandler(_javascriptObjects)); |
162 |
| - auto isObjectCachedFunction = CefV8Value::CreateFunction(kIsObjectCached, new RegisterBoundObjectHandler(_javascriptObjects)); |
163 |
| - auto postMessageFunction = CefV8Value::CreateFunction(kPostMessage, new JavascriptPostMessageHandler(rootObject == nullptr ? nullptr : rootObject->CallbackRegistry)); |
164 |
| - auto promiseHandlerFunction = CefV8Value::CreateFunction(kSendEvalScriptResponse, new JavascriptPromiseHandler()); |
165 |
| - |
166 |
| - //By default We'll support both CefSharp and cefSharp, for those who prefer the JS style |
167 |
| - auto createCefSharpObj = !_jsBindingPropertyName.empty(); |
168 |
| - auto createCefSharpObjCamelCase = !_jsBindingPropertyNameCamelCase.empty(); |
169 |
| - |
170 |
| - if (createCefSharpObj) |
| 163 | + auto createObjects = true; |
| 164 | + |
| 165 | + if (_jsBindingApiHasAllowOrigins) |
171 | 166 | {
|
172 |
| - auto cefSharpObj = CefV8Value::CreateObject(nullptr, nullptr); |
173 |
| - cefSharpObj->SetValue(kBindObjectAsync, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
174 |
| - cefSharpObj->SetValue(kDeleteBoundObject, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
175 |
| - cefSharpObj->SetValue(kRemoveObjectFromCache, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
176 |
| - cefSharpObj->SetValue(kIsObjectCached, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
177 |
| - cefSharpObj->SetValue(kPostMessage, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
178 |
| - cefSharpObj->SetValue(kSendEvalScriptResponse, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
179 |
| - cefSharpObj->SetValue(kRenderProcessId, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
180 |
| - |
181 |
| - global->SetValue(_jsBindingPropertyName, cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); |
| 167 | + createObjects = false; |
| 168 | + |
| 169 | + auto frameUrl = frame->GetURL(); |
| 170 | + |
| 171 | + CefURLParts frameUrlParts; |
| 172 | + |
| 173 | + if (CefParseURL(frameUrl, frameUrlParts)) |
| 174 | + { |
| 175 | + auto frameUrlOrigin = CefString(frameUrlParts.origin.str, frameUrlParts.origin.length); |
| 176 | + |
| 177 | + auto size = static_cast<int>(_jsBindingApiAllowOrigins->GetSize()); |
| 178 | + |
| 179 | + for (int i = 0; i < size; i++) |
| 180 | + { |
| 181 | + auto origin = _jsBindingApiAllowOrigins->GetString(i); |
| 182 | + |
| 183 | + if (origin.compare(frameUrlOrigin) == 0) |
| 184 | + { |
| 185 | + createObjects = true; |
| 186 | + |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
182 | 191 | }
|
183 | 192 |
|
184 |
| - if (createCefSharpObjCamelCase) |
| 193 | + if (createObjects) |
185 | 194 | {
|
186 |
| - auto cefSharpObjCamelCase = CefV8Value::CreateObject(nullptr, nullptr); |
187 |
| - cefSharpObjCamelCase->SetValue(kBindObjectAsyncCamelCase, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
188 |
| - cefSharpObjCamelCase->SetValue(kDeleteBoundObjectCamelCase, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
189 |
| - cefSharpObjCamelCase->SetValue(kRemoveObjectFromCacheCamelCase, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
190 |
| - cefSharpObjCamelCase->SetValue(kIsObjectCachedCamelCase, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
191 |
| - cefSharpObjCamelCase->SetValue(kPostMessageCamelCase, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
192 |
| - cefSharpObjCamelCase->SetValue(kSendEvalScriptResponseCamelCase, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
193 |
| - cefSharpObjCamelCase->SetValue(kRenderProcessIdCamelCase, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
194 |
| - |
195 |
| - global->SetValue(_jsBindingPropertyNameCamelCase, cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); |
| 195 | + //TODO: Look at adding some sort of javascript mapping layer to reduce the code duplication |
| 196 | + auto global = context->GetGlobal(); |
| 197 | + auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier()); |
| 198 | + auto processId = System::Diagnostics::Process::GetCurrentProcess()->Id; |
| 199 | + |
| 200 | + //TODO: JSB: Split functions into their own classes |
| 201 | + //Browser wrapper is only used for BindObjectAsync |
| 202 | + auto bindObjAsyncFunction = CefV8Value::CreateFunction(kBindObjectAsync, new BindObjectAsyncHandler(_registerBoundObjectRegistry, _javascriptObjects, browserWrapper)); |
| 203 | + auto unBindObjFunction = CefV8Value::CreateFunction(kDeleteBoundObject, new RegisterBoundObjectHandler(_javascriptObjects)); |
| 204 | + auto removeObjectFromCacheFunction = CefV8Value::CreateFunction(kRemoveObjectFromCache, new RegisterBoundObjectHandler(_javascriptObjects)); |
| 205 | + auto isObjectCachedFunction = CefV8Value::CreateFunction(kIsObjectCached, new RegisterBoundObjectHandler(_javascriptObjects)); |
| 206 | + auto postMessageFunction = CefV8Value::CreateFunction(kPostMessage, new JavascriptPostMessageHandler(rootObject == nullptr ? nullptr : rootObject->CallbackRegistry)); |
| 207 | + auto promiseHandlerFunction = CefV8Value::CreateFunction(kSendEvalScriptResponse, new JavascriptPromiseHandler()); |
| 208 | + |
| 209 | + //By default We'll support both CefSharp and cefSharp, for those who prefer the JS style |
| 210 | + auto createCefSharpObj = !_jsBindingPropertyName.empty(); |
| 211 | + auto createCefSharpObjCamelCase = !_jsBindingPropertyNameCamelCase.empty(); |
| 212 | + |
| 213 | + if (createCefSharpObj) |
| 214 | + { |
| 215 | + auto cefSharpObj = CefV8Value::CreateObject(nullptr, nullptr); |
| 216 | + cefSharpObj->SetValue(kBindObjectAsync, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 217 | + cefSharpObj->SetValue(kDeleteBoundObject, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 218 | + cefSharpObj->SetValue(kRemoveObjectFromCache, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 219 | + cefSharpObj->SetValue(kIsObjectCached, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 220 | + cefSharpObj->SetValue(kPostMessage, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 221 | + cefSharpObj->SetValue(kSendEvalScriptResponse, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 222 | + cefSharpObj->SetValue(kRenderProcessId, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 223 | + |
| 224 | + global->SetValue(_jsBindingPropertyName, cefSharpObj, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); |
| 225 | + } |
| 226 | + |
| 227 | + if (createCefSharpObjCamelCase) |
| 228 | + { |
| 229 | + auto cefSharpObjCamelCase = CefV8Value::CreateObject(nullptr, nullptr); |
| 230 | + cefSharpObjCamelCase->SetValue(kBindObjectAsyncCamelCase, bindObjAsyncFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 231 | + cefSharpObjCamelCase->SetValue(kDeleteBoundObjectCamelCase, unBindObjFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 232 | + cefSharpObjCamelCase->SetValue(kRemoveObjectFromCacheCamelCase, removeObjectFromCacheFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 233 | + cefSharpObjCamelCase->SetValue(kIsObjectCachedCamelCase, isObjectCachedFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 234 | + cefSharpObjCamelCase->SetValue(kPostMessageCamelCase, postMessageFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 235 | + cefSharpObjCamelCase->SetValue(kSendEvalScriptResponseCamelCase, promiseHandlerFunction, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 236 | + cefSharpObjCamelCase->SetValue(kRenderProcessIdCamelCase, CefV8Value::CreateInt(processId), CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); |
| 237 | + |
| 238 | + global->SetValue(_jsBindingPropertyNameCamelCase, cefSharpObjCamelCase, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_READONLY); |
| 239 | + } |
196 | 240 | }
|
197 | 241 | }
|
198 | 242 |
|
|
0 commit comments