| 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 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 87 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
| 88 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 88 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
| 89 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 89 registrar->Add(base::StringPrintf("%s.%s", map_name, script), callback); |
| 90 registrar->Add(pref_name.c_str(), callback); | |
| 91 } | 90 } |
| 92 } | 91 } |
| 93 | 92 |
| 94 } // namespace | 93 } // namespace |
| 95 | 94 |
| 96 FontSettingsEventRouter::FontSettingsEventRouter( | 95 FontSettingsEventRouter::FontSettingsEventRouter( |
| 97 Profile* profile) : profile_(profile) { | 96 Profile* profile) : profile_(profile) { |
| 98 registrar_.Init(profile_->GetPrefs()); | 97 registrar_.Init(profile_->GetPrefs()); |
| 99 | 98 |
| 100 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, | 99 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 148 } |
| 150 | 149 |
| 151 NOTREACHED(); | 150 NOTREACHED(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void FontSettingsEventRouter::OnFontNamePrefChanged( | 153 void FontSettingsEventRouter::OnFontNamePrefChanged( |
| 155 const std::string& pref_name, | 154 const std::string& pref_name, |
| 156 const std::string& generic_family, | 155 const std::string& generic_family, |
| 157 const std::string& script) { | 156 const std::string& script) { |
| 158 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 157 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
| 159 pref_name.c_str()); | 158 pref_name); |
| 160 CHECK(pref); | 159 CHECK(pref); |
| 161 | 160 |
| 162 std::string font_name; | 161 std::string font_name; |
| 163 if (!pref->GetValue()->GetAsString(&font_name)) { | 162 if (!pref->GetValue()->GetAsString(&font_name)) { |
| 164 NOTREACHED(); | 163 NOTREACHED(); |
| 165 return; | 164 return; |
| 166 } | 165 } |
| 167 font_name = MaybeGetLocalizedFontName(font_name); | 166 font_name = MaybeGetLocalizedFontName(font_name); |
| 168 | 167 |
| 169 base::ListValue args; | 168 base::ListValue args; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 180 APIPermission::kFontSettings, | 179 APIPermission::kFontSettings, |
| 181 false, | 180 false, |
| 182 pref_name); | 181 pref_name); |
| 183 } | 182 } |
| 184 | 183 |
| 185 void FontSettingsEventRouter::OnFontPrefChanged( | 184 void FontSettingsEventRouter::OnFontPrefChanged( |
| 186 const std::string& event_name, | 185 const std::string& event_name, |
| 187 const std::string& key, | 186 const std::string& key, |
| 188 const std::string& pref_name) { | 187 const std::string& pref_name) { |
| 189 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 188 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
| 190 pref_name.c_str()); | 189 pref_name); |
| 191 CHECK(pref); | 190 CHECK(pref); |
| 192 | 191 |
| 193 base::ListValue args; | 192 base::ListValue args; |
| 194 base::DictionaryValue* dict = new base::DictionaryValue(); | 193 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 195 args.Append(dict); | 194 args.Append(dict); |
| 196 dict->Set(key, pref->GetValue()->DeepCopy()); | 195 dict->Set(key, pref->GetValue()->DeepCopy()); |
| 197 | 196 |
| 198 extensions::preference_helpers::DispatchEventToExtensions( | 197 extensions::preference_helpers::DispatchEventToExtensions( |
| 199 profile_, | 198 profile_, |
| 200 event_name, | 199 event_name, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 228 | 227 |
| 229 scoped_ptr<fonts::ClearFont::Params> params( | 228 scoped_ptr<fonts::ClearFont::Params> params( |
| 230 fonts::ClearFont::Params::Create(*args_)); | 229 fonts::ClearFont::Params::Create(*args_)); |
| 231 EXTENSION_FUNCTION_VALIDATE(params.get()); | 230 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 232 | 231 |
| 233 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 232 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 234 params->details.script); | 233 params->details.script); |
| 235 | 234 |
| 236 // Ensure |pref_path| really is for a registered per-script font pref. | 235 // Ensure |pref_path| really is for a registered per-script font pref. |
| 237 EXTENSION_FUNCTION_VALIDATE( | 236 EXTENSION_FUNCTION_VALIDATE( |
| 238 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); | 237 GetProfile()->GetPrefs()->FindPreference(pref_path)); |
| 239 | 238 |
| 240 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( | 239 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( |
| 241 extension_id(), pref_path.c_str(), kExtensionPrefsScopeRegular); | 240 extension_id(), pref_path, kExtensionPrefsScopeRegular); |
| 242 return true; | 241 return true; |
| 243 } | 242 } |
| 244 | 243 |
| 245 bool FontSettingsGetFontFunction::RunSync() { | 244 bool FontSettingsGetFontFunction::RunSync() { |
| 246 scoped_ptr<fonts::GetFont::Params> params( | 245 scoped_ptr<fonts::GetFont::Params> params( |
| 247 fonts::GetFont::Params::Create(*args_)); | 246 fonts::GetFont::Params::Create(*args_)); |
| 248 EXTENSION_FUNCTION_VALIDATE(params.get()); | 247 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 249 | 248 |
| 250 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 249 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 251 params->details.script); | 250 params->details.script); |
| 252 | 251 |
| 253 PrefService* prefs = GetProfile()->GetPrefs(); | 252 PrefService* prefs = GetProfile()->GetPrefs(); |
| 254 const PrefService::Preference* pref = | 253 const PrefService::Preference* pref = |
| 255 prefs->FindPreference(pref_path.c_str()); | 254 prefs->FindPreference(pref_path); |
| 256 | 255 |
| 257 std::string font_name; | 256 std::string font_name; |
| 258 EXTENSION_FUNCTION_VALIDATE( | 257 EXTENSION_FUNCTION_VALIDATE( |
| 259 pref && pref->GetValue()->GetAsString(&font_name)); | 258 pref && pref->GetValue()->GetAsString(&font_name)); |
| 260 font_name = MaybeGetLocalizedFontName(font_name); | 259 font_name = MaybeGetLocalizedFontName(font_name); |
| 261 | 260 |
| 262 // We don't support incognito-specific font prefs, so don't consider them when | 261 // We don't support incognito-specific font prefs, so don't consider them when |
| 263 // getting level of control. | 262 // getting level of control. |
| 264 const bool kIncognito = false; | 263 const bool kIncognito = false; |
| 265 std::string level_of_control = | 264 std::string level_of_control = |
| (...skipping 15 matching lines...) Expand all Loading... |
| 281 | 280 |
| 282 scoped_ptr<fonts::SetFont::Params> params( | 281 scoped_ptr<fonts::SetFont::Params> params( |
| 283 fonts::SetFont::Params::Create(*args_)); | 282 fonts::SetFont::Params::Create(*args_)); |
| 284 EXTENSION_FUNCTION_VALIDATE(params.get()); | 283 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 285 | 284 |
| 286 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 285 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
| 287 params->details.script); | 286 params->details.script); |
| 288 | 287 |
| 289 // Ensure |pref_path| really is for a registered font pref. | 288 // Ensure |pref_path| really is for a registered font pref. |
| 290 EXTENSION_FUNCTION_VALIDATE( | 289 EXTENSION_FUNCTION_VALIDATE( |
| 291 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); | 290 GetProfile()->GetPrefs()->FindPreference(pref_path)); |
| 292 | 291 |
| 293 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( | 292 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( |
| 294 extension_id(), | 293 extension_id(), |
| 295 pref_path.c_str(), | 294 pref_path, |
| 296 kExtensionPrefsScopeRegular, | 295 kExtensionPrefsScopeRegular, |
| 297 new base::StringValue(params->details.font_id)); | 296 new base::StringValue(params->details.font_id)); |
| 298 return true; | 297 return true; |
| 299 } | 298 } |
| 300 | 299 |
| 301 bool FontSettingsGetFontListFunction::RunAsync() { | 300 bool FontSettingsGetFontListFunction::RunAsync() { |
| 302 content::GetFontListAsync( | 301 content::GetFontListAsync( |
| 303 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); | 302 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); |
| 304 return true; | 303 return true; |
| 305 } | 304 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 | 447 |
| 449 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { | 448 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { |
| 450 return prefs::kWebKitMinimumFontSize; | 449 return prefs::kWebKitMinimumFontSize; |
| 451 } | 450 } |
| 452 | 451 |
| 453 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { | 452 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { |
| 454 return kPixelSizeKey; | 453 return kPixelSizeKey; |
| 455 } | 454 } |
| 456 | 455 |
| 457 } // namespace extensions | 456 } // namespace extensions |
| OLD | NEW |