BUG-2141: Fix for old browser version when embedding IE. And some other small imgur stability changes.

This commit is contained in:
Krom, Robertus 2017-07-31 09:13:08 +02:00
parent f695ce8182
commit b8009fe256
6 changed files with 194 additions and 57 deletions

View file

@ -18,7 +18,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using log4net;
using Microsoft.Win32;
namespace GreenshotPlugin.Core {
@ -26,24 +31,124 @@ namespace GreenshotPlugin.Core {
/// Description of IEHelper.
/// </summary>
public static class IEHelper {
private static readonly ILog Log = LogManager.GetLogger(typeof(IEHelper));
// Internet explorer Registry key
private const string IE_KEY = @"Software\Microsoft\Internet Explorer";
private const string IeKey = @"Software\Microsoft\Internet Explorer";
/// <summary>
/// Helper method to get the IE version
/// Get the current browser version
/// </summary>
/// <returns></returns>
public static int IEVersion() {
int version = 7;
// Seeing if IE 9 is used, here we need another offset!
using (RegistryKey ieKey = Registry.LocalMachine.OpenSubKey(IE_KEY, false)) {
object versionKey = ieKey?.GetValue("Version");
if (versionKey != null) {
int.TryParse(versionKey.ToString().Substring(0,1), out version);
/// <returns>int with browser version</returns>
public static int IEVersion
{
get
{
var maxVer = 7;
using (var ieKey = Registry.LocalMachine.OpenSubKey(IeKey, false))
{
foreach (var value in new[] { "svcVersion", "svcUpdateVersion", "Version", "W2kVersion" })
{
var objVal = ieKey.GetValue(value, "0");
var strVal = Convert.ToString(objVal);
var iPos = strVal.IndexOf('.');
if (iPos > 0)
{
strVal = strVal.Substring(0, iPos);
}
int res;
if (int.TryParse(strVal, out res))
{
maxVer = Math.Max(maxVer, res);
}
}
}
return maxVer;
}
return version;
}
/// <summary>
/// Get the highest possible version for the embedded browser
/// </summary>
/// <param name="ignoreDoctype">true to ignore the doctype when loading a page</param>
/// <returns>IE Feature</returns>
public static int GetEmbVersion(bool ignoreDoctype = true)
{
var ieVersion = IEVersion;
if (ieVersion > 9)
{
return ieVersion * 1000 + (ignoreDoctype ? 1 : 0);
}
if (ieVersion > 7)
{
return ieVersion * 1111;
}
return 7000;
}
/// <summary>
/// Fix browser version to the highest possible
/// </summary>
/// <param name="ignoreDoctype">true to ignore the doctype when loading a page</param>
public static void FixBrowserVersion(bool ignoreDoctype = true)
{
var applicationName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
FixBrowserVersion(applicationName, ignoreDoctype);
}
/// <summary>
/// Fix the browser version for the specified application
/// </summary>
/// <param name="applicationName">Name of the process</param>
/// <param name="ignoreDoctype">true to ignore the doctype when loading a page</param>
public static void FixBrowserVersion(string applicationName, bool ignoreDoctype = true)
{
FixBrowserVersion(applicationName, GetEmbVersion(ignoreDoctype));
}
/// <summary>
/// Fix the browser version for the specified application
/// </summary>
/// <param name="applicationName">Name of the process</param>
/// <param name="ieVersion">
/// Version, see
/// <a href="https://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation">Browser Emulation</a>
/// </param>
public static void FixBrowserVersion(string applicationName, int ieVersion)
{
ModifyRegistry("HKEY_CURRENT_USER", applicationName + ".exe", ieVersion);
#if DEBUG
ModifyRegistry("HKEY_CURRENT_USER", applicationName + ".vshost.exe", ieVersion);
#endif
}
/// <summary>
/// Make the change to the registry
/// </summary>
/// <param name="root">HKEY_CURRENT_USER or something</param>
/// <param name="applicationName">Name of the executable</param>
/// <param name="ieFeatureVersion">Version to use</param>
private static void ModifyRegistry(string root, string applicationName, int ieFeatureVersion)
{
var regKey = root + @"\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
try
{
Registry.SetValue(regKey, applicationName, ieFeatureVersion);
}
catch (Exception ex)
{
// some config will hit access rights exceptions
// this is why we try with both LOCAL_MACHINE and CURRENT_USER
Log.Error(ex);
Log.ErrorFormat("couldn't modify the registry key {0}", regKey);
}
}
/// <summary>
/// Find the DirectUI window for MSAA (Accessible)
/// </summary>
@ -55,7 +160,7 @@ namespace GreenshotPlugin.Core {
}
WindowDetails tmpWd = browserWindowDetails;
// Since IE 9 the TabBandClass is less deep!
if (IEVersion() < 9) {
if (IEVersion < 9) {
tmpWd = tmpWd.GetChild("CommandBarClass");
tmpWd = tmpWd?.GetChild("ReBarWindow32");
}