Skip to content

Commit

Permalink
SendProcessMessage - Remove frame/browserid from messages
Browse files Browse the repository at this point in the history
Not complete or tested yet
  • Loading branch information
amaitland committed Jun 13, 2019
1 parent 165b267 commit df617c1
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ namespace CefSharp
SerializeV8Object(arguments[i], params, i, _callbackRegistry);
}

SetInt64(argList, 0, frame->GetIdentifier());
SetInt64(argList, 1, _objectId);
SetInt64(argList, 2, callbackId);
argList->SetString(3, name);
argList->SetList(4, params);
SetInt64(argList, 0, _objectId);
SetInt64(argList, 1, callbackId);
argList->SetString(2, name);
argList->SetList(3, params);

frame->SendProcessMessage(CefProcessId::PID_BROWSER, request);

Expand Down
70 changes: 28 additions & 42 deletions CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ namespace CefSharp
//have been created
auto contextCreatedMessage = CefProcessMessage::Create(kOnContextCreatedRequest);

SetInt64(contextCreatedMessage->GetArgumentList(), 0, frame->GetIdentifier());

frame->SendProcessMessage(CefProcessId::PID_BROWSER, contextCreatedMessage);
};

Expand All @@ -141,8 +139,6 @@ namespace CefSharp

auto contextReleasedMessage = CefProcessMessage::Create(kOnContextReleasedRequest);

SetInt64(contextReleasedMessage->GetArgumentList(), 0, frame->GetIdentifier());

frame->SendProcessMessage(CefProcessId::PID_BROWSER, contextReleasedMessage);

auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier());
Expand Down Expand Up @@ -172,19 +168,16 @@ namespace CefSharp
auto focusedNodeChangedMessage = CefProcessMessage::Create(kOnFocusedNodeChanged);
auto list = focusedNodeChangedMessage->GetArgumentList();

// Needed in the browser process to get the frame.
SetInt64(list, 0, frame->GetIdentifier());

// The node will be empty if an element loses focus but another one
// doesn't gain focus. Only transfer information if the node is an
// element.
if (node != nullptr && node->IsElement())
{
// True when a node exists, false if it doesn't.
list->SetBool(1, true);
list->SetBool(0, true);

// Store the tag name.
list->SetString(2, node->GetElementTagName());
list->SetString(1, node->GetElementTagName());

// Transfer the attributes in a Dictionary.
auto attributes = CefDictionaryValue::Create();
Expand All @@ -195,11 +188,11 @@ namespace CefSharp
attributes->SetString(iter.first, iter.second);
}

list->SetDictionary(3, attributes);
list->SetDictionary(2, attributes);
}
else
{
list->SetBool(1, false);
list->SetBool(0, false);
}

frame->SendProcessMessage(CefProcessId::PID_BROWSER, focusedNodeChangedMessage);
Expand All @@ -210,9 +203,7 @@ namespace CefSharp
auto uncaughtExceptionMessage = CefProcessMessage::Create(kOnUncaughtException);
auto list = uncaughtExceptionMessage->GetArgumentList();

// Needed in the browser process to get the frame.
SetInt64(list, 0, frame->GetIdentifier());
list->SetString(1, exception->GetMessage());
list->SetString(0, exception->GetMessage());

auto frames = CefListValue::Create();
for (auto i = 0; i < stackTrace->GetFrameCount(); i++)
Expand All @@ -228,7 +219,7 @@ namespace CefSharp
frames->SetList(i, frame);
}

list->SetList(2, frames);
list->SetList(1, frames);

frame->SendProcessMessage(CefProcessId::PID_BROWSER, uncaughtExceptionMessage);
}
Expand Down Expand Up @@ -304,7 +295,7 @@ namespace CefSharp
throw gcnew Exception("Unsupported message type");
}

auto callbackId = GetInt64(argList, 1);
auto callbackId = GetInt64(argList, 0);
auto response = CefProcessMessage::Create(responseName);
auto responseArgList = response->GetArgumentList();
auto errorMessage = String::Format("Request BrowserId : {0} not found it's likely the browser is already closed", browser->GetIdentifier());
Expand Down Expand Up @@ -335,13 +326,12 @@ namespace CefSharp
response = CefProcessMessage::Create(kJavascriptCallbackResponse);
}

