forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCefRenderProcess.cs
More file actions
115 lines (89 loc) · 3.29 KB
/
Copy pathCefRenderProcess.cs
File metadata and controls
115 lines (89 loc) · 3.29 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
using System;
using System.Linq;
using System.Threading.Tasks;
// Copyright © 2010-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.
using CefSharp.Internals;
using System.Collections.Generic;
using System.ServiceModel;
using TaskExtensions = CefSharp.Internals.TaskExtensions;
namespace CefSharp.BrowserSubprocess
{
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults=true)]
public class CefRenderProcess : CefSubProcess
{
private int? parentBrowserId;
private List<CefBrowserWrapper> browsers = new List<CefBrowserWrapper>();
public CefRenderProcess(IEnumerable<string> args)
: base(args)
{
}
protected override void DoDispose(bool isDisposing)
{
foreach(var browser in browsers)
{
browser.Dispose();
}
browsers = null;
base.DoDispose(isDisposing);
}
public override void OnBrowserCreated(CefBrowserWrapper browser)
{
browsers.Add(browser);
if (parentBrowserId == null)
{
parentBrowserId = browser.BrowserId;
}
if (ParentProcessId == null || parentBrowserId == null)
{
return;
}
var browserId = browser.IsPopup ? parentBrowserId.Value : browser.BrowserId;
var serviceName = RenderprocessClientFactory.GetServiceName(ParentProcessId.Value, browserId);
var binding = BrowserProcessServiceHost.CreateBinding();
var channelFactory = new ChannelFactory<IBrowserProcess>(
binding,
new EndpointAddress(serviceName)
);
channelFactory.Open();
var browserProcess = channelFactory.CreateChannel();
var clientChannel = ((IClientChannel)browserProcess);
try
{
clientChannel.Open();
if (!browser.IsPopup)
{
browserProcess.Connect();
}
var javascriptObject = browserProcess.GetRegisteredJavascriptObjects();
if (javascriptObject.MemberObjects.Count > 0)
{
browser.JavascriptRootObject = javascriptObject;
}
browser.ChannelFactory = channelFactory;
browser.BrowserProcess = browserProcess;
}
catch(Exception)
{
}
}
public override void OnBrowserDestroyed(CefBrowserWrapper browser)
{
browsers.Remove(browser);
var channelFactory = browser.ChannelFactory;
if (channelFactory.State == CommunicationState.Opened)
{
channelFactory.Close();
}
var clientChannel = ((IClientChannel)browser.BrowserProcess);
if (clientChannel.State == CommunicationState.Opened)
{
clientChannel.Close();
}
browser.ChannelFactory = null;
browser.BrowserProcess = null;
browser.JavascriptRootObject = null;
}
}
}