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 // Font Settings Extension API implementation. | 5 // Font Settings Extension API implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/font_settings/font_settings_api.h" | 7 #include "chrome/browser/extensions/api/font_settings/font_settings_api.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 | 54 |
| 55 // Gets the font name preference path for |generic_family| and |script|. If | 55 // Gets the font name preference path for |generic_family| and |script|. If |
| 56 // |script| is NULL, uses prefs::kWebKitCommonScript. | 56 // |script| is NULL, uses prefs::kWebKitCommonScript. |
| 57 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, | 57 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, |
| 58 fonts::ScriptCode script_enum) { | 58 fonts::ScriptCode script_enum) { |
| 59 std::string script = fonts::ToString(script_enum); | 59 std::string script = fonts::ToString(script_enum); |
| 60 if (script.empty()) | 60 if (script.empty()) |
| 61 script = prefs::kWebKitCommonScript; | 61 script = prefs::kWebKitCommonScript; |
| 62 std::string generic_family = fonts::ToString(generic_family_enum); | 62 std::string generic_family = fonts::ToString(generic_family_enum); |
| 63 return base::StringPrintf(kWebKitFontPrefFormat, | 63 return base::StringPrintf(kWebKitFontPrefFormat, |
| 64 generic_family.c_str(), | 64 generic_family, |
| 65 script.c_str()); | 65 script); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Returns the localized name of a font so that it can be matched within the | 68 // Returns the localized name of a font so that it can be matched within the |
| 69 // list of system fonts. On Windows, the list of system fonts has names only | 69 // list of system fonts. On Windows, the list of system fonts has names only |
| 70 // for the system locale, but the pref value may be in the English name. | 70 // for the system locale, but the pref value may be in the English name. |
| 71 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 71 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
| 72 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
| 73 if (!font_name.empty()) { | 73 if (!font_name.empty()) { |
| 74 gfx::Font font(font_name, 12); // dummy font size | 74 gfx::Font font(font_name, 12); // dummy font size |
| 75 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 75 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
| 76 GetLocalizedFontName(); | 76 GetLocalizedFontName(); |
| 77 } | 77 } |
| 78 #endif | 78 #endif |
| 79 return font_name; | 79 return font_name; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Registers |obs| to observe per-script font prefs under the path |map_name|. | 82 // Registers |obs| to observe per-script font prefs under the path |map_name|. |
| 83 void RegisterFontFamilyMapObserver( | 83 void RegisterFontFamilyMapObserver( |
| 84 PrefChangeRegistrar* registrar, | 84 PrefChangeRegistrar* registrar, |
| 85 const char* map_name, | 85 const char* map_name, |
| 86 const PrefChangeRegistrar::NamedChangeCallback& callback) { | 86 const PrefChangeRegistrar::NamedChangeCallback& callback) { |
| 87 std::string pref_name; | |
| 88 pref_name.reserve(512); // Reduces number of grows. | |
| 87 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 89 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
| 88 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 90 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
| 89 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 91 pref_name.assign(map_name); |
|
Nico
2014/12/03 20:34:34
doesn't look restored?
| |
| 90 registrar->Add(pref_name.c_str(), callback); | 92 pref_name.append(1, '.'); |
| 93 pref_name.append(script); | |
| 94 registrar->Add(pref_name, callback); | |
| 91 } | 95 } |
| 92 } | 96 } |
| 93 | 97 |
| 94 } // namespace | 98 } // namespace |
| 95 | 99 |
| 96 FontSettingsEventRouter::FontSettingsEventRouter( | 100 FontSettingsEventRouter::FontSettingsEventRouter( |
| 97 Profile* profile) : profile_(profile) { | 101 Profile* profile) : profile_(profile) { |
| 98 registrar_.Init(profile_->GetPrefs()); | 102 registrar_.Init(profile_->GetPrefs()); |
| 99 | 103 |
| 100 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, | 104 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 } | 153 } |
| 150 | 154 |
| 151 NOTREACHED(); | 155 NOTREACHED(); |
| 152 } | 156 } |
| 153 | 157 |
| 154 void FontSettingsEventRouter::OnFontNamePrefChanged( | 158 void FontSettingsEventRouter::OnFontNamePrefChanged( |
| 155 const std::string& pref_name, | 159 const std::string& pref_name, |
| 156 const std::string& generic_family, | 160 const std::string& generic_family, |
| 157 const std::string& script) { | 161 const std::string& script) { |
| 158 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 162 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
| 159 pref_name.c_str()); | 163 pref_name); |
| 160 CHECK(pref); | 164 CHECK(pref); |
| 161 | 165 |
| 162 std::string font_name; | 166 std::string font_name; |
| 163 if (!pref->GetValue()->GetAsString(&font_name)) { | 167 if (!pref->GetValue()->GetAsString(&font_name)) { |
| 164 NOTREACHED(); | 168 NOTREACHED(); |
| 165 return; | 169 return; |
| 166 } | 170 } |
| 167 font_name = MaybeGetLocalizedFontName(font_name); | 171 font_name = MaybeGetLocalizedFontName(font_name); |
| 168 | 172 |
| 169 base::ListValue args; | 173 base::ListValue args; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 180 APIPermission::kFontSettings, | 184 APIPermission::kFontSettings, |
| 181 false, | 185 false, |
| 182 pref_name); | 186 pref_name); |
| 183 } | 187 } |
| 184 | 188 |
| 185 void FontSettingsEventRouter::OnFontPrefChanged( | 189 void FontSettingsEventRouter::OnFontPrefChanged( |
| 186 const std::string& event_name, | 190 const std::string& event_name, |
| 187 const std::string& key, | 191 const std::string& key, |
| 188 const std::string& pref_name) { | 192 const std::string& pref_name) { |
| 189 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 193 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
| 190 pref_name.c_str()); | 194 pref_name); |
| 191 CHECK(pref); | 195 CHECK(pref); |
| 192 | 196 |
| 193 base::ListValue args; | 197 base::ListValue args; |
| 194 base::DictionaryValue* dict = new base::DictionaryValue(); | 198 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 195 args.Append(dict); | 199 args.Append(dict); |
| 196 dict->Set(key, pref->GetValue()->DeepCopy()); | 200 dict->Set(key, pref->GetValue()->DeepCopy()); |
| 197 | 201 |
| 198 extensions::preference_helpers::DispatchEventToExtensions( | 202 extensions::preference_helpers::DispatchEventToExtensions( |
| 199 profile_, | 203 profile_, |
| 200 event_name, | 204 event_name, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 228 | 232 |
| 229 scoped_ptr<fonts::ClearFont::Params> params( | 233 scoped_ptr<fonts::ClearFont::Params> params( |
| 230 fonts::ClearFont::Params::Create(*args_)); | 234 fonts::ClearFont::Params::Create(*args_)); |
| 231 EXTENSION_FUNCTION_VALIDATE(params.get()); | 235 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 232 | 236 |
| 233 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 237 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 234 params->details.script); | 238 params->details.script); |
| 235 | 239 |
| 236 // Ensure |pref_path| really is for a registered per-script font pref. | 240 // Ensure |pref_path| really is for a registered per-script font pref. |
| 237 EXTENSION_FUNCTION_VALIDATE( | 241 EXTENSION_FUNCTION_VALIDATE( |
| 238 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); | 242 GetProfile()->GetPrefs()->FindPreference(pref_path)); |
| 239 | 243 |
| 240 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( | 244 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( |
| 241 extension_id(), pref_path.c_str(), kExtensionPrefsScopeRegular); | 245 extension_id(), pref_path, kExtensionPrefsScopeRegular); |
| 242 return true; | 246 return true; |
| 243 } | 247 } |
| 244 | 248 |
| 245 bool FontSettingsGetFontFunction::RunSync() { | 249 bool FontSettingsGetFontFunction::RunSync() { |
| 246 scoped_ptr<fonts::GetFont::Params> params( | 250 scoped_ptr<fonts::GetFont::Params> params( |
| 247 fonts::GetFont::Params::Create(*args_)); | 251 fonts::GetFont::Params::Create(*args_)); |
| 248 EXTENSION_FUNCTION_VALIDATE(params.get()); | 252 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 249 | 253 |
| 250 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 254 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 251 params->details.script); | 255 params->details.script); |
| 252 | 256 |
| 253 PrefService* prefs = GetProfile()->GetPrefs(); | 257 PrefService* prefs = GetProfile()->GetPrefs(); |
| 254 const PrefService::Preference* pref = | 258 const PrefService::Preference* pref = |
| 255 prefs->FindPreference(pref_path.c_str()); | 259 prefs->FindPreference(pref_path); |
| 256 | 260 |
| 257 std::string font_name; | 261 std::string font_name; |
| 258 EXTENSION_FUNCTION_VALIDATE( | 262 EXTENSION_FUNCTION_VALIDATE( |
| 259 pref && pref->GetValue()->GetAsString(&font_name)); | 263 pref && pref->GetValue()->GetAsString(&font_name)); |
| 260 font_name = MaybeGetLocalizedFontName(font_name); | 264 font_name = MaybeGetLocalizedFontName(font_name); |
| 261 | 265 |
| 262 // We don't support incognito-specific font prefs, so don't consider them when | 266 // We don't support incognito-specific font prefs, so don't consider them when |
| 263 // getting level of control. | 267 // getting level of control. |
| 264 const bool kIncognito = false; | 268 const bool kIncognito = false; |
| 265 std::string level_of_control = | 269 std::string level_of_control = |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 281 | 285 |
| 282 scoped_ptr<fonts::SetFont::Params> params( | 286 scoped_ptr<fonts::SetFont::Params> params( |
| 283 fonts::SetFont::Params::Create(*args_)); | 287 fonts::SetFont::Params::Create(*args_)); |
| 284 EXTENSION_FUNCTION_VALIDATE(params.get()); | 288 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 285 | 289 |
| 286 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 290 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 287 params->details.script); | 291 params->details.script); |
| 288 | 292 |
| 289 // Ensure |pref_path| really is for a registered font pref. | 293 // Ensure |pref_path| really is for a registered font pref. |
| 290 EXTENSION_FUNCTION_VALIDATE( | 294 EXTENSION_FUNCTION_VALIDATE( |
| 291 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); | 295 GetProfile()->GetPrefs()->FindPreference(pref_path)); |
| 292 | 296 |
| 293 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( | 297 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( |
| 294 extension_id(), | 298 extension_id(), |
| 295 pref_path.c_str(), | 299 pref_path, |
| 296 kExtensionPrefsScopeRegular, | 300 kExtensionPrefsScopeRegular, |
| 297 new base::StringValue(params->details.font_id)); | 301 new base::StringValue(params->details.font_id)); |
| 298 return true; | 302 return true; |
| 299 } | 303 } |
| 300 | 304 |
| 301 bool FontSettingsGetFontListFunction::RunAsync() { | 305 bool FontSettingsGetFontListFunction::RunAsync() { |
| 302 content::GetFontListAsync( | 306 content::GetFontListAsync( |
| 303 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); | 307 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); |
| 304 return true; | 308 return true; |
| 305 } | 309 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 | 452 |
| 449 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { | 453 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { |
| 450 return prefs::kWebKitMinimumFontSize; | 454 return prefs::kWebKitMinimumFontSize; |
| 451 } | 455 } |
| 452 | 456 |
| 453 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { | 457 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { |
| 454 return kPixelSizeKey; | 458 return kPixelSizeKey; |
| 455 } | 459 } |
| 456 | 460 |
| 457 } // namespace extensions | 461 } // namespace extensions |
| OLD | NEW |