Commit 4d59bf78 authored by falken@chromium.org's avatar falken@chromium.org

Font Settings Extension API: Add functions to clear prefs.

BUG=114148
TEST=browser_tests --gtest_filter=ExtensionApiTest.FontSettings


Review URL: http://codereview.chromium.org/10177003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133638 0039d316-1c4b-4281-b951-d872f2087c98
parent 94545952
...@@ -276,6 +276,24 @@ void ExtensionFontSettingsEventRouter::OnFontPrefChanged( ...@@ -276,6 +276,24 @@ void ExtensionFontSettingsEventRouter::OnFontPrefChanged(
pref_name); pref_name);
} }
bool ClearFontFunction::RunImpl() {
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
std::string pref_path;
EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path));
// Ensure |pref_path| really is for a registered per-script font pref.
EXTENSION_FUNCTION_VALIDATE(
profile_->GetPrefs()->FindPreference(pref_path.c_str()));
ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
prefs->RemoveExtensionControlledPref(extension_id(),
pref_path.c_str(),
kExtensionPrefsScopeRegular);
return true;
}
bool GetFontFunction::RunImpl() { bool GetFontFunction::RunImpl() {
DictionaryValue* details = NULL; DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
...@@ -361,6 +379,14 @@ bool GetFontListFunction::CopyFontsToResult(ListValue* fonts) { ...@@ -361,6 +379,14 @@ bool GetFontListFunction::CopyFontsToResult(ListValue* fonts) {
return true; return true;
} }
bool ClearFontPrefExtensionFunction::RunImpl() {
ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
prefs->RemoveExtensionControlledPref(extension_id(),
GetPrefName(),
kExtensionPrefsScopeRegular);
return true;
}
bool GetFontPrefExtensionFunction::RunImpl() { bool GetFontPrefExtensionFunction::RunImpl() {
PrefService* prefs = profile_->GetPrefs(); PrefService* prefs = profile_->GetPrefs();
const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); const PrefService::Preference* pref = prefs->FindPreference(GetPrefName());
...@@ -387,6 +413,10 @@ bool SetFontPrefExtensionFunction::RunImpl() { ...@@ -387,6 +413,10 @@ bool SetFontPrefExtensionFunction::RunImpl() {
return true; return true;
} }
const char* ClearDefaultFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalDefaultFontSize;
}
const char* GetDefaultFontSizeFunction::GetPrefName() { const char* GetDefaultFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalDefaultFontSize; return prefs::kWebKitGlobalDefaultFontSize;
} }
...@@ -403,6 +433,10 @@ const char* SetDefaultFontSizeFunction::GetKey() { ...@@ -403,6 +433,10 @@ const char* SetDefaultFontSizeFunction::GetKey() {
return kPixelSizeKey; return kPixelSizeKey;
} }
const char* ClearDefaultFixedFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalDefaultFixedFontSize;
}
const char* GetDefaultFixedFontSizeFunction::GetPrefName() { const char* GetDefaultFixedFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalDefaultFixedFontSize; return prefs::kWebKitGlobalDefaultFixedFontSize;
} }
...@@ -419,6 +453,10 @@ const char* SetDefaultFixedFontSizeFunction::GetKey() { ...@@ -419,6 +453,10 @@ const char* SetDefaultFixedFontSizeFunction::GetKey() {
return kPixelSizeKey; return kPixelSizeKey;
} }
const char* ClearMinimumFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalMinimumFontSize;
}
const char* GetMinimumFontSizeFunction::GetPrefName() { const char* GetMinimumFontSizeFunction::GetPrefName() {
return prefs::kWebKitGlobalMinimumFontSize; return prefs::kWebKitGlobalMinimumFontSize;
} }
...@@ -435,6 +473,10 @@ const char* SetMinimumFontSizeFunction::GetKey() { ...@@ -435,6 +473,10 @@ const char* SetMinimumFontSizeFunction::GetKey() {
return kPixelSizeKey; return kPixelSizeKey;
} }
const char* ClearDefaultCharacterSetFunction::GetPrefName() {
return prefs::kGlobalDefaultCharset;
}
const char* GetDefaultCharacterSetFunction::GetPrefName() { const char* GetDefaultCharacterSetFunction::GetPrefName() {
return prefs::kGlobalDefaultCharset; return prefs::kGlobalDefaultCharset;
} }
......
...@@ -56,6 +56,12 @@ class ExtensionFontSettingsEventRouter : public content::NotificationObserver { ...@@ -56,6 +56,12 @@ class ExtensionFontSettingsEventRouter : public content::NotificationObserver {
DISALLOW_COPY_AND_ASSIGN(ExtensionFontSettingsEventRouter); DISALLOW_COPY_AND_ASSIGN(ExtensionFontSettingsEventRouter);
}; };
class ClearFontFunction : public SyncExtensionFunction {
public:
virtual bool RunImpl() OVERRIDE;
DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.clearFont")
};
class GetFontFunction : public SyncExtensionFunction { class GetFontFunction : public SyncExtensionFunction {
public: public:
virtual bool RunImpl() OVERRIDE; virtual bool RunImpl() OVERRIDE;
...@@ -78,6 +84,16 @@ class GetFontListFunction : public AsyncExtensionFunction { ...@@ -78,6 +84,16 @@ class GetFontListFunction : public AsyncExtensionFunction {
bool CopyFontsToResult(base::ListValue* fonts); bool CopyFontsToResult(base::ListValue* fonts);
}; };
// Base class for functions that clear a font pref.
class ClearFontPrefExtensionFunction : public SyncExtensionFunction {
protected:
virtual bool RunImpl() OVERRIDE;
// Implementations should return the name of the preference to clear, like
// "webkit.webprefs.default_font_size".
virtual const char* GetPrefName() = 0;
};
// Base class for functions that get a font pref. // Base class for functions that get a font pref.
class GetFontPrefExtensionFunction : public SyncExtensionFunction { class GetFontPrefExtensionFunction : public SyncExtensionFunction {
protected: protected:
...@@ -106,6 +122,15 @@ class SetFontPrefExtensionFunction : public SyncExtensionFunction { ...@@ -106,6 +122,15 @@ class SetFontPrefExtensionFunction : public SyncExtensionFunction {
virtual const char* GetKey() = 0; virtual const char* GetKey() = 0;
}; };
class ClearDefaultFontSizeFunction : public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME(
"experimental.fontSettings.clearDefaultFontSize")
protected:
virtual const char* GetPrefName() OVERRIDE;
};
class GetDefaultFontSizeFunction : public GetFontPrefExtensionFunction { class GetDefaultFontSizeFunction : public GetFontPrefExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION_NAME( DECLARE_EXTENSION_FUNCTION_NAME(
...@@ -126,6 +151,16 @@ class SetDefaultFontSizeFunction : public SetFontPrefExtensionFunction { ...@@ -126,6 +151,16 @@ class SetDefaultFontSizeFunction : public SetFontPrefExtensionFunction {
virtual const char* GetKey() OVERRIDE; virtual const char* GetKey() OVERRIDE;
}; };
class ClearDefaultFixedFontSizeFunction
: public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME(
"experimental.fontSettings.clearDefaultFixedFontSize")
protected:
virtual const char* GetPrefName() OVERRIDE;
};
class GetDefaultFixedFontSizeFunction : public GetFontPrefExtensionFunction { class GetDefaultFixedFontSizeFunction : public GetFontPrefExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION_NAME( DECLARE_EXTENSION_FUNCTION_NAME(
...@@ -146,6 +181,15 @@ class SetDefaultFixedFontSizeFunction : public SetFontPrefExtensionFunction { ...@@ -146,6 +181,15 @@ class SetDefaultFixedFontSizeFunction : public SetFontPrefExtensionFunction {
virtual const char* GetKey() OVERRIDE; virtual const char* GetKey() OVERRIDE;
}; };
class ClearMinimumFontSizeFunction : public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME(
"experimental.fontSettings.clearMinimumFontSize")
protected:
virtual const char* GetPrefName() OVERRIDE;
};
class GetMinimumFontSizeFunction : public GetFontPrefExtensionFunction { class GetMinimumFontSizeFunction : public GetFontPrefExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION_NAME( DECLARE_EXTENSION_FUNCTION_NAME(
...@@ -166,6 +210,15 @@ class SetMinimumFontSizeFunction : public SetFontPrefExtensionFunction { ...@@ -166,6 +210,15 @@ class SetMinimumFontSizeFunction : public SetFontPrefExtensionFunction {
virtual const char* GetKey() OVERRIDE; virtual const char* GetKey() OVERRIDE;
}; };
class ClearDefaultCharacterSetFunction : public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME(
"experimental.fontSettings.clearDefaultCharacterSet")
protected:
virtual const char* GetPrefName() OVERRIDE;
};
class GetDefaultCharacterSetFunction : public GetFontPrefExtensionFunction { class GetDefaultCharacterSetFunction : public GetFontPrefExtensionFunction {
public: public:
DECLARE_EXTENSION_FUNCTION_NAME( DECLARE_EXTENSION_FUNCTION_NAME(
......
...@@ -13,5 +13,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FontSettings) { ...@@ -13,5 +13,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FontSettings) {
CommandLine::ForCurrentProcess()->AppendSwitch( CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis); switches::kEnableExperimentalExtensionApis);
PrefService* prefs = browser()->profile()->GetPrefs();
prefs->SetString(prefs::kWebKitStandardFontFamilyKorean, "Tahoma");
prefs->SetString(prefs::kWebKitGlobalSansSerifFontFamily, "Arial");
prefs->SetInteger(prefs::kWebKitGlobalDefaultFontSize, 16);
prefs->SetInteger(prefs::kWebKitGlobalDefaultFixedFontSize, 14);
prefs->SetInteger(prefs::kWebKitGlobalMinimumFontSize, 8);
prefs->SetString(prefs::kGlobalDefaultCharset, "Shift_JIS");
EXPECT_TRUE(RunExtensionTest("font_settings")) << message_; EXPECT_TRUE(RunExtensionTest("font_settings")) << message_;
} }
...@@ -422,14 +422,19 @@ void ExtensionFunctionRegistry::ResetFunctions() { ...@@ -422,14 +422,19 @@ void ExtensionFunctionRegistry::ResetFunctions() {
// Font settings. // Font settings.
RegisterFunction<GetFontListFunction>(); RegisterFunction<GetFontListFunction>();
RegisterFunction<ClearFontFunction>();
RegisterFunction<GetFontFunction>(); RegisterFunction<GetFontFunction>();
RegisterFunction<SetFontFunction>(); RegisterFunction<SetFontFunction>();
RegisterFunction<ClearDefaultFontSizeFunction>();
RegisterFunction<GetDefaultFontSizeFunction>(); RegisterFunction<GetDefaultFontSizeFunction>();
RegisterFunction<SetDefaultFontSizeFunction>(); RegisterFunction<SetDefaultFontSizeFunction>();
RegisterFunction<ClearDefaultFixedFontSizeFunction>();
RegisterFunction<GetDefaultFixedFontSizeFunction>(); RegisterFunction<GetDefaultFixedFontSizeFunction>();
RegisterFunction<SetDefaultFixedFontSizeFunction>(); RegisterFunction<SetDefaultFixedFontSizeFunction>();
RegisterFunction<ClearMinimumFontSizeFunction>();
RegisterFunction<GetMinimumFontSizeFunction>(); RegisterFunction<GetMinimumFontSizeFunction>();
RegisterFunction<SetMinimumFontSizeFunction>(); RegisterFunction<SetMinimumFontSizeFunction>();
RegisterFunction<ClearDefaultCharacterSetFunction>();
RegisterFunction<GetDefaultCharacterSetFunction>(); RegisterFunction<GetDefaultCharacterSetFunction>();
RegisterFunction<SetDefaultCharacterSetFunction>(); RegisterFunction<SetDefaultCharacterSetFunction>();
......
...@@ -45,9 +45,36 @@ ...@@ -45,9 +45,36 @@
} }
], ],
"functions": [ "functions": [
{
"name": "clearFont",
"description": "Clears the font set by this extension, if any.",
"parameters": [
{
"name": "details",
"type": "object",
"properties": {
"script": {
"$ref": "ScriptCode",
"description": "The script for which the font should be cleared. If omitted, the global script font setting is cleared.",
"optional": true
},
"genericFamily": {
"$ref": "GenericFamily",
"description": "The generic font family for which the font should be cleared."
}
}
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{ {
"name": "getFont", "name": "getFont",
"description": "Gets the current font setting for a given script and generic font family.", "description": "Gets the font for a given script and generic font family.",
"parameters": [ "parameters": [
{ {
"name": "details", "name": "details",
...@@ -55,12 +82,12 @@ ...@@ -55,12 +82,12 @@
"properties": { "properties": {
"script": { "script": {
"$ref": "ScriptCode", "$ref": "ScriptCode",
"description": "The script for which the font setting should be retrieved. If omitted, the global script font setting is retrieved.", "description": "The script for which the font should be retrieved. If omitted, the font for the global script is retrieved.",
"optional": true "optional": true
}, },
"genericFamily": { "genericFamily": {
"$ref": "GenericFamily", "$ref": "GenericFamily",
"description": "The generic font family for which the font setting should be retrieved." "description": "The generic font family for which the font should be retrieved."
} }
} }
}, },
...@@ -85,7 +112,7 @@ ...@@ -85,7 +112,7 @@
}, },
{ {
"name": "setFont", "name": "setFont",
"description": "Sets the font setting for a given script and generic font family.", "description": "Sets the font for a given script and generic font family.",
"parameters": [ "parameters": [
{ {
"name": "details", "name": "details",
...@@ -93,12 +120,12 @@ ...@@ -93,12 +120,12 @@
"properties": { "properties": {
"script": { "script": {
"$ref": "ScriptCode", "$ref": "ScriptCode",
"description": "The script code which the font setting should be set. If omitted, the global script font setting is set.", "description": "The script code which the font should be set. If omitted, the font for the global script is set.",
"optional": true "optional": true
}, },
"genericFamily": { "genericFamily": {
"$ref": "GenericFamily", "$ref": "GenericFamily",
"description": "The generic font family for which the font setting should be set." "description": "The generic font family for which the font should be set."
}, },
"fontName": { "fontName": {
"type": "string", "type": "string",
...@@ -131,6 +158,24 @@ ...@@ -131,6 +158,24 @@
} }
] ]
}, },
{
"name": "clearDefaultFontSize",
"description": "Clears the default font size set by this extension, if any.",
"parameters": [
{
"name": "details",
"type": "object",
"optional": true,
"description": "This parameter is currently unused."
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{ {
"name": "getDefaultFontSize", "name": "getDefaultFontSize",
"description": "Gets the default font size.", "description": "Gets the default font size.",
...@@ -182,6 +227,24 @@ ...@@ -182,6 +227,24 @@
} }
] ]
}, },
{
"name": "clearDefaultFixedFontSize",
"description": "Clears the default fixed font size set by this extension, if any.",
"parameters": [
{
"name": "details",
"type": "object",
"optional": true,
"description": "This parameter is currently unused."
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{ {
"name": "getDefaultFixedFontSize", "name": "getDefaultFixedFontSize",
"description": "Gets the default size for fixed width fonts.", "description": "Gets the default size for fixed width fonts.",
...@@ -233,6 +296,24 @@ ...@@ -233,6 +296,24 @@
} }
] ]
}, },
{
"name": "clearMinimumFontSize",
"description": "Clears the minimum font size set by this extension, if any.",
"parameters": [
{
"name": "details",
"type": "object",
"optional": true,
"description": "This parameter is currently unused."
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{ {
"name": "getMinimumFontSize", "name": "getMinimumFontSize",
"description": "Gets the minimum font size.", "description": "Gets the minimum font size.",
...@@ -284,6 +365,24 @@ ...@@ -284,6 +365,24 @@
} }
] ]
}, },
{
"name": "clearDefaultCharacterSet",
"description": "Clears the default character set set by this extension, if any.",
"parameters": [
{
"name": "details",
"type": "object",
"optional": true,
"description": "This parameter is currently unused."
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{ {
"name": "getDefaultCharacterSet", "name": "getDefaultCharacterSet",
"description": "Gets the default character set.", "description": "Gets the default character set.",
......
...@@ -121,6 +121,11 @@ ...@@ -121,6 +121,11 @@
"chrome.experimental.devtools.console.onMessageAdded": "experimental.devtools.console.html#event-onMessageAdded", "chrome.experimental.devtools.console.onMessageAdded": "experimental.devtools.console.html#event-onMessageAdded",
"chrome.experimental.dns.resolve": "experimental.dns.html#method-resolve", "chrome.experimental.dns.resolve": "experimental.dns.html#method-resolve",
"chrome.experimental.downloads.download": "experimental.downloads.html#method-download", "chrome.experimental.downloads.download": "experimental.downloads.html#method-download",
"chrome.experimental.fontSettings.clearDefaultCharacterSet": "experimental.fontSettings.html#method-clearDefaultCharacterSet",
"chrome.experimental.fontSettings.clearDefaultFixedFontSize": "experimental.fontSettings.html#method-clearDefaultFixedFontSize",
"chrome.experimental.fontSettings.clearDefaultFontSize": "experimental.fontSettings.html#method-clearDefaultFontSize",
"chrome.experimental.fontSettings.clearFont": "experimental.fontSettings.html#method-clearFont",
"chrome.experimental.fontSettings.clearMinimumFontSize": "experimental.fontSettings.html#method-clearMinimumFontSize",
"chrome.experimental.fontSettings.getDefaultCharacterSet": "experimental.fontSettings.html#method-getDefaultCharacterSet", "chrome.experimental.fontSettings.getDefaultCharacterSet": "experimental.fontSettings.html#method-getDefaultCharacterSet",
"chrome.experimental.fontSettings.getDefaultFixedFontSize": "experimental.fontSettings.html#method-getDefaultFixedFontSize", "chrome.experimental.fontSettings.getDefaultFixedFontSize": "experimental.fontSettings.html#method-getDefaultFixedFontSize",
"chrome.experimental.fontSettings.getDefaultFontSize": "experimental.fontSettings.html#method-getDefaultFontSize", "chrome.experimental.fontSettings.getDefaultFontSize": "experimental.fontSettings.html#method-getDefaultFontSize",
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// Run with browser_tests --gtest_filter=ExtensionApiTest.FontSettings // Run with browser_tests --gtest_filter=ExtensionApiTest.FontSettings
var fs = chrome.experimental.fontSettings; var fs = chrome.experimental.fontSettings;
var CONTROLLABLE_BY_THIS_EXTENSION = 'controllable_by_this_extension';
function expect(expected, message) { function expect(expected, message) {
return chrome.test.callbackPass(function(value) { return chrome.test.callbackPass(function(value) {
...@@ -173,4 +174,93 @@ chrome.test.runTests([ ...@@ -173,4 +174,93 @@ chrome.test.runTests([
var message = 'Setting for default character set should be ' + expected; var message = 'Setting for default character set should be ' + expected;
fs.getDefaultCharacterSet(expect({charset: expected}, message)); fs.getDefaultCharacterSet(expect({charset: expected}, message));
}, },
// This test may fail on Windows if the font is not installed on the
// system. See crbug.com/122303
function clearPerScriptFont() {
var script = 'Hang';
var genericFamily = 'standard';
var fontName = 'Tahoma';
chrome.test.listenOnce(fs.onFontChanged, function(details) {
chrome.test.assertEq(details, {
script: script,
genericFamily: genericFamily,
fontName: fontName,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearFont({
script: script,
genericFamily: genericFamily,
}, chrome.test.callbackPass());
},
// This test may fail on Windows if the font is not installed on the
// system. See crbug.com/122303
function clearGlobalFont() {
var genericFamily = 'sansserif';
var fontName = 'Arial';
chrome.test.listenOnce(fs.onFontChanged, function(details) {
chrome.test.assertEq(details, {
genericFamily: genericFamily,
fontName: fontName,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearFont({
genericFamily: genericFamily,
}, chrome.test.callbackPass());
},
function clearDefaultFontSize() {
var pixelSize = 16;
chrome.test.listenOnce(fs.onDefaultFontSizeChanged, function(details) {
chrome.test.assertEq(details, {
pixelSize: pixelSize,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearDefaultFontSize({}, chrome.test.callbackPass());
},
function clearDefaultFixedFontSize() {
var pixelSize = 14;
chrome.test.listenOnce(fs.onDefaultFixedFontSizeChanged, function(details) {
chrome.test.assertEq(details, {
pixelSize: pixelSize,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearDefaultFixedFontSize({}, chrome.test.callbackPass());
},
function clearMinimumFontSize() {
var pixelSize = 8;
chrome.test.listenOnce(fs.onMinimumFontSizeChanged, function(details) {
chrome.test.assertEq(details, {
pixelSize: pixelSize,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearMinimumFontSize({}, chrome.test.callbackPass());
},
function clearDefaultCharacterSet() {
var charset = 'Shift_JIS';
chrome.test.listenOnce(fs.onDefaultCharacterSetChanged, function(details) {
chrome.test.assertEq(details, {
charset: charset,
levelOfControl: CONTROLLABLE_BY_THIS_EXTENSION
});
});
fs.clearDefaultCharacterSet({}, chrome.test.callbackPass());
},
]); ]);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment