| 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 19 matching lines...) Expand all Loading... |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const char kGenericFamilyKey[] = "genericFamily"; | 32 const char kGenericFamilyKey[] = "genericFamily"; |
| 33 const char kFontNameKey[] = "fontName"; | 33 const char kFontNameKey[] = "fontName"; |
| 34 const char kLocalizedNameKey[] = "localizedName"; | 34 const char kLocalizedNameKey[] = "localizedName"; |
| 35 const char kPixelSizeKey[] = "pixelSize"; | 35 const char kPixelSizeKey[] = "pixelSize"; |
| 36 const char kScriptKey[] = "script"; | 36 const char kScriptKey[] = "script"; |
| 37 | 37 |
| 38 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; | 38 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; |
| 39 | 39 |
| 40 // Format for per-script font preference keys. | 40 // Format for font preference keys. |
| 41 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 41 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
| 42 const char kWebKitPerScriptFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 42 const char kWebKitFontPrefPrefix[] = "webkit.webprefs.fonts."; |
| 43 const char kWebKitPerScriptFontPrefPrefix[] = "webkit.webprefs.fonts."; | |
| 44 | |
| 45 // Format for global (non per-script) font preference keys. | |
| 46 // E.g., "webkit.webprefs.global.fixed_font_family" | |
| 47 // Note: there are two meanings of "global" here. The "Global" in the const name | |
| 48 // means "not per-script". The "global" in the key itself means "not per-tab" | |
| 49 // (per-profile). | |
| 50 const char kWebKitGlobalFontPrefFormat[] = | |
| 51 "webkit.webprefs.global.%s_font_family"; | |
| 52 const char kWebKitGlobalFontPrefPrefix[] = "webkit.webprefs.global."; | |
| 53 const char kWebKitGlobalFontPrefSuffix[] = "_font_family"; | |
| 54 | 43 |
| 55 // Gets the font name preference path from |details| which contains key | 44 // Gets the font name preference path from |details| which contains key |
| 56 // |kGenericFamilyKey| and optionally |kScriptKey|. | 45 // |kGenericFamilyKey| and optionally |kScriptKey|. |
| 57 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { | 46 bool GetFontNamePrefPath(DictionaryValue* details, std::string* pref_path) { |
| 58 std::string generic_family; | 47 std::string generic_family; |
| 59 if (!details->GetString(kGenericFamilyKey, &generic_family)) | 48 if (!details->GetString(kGenericFamilyKey, &generic_family)) |
| 60 return false; | 49 return false; |
| 61 | 50 |
| 62 if (details->HasKey(kScriptKey)) { | 51 std::string script; |
| 63 std::string script; | 52 if (!details->HasKey(kScriptKey)) |
| 64 if (!details->GetString(kScriptKey, &script)) | 53 script = prefs::kWebKitCommonScript; |
| 65 return false; | 54 else if (!details->GetString(kScriptKey, &script)) |
| 66 *pref_path = StringPrintf(kWebKitPerScriptFontPrefFormat, | 55 return false; |
| 67 generic_family.c_str(), | 56 *pref_path = StringPrintf(kWebKitFontPrefFormat, |
| 68 script.c_str()); | 57 generic_family.c_str(), |
| 69 } else { | 58 script.c_str()); |
| 70 *pref_path = StringPrintf(kWebKitGlobalFontPrefFormat, | |
| 71 generic_family.c_str()); | |
| 72 } | |
| 73 | |
| 74 return true; | 59 return true; |
| 75 } | 60 } |
| 76 | 61 |
| 77 // Extracts the generic family and script from font name pref path |pref_path|. | 62 // Extracts the generic family and script from font name pref path |pref_path|. |
| 78 bool ParseFontNamePrefPath(std::string pref_path, | 63 bool ParseFontNamePrefPath(std::string pref_path, |
| 79 std::string* generic_family, | 64 std::string* generic_family, |
| 80 std::string* script) { | 65 std::string* script) { |
| 81 if (StartsWithASCII(pref_path, kWebKitPerScriptFontPrefPrefix, true)) { | 66 if (!StartsWithASCII(pref_path, kWebKitFontPrefPrefix, true)) |
| 82 size_t start = strlen(kWebKitPerScriptFontPrefPrefix); | 67 return false; |
| 83 size_t pos = pref_path.find('.', start); | 68 |
| 84 if (pos == std::string::npos || pos + 1 == pref_path.length()) | 69 size_t start = strlen(kWebKitFontPrefPrefix); |
| 85 return false; | 70 size_t pos = pref_path.find('.', start); |
| 86 *generic_family = pref_path.substr(start, pos - start); | 71 if (pos == std::string::npos || pos + 1 == pref_path.length()) |
| 87 *script = pref_path.substr(pos + 1); | 72 return false; |
| 88 return true; | 73 *generic_family = pref_path.substr(start, pos - start); |
| 89 } else if (StartsWithASCII(pref_path, kWebKitGlobalFontPrefPrefix, true) && | 74 *script = pref_path.substr(pos + 1); |
| 90 EndsWith(pref_path, kWebKitGlobalFontPrefSuffix, true)) { | 75 return true; |
| 91 size_t start = strlen(kWebKitGlobalFontPrefPrefix); | |
| 92 size_t pos = pref_path.find('_', start); | |
| 93 if (pos == std::string::npos || pos + 1 == pref_path.length()) | |
| 94 return false; | |
| 95 *generic_family = pref_path.substr(start, pos - start); | |
| 96 *script = ""; | |
| 97 return true; | |
| 98 } | |
| 99 return false; | |
| 100 } | 76 } |
| 101 | 77 |
| 102 // Returns the localized name of a font so that it can be matched within the | 78 // Returns the localized name of a font so that it can be matched within the |
| 103 // list of system fonts. On Windows, the list of system fonts has names only | 79 // list of system fonts. On Windows, the list of system fonts has names only |
| 104 // for the system locale, but the pref value may be in the English name. | 80 // for the system locale, but the pref value may be in the English name. |
| 105 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 81 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
| 106 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
| 107 if (!font_name.empty()) { | 83 if (!font_name.empty()) { |
| 108 gfx::Font font(font_name, 12); // dummy font size | 84 gfx::Font font(font_name, 12); // dummy font size |
| 109 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 85 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 126 | 102 |
| 127 } // namespace | 103 } // namespace |
| 128 | 104 |
| 129 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( | 105 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( |
| 130 Profile* profile) : profile_(profile) {} | 106 Profile* profile) : profile_(profile) {} |
| 131 | 107 |
| 132 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} | 108 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} |
| 133 | 109 |
| 134 void ExtensionFontSettingsEventRouter::Init() { | 110 void ExtensionFontSettingsEventRouter::Init() { |
| 135 registrar_.Init(profile_->GetPrefs()); | 111 registrar_.Init(profile_->GetPrefs()); |
| 136 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); | |
| 137 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); | |
| 138 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); | |
| 139 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); | |
| 140 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); | |
| 141 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); | |
| 142 RegisterFontFamilyMapObserver(®istrar_, | 112 RegisterFontFamilyMapObserver(®istrar_, |
| 143 prefs::kWebKitStandardFontFamilyMap, this); | 113 prefs::kWebKitStandardFontFamilyMap, this); |
| 144 RegisterFontFamilyMapObserver(®istrar_, | 114 RegisterFontFamilyMapObserver(®istrar_, |
| 145 prefs::kWebKitSerifFontFamilyMap, this); | 115 prefs::kWebKitSerifFontFamilyMap, this); |
| 146 RegisterFontFamilyMapObserver(®istrar_, | 116 RegisterFontFamilyMapObserver(®istrar_, |
| 147 prefs::kWebKitSansSerifFontFamilyMap, this); | 117 prefs::kWebKitSansSerifFontFamilyMap, this); |
| 148 RegisterFontFamilyMapObserver(®istrar_, | 118 RegisterFontFamilyMapObserver(®istrar_, |
| 149 prefs::kWebKitFixedFontFamilyMap, this); | 119 prefs::kWebKitFixedFontFamilyMap, this); |
| 150 RegisterFontFamilyMapObserver(®istrar_, | 120 RegisterFontFamilyMapObserver(®istrar_, |
| 151 prefs::kWebKitCursiveFontFamilyMap, this); | 121 prefs::kWebKitCursiveFontFamilyMap, this); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 NOTREACHED(); | 154 NOTREACHED(); |
| 185 return; | 155 return; |
| 186 } | 156 } |
| 187 font_name = MaybeGetLocalizedFontName(font_name); | 157 font_name = MaybeGetLocalizedFontName(font_name); |
| 188 | 158 |
| 189 ListValue args; | 159 ListValue args; |
| 190 DictionaryValue* dict = new DictionaryValue(); | 160 DictionaryValue* dict = new DictionaryValue(); |
| 191 args.Append(dict); | 161 args.Append(dict); |
| 192 dict->SetString(kFontNameKey, font_name); | 162 dict->SetString(kFontNameKey, font_name); |
| 193 dict->SetString(kGenericFamilyKey, generic_family); | 163 dict->SetString(kGenericFamilyKey, generic_family); |
| 194 if (!script.empty()) | 164 dict->SetString(kScriptKey, script); |
| 195 dict->SetString(kScriptKey, script); | |
| 196 | 165 |
| 197 extension_preference_helpers::DispatchEventToExtensions( | 166 extension_preference_helpers::DispatchEventToExtensions( |
| 198 profile_, | 167 profile_, |
| 199 kOnFontChanged, | 168 kOnFontChanged, |
| 200 &args, | 169 &args, |
| 201 ExtensionAPIPermission::kExperimental, | 170 ExtensionAPIPermission::kExperimental, |
| 202 incognito, | 171 incognito, |
| 203 *pref_key); | 172 *pref_key); |
| 204 } | 173 } |
| 205 | 174 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 228 bool SetFontFunction::RunImpl() { | 197 bool SetFontFunction::RunImpl() { |
| 229 DictionaryValue* details = NULL; | 198 DictionaryValue* details = NULL; |
| 230 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 199 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 231 | 200 |
| 232 std::string pref_path; | 201 std::string pref_path; |
| 233 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); | 202 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); |
| 234 | 203 |
| 235 std::string font_name; | 204 std::string font_name; |
| 236 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); | 205 EXTENSION_FUNCTION_VALIDATE(details->GetString(kFontNameKey, &font_name)); |
| 237 | 206 |
| 238 // Ensure |pref_path| really is for a registered per-script font pref. | 207 // Ensure |pref_path| really is for a registered font pref. |
| 239 EXTENSION_FUNCTION_VALIDATE( | 208 EXTENSION_FUNCTION_VALIDATE( |
| 240 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 209 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
| 241 | 210 |
| 242 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 211 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 243 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), | 212 prefs->SetExtensionControlledPref(extension_id(), pref_path.c_str(), |
| 244 kExtensionPrefsScopeRegular, | 213 kExtensionPrefsScopeRegular, |
| 245 Value::CreateStringValue(font_name)); | 214 Value::CreateStringValue(font_name)); |
| 246 return true; | 215 return true; |
| 247 } | 216 } |
| 248 | 217 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 return prefs::kWebKitGlobalDefaultFixedFontSize; | 298 return prefs::kWebKitGlobalDefaultFixedFontSize; |
| 330 } | 299 } |
| 331 | 300 |
| 332 const char* GetMinimumFontSizeFunction::GetPrefName() { | 301 const char* GetMinimumFontSizeFunction::GetPrefName() { |
| 333 return prefs::kWebKitGlobalMinimumFontSize; | 302 return prefs::kWebKitGlobalMinimumFontSize; |
| 334 } | 303 } |
| 335 | 304 |
| 336 const char* SetMinimumFontSizeFunction::GetPrefName() { | 305 const char* SetMinimumFontSizeFunction::GetPrefName() { |
| 337 return prefs::kWebKitGlobalMinimumFontSize; | 306 return prefs::kWebKitGlobalMinimumFontSize; |
| 338 } | 307 } |
| OLD | NEW |