🐛 Fix webpreferences not accepting numeric options

The webpreferences attribute values are parsed as strings instead
of numbers. Therefore, a conversion is required.
This commit is contained in:
Tan Wang Leng 2017-01-31 10:43:12 +08:00 committed by Kevin Sawicki
parent 852519a826
commit 42f65c52fb
2 changed files with 27 additions and 3 deletions

View file

@ -5,6 +5,7 @@
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
#include <algorithm> #include <algorithm>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -215,6 +216,25 @@ bool WebContentsPreferences::IsSandboxed(content::WebContents* web_contents) {
return sandboxed; 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 // static
void WebContentsPreferences::OverrideWebkitPrefs( void WebContentsPreferences::OverrideWebkitPrefs(
content::WebContents* web_contents, content::WebPreferences* prefs) { content::WebContents* web_contents, content::WebPreferences* prefs) {
@ -254,11 +274,11 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->fantasy_font_family_map[content::kCommonScript] = font; prefs->fantasy_font_family_map[content::kCommonScript] = font;
} }
int size; int size;
if (self->web_preferences_.GetInteger("defaultFontSize", &size)) if (ConvertValueToIntegerFromString(self, "defaultFontSize", &size))
prefs->default_font_size = size; prefs->default_font_size = size;
if (self->web_preferences_.GetInteger("defaultMonospaceFontSize", &size)) if (ConvertValueToIntegerFromString(self, "defaultMonospaceFontSize", &size))
prefs->default_fixed_font_size = size; prefs->default_fixed_font_size = size;
if (self->web_preferences_.GetInteger("minimumFontSize", &size)) if (ConvertValueToIntegerFromString(self, "minimumFontSize", &size))
prefs->minimum_font_size = size; prefs->minimum_font_size = size;
std::string encoding; std::string encoding;
if (self->web_preferences_.GetString("defaultEncoding", &encoding)) if (self->web_preferences_.GetString("defaultEncoding", &encoding))

View file

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #ifndef ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_ #define ATOM_BROWSER_WEB_CONTENTS_PREFERENCES_H_
#include <string>
#include <vector> #include <vector>
#include "base/values.h" #include "base/values.h"
@ -38,6 +39,9 @@ class WebContentsPreferences
static bool IsSandboxed(content::WebContents* web_contents); 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. // Modify the WebPreferences according to |web_contents|'s preferences.
static void OverrideWebkitPrefs( static void OverrideWebkitPrefs(
content::WebContents* web_contents, content::WebPreferences* prefs); content::WebContents* web_contents, content::WebPreferences* prefs);