1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_font_settings_api.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_preference_helpers.h"
#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.
// E.g., "webkit.webprefs.fonts.standard.Hrkt"
const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s";
// Format for global (non per-script) font preference keys.
// E.g., "webkit.webprefs.global.fixed_font_family"
// Note: there are two meanings of "global" here. The "Global" in the const name
// means "not per-script". The "global" in the key itself means "not per-tab"
// (per-profile).
const char kWebKitGlobalFontPrefFormat[] =
"webkit.webprefs.global.%s_font_family";
// Gets the font name preference path from |details| which contains key
// |kGenericFamilyKey| and optionally |kScriptKey|.
bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path)
{
std::string generic_family;
if (!details->GetString(kGenericFamilyKey, &generic_family))
return false;
if (details->HasKey(kScriptKey)) {
std::string script;
if (!details->GetString(kScriptKey, &script))
return false;
*pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat,
generic_family.c_str(),
script.c_str());
} else {
*pref_path = StringPrintf(kWebKitGlobalFontPrefFormat,
generic_family.c_str());
}
return true;
}
} // namespace
bool GetFontNameFunction::RunImpl() {
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
std::string pref_path;
EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path));
PrefService* prefs = profile_->GetPrefs();
const PrefService::Preference* pref =
prefs->FindPreference(pref_path.c_str());
std::string font_name;
EXTENSION_FUNCTION_VALIDATE(
pref && pref->GetValue()->GetAsString(&font_name));
DictionaryValue* result = new DictionaryValue();
result->SetString(kFontNameKey, font_name);
result_.reset(result);
return true;
}
bool SetFontNameFunction::RunImpl() {
DictionaryValue* details = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
std::string pref_path;
EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path));
std::string font_name;
EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name));
// 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->SetExtensionControlledPref(extension_id(), pref_path.c_str(),
kExtensionPrefsScopeRegular,
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;
}
|