//both messages have the frameId stored at 0 and callbackId stored at index 1
auto frameId = GetInt64(argList, 0);
int64 callbackId = GetInt64(argList, 1);
//both messages have callbackId stored at index 0
auto frameId = frame->GetIdentifier();
int64 callbackId = GetInt64(argList, 0);

if (name == kEvaluateJavascriptRequest)
{

JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);

Expand All @@ -357,12 +347,11 @@ namespace CefSharp

auto callbackRegistry = rootObjectWrapper->CallbackRegistry;

auto script = argList->GetString(2);
auto scriptUrl = argList->GetString(3);
auto startLine = argList->GetInt(4);
auto script = argList->GetString(1);
auto scriptUrl = argList->GetString(2);
auto startLine = argList->GetInt(3);

auto frame = browser->GetFrame(frameId);
if (frame.get())
if (frame.get() && frame->IsValid())
{
auto context = frame->GetV8Context();

Expand Down Expand Up @@ -410,7 +399,7 @@ namespace CefSharp
}
else
{
auto jsCallbackId = GetInt64(argList, 2);
auto jsCallbackId = GetInt64(argList, 1);

auto callbackWrapper = callbackRegistry->FindWrapper(jsCallbackId);
if (callbackWrapper == nullptr)
Expand All @@ -426,7 +415,7 @@ namespace CefSharp
{
try
{
auto parameterList = argList->GetList(3);
auto parameterList = argList->GetList(2);
CefV8ValueList params;

//Needs to be called within the context as for Dictionary (mapped to struct)
Expand Down Expand Up @@ -478,7 +467,7 @@ namespace CefSharp
else if (name == kJavascriptCallbackDestroyRequest)
{
auto jsCallbackId = GetInt64(argList, 0);
auto frameId = GetInt64(argList, 1);
auto frameId = frame->GetIdentifier();
JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);
if (rootObjectWrapper != nullptr && rootObjectWrapper->CallbackRegistry != nullptr)
Expand Down Expand Up @@ -517,10 +506,10 @@ namespace CefSharp
}
else
{
auto browserId = argList->GetInt(1);
auto frameId = GetInt64(argList, 2);
auto callbackId = GetInt64(argList, 3);
auto javascriptObjects = DeserializeJsObjects(argList, 4);
auto browserId = browser->GetIdentifier();
auto frameId = frame->GetIdentifier();
auto callbackId = GetInt64(argList, 1);
auto javascriptObjects = DeserializeJsObjects(argList, 2);

//Caching of JavascriptObjects
//TODO: JSB Should caching be configurable? On a per object basis?
Expand All @@ -533,8 +522,7 @@ namespace CefSharp
_javascriptObjects->Add(obj->JavascriptName, obj);
}

auto frame = browser->GetFrame(frameId);
if (frame.get())
if (frame.get() && frame->IsValid())
{
auto rootObject = GetJsRootObjectWrapper(browser->GetIdentifier(), frameId);

Expand Down Expand Up @@ -609,8 +597,8 @@ namespace CefSharp
}
else if (name == kJavascriptAsyncMethodCallResponse)
{
auto frameId = GetInt64(argList, 0);
auto callbackId = GetInt64(argList, 1);
auto frameId = frame->GetIdentifier();
auto callbackId = GetInt64(argList, 0);

JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);
Expand All @@ -620,26 +608,24 @@ namespace CefSharp
JavascriptAsyncMethodCallback^ callback;
if (rootObjectWrapper->TryGetAndRemoveMethodCallback(callbackId, callback))
{

try
{
auto frame = browser->GetFrame(frameId);
if (frame.get())
if (frame.get() && frame->IsValid())
{
auto context = frame->GetV8Context();

if (context.get() && context->Enter())
{
try
{
auto success = argList->GetBool(2);
auto success = argList->GetBool(1);
if (success)
{
callback->Success(DeserializeV8Object(argList, 3));
callback->Success(DeserializeV8Object(argList, 2));
}
else
{
callback->Fail(argList->GetString(3));
callback->Fail(argList->GetString(2));
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace CefSharp
{
auto frame = context->GetFrame();

if (context.get() && context->Enter())
if (frame.get() && frame->IsValid() && context->Enter())
{
try
{
Expand All @@ -53,15 +53,13 @@ namespace CefSharp
auto request = CefProcessMessage::Create(kJavascriptMessageReceived);
auto argList = request->GetArgumentList();

SetInt64(argList, 0, frame->GetIdentifier());

auto params = CefListValue::Create();
SerializeV8Object(arguments[0], params, 0, _javascriptCallbackRegistry);

//We're only interested in the first param
if (params->GetSize() > 0)
{
argList->SetValue(1, params->GetValue(0));
argList->SetValue(0, params->GetValue(0));
}

frame->SendProcessMessage(CefProcessId::PID_BROWSER, request);
Expand Down
10 changes: 4 additions & 6 deletions CefSharp.BrowserSubprocess.Core/RegisterBoundObjectHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,13 @@ namespace CefSharp
else
{
auto frame = context->GetFrame();
if (frame.get())
if (frame.get() && frame->IsValid())
{
//Obtain a callbackId then send off the Request for objects
auto callbackId = _callbackRegistry->SaveMethodCallback(callback);

argList->SetInt(0, browser->GetIdentifier());
SetInt64(argList, 1, frame->GetIdentifier());
SetInt64(argList, 2, callbackId);
argList->SetList(3, params);
SetInt64(argList, 0, callbackId);
argList->SetList(1, params);

frame->SendProcessMessage(CefProcessId::PID_BROWSER, request);
}
Expand All @@ -295,7 +293,7 @@ namespace CefSharp
{
auto frame = context->GetFrame();

if (frame.get())
if (frame.get() && frame->IsValid())
{
//Objects already bound or ignore cache

Expand Down
29 changes: 27 additions & 2 deletions CefSharp.Core/Internals/CefFrameWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include "Internals\CefFrameWrapper.h"
#include "Internals\StringVisitor.h"
#include "Internals\ClientAdapter.h"
#include "Internals\Serialization\Primitives.h"
#include "Internals\Messaging\Messages.h"

using namespace CefSharp::Internals::Messaging;
using namespace CefSharp::Internals::Serialization;

///
// True if this object is currently attached to a valid frame.
Expand Down Expand Up @@ -240,9 +245,29 @@ Task<JavascriptResponse^>^ CefFrameWrapper::EvaluateScriptAsync(String^ script,
auto browser = _frame->GetBrowser();
auto host = browser->GetHost();

//If we're unable to get the underlying browser/browserhost then return null
if (!browser.get() || !host.get())
{
return nullptr;
}

auto client = static_cast<ClientAdapter*>(host->GetClient().get());

return client->EvaluateScriptAsync(browser->GetIdentifier(), browser->IsPopup(), _frame->GetIdentifier(), script, scriptUrl, startLine, timeout);
auto pendingTaskRepository = client->GetPendingTaskRepository();

//create a new taskcompletionsource
auto idAndComplectionSource = pendingTaskRepository->CreatePendingTask(timeout);

auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest);
auto argList = message->GetArgumentList();
SetInt64(argList, 0, idAndComplectionSource.Key);
argList->SetString(1, StringUtils::ToNative(script));
argList->SetString(2, StringUtils::ToNative(scriptUrl));
argList->SetInt(3, startLine);

_frame->SendProcessMessage(CefProcessId::PID_RENDERER, message);

return idAndComplectionSource.Value->Task;
}

///
Expand Down Expand Up @@ -388,4 +413,4 @@ void CefFrameWrapper::ThrowIfFrameInvalid()
{
throw gcnew Exception(L"The underlying frame is no longer valid - please check the IsValid property before calling!");
}
}
}
Loading

0 comments on commit df617c1

Please sign in to comment.