| 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 <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "chrome/browser/chrome_notification_types.h" | 24 #include "chrome/browser/chrome_notification_types.h" |
| 25 #include "chrome/browser/extensions/api/preference/preference_api.h" | 25 #include "chrome/browser/extensions/api/preference/preference_api.h" |
| 26 #include "chrome/browser/extensions/api/preference/preference_helpers.h" | 26 #include "chrome/browser/extensions/api/preference/preference_helpers.h" |
| 27 #include "chrome/browser/extensions/extension_service.h" | 27 #include "chrome/browser/extensions/extension_service.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/ui/webui/options/font_settings_utils.h" |
| 29 #include "chrome/common/extensions/api/font_settings.h" | 30 #include "chrome/common/extensions/api/font_settings.h" |
| 30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/common/pref_names_util.h" | 32 #include "chrome/common/pref_names_util.h" |
| 32 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 33 #include "content/public/browser/font_list_async.h" | 34 #include "content/public/browser/font_list_async.h" |
| 34 #include "content/public/browser/notification_details.h" | 35 #include "content/public/browser/notification_details.h" |
| 35 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
| 36 #include "extensions/browser/extension_system.h" | 37 #include "extensions/browser/extension_system.h" |
| 37 #include "extensions/common/error_utils.h" | 38 #include "extensions/common/error_utils.h" |
| 38 | 39 |
| 39 #if defined(OS_WIN) | |
| 40 #include "ui/gfx/font.h" | |
| 41 #include "ui/gfx/platform_font_win.h" | |
| 42 #endif | |
| 43 | |
| 44 namespace extensions { | 40 namespace extensions { |
| 45 | 41 |
| 46 namespace fonts = api::font_settings; | 42 namespace fonts = api::font_settings; |
| 43 using options::FontSettingsUtilities; |
| 47 | 44 |
| 48 namespace { | 45 namespace { |
| 49 | 46 |
| 50 const char kFontIdKey[] = "fontId"; | 47 const char kFontIdKey[] = "fontId"; |
| 51 const char kGenericFamilyKey[] = "genericFamily"; | 48 const char kGenericFamilyKey[] = "genericFamily"; |
| 52 const char kLevelOfControlKey[] = "levelOfControl"; | 49 const char kLevelOfControlKey[] = "levelOfControl"; |
| 53 const char kDisplayNameKey[] = "displayName"; | 50 const char kDisplayNameKey[] = "displayName"; |
| 54 const char kPixelSizeKey[] = "pixelSize"; | 51 const char kPixelSizeKey[] = "pixelSize"; |
| 55 const char kScriptKey[] = "script"; | 52 const char kScriptKey[] = "script"; |
| 56 | 53 |
| 57 const char kSetFromIncognitoError[] = | 54 const char kSetFromIncognitoError[] = |
| 58 "Can't modify regular settings from an incognito context."; | 55 "Can't modify regular settings from an incognito context."; |
| 59 | 56 |
| 60 // Format for font name preference paths. | 57 // Format for font name preference paths. |
| 61 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 58 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
| 62 | 59 |
| 63 // Gets the font name preference path for |generic_family| and |script|. If | 60 // Gets the font name preference path for |generic_family| and |script|. If |
| 64 // |script| is NULL, uses prefs::kWebKitCommonScript. | 61 // |script| is NULL, uses prefs::kWebKitCommonScript. |
| 65 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, | 62 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, |
| 66 fonts::ScriptCode script_enum) { | 63 fonts::ScriptCode script_enum) { |
| 67 std::string script = fonts::ToString(script_enum); | 64 std::string script = fonts::ToString(script_enum); |
| 68 if (script.empty()) | 65 if (script.empty()) |
| 69 script = prefs::kWebKitCommonScript; | 66 script = prefs::kWebKitCommonScript; |
| 70 std::string generic_family = fonts::ToString(generic_family_enum); | 67 std::string generic_family = fonts::ToString(generic_family_enum); |
| 71 return base::StringPrintf(kWebKitFontPrefFormat, | 68 return base::StringPrintf(kWebKitFontPrefFormat, |
| 72 generic_family.c_str(), | 69 generic_family.c_str(), |
| 73 script.c_str()); | 70 script.c_str()); |
| 74 } | 71 } |
| 75 | 72 |
| 76 // Returns the localized name of a font so that it can be matched within the | |
| 77 // list of system fonts. On Windows, the list of system fonts has names only | |
| 78 // for the system locale, but the pref value may be in the English name. | |
| 79 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | |
| 80 #if defined(OS_WIN) | |
| 81 if (!font_name.empty()) { | |
| 82 gfx::Font font(font_name, 12); // dummy font size | |
| 83 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | |
| 84 GetLocalizedFontName(); | |
| 85 } | |
| 86 #endif | |
| 87 return font_name; | |
| 88 } | |
| 89 | |
| 90 // Registers |obs| to observe per-script font prefs under the path |map_name|. | 73 // Registers |obs| to observe per-script font prefs under the path |map_name|. |
| 91 void RegisterFontFamilyMapObserver( | 74 void RegisterFontFamilyMapObserver( |
| 92 PrefChangeRegistrar* registrar, | 75 PrefChangeRegistrar* registrar, |
| 93 const char* map_name, | 76 const char* map_name, |
| 94 const PrefChangeRegistrar::NamedChangeCallback& callback) { | 77 const PrefChangeRegistrar::NamedChangeCallback& callback) { |
| 95 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 78 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
| 96 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 79 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
| 97 registrar->Add(base::StringPrintf("%s.%s", map_name, script), callback); | 80 registrar->Add(base::StringPrintf("%s.%s", map_name, script), callback); |
| 98 } | 81 } |
| 99 } | 82 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 const std::string& script) { | 153 const std::string& script) { |
| 171 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 154 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
| 172 pref_name); | 155 pref_name); |
| 173 CHECK(pref); | 156 CHECK(pref); |
| 174 | 157 |
| 175 std::string font_name; | 158 std::string font_name; |
| 176 if (!pref->GetValue()->GetAsString(&font_name)) { | 159 if (!pref->GetValue()->GetAsString(&font_name)) { |
| 177 NOTREACHED(); | 160 NOTREACHED(); |
| 178 return; | 161 return; |
| 179 } | 162 } |
| 180 font_name = MaybeGetLocalizedFontName(font_name); | 163 font_name = FontSettingsUtilities::MaybeGetLocalizedFontName(font_name); |
| 181 | 164 |
| 182 base::ListValue args; | 165 base::ListValue args; |
| 183 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 166 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 184 dict->SetString(kFontIdKey, font_name); | 167 dict->SetString(kFontIdKey, font_name); |
| 185 dict->SetString(kGenericFamilyKey, generic_family); | 168 dict->SetString(kGenericFamilyKey, generic_family); |
| 186 dict->SetString(kScriptKey, script); | 169 dict->SetString(kScriptKey, script); |
| 187 args.Append(std::move(dict)); | 170 args.Append(std::move(dict)); |
| 188 | 171 |
| 189 extensions::preference_helpers::DispatchEventToExtensions( | 172 extensions::preference_helpers::DispatchEventToExtensions( |
| 190 profile_, events::FONT_SETTINGS_ON_FONT_CHANGED, | 173 profile_, events::FONT_SETTINGS_ON_FONT_CHANGED, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 params->details.script); | 239 params->details.script); |
| 257 | 240 |
| 258 Profile* profile = Profile::FromBrowserContext(browser_context()); | 241 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 259 PrefService* prefs = profile->GetPrefs(); | 242 PrefService* prefs = profile->GetPrefs(); |
| 260 const PrefService::Preference* pref = | 243 const PrefService::Preference* pref = |
| 261 prefs->FindPreference(pref_path); | 244 prefs->FindPreference(pref_path); |
| 262 | 245 |
| 263 std::string font_name; | 246 std::string font_name; |
| 264 EXTENSION_FUNCTION_VALIDATE( | 247 EXTENSION_FUNCTION_VALIDATE( |
| 265 pref && pref->GetValue()->GetAsString(&font_name)); | 248 pref && pref->GetValue()->GetAsString(&font_name)); |
| 266 font_name = MaybeGetLocalizedFontName(font_name); | 249 font_name = FontSettingsUtilities::MaybeGetLocalizedFontName(font_name); |
| 267 | 250 |
| 268 // We don't support incognito-specific font prefs, so don't consider them when | 251 // We don't support incognito-specific font prefs, so don't consider them when |
| 269 // getting level of control. | 252 // getting level of control. |
| 270 const bool kIncognito = false; | 253 const bool kIncognito = false; |
| 271 std::string level_of_control = | 254 std::string level_of_control = |
| 272 extensions::preference_helpers::GetLevelOfControl(profile, extension_id(), | 255 extensions::preference_helpers::GetLevelOfControl(profile, extension_id(), |
| 273 pref_path, kIncognito); | 256 pref_path, kIncognito); |
| 274 | 257 |
| 275 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 258 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 276 result->SetString(kFontIdKey, font_name); | 259 result->SetString(kFontIdKey, font_name); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 | 429 |
| 447 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { | 430 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { |
| 448 return prefs::kWebKitMinimumFontSize; | 431 return prefs::kWebKitMinimumFontSize; |
| 449 } | 432 } |
| 450 | 433 |
| 451 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { | 434 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { |
| 452 return kPixelSizeKey; | 435 return kPixelSizeKey; |
| 453 } | 436 } |
| 454 | 437 |
| 455 } // namespace extensions | 438 } // namespace extensions |
| OLD | NEW |