OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_font_settings_api.h" | 5 #include "chrome/browser/extensions/extension_font_settings_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/extension_preference_helpers.h" | 11 #include "chrome/browser/extensions/extension_preference_helpers.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
15 #include "content/public/browser/font_list_async.h" | |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char kGenericFamilyKey[] = "genericFamily"; | 19 const char kGenericFamilyKey[] = "genericFamily"; |
19 const char kFontNameKey[] = "fontName"; | 20 const char kFontNameKey[] = "fontName"; |
21 const char kLocalizedNameKey[] = "localizedName"; | |
20 const char kScriptKey[] = "script"; | 22 const char kScriptKey[] = "script"; |
21 | 23 |
22 // Format for per-script font preference keys. | 24 // Format for per-script font preference keys. |
23 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 25 // E.g., "webkit.webprefs.fonts.standard.Hrkt" |
24 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 26 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
25 | 27 |
26 // Format for global (non per-script) font preference keys. | 28 // Format for global (non per-script) font preference keys. |
27 // E.g., "webkit.webprefs.global.fixed_font_family" | 29 // E.g., "webkit.webprefs.global.fixed_font_family" |
28 // Note: there are two meanings of "global" here. The "Global" in the const name | 30 // Note: there are two meanings of "global" here. The "Global" in the const name |
29 // means "not per-script". The "global" in the key itself means "not per-tab" | 31 // means "not per-script". The "global" in the key itself means "not per-tab" |
30 // (per-profile). | 32 // (per-profile). |
31 const char kWebKitGlobalFontPrefFormat[] = | 33 const char kWebKitGlobalFontPrefFormat[] = |
32 "webkit.webprefs.global.%s_font_family"; | 34 "webkit.webprefs.global.%s_font_family"; |
33 | 35 |
34 // Gets the font name preference path from |details| which contains key | 36 // Gets the font name preference path from |details| which contains key |
35 // |kGenericFamilyKey| and optionally |kScriptKey|. | 37 // |kGenericFamilyKey| and optionally |kScriptKey|. |
36 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) | 38 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) |
37 { | 39 { |
38 std::string generic_family; | 40 std::string generic_family; |
39 if (!details->GetString(kGenericFamilyKey, &generic_family)) | 41 if (!details->GetString(kGenericFamilyKey, &generic_family)) |
40 return false; | 42 return false; |
41 | 43 |
42 if (details->HasKey(kScriptKey)) { | 44 if (details->HasKey(kScriptKey)) { |
43 std::string script; | 45 std::string script; |
44 if (!details->GetString(kScriptKey, &script)) | 46 if (!details->GetString(kScriptKey, &script)) |
45 return false; | 47 return false; |
46 *pref_path = base::StringPrintf(kWebKitPerScriptFontPrefFormat, | 48 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, |
47 generic_family.c_str(), | 49 generic_family.c_str(), |
48 script.c_str()); | 50 script.c_str()); |
49 } else { | 51 } else { |
50 *pref_path = base::StringPrintf(kWebKitGlobalFontPrefFormat, | 52 *pref_path = StringPrintf(kWebKitGlobalFontPrefFormat, |
51 generic_family.c_str()); | 53 generic_family.c_str()); |
52 } | 54 } |
53 | 55 |
54 return true; | 56 return true; |
55 } | 57 } |
56 | 58 |
57 } // namespace | 59 } // namespace |
58 | 60 |
59 bool GetFontNameFunction::RunImpl() { | 61 bool GetFontNameFunction::RunImpl() { |
60 DictionaryValue* details = NULL; | 62 DictionaryValue* details = NULL; |
(...skipping 25 matching lines...) Expand all Loading... | |
86 std::string font_name; | 88 std::string font_name; |
87 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); | 89 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); |
88 | 90 |
89 // Ensure |pref_path| really is for a registered per-script font pref. | 91 // Ensure |pref_path| really is for a registered per-script font pref. |
90 EXTENSION_FUNCTION_VALIDATE( | 92 EXTENSION_FUNCTION_VALIDATE( |
91 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 93 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
92 | 94 |
93 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 95 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
94 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), | 96 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), |
95 kExtensionPrefsScopeRegular, | 97 kExtensionPrefsScopeRegular, |
96 base::Value::CreateStringValue(font_name)); | 98 Value::CreateStringValue(font_name)); |
97 return true; | 99 return true; |
98 } | 100 } |
101 | |
102 bool GetFontListFunction::RunImpl() { | |
103 content::GetFontListAsync( | |
104 Bind(&GetFontListFunction::FontListHasLoaded, this)); | |
105 return true; | |
106 } | |
107 | |
108 void GetFontListFunction::FontListHasLoaded(scoped_ptr<ListValue> list) { | |
109 bool success = CopyFontsToResult(list.get()); | |
110 SendResponse(success); | |
111 } | |
112 | |
113 bool GetFontListFunction::CopyFontsToResult(ListValue* fonts) | |
114 { | |
115 ListValue* result = new ListValue(); | |
Matt Perry
2012/03/01 22:57:29
Use a scoped_ptr to avoid leaking result.
falken
2012/03/01 23:30:52
Done.
| |
116 for (ListValue::iterator it = fonts->begin(); it != fonts->end(); ++it) { | |
117 ListValue *fontListValue; | |
Matt Perry
2012/03/01 22:57:29
ListValue* font_list_value
falken
2012/03/01 23:30:52
Done.
| |
118 if (!(*it)->GetAsList(&fontListValue)) { | |
119 NOTREACHED(); | |
120 return false; | |
121 } | |
122 | |
123 std::string name; | |
124 if (!fontListValue->GetString(0, &name)) { | |
125 NOTREACHED(); | |
126 return false; | |
127 } | |
128 | |
129 std::string localizedName; | |
Matt Perry
2012/03/01 22:57:29
naming style
falken
2012/03/01 23:30:52
Done.
| |
130 if (!fontListValue->GetString(1, &localizedName)) { | |
131 NOTREACHED(); | |
132 return false; | |
133 } | |
134 | |
135 DictionaryValue* fontName = new DictionaryValue(); | |
Matt Perry
2012/03/01 22:57:29
ditto
falken
2012/03/01 23:30:52
Done.
| |
136 fontName->Set(kFontNameKey, Value::CreateStringValue(name)); | |
137 fontName->Set(kLocalizedNameKey, Value::CreateStringValue(localizedName)); | |
138 result->Append(fontName); | |
139 } | |
140 | |
141 result_.reset(result); | |
142 return true; | |
143 } | |
OLD | NEW |