Commit ab813c43 authored by falken@chromium.org's avatar falken@chromium.org

Add getFontList() to Font Settings Extension API.

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


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124648 0039d316-1c4b-4281-b951-d872f2087c98
parent ca7a3d79
......@@ -12,11 +12,13 @@
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "content/public/browser/font_list_async.h"
namespace {
const char kGenericFamilyKey[] = "genericFamily";
const char kFontNameKey[] = "fontName";
const char kLocalizedNameKey[] = "localizedName";
const char kScriptKey[] = "script";
// Format for per-script font preference keys.
......@@ -43,11 +45,11 @@ bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path)
std::string script;
if (!details->GetString(kScriptKey, &script))
return false;
*pref_path = base::StringPrintf(kWebKitPerScriptFontPrefFormat,
*pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat,
generic_family.c_str(),
script.c_str());
} else {
*pref_path = base::StringPrintf(kWebKitGlobalFontPrefFormat,
*pref_path = StringPrintf(kWebKitGlobalFontPrefFormat,
generic_family.c_str());
}
......@@ -93,6 +95,49 @@ bool SetFontNameFunction::RunImpl() {
ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(),
kExtensionPrefsScopeRegular,
base::Value::CreateStringValue(font_name));
Value::CreateStringValue(font_name));
return true;
}
bool GetFontListFunction::RunImpl() {
content::GetFontListAsync(
Bind(&GetFontListFunction::FontListHasLoaded, this));
return true;
}
void GetFontListFunction::FontListHasLoaded(scoped_ptr<ListValue> list) {
bool success = CopyFontsToResult(list.get());
SendResponse(success);
}
bool GetFontListFunction::CopyFontsToResult(ListValue* fonts)
{
scoped_ptr<ListValue> result(new ListValue());
for (ListValue::iterator it = fonts->begin(); it != fonts->end(); ++it) {
ListValue* font_list_value;
if (!(*it)->GetAsList(&font_list_value)) {
NOTREACHED();
return false;
}
std::string name;
if (!font_list_value->GetString(0, &name)) {
NOTREACHED();
return false;
}
std::string localized_name;
if (!font_list_value->GetString(1, &localized_name)) {
NOTREACHED();
return false;
}
DictionaryValue* font_name = new DictionaryValue();
font_name->Set(kFontNameKey, Value::CreateStringValue(name));
font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name));
result->Append(font_name);
}
result_.reset(result.release());
return true;
}
......@@ -20,4 +20,14 @@ class SetFontNameFunction : public SyncExtensionFunction {
DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.setFontName")
};
class GetFontListFunction : public AsyncExtensionFunction {
public:
virtual bool RunImpl() OVERRIDE;
DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.getFontList")
private:
void FontListHasLoaded(scoped_ptr<base::ListValue> list);
bool CopyFontsToResult(base::ListValue* fonts);
};
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__
......@@ -458,6 +458,7 @@ void FactoryRegistry::ResetFunctions() {
RegisterFunction<SetContentSettingFunction>();
// Font settings.
RegisterFunction<GetFontListFunction>();
RegisterFunction<GetFontNameFunction>();
RegisterFunction<SetFontNameFunction>();
......
[
{
"namespace": "experimental.fontSettings",
"types": [
{
"id": "FontName",
"type": "object",
"description": "Represents a font name.",
"properties": {
"fontName": {
"type": "string",
"description": "The font name."
},
"localizedName": {
"type": "string",
"description": "The font name localized for the current locale."
}
}
}
],
"functions": [
{
"name": "getFontName",
"type": "function",
"description": "Gets the font name of the current setting for a given script and font family.",
"description": "Gets the font name of the current setting for a given script and generic font family.",
"parameters": [
{
"name": "details",
......@@ -46,7 +63,7 @@
{
"name": "setFontName",
"type": "function",
"description": "Sets the font name of the current setting for a given script and font family.",
"description": "Sets the font name of the current setting for a given script and generic font family.",
"parameters": [
{
"name": "details",
......@@ -76,6 +93,24 @@
"parameters": []
}
]
},
{
"name": "getFontList",
"type": "function",
"description": "Gets a list of fonts on the system.",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "results",
"type": "array",
"items": { "$ref": "FontName" }
}
]
}
]
}
]
}
......
......@@ -45,5 +45,16 @@ chrome.test.runTests([
fs.getFontName({
genericFamily: 'sansserif'
}, expect({fontName: expected}, message));
},
function getFontList() {
var message = 'getFontList should return an array of objects with ' +
'fontName and localizedName properties.';
fs.getFontList(chrome.test.callbackPass(function(value) {
chrome.test.assertTrue(value.length > 0,
"Font list is not expected to be empty.");
chrome.test.assertEq('string', typeof(value[0].fontName), message);
chrome.test.assertEq('string', typeof(value[0].localizedName), message);
}));
}
]);
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