Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 const char kOnDefaultCharacterSetChanged[] = | 39 const char kOnDefaultCharacterSetChanged[] = |
| 40 "experimental.fontSettings.onDefaultCharacterSetChanged"; | 40 "experimental.fontSettings.onDefaultCharacterSetChanged"; |
| 41 const char kOnDefaultFixedFontSizeChanged[] = | 41 const char kOnDefaultFixedFontSizeChanged[] = |
| 42 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; | 42 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; |
| 43 const char kOnDefaultFontSizeChanged[] = | 43 const char kOnDefaultFontSizeChanged[] = |
| 44 "experimental.fontSettings.onDefaultFontSizeChanged"; | 44 "experimental.fontSettings.onDefaultFontSizeChanged"; |
| 45 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; | 45 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; |
| 46 const char kOnMinimumFontSizeChanged[] = | 46 const char kOnMinimumFontSizeChanged[] = |
| 47 "experimental.fontSettings.onMinimumFontSizeChanged"; | 47 "experimental.fontSettings.onMinimumFontSizeChanged"; |
| 48 | 48 |
| 49 // Format for per-script font preference keys. | 49 // Format for font preference keys. |
| 50 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 50 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
| 51 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 51 const char kWebKitFontPrefPrefix[] = "webkit.webprefs.fonts."; |
| 52 const char kWebKitPerScriptFontPrefPrefix[] = "webkit.webprefs.fonts."; | |
| 53 | |
| 54 // Format for global (non per-script) font preference keys. | |
| 55 // E.g., "webkit.webprefs.global.fixed_font_family" | |
| 56 // Note: there are two meanings of "global" here. The "Global" in the const name | |
| 57 // means "not per-script". The "global" in the key itself means "not per-tab" | |
| 58 // (per-profile). | |
| 59 const char kWebKitGlobalFontPrefFormat[] = | |
| 60 "webkit.webprefs.global.%s_font_family"; | |
| 61 const char kWebKitGlobalFontPrefPrefix[] = "webkit.webprefs.global."; | |
| 62 const char kWebKitGlobalFontPrefSuffix[] = "_font_family"; | |
| 63 | 52 |
| 64 // Gets the font name preference path from |details| which contains key | 53 // Gets the font name preference path from |details| which contains key |
| 65 // |kGenericFamilyKey| and optionally |kScriptKey|. | 54 // |kGenericFamilyKey| and optionally |kScriptKey|. |
| 66 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { | 55 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { |
| 67 std::string generic_family; | 56 std::string generic_family; |
| 68 if (!details->GetString(kGenericFamilyKey, &generic_family)) | 57 if (!details->GetString(kGenericFamilyKey, &generic_family)) |
| 69 return false; | 58 return false; |
| 70 | 59 |
| 71 if (details->HasKey(kScriptKey)) { | 60 std::string script; |
| 72 std::string script; | 61 if (!details->HasKey(kScriptKey)) |
|
Matt Perry
2012/05/11 20:47:15
use braces when an else is present.
falken
2012/05/14 09:08:23
Always? The style guide says "if one part of an if
Peter Kasting
2012/05/14 18:18:32
Braces aren't needed here.
| |
| 73 if (!details->GetString(kScriptKey, &script)) | 62 script = prefs::kWebKitCommonScript; |
| 74 return false; | 63 else if (!details->GetString(kScriptKey, &script)) |
|
Matt Perry
2012/05/11 20:47:15
extra space before (
falken
2012/05/14 09:08:23
Done.
| |
| 75 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, | 64 return false; |
| 76 generic_family.c_str(), | 65 *pref_path = StringPrintf(kWebKitFontPrefFormat, |
| 77 script.c_str()); | 66 generic_family.c_str(), |
| 78 } else { | 67 script.c_str()); |
| 79 *pref_path = StringPrintf(kWebKitGlobalFontPrefFormat, | |
| 80 generic_family.c_str()); | |
| 81 } | |
| 82 | |
| 83 return true; | 68 return true; |
| 84 } | 69 } |
| 85 | 70 |
| 86 // Extracts the generic family and script from font name pref path |pref_path|. | 71 // Extracts the generic family and script from font name pref path |pref_path|. |
| 87 bool ParseFontNamePrefPath(std::string pref_path, | 72 bool ParseFontNamePrefPath(std::string pref_path, |
| 88 std::string* generic_family, | 73 std::string* generic_family, |
| 89 std::string* script) { | 74 std::string* script) { |
| 90 if (StartsWithASCII(pref_path, kWebKitPerScriptFontPrefPrefix, true)) { | 75 if (!StartsWithASCII(pref_path, kWebKitFontPrefPrefix, true)) |
| 91 size_t start = strlen(kWebKitPerScriptFontPrefPrefix); | 76 return false; |
| 92 size_t pos = pref_path.find('.', start); | 77 |
| 93 if (pos == std::string::npos || pos + 1 == pref_path.length()) | 78 size_t start = strlen(kWebKitFontPrefPrefix); |
| 94 return false; | 79 size_t pos = pref_path.find('.', start); |
| 95 *generic_family = pref_path.substr(start, pos - start); | 80 if (pos == std::string::npos || pos + 1 == pref_path.length()) |
| 96 *script = pref_path.substr(pos + 1); | 81 return false; |
| 97 return true; | 82 *generic_family = pref_path.substr(start, pos - start); |
| 98 } else if (StartsWithASCII(pref_path, kWebKitGlobalFontPrefPrefix, true) && | 83 *script = pref_path.substr(pos + 1); |
| 99 EndsWith(pref_path, kWebKitGlobalFontPrefSuffix, true)) { | 84 return true; |
| 100 size_t start = strlen(kWebKitGlobalFontPrefPrefix); | |
| 101 size_t pos = pref_path.find('_', start); | |
| 102 if (pos == std::string::npos || pos + 1 == pref_path.length()) | |
| 103 return false; | |
| 104 *generic_family = pref_path.substr(start, pos - start); | |
| 105 *script = ""; | |
| 106 return true; | |
| 107 } | |
| 108 return false; | |
| 109 } | 85 } |
| 110 | 86 |
| 111 // Returns the localized name of a font so that it can be matched within the | 87 // Returns the localized name of a font so that it can be matched within the |
| 112 // list of system fonts. On Windows, the list of system fonts has names only | 88 // list of system fonts. On Windows, the list of system fonts has names only |
| 113 // for the system locale, but the pref value may be in the English name. | 89 // for the system locale, but the pref value may be in the English name. |
| 114 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 90 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
| 115 #if defined(OS_WIN) | 91 #if defined(OS_WIN) |
| 116 if (!font_name.empty()) { | 92 if (!font_name.empty()) { |
| 117 gfx::Font font(font_name, 12); // dummy font size | 93 gfx::Font font(font_name, 12); // dummy font size |
| 118 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 94 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 149 AddPrefToObserve(prefs::kWebKitGlobalDefaultFontSize, | 125 AddPrefToObserve(prefs::kWebKitGlobalDefaultFontSize, |
| 150 kOnDefaultFontSizeChanged, | 126 kOnDefaultFontSizeChanged, |
| 151 kPixelSizeKey); | 127 kPixelSizeKey); |
| 152 AddPrefToObserve(prefs::kWebKitGlobalMinimumFontSize, | 128 AddPrefToObserve(prefs::kWebKitGlobalMinimumFontSize, |
| 153 kOnMinimumFontSizeChanged, | 129 kOnMinimumFontSizeChanged, |
| 154 kPixelSizeKey); | 130 kPixelSizeKey); |
| 155 AddPrefToObserve(prefs::kGlobalDefaultCharset, | 131 AddPrefToObserve(prefs::kGlobalDefaultCharset, |
| 156 kOnDefaultCharacterSetChanged, | 132 kOnDefaultCharacterSetChanged, |
| 157 kCharsetKey); | 133 kCharsetKey); |
| 158 | 134 |
| 159 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); | |
| 160 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); | |
| 161 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); | |
| 162 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); | |
| 163 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); | |
| 164 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); | |
| 165 RegisterFontFamilyMapObserver(®istrar_, | 135 RegisterFontFamilyMapObserver(®istrar_, |
| 166 prefs::kWebKitStandardFontFamilyMap, this); | 136 prefs::kWebKitStandardFontFamilyMap, this); |
| 167 RegisterFontFamilyMapObserver(®istrar_, | 137 RegisterFontFamilyMapObserver(®istrar_, |
| 168 prefs::kWebKitSerifFontFamilyMap, this); | 138 prefs::kWebKitSerifFontFamilyMap, this); |
| 169 RegisterFontFamilyMapObserver(®istrar_, | 139 RegisterFontFamilyMapObserver(®istrar_, |
| 170 prefs::kWebKitSansSerifFontFamilyMap, this); | 140 prefs::kWebKitSansSerifFontFamilyMap, this); |
| 171 RegisterFontFamilyMapObserver(®istrar_, | 141 RegisterFontFamilyMapObserver(®istrar_, |
| 172 prefs::kWebKitFixedFontFamilyMap, this); | 142 prefs::kWebKitFixedFontFamilyMap, this); |
| 173 RegisterFontFamilyMapObserver(®istrar_, | 143 RegisterFontFamilyMapObserver(®istrar_, |
| 174 prefs::kWebKitCursiveFontFamilyMap, this); | 144 prefs::kWebKitCursiveFontFamilyMap, this); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 NOTREACHED(); | 203 NOTREACHED(); |
| 234 return; | 204 return; |
| 235 } | 205 } |
| 236 font_name = MaybeGetLocalizedFontName(font_name); | 206 font_name = MaybeGetLocalizedFontName(font_name); |
| 237 | 207 |
| 238 ListValue args; | 208 ListValue args; |
| 239 DictionaryValue* dict = new DictionaryValue(); | 209 DictionaryValue* dict = new DictionaryValue(); |
| 240 args.Append(dict); | 210 args.Append(dict); |
| 241 dict->SetString(kFontNameKey, font_name); | 211 dict->SetString(kFontNameKey, font_name); |
| 242 dict->SetString(kGenericFamilyKey, generic_family); | 212 dict->SetString(kGenericFamilyKey, generic_family); |
| 243 if (!script.empty()) | 213 dict->SetString(kScriptKey, script); |
| 244 dict->SetString(kScriptKey, script); | |
| 245 | 214 |
| 246 extension_preference_helpers::DispatchEventToExtensions( | 215 extension_preference_helpers::DispatchEventToExtensions( |
| 247 profile_, | 216 profile_, |
| 248 kOnFontChanged, | 217 kOnFontChanged, |
| 249 &args, | 218 &args, |
| 250 ExtensionAPIPermission::kExperimental, | 219 ExtensionAPIPermission::kExperimental, |
| 251 incognito, | 220 incognito, |
| 252 pref_name); | 221 pref_name); |
| 253 } | 222 } |
| 254 | 223 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 bool SetFontFunction::RunImpl() { | 288 bool SetFontFunction::RunImpl() { |
| 320 DictionaryValue* details = NULL; | 289 DictionaryValue* details = NULL; |
| 321 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 290 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 322 | 291 |
| 323 std::string pref_path; | 292 std::string pref_path; |
| 324 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); | 293 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); |
| 325 | 294 |
| 326 std::string font_name; | 295 std::string font_name; |
| 327 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); | 296 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); |
| 328 | 297 |
| 329 // Ensure |pref_path| really is for a registered per-script font pref. | 298 // Ensure |pref_path| really is for a registered font pref. |
| 330 EXTENSION_FUNCTION_VALIDATE( | 299 EXTENSION_FUNCTION_VALIDATE( |
| 331 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 300 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
| 332 | 301 |
| 333 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 302 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 334 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), | 303 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), |
| 335 kExtensionPrefsScopeRegular, | 304 kExtensionPrefsScopeRegular, |
| 336 Value::CreateStringValue(font_name)); | 305 Value::CreateStringValue(font_name)); |
| 337 return true; | 306 return true; |
| 338 } | 307 } |
| 339 | 308 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 return kCharsetKey; | 454 return kCharsetKey; |
| 486 } | 455 } |
| 487 | 456 |
| 488 const char* SetDefaultCharacterSetFunction::GetPrefName() { | 457 const char* SetDefaultCharacterSetFunction::GetPrefName() { |
| 489 return prefs::kGlobalDefaultCharset; | 458 return prefs::kGlobalDefaultCharset; |
| 490 } | 459 } |
| 491 | 460 |
| 492 const char* SetDefaultCharacterSetFunction::GetKey() { | 461 const char* SetDefaultCharacterSetFunction::GetKey() { |
| 493 return kCharsetKey; | 462 return kCharsetKey; |
| 494 } | 463 } |
| OLD | NEW |