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/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 const char kOnDefaultCharacterSetChanged[] = | 43 const char kOnDefaultCharacterSetChanged[] = |
44 "experimental.fontSettings.onDefaultCharacterSetChanged"; | 44 "experimental.fontSettings.onDefaultCharacterSetChanged"; |
45 const char kOnDefaultFixedFontSizeChanged[] = | 45 const char kOnDefaultFixedFontSizeChanged[] = |
46 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; | 46 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; |
47 const char kOnDefaultFontSizeChanged[] = | 47 const char kOnDefaultFontSizeChanged[] = |
48 "experimental.fontSettings.onDefaultFontSizeChanged"; | 48 "experimental.fontSettings.onDefaultFontSizeChanged"; |
49 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; | 49 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; |
50 const char kOnMinimumFontSizeChanged[] = | 50 const char kOnMinimumFontSizeChanged[] = |
51 "experimental.fontSettings.onMinimumFontSizeChanged"; | 51 "experimental.fontSettings.onMinimumFontSizeChanged"; |
52 | 52 |
53 // Format for per-script font preference keys. | 53 // Format for font preference keys. |
54 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 54 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
55 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 55 const char kWebKitFontPrefPrefix[] = "webkit.webprefs.fonts."; |
56 const char kWebKitPerScriptFontPrefPrefix[] = "webkit.webprefs.fonts."; | |
57 | |
58 // Format for global (non per-script) font preference keys. | |
59 // E.g., "webkit.webprefs.global.fixed_font_family" | |
60 // Note: there are two meanings of "global" here. The "Global" in the const name | |
61 // means "not per-script". The "global" in the key itself means "not per-tab" | |
62 // (per-profile). | |
63 const char kWebKitGlobalFontPrefFormat[] = | |
64 "webkit.webprefs.global.%s_font_family"; | |
65 const char kWebKitGlobalFontPrefPrefix[] = "webkit.webprefs.global."; | |
66 const char kWebKitGlobalFontPrefSuffix[] = "_font_family"; | |
67 | 56 |
68 // Gets the font name preference path from |details| which contains key | 57 // Gets the font name preference path from |details| which contains key |
69 // |kGenericFamilyKey| and optionally |kScriptKey|. | 58 // |kGenericFamilyKey| and optionally |kScriptKey|. |
70 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { | 59 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { |
71 std::string generic_family; | 60 std::string generic_family; |
72 if (!details->GetString(kGenericFamilyKey, &generic_family)) | 61 if (!details->GetString(kGenericFamilyKey, &generic_family)) |
73 return false; | 62 return false; |
74 | 63 |
75 if (details->HasKey(kScriptKey)) { | 64 std::string script; |
76 std::string script; | 65 if (!details->HasKey(kScriptKey)) |
77 if (!details->GetString(kScriptKey, &script)) | 66 script = prefs::kWebKitCommonScript; |
78 return false; | 67 else if (!details->GetString(kScriptKey, &script)) |
79 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, | 68 return false; |
80 generic_family.c_str(), | 69 *pref_path = StringPrintf(kWebKitFontPrefFormat, |
81 script.c_str()); | 70 generic_family.c_str(), |
82 } else { | 71 script.c_str()); |
83 *pref_path = StringPrintf(kWebKitGlobalFontPrefFormat, | |
84 generic_family.c_str()); | |
85 } | |
86 | |
87 return true; | 72 return true; |
88 } | 73 } |
89 | 74 |
90 // Extracts the generic family and script from font name pref path |pref_path|. | 75 // Extracts the generic family and script from font name pref path |pref_path|. |
91 bool ParseFontNamePrefPath(std::string pref_path, | 76 bool ParseFontNamePrefPath(std::string pref_path, |
92 std::string* generic_family, | 77 std::string* generic_family, |
93 std::string* script) { | 78 std::string* script) { |
94 if (StartsWithASCII(pref_path, kWebKitPerScriptFontPrefPrefix, true)) { | 79 if (!StartsWithASCII(pref_path, kWebKitFontPrefPrefix, true)) |
95 size_t start = strlen(kWebKitPerScriptFontPrefPrefix); | 80 return false; |
96 size_t pos = pref_path.find('.', start); | 81 |
97 if (pos == std::string::npos || pos + 1 == pref_path.length()) | 82 size_t start = strlen(kWebKitFontPrefPrefix); |
98 return false; | 83 size_t pos = pref_path.find('.', start); |
99 *generic_family = pref_path.substr(start, pos - start); | 84 if (pos == std::string::npos || pos + 1 == pref_path.length()) |
100 *script = pref_path.substr(pos + 1); | 85 return false; |
101 return true; | 86 *generic_family = pref_path.substr(start, pos - start); |
102 } else if (StartsWithASCII(pref_path, kWebKitGlobalFontPrefPrefix, true) && | 87 *script = pref_path.substr(pos + 1); |
103 EndsWith(pref_path, kWebKitGlobalFontPrefSuffix, true)) { | 88 return true; |
104 size_t start = strlen(kWebKitGlobalFontPrefPrefix); | |
105 size_t pos = pref_path.find('_', start); | |
106 if (pos == std::string::npos || pos + 1 == pref_path.length()) | |
107 return false; | |
108 *generic_family = pref_path.substr(start, pos - start); | |
109 *script = ""; | |
110 return true; | |
111 } | |
112 return false; | |
113 } | 89 } |
114 | 90 |
115 // Returns the localized name of a font so that it can be matched within the | 91 // Returns the localized name of a font so that it can be matched within the |
116 // list of system fonts. On Windows, the list of system fonts has names only | 92 // list of system fonts. On Windows, the list of system fonts has names only |
117 // for the system locale, but the pref value may be in the English name. | 93 // for the system locale, but the pref value may be in the English name. |
118 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 94 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
119 #if defined(OS_WIN) | 95 #if defined(OS_WIN) |
120 if (!font_name.empty()) { | 96 if (!font_name.empty()) { |
121 gfx::Font font(font_name, 12); // dummy font size | 97 gfx::Font font(font_name, 12); // dummy font size |
122 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 98 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
(...skipping 30 matching lines...) Expand all Loading... |
153 AddPrefToObserve(prefs::kWebKitGlobalDefaultFontSize, | 129 AddPrefToObserve(prefs::kWebKitGlobalDefaultFontSize, |
154 kOnDefaultFontSizeChanged, | 130 kOnDefaultFontSizeChanged, |
155 kPixelSizeKey); | 131 kPixelSizeKey); |
156 AddPrefToObserve(prefs::kWebKitGlobalMinimumFontSize, | 132 AddPrefToObserve(prefs::kWebKitGlobalMinimumFontSize, |
157 kOnMinimumFontSizeChanged, | 133 kOnMinimumFontSizeChanged, |
158 kPixelSizeKey); | 134 kPixelSizeKey); |
159 AddPrefToObserve(prefs::kGlobalDefaultCharset, | 135 AddPrefToObserve(prefs::kGlobalDefaultCharset, |
160 kOnDefaultCharacterSetChanged, | 136 kOnDefaultCharacterSetChanged, |
161 kCharsetKey); | 137 kCharsetKey); |
162 | 138 |
163 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); | |
164 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); | |
165 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); | |
166 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); | |
167 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); | |
168 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); | |
169 RegisterFontFamilyMapObserver(®istrar_, | 139 RegisterFontFamilyMapObserver(®istrar_, |
170 prefs::kWebKitStandardFontFamilyMap, this); | 140 prefs::kWebKitStandardFontFamilyMap, this); |
171 RegisterFontFamilyMapObserver(®istrar_, | 141 RegisterFontFamilyMapObserver(®istrar_, |
172 prefs::kWebKitSerifFontFamilyMap, this); | 142 prefs::kWebKitSerifFontFamilyMap, this); |
173 RegisterFontFamilyMapObserver(®istrar_, | 143 RegisterFontFamilyMapObserver(®istrar_, |
174 prefs::kWebKitSansSerifFontFamilyMap, this); | 144 prefs::kWebKitSansSerifFontFamilyMap, this); |
175 RegisterFontFamilyMapObserver(®istrar_, | 145 RegisterFontFamilyMapObserver(®istrar_, |
176 prefs::kWebKitFixedFontFamilyMap, this); | 146 prefs::kWebKitFixedFontFamilyMap, this); |
177 RegisterFontFamilyMapObserver(®istrar_, | 147 RegisterFontFamilyMapObserver(®istrar_, |
178 prefs::kWebKitCursiveFontFamilyMap, this); | 148 prefs::kWebKitCursiveFontFamilyMap, this); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 NOTREACHED(); | 207 NOTREACHED(); |
238 return; | 208 return; |
239 } | 209 } |
240 font_name = MaybeGetLocalizedFontName(font_name); | 210 font_name = MaybeGetLocalizedFontName(font_name); |
241 | 211 |
242 ListValue args; | 212 ListValue args; |
243 DictionaryValue* dict = new DictionaryValue(); | 213 DictionaryValue* dict = new DictionaryValue(); |
244 args.Append(dict); | 214 args.Append(dict); |
245 dict->SetString(kFontNameKey, font_name); | 215 dict->SetString(kFontNameKey, font_name); |
246 dict->SetString(kGenericFamilyKey, generic_family); | 216 dict->SetString(kGenericFamilyKey, generic_family); |
247 if (!script.empty()) | 217 dict->SetString(kScriptKey, script); |
248 dict->SetString(kScriptKey, script); | |
249 | 218 |
250 extension_preference_helpers::DispatchEventToExtensions( | 219 extension_preference_helpers::DispatchEventToExtensions( |
251 profile_, | 220 profile_, |
252 kOnFontChanged, | 221 kOnFontChanged, |
253 &args, | 222 &args, |
254 ExtensionAPIPermission::kExperimental, | 223 ExtensionAPIPermission::kExperimental, |
255 incognito, | 224 incognito, |
256 pref_name); | 225 pref_name); |
257 } | 226 } |
258 | 227 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 | 312 |
344 DictionaryValue* details = NULL; | 313 DictionaryValue* details = NULL; |
345 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 314 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
346 | 315 |
347 std::string pref_path; | 316 std::string pref_path; |
348 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); | 317 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); |
349 | 318 |
350 std::string font_name; | 319 std::string font_name; |
351 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); | 320 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); |
352 | 321 |
353 // Ensure |pref_path| really is for a registered per-script font pref. | 322 // Ensure |pref_path| really is for a registered font pref. |
354 EXTENSION_FUNCTION_VALIDATE( | 323 EXTENSION_FUNCTION_VALIDATE( |
355 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 324 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
356 | 325 |
357 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 326 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
358 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), | 327 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), |
359 kExtensionPrefsScopeRegular, | 328 kExtensionPrefsScopeRegular, |
360 Value::CreateStringValue(font_name)); | 329 Value::CreateStringValue(font_name)); |
361 return true; | 330 return true; |
362 } | 331 } |
363 | 332 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 return kCharsetKey; | 499 return kCharsetKey; |
531 } | 500 } |
532 | 501 |
533 const char* SetDefaultCharacterSetFunction::GetPrefName() { | 502 const char* SetDefaultCharacterSetFunction::GetPrefName() { |
534 return prefs::kGlobalDefaultCharset; | 503 return prefs::kGlobalDefaultCharset; |
535 } | 504 } |
536 | 505 |
537 const char* SetDefaultCharacterSetFunction::GetKey() { | 506 const char* SetDefaultCharacterSetFunction::GetKey() { |
538 return kCharsetKey; | 507 return kCharsetKey; |
539 } | 508 } |
OLD | NEW |