Skip to content

Commit a434d3c

Browse files
committed
Merge pull request #900 from amaitland/feature/dependencychecker
Implement DependencyChecker
2 parents da63ffe + ccbaebd commit a434d3c

File tree

5 files changed

+221
-5
lines changed

5 files changed

+221
-5
lines changed

CefSharp.Core/Cef.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,27 @@ namespace CefSharp
139139
/// <return>true if successful; otherwise, false.</return>
140140
static bool Initialize(CefSettings^ cefSettings)
141141
{
142-
return Initialize(cefSettings, true);
142+
return Initialize(cefSettings, true, false);
143143
}
144144

145145
/// <summary>Initializes CefSharp with user-provided settings.</summary>
146146
/// <param name="cefSettings">CefSharp configuration settings.</param>
147147
/// <param name="shutdownOnProcessExit">When the Current AppDomain (relative to the thread called on)
148148
/// Exits(ProcessExit event) then Shudown will be called.</param>
149+
/// <param name="performDependencyCheck">Check that all relevant dependencies avaliable, throws exception if any are missing</param>
149150
/// <return>true if successful; otherwise, false.</return>
150-
static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit)
151+
static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit, bool performDependencyCheck)
151152
{
152153
bool success = false;
153154

154155
// NOTE: Can only initialize Cef once, so subsiquent calls are ignored.
155156
if (!IsInitialized)
156157
{
158+
if(performDependencyCheck)
159+
{
160+
DependencyChecker::AssertAllDependenciesPresent(cefSettings->Locale, cefSettings->LocalesDirPath, cefSettings->ResourcesDirPath, cefSettings->PackLoadingDisabled);
161+
}
162+
157163
CefMainArgs main_args;
158164
CefRefPtr<CefSharpApp> app(new CefSharpApp(cefSettings));
159165

CefSharp.Core/CefSettings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ namespace CefSharp
7979
void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->locales_dir_path, value); }
8080
}
8181

82+
virtual property String^ ResourcesDirPath
83+
{
84+
String^ get() { return StringUtils::ToClr(_cefSettings->resources_dir_path); }
85+
void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->resources_dir_path, value); }
86+
}
87+
8288
virtual property String^ LogFile
8389
{
8490
String^ get() { return StringUtils::ToClr(_cefSettings->log_file); }

CefSharp.Example/CefExample.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.Linq;
43

54
namespace CefSharp.Example
65
{
@@ -22,7 +21,7 @@ public static void Init()
2221

2322
//Chromium Command Line args
2423
//http://peter.sh/experiments/chromium-command-line-switches/
25-
//NOTE: Note all relevant in relation to `CefSharp`, use for reference purposes only.
24+
//NOTE: Not all relevant in relation to `CefSharp`, use for reference purposes only.
2625

2726
var settings = new CefSettings();
2827
settings.RemoteDebuggingPort = 8088;
@@ -52,7 +51,11 @@ public static void Init()
5251
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
5352
});
5453

55-
if (!Cef.Initialize(settings))
54+
//Cef will check if all dependencies are present
55+
//For special case when Checking Windows Xp Dependencies
56+
//DependencyChecker.IsWindowsXp = true;
57+
58+
if (!Cef.Initialize(settings, shutdownOnProcessExit: true, performDependencyCheck: true))
5659
{
5760
throw new Exception("Unable to Initialize Cef");
5861
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="CefDirtyRect.cs" />
9292
<Compile Include="CefFileDialogMode.cs" />
9393
<Compile Include="DefaultResourceHandlerFactory.cs" />
94+
<Compile Include="DependencyChecker.cs" />
9495
<Compile Include="IJavascriptCallback.cs" />
9596
<Compile Include="Internals\IntPtrExtensions.cs" />
9697
<Compile Include="Internals\JavascriptCallbackProxy.cs" />

CefSharp/DependencyChecker.cs

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Reflection;
9+
using System.Text;
10+
11+
namespace CefSharp
12+
{
13+
/// <summary>
14+
/// DependencyChecker provides a known list of Cef/CefSharp dependencies and
15+
/// provides helper methods to check for their existance.
16+
/// </summary>
17+
public static class DependencyChecker
18+
{
19+
public const string LocalesPackFile = @"locales\en-US.pak";
20+
21+
/// <summary>
22+
/// IsWindowsXp - Special case for legacy XP support
23+
/// </summary>
24+
public static bool IsWindowsXp { get; set; }
25+
26+
/// <summary>
27+
/// List of Cef Dependencies
28+
/// </summary>
29+
public static string[] CefDependencies =
30+
{
31+
// CEF core library
32+
"libcef.dll",
33+
// Unicode support
34+
"icudtl.dat"
35+
};
36+
37+
/// <summary>
38+
/// List of Cef Resources (pack files)
39+
/// </summary>
40+
public static string[] CefResources =
41+
{
42+
// Pack Files
43+
// Note: Contains WebKit image and inspector resources.
44+
"devtools_resources.pak",
45+
"cef.pak",
46+
"cef_100_percent.pak",
47+
"cef_200_percent.pak"
48+
};
49+
50+
public static string[] CefOptionalDependencies =
51+
{
52+
// Angle and Direct3D support
53+
// Note: Without these components HTML5 accelerated content like 2D canvas, 3D CSS and WebGL will not function.
54+
"libEGL.dll",
55+
"libGLESv2.dll",
56+
(IsWindowsXp ? "d3dcompiler_43.dll" : "d3dcompiler_47.dll"),
57+
// PDF support
58+
// Note: Without this component printing will not function.
59+
"pdf.dll",
60+
//FFmpeg audio and video support
61+
// Note: Without this component HTML5 audio and video will not function.
62+
"ffmpegsumo.dll"
63+
};
64+
65+
/// <summary>
66+
/// List of CefSharp Dependencies
67+
/// </summary>
68+
public static string[] CefSharpDependencies =
69+
{
70+
"CefSharp.Core.dll",
71+
"CefSharp.dll",
72+
"CefSharp.BrowserSubprocess.Core.dll",
73+
"CefSharp.BrowserSubprocess.exe"
74+
};
75+
76+
/// <summary>
77+
/// CheckDependencies iterates through the list of Cef and CefSharp dependencines
78+
/// relative to the path provided and returns a list of missing ones
79+
/// </summary>
80+
/// <param name="checkOptional">check to see if optional dependencies are present</param>
81+
/// <param name="packLoadingDisabled">Is loading of pack files disabled?</param>
82+
/// <param name="path">path to check for dependencies</param>
83+
/// <param name="resourcesDirPath"></param>
84+
/// <param name="localePackFile">The locale pack file e.g. <see cref="LocalesPackFile"/> </param>
85+
/// <returns>List of missing dependencies, if all present an empty List will be returned</returns>
86+
public static List<string> CheckDependencies(bool checkOptional, bool packLoadingDisabled, string path, string resourcesDirPath, string localePackFile = LocalesPackFile)
87+
{
88+
var missingDependencies = new List<string>();
89+
90+
//Loop through Cef dependencies and add to list if not found
91+
foreach (var cefDependency in CefDependencies)
92+
{
93+
var dependencyPath = Path.Combine(path, cefDependency);
94+
95+
if (!File.Exists(dependencyPath))
96+
{
97+
missingDependencies.Add(cefDependency);
98+
}
99+
}
100+
101+
if (!packLoadingDisabled)
102+
{
103+
//Loop through Cef Resources and add to list if not found
104+
foreach (var cefResource in CefResources)
105+
{
106+
var resourcePath = Path.Combine(resourcesDirPath, cefResource);
107+
108+
if (!File.Exists(resourcePath))
109+
{
110+
missingDependencies.Add(cefResource);
111+
}
112+
}
113+
}
114+
115+
if (checkOptional)
116+
{
117+
//Loop through Cef Optional dependencies and add to list if not found
118+
foreach (var cefDependency in CefOptionalDependencies)
119+
{
120+
var dependencyPath = Path.Combine(path, cefDependency);
121+
122+
if (!File.Exists(dependencyPath))
123+
{
124+
missingDependencies.Add(cefDependency);
125+
}
126+
}
127+
}
128+
129+
// Loop through CefSharp dependencies and add to list if not found
130+
foreach (var cefSharpDependency in CefSharpDependencies)
131+
{
132+
var dependencyPath = Path.Combine(path, cefSharpDependency);
133+
134+
if (!File.Exists(dependencyPath))
135+
{
136+
missingDependencies.Add(cefSharpDependency);
137+
}
138+
}
139+
140+
//If path path is not rooted (doesn't start with a drive letter + folder)
141+
//then make it relative to the executing assembly.
142+
var localePath = Path.IsPathRooted(localePackFile) ? localePackFile : Path.Combine(path, localePackFile);
143+
144+
if (!File.Exists(localePath))
145+
{
146+
missingDependencies.Add(localePackFile);
147+
}
148+
149+
return missingDependencies;
150+
}
151+
152+
/// <summary>
153+
/// Checks if all Cef and CefSharp dependencies were found relative to the Executing Assembly.
154+
/// Shortcut method that calls <see cref="CheckDependencies"/>, throws an Exception if not files are missing.
155+
/// </summary>
156+
/// <param name="locale">The locale, if empty then en-US will be used.</param>
157+
/// <param name="localesDirPath">The path to the locales directory, if empty locales\ will be used.</param>
158+
/// <param name="resourcesDirPath">The path to the resources directory, if empty the Executing Assembly path is used.</param>
159+
/// <param name="packLoadingDisabled">Is loading of pack files disabled?</param>
160+
/// <exception cref="Exception">Throw when not all dependencies are present</exception>
161+
public static void AssertAllDependenciesPresent(string locale, string localesDirPath, string resourcesDirPath, bool packLoadingDisabled)
162+
{
163+
var executingAssembly = Assembly.GetExecutingAssembly();
164+
165+
var path = Path.GetDirectoryName(executingAssembly.Location);
166+
167+
if(string.IsNullOrEmpty(locale))
168+
{
169+
locale = "en-US";
170+
}
171+
172+
if (string.IsNullOrEmpty(localesDirPath))
173+
{
174+
localesDirPath = @"locales";
175+
}
176+
177+
if (string.IsNullOrEmpty(resourcesDirPath))
178+
{
179+
resourcesDirPath = path;
180+
}
181+
182+
var missingDependencies = CheckDependencies(true, packLoadingDisabled, path, resourcesDirPath, Path.Combine(localesDirPath, locale + ".pak"));
183+
184+
if (missingDependencies.Count > 0)
185+
{
186+
var builder = new StringBuilder();
187+
builder.AppendLine("Unable to locate required Cef/CefSharp dependencies:");
188+
189+
foreach (var missingDependency in missingDependencies)
190+
{
191+
builder.AppendLine("Missing:" + missingDependency);
192+
}
193+
194+
builder.AppendLine("Executing Assembly Path:" + path);
195+
196+
throw new Exception(builder.ToString());
197+
}
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)