-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathManagedCefBrowserAdapter.cpp
More file actions
125 lines (106 loc) · 4.27 KB
/
ManagedCefBrowserAdapter.cpp
File metadata and controls
125 lines (106 loc) · 4.27 KB
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
// Copyright © 2014 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 "ManagedCefBrowserAdapter.h"
#include "WindowInfo.h"
#include "Internals/Messaging/Messages.h"
#include "Internals/CefFrameWrapper.h"
#include "Internals/CefSharpBrowserWrapper.h"
using namespace CefSharp::Internals::Messaging;
bool ManagedCefBrowserAdapter::IsDisposed::get()
{
return _isDisposed;
}
void ManagedCefBrowserAdapter::CreateBrowser(IWindowInfo^ windowInfo, BrowserSettings^ browserSettings, RequestContext^ requestContext, String^ address)
{
if (browserSettings->IsDisposed)
{
throw gcnew ObjectDisposedException("browserSettings", "The BrowserSettings reference you have passed has already been disposed. " +
"You cannot reuse the BrowserSettings class at the moment. This may change in the next version, check " +
"https://github.com/cefsharp/CefSharp/issues/2643 for details.");
}
auto cefWindowInfoWrapper = static_cast<WindowInfo^>(windowInfo);
CefString addressNative = StringUtils::ToNative(address);
if (!CefBrowserHost::CreateBrowser(*cefWindowInfoWrapper->GetWindowInfo(), _clientAdapter.get(), addressNative,
*browserSettings->_browserSettings, static_cast<CefRefPtr<CefRequestContext>>(requestContext)))
{
throw gcnew InvalidOperationException("CefBrowserHost::CreateBrowser call failed, review the CEF log file for more details.");
}
}
void ManagedCefBrowserAdapter::OnAfterBrowserCreated(IBrowser^ browser)
{
if (!_isDisposed)
{
_browserWrapper = browser;
_javascriptCallbackFactory->BrowserAdapter = gcnew WeakReference(this);
//Browser has been initialized, it's now too late to register a sync JSB object if Wcf wasn't enabled
_javaScriptObjectRepository->IsBrowserInitialized = true;
if (CefSharpSettings::WcfEnabled)
{
_browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browser->Identifier, _javascriptCallbackFactory);
//NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
// an exception about the process already being closed. Two relevant issues are #862 and #804.
if (_browserProcessServiceHost->State == CommunicationState::Created)
{
try
{
_browserProcessServiceHost->Open();
}
catch (Exception^)
{
//Ignore exception as it's likely cause when the browser is closing
}
}
}
if (_webBrowserInternal != nullptr)
{
_webBrowserInternal->OnAfterBrowserCreated(browser);
}
}
}
void ManagedCefBrowserAdapter::Resize(int width, int height)
{
HWND browserHwnd = _clientAdapter->GetBrowserHwnd();
if (browserHwnd)
{
if (width == 0 && height == 0)
{
// For windowed browsers when the frame window is minimized set the
// browser window size to 0x0 to reduce resource usage.
SetWindowPos(browserHwnd, NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
}
else
{
SetWindowPos(browserHwnd, NULL, 0, 0, width, height, SWP_NOZORDER);
}
}
}
IBrowser^ ManagedCefBrowserAdapter::GetBrowser(int browserId)
{
return _clientAdapter->GetBrowserWrapper(browserId);
}
IJavascriptCallbackFactory^ ManagedCefBrowserAdapter::JavascriptCallbackFactory::get()
{
return _javascriptCallbackFactory;
}
JavascriptObjectRepository^ ManagedCefBrowserAdapter::JavascriptObjectRepository::get()
{
return _javaScriptObjectRepository;
}
MethodRunnerQueue^ ManagedCefBrowserAdapter::MethodRunnerQueue::get()
{
return _methodRunnerQueue;
}
void ManagedCefBrowserAdapter::MethodInvocationComplete(Object^ sender, MethodInvocationCompleteArgs^ e)
{
auto result = e->Result;
if (result->CallbackId.HasValue)
{
_clientAdapter->MethodInvocationComplete(result);
}
}
MCefRefPtr<ClientAdapter> ManagedCefBrowserAdapter::GetClientAdapter()
{
return _clientAdapter;
}