2015-04-25 02:45:21 -04:00
|
|
|
// Copyright © 2010-2015 The CefSharp Project. All rights reserved.
|
2014-08-29 10:53:50 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "Stdafx.h"
|
|
|
|
|
|
2014-09-12 00:40:33 +10:00
|
|
|
#include "CefBrowserWrapper.h"
|
2014-08-29 10:53:50 +10:00
|
|
|
#include "CefAppUnmanagedWrapper.h"
|
2014-11-27 20:57:11 +10:00
|
|
|
#include "JavascriptRootObjectWrapper.h"
|
2015-06-29 15:08:53 +10:00
|
|
|
#include "Serialization\V8Serialization.h"
|
|
|
|
|
#include "..\CefSharp.Core\Internals\Messaging\Messages.h"
|
|
|
|
|
#include "..\CefSharp.Core\Internals\Serialization\Primitives.h"
|
2014-08-29 10:53:50 +10:00
|
|
|
|
|
|
|
|
using namespace System;
|
|
|
|
|
using namespace System::Diagnostics;
|
|
|
|
|
using namespace System::Collections::Generic;
|
2015-06-29 15:08:53 +10:00
|
|
|
using namespace CefSharp::Internals::Messaging;
|
|
|
|
|
using namespace CefSharp::Internals::Serialization;
|
2014-08-29 10:53:50 +10:00
|
|
|
|
|
|
|
|
namespace CefSharp
|
|
|
|
|
{
|
|
|
|
|
CefRefPtr<CefRenderProcessHandler> CefAppUnmanagedWrapper::GetRenderProcessHandler()
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CefRenderProcessHandler
|
|
|
|
|
void CefAppUnmanagedWrapper::OnBrowserCreated(CefRefPtr<CefBrowser> browser)
|
|
|
|
|
{
|
|
|
|
|
auto wrapper = gcnew CefBrowserWrapper(browser);
|
|
|
|
|
_onBrowserCreated->Invoke(wrapper);
|
2014-09-15 00:21:22 +10:00
|
|
|
|
|
|
|
|
//Multiple CefBrowserWrappers created when opening popups
|
2014-09-15 12:05:01 +10:00
|
|
|
_browserWrappers->Add(browser->GetIdentifier(), wrapper);
|
2014-08-29 10:53:50 +10:00
|
|
|
}
|
|
|
|
|
|
2014-09-12 16:27:47 +10:00
|
|
|
void CefAppUnmanagedWrapper::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser)
|
|
|
|
|
{
|
2015-06-29 12:00:33 +10:00
|
|
|
auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), false);
|
2014-09-15 12:02:58 +10:00
|
|
|
|
2014-09-15 00:21:22 +10:00
|
|
|
if (wrapper != nullptr)
|
|
|
|
|
{
|
2014-10-28 11:25:53 -04:00
|
|
|
_browserWrappers->Remove(wrapper->BrowserId);
|
2014-09-15 00:21:22 +10:00
|
|
|
_onBrowserDestroyed->Invoke(wrapper);
|
|
|
|
|
delete wrapper;
|
|
|
|
|
}
|
2014-09-12 16:27:47 +10:00
|
|
|
};
|
|
|
|
|
|
2014-08-29 10:53:50 +10:00
|
|
|
void CefAppUnmanagedWrapper::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
|
|
|
|
|
{
|
2015-06-29 12:00:33 +10:00
|
|
|
auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true);
|
2014-10-28 11:25:53 -04:00
|
|
|
|
|
|
|
|
if (wrapper->JavascriptRootObject != nullptr)
|
2014-08-29 10:53:50 +10:00
|
|
|
{
|
2014-09-15 16:38:05 +10:00
|
|
|
auto window = context->GetGlobal();
|
2014-10-28 11:25:53 -04:00
|
|
|
|
2014-11-25 09:34:15 -05:00
|
|
|
wrapper->JavascriptRootObjectWrapper = gcnew JavascriptRootObjectWrapper(wrapper->JavascriptRootObject, wrapper->BrowserProcess);
|
2014-10-28 11:25:53 -04:00
|
|
|
|
2014-11-25 09:34:15 -05:00
|
|
|
wrapper->JavascriptRootObjectWrapper->V8Value = window;
|
|
|
|
|
wrapper->JavascriptRootObjectWrapper->Bind();
|
2014-08-29 10:53:50 +10:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-12 16:27:47 +10:00
|
|
|
void CefAppUnmanagedWrapper::OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
|
2014-11-25 09:34:15 -05:00
|
|
|
{
|
2015-06-29 12:00:33 +10:00
|
|
|
auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true);
|
2014-11-25 09:34:15 -05:00
|
|
|
|
|
|
|
|
if (wrapper->JavascriptRootObjectWrapper != nullptr)
|
|
|
|
|
{
|
|
|
|
|
delete wrapper->JavascriptRootObjectWrapper;
|
|
|
|
|
wrapper->JavascriptRootObjectWrapper = nullptr;
|
|
|
|
|
}
|
2014-08-29 10:53:50 +10:00
|
|
|
};
|
2014-09-12 12:48:10 +10:00
|
|
|
|
2015-06-27 18:24:32 +02:00
|
|
|
CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(int browserId, bool mustExist)
|
|
|
|
|
{
|
2014-10-28 11:25:53 -04:00
|
|
|
CefBrowserWrapper^ wrapper = nullptr;
|
|
|
|
|
|
|
|
|
|
_browserWrappers->TryGetValue(browserId, wrapper);
|
|
|
|
|
|
|
|
|
|
if (mustExist && wrapper == nullptr)
|
|
|
|
|
{
|
|
|
|
|
throw gcnew InvalidOperationException(String::Format("Failed to identify BrowserWrapper in OnContextCreated. : {0}", browserId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wrapper;
|
2015-06-27 18:24:32 +02:00
|
|
|
}
|
2015-06-27 17:01:51 +02:00
|
|
|
|
2015-06-29 12:03:01 +10:00
|
|
|
bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId sourceProcessId, CefRefPtr<CefProcessMessage> message)
|
2015-06-27 17:01:51 +02:00
|
|
|
{
|
2015-06-29 15:08:53 +10:00
|
|
|
auto handled = false;
|
|
|
|
|
auto name = message->GetName();
|
|
|
|
|
if (name == kEvaluateJavascriptRequest)
|
2015-06-27 17:01:51 +02:00
|
|
|
{
|
2015-06-29 15:08:53 +10:00
|
|
|
auto argList = message->GetArgumentList();
|
|
|
|
|
auto browserId = argList->GetInt(0);
|
|
|
|
|
auto frameId = GetInt64(argList, 1);
|
|
|
|
|
auto callbackId = GetInt64(argList, 2);
|
|
|
|
|
auto script = argList->GetString(3);
|
|
|
|
|
|
|
|
|
|
if(browser->GetIdentifier() != browserId)
|
|
|
|
|
{
|
2015-06-29 15:35:30 +10:00
|
|
|
throw gcnew InvalidOperationException(String::Format("Request BrowserId : {0} does not match browser Id : {1}", browserId, browser->GetIdentifier()));
|
2015-06-29 15:08:53 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto browserWrapper = FindBrowserWrapper(browserId, true);
|
|
|
|
|
auto frame = browser->GetFrame(frameId);
|
|
|
|
|
if (frame.get())
|
|
|
|
|
{
|
|
|
|
|
auto context = frame->GetV8Context();
|
|
|
|
|
|
|
|
|
|
if (context.get() && context->Enter())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CefRefPtr<CefV8Value> result;
|
|
|
|
|
CefRefPtr<CefV8Exception> exception;
|
|
|
|
|
auto success = context->Eval(script, result, exception);
|
|
|
|
|
auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse);
|
|
|
|
|
auto argList = response->GetArgumentList();
|
|
|
|
|
|
|
|
|
|
argList->SetBool(0, success);
|
|
|
|
|
SetInt64(callbackId, argList, 1);
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
SerializeV8Object(result, argList, 2, browserWrapper->CallbackRegistry);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
argList->SetString(2, exception->GetMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.get())
|
|
|
|
|
{
|
|
|
|
|
browser->SendProcessMessage(sourceProcessId, response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
context->Exit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//TODO handle error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = true;
|
2015-06-27 17:01:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
};
|
2014-08-29 10:53:50 +10:00
|
|
|
}
|