From 42f65c52fb7cc72e734c62daeb88b276451d52e4 Mon Sep 17 00:00:00 2001 From: Tan Wang Leng Date: Tue, 31 Jan 2017 10:43:12 +0800 Subject: [PATCH 1/3] :bug: Fix webpreferences not accepting numeric options The webpreferences attribute values are parsed as strings instead of numbers. Therefore, a conversion is required. --- atom/browser/web_contents_preferences.cc | 26 +++++++++++++++++++++--- atom/browser/web_contents_preferences.h | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index 5afa3070f06f..96722a9a9b55 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -5,6 +5,7 @@ #include "atom/browser/web_contents_preferences.h" #include +#include #include #include @@ -215,6 +216,25 @@ bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) { return sandboxed; } +// static +bool WebContentsPreferences::ConvertValueToIntegerFromString( + WebContentsPreferences* pref, std::string attributeName, int* intValue) { + + // if it is already an integer, no conversion needed + if (pref->web_preferences_.GetInteger(attributeName, intValue)) { + return true; + } + + std::string stringValue; + + if (pref->web_preferences_.GetString(attributeName, &stringValue)) { + std::stringstream(stringValue) >> *intValue; + return true; + } + + return false; +} + // static void WebContentsPreferences::OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs) { @@ -254,11 +274,11 @@ void WebContentsPreferences::OverrideWebkitPrefs( prefs->fantasy_font_family_map[content::kCommonScript] = font; } int size; - if (self->web_preferences_.GetInteger("defaultFontSize", &size)) + if (ConvertValueToIntegerFromString(self, "defaultFontSize", &size)) prefs->default_font_size = size; - if (self->web_preferences_.GetInteger("defaultMonospaceFontSize", &size)) + if (ConvertValueToIntegerFromString(self, "defaultMonospaceFontSize", &size)) prefs->default_fixed_font_size = size; - if (self->web_preferences_.GetInteger("minimumFontSize", &size)) + if (ConvertValueToIntegerFromString(self, "minimumFontSize", &size)) prefs->minimum_font_size = size; std::string encoding; if (self->web_preferences_.GetString("defaultEncoding", &encoding)) diff --git a/atom/browser/web_contents_preferences.h b/atom/browser/web_contents_preferences.h index 3a04ea9eea0b..7fa1944408b2 100644 --- a/atom/browser/web_contents_preferences.h +++ b/atom/browser/web_contents_preferences.h @@ -5,6 +5,7 @@ #ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ +#include #include #include "base/values.h" @@ -38,6 +39,9 @@ class WebContentsPreferences static bool IsSandboxed(content::WebContents* web_contents); + static bool ConvertValueToIntegerFromString( + WebContentsPreferences* pref, std::string attributeName, int* intValue); + // Modify the WebPreferences according to |web_contents|'s preferences. static void OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs); From 7ec88d16a6f2fedd707b01340ee508bd36af9ede Mon Sep 17 00:00:00 2001 From: Tan Wang Leng Date: Sat, 4 Feb 2017 09:42:52 +0800 Subject: [PATCH 2/3] Use base::StringToInt() instead of std::stringstream --- atom/browser/web_contents_preferences.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index 96722a9a9b55..cfb33de14e77 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -5,7 +5,6 @@ #include "atom/browser/web_contents_preferences.h" #include -#include #include #include @@ -225,11 +224,10 @@ bool WebContentsPreferences::ConvertValueToIntegerFromString( return true; } - std::string stringValue; + base::string16 stringValue; if (pref->web_preferences_.GetString(attributeName, &stringValue)) { - std::stringstream(stringValue) >> *intValue; - return true; + return base::StringToInt(stringValue, intValue); } return false; From 4de637779bed9d0ed6c35c4c87c8f2d2567a009d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Feb 2017 09:13:42 -0800 Subject: [PATCH 3/3] Make int converter helper an instance method --- atom/browser/web_contents_preferences.cc | 37 ++++++++++-------------- atom/browser/web_contents_preferences.h | 6 ++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index cfb33de14e77..1182e2859fc3 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -215,24 +215,6 @@ bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) { return sandboxed; } -// static -bool WebContentsPreferences::ConvertValueToIntegerFromString( - WebContentsPreferences* pref, std::string attributeName, int* intValue) { - - // if it is already an integer, no conversion needed - if (pref->web_preferences_.GetInteger(attributeName, intValue)) { - return true; - } - - base::string16 stringValue; - - if (pref->web_preferences_.GetString(attributeName, &stringValue)) { - return base::StringToInt(stringValue, intValue); - } - - return false; -} - // static void WebContentsPreferences::OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs) { @@ -272,15 +254,28 @@ void WebContentsPreferences::OverrideWebkitPrefs( prefs->fantasy_font_family_map[content::kCommonScript] = font; } int size; - if (ConvertValueToIntegerFromString(self, "defaultFontSize", &size)) + if (self->GetInteger("defaultFontSize", &size)) prefs->default_font_size = size; - if (ConvertValueToIntegerFromString(self, "defaultMonospaceFontSize", &size)) + if (self->GetInteger("defaultMonospaceFontSize", &size)) prefs->default_fixed_font_size = size; - if (ConvertValueToIntegerFromString(self, "minimumFontSize", &size)) + if (self->GetInteger("minimumFontSize", &size)) prefs->minimum_font_size = size; std::string encoding; if (self->web_preferences_.GetString("defaultEncoding", &encoding)) prefs->default_encoding = encoding; } +bool WebContentsPreferences::GetInteger(const std::string& attributeName, + int* intValue) { + // if it is already an integer, no conversion needed + if (web_preferences_.GetInteger(attributeName, intValue)) + return true; + + base::string16 stringValue; + if (web_preferences_.GetString(attributeName, &stringValue)) + return base::StringToInt(stringValue, intValue); + + return false; +} + } // namespace atom diff --git a/atom/browser/web_contents_preferences.h b/atom/browser/web_contents_preferences.h index 7fa1944408b2..f6e44c51b10a 100644 --- a/atom/browser/web_contents_preferences.h +++ b/atom/browser/web_contents_preferences.h @@ -39,9 +39,6 @@ class WebContentsPreferences static bool IsSandboxed(content::WebContents* web_contents); - static bool ConvertValueToIntegerFromString( - WebContentsPreferences* pref, std::string attributeName, int* intValue); - // Modify the WebPreferences according to |web_contents|'s preferences. static void OverrideWebkitPrefs( content::WebContents* web_contents, content::WebPreferences* prefs); @@ -64,6 +61,9 @@ class WebContentsPreferences content::WebContents* web_contents_; base::DictionaryValue web_preferences_; + // Get preferences value as integer possibly coercing it from a string + bool GetInteger(const std::string& attributeName, int* intValue); + DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences); };