| 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" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/preference/preference_helpers.h" | 15 #include "chrome/browser/extensions/api/preference/preference_helpers.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "chrome/common/extensions/api/font_settings.h" | 20 #include "chrome/common/extensions/api/font_settings.h" |
| 21 #include "chrome/common/extensions/extension_error_utils.h" | 21 #include "chrome/common/extensions/extension_error_utils.h" |
| 22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/common/pref_names_util.h" |
| 23 #include "content/public/browser/font_list_async.h" | 24 #include "content/public/browser/font_list_async.h" |
| 24 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| 26 | 27 |
| 27 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
| 28 #include "ui/gfx/font.h" | 29 #include "ui/gfx/font.h" |
| 29 #include "ui/gfx/platform_font_win.h" | 30 #include "ui/gfx/platform_font_win.h" |
| 30 #endif | 31 #endif |
| 31 | 32 |
| 32 namespace extensions { | 33 namespace extensions { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 48 const char kOnDefaultFixedFontSizeChanged[] = | 49 const char kOnDefaultFixedFontSizeChanged[] = |
| 49 "fontSettings.onDefaultFixedFontSizeChanged"; | 50 "fontSettings.onDefaultFixedFontSizeChanged"; |
| 50 const char kOnDefaultFontSizeChanged[] = | 51 const char kOnDefaultFontSizeChanged[] = |
| 51 "fontSettings.onDefaultFontSizeChanged"; | 52 "fontSettings.onDefaultFontSizeChanged"; |
| 52 const char kOnFontChanged[] = "fontSettings.onFontChanged"; | 53 const char kOnFontChanged[] = "fontSettings.onFontChanged"; |
| 53 const char kOnMinimumFontSizeChanged[] = | 54 const char kOnMinimumFontSizeChanged[] = |
| 54 "fontSettings.onMinimumFontSizeChanged"; | 55 "fontSettings.onMinimumFontSizeChanged"; |
| 55 | 56 |
| 56 // Format for font name preference paths. | 57 // Format for font name preference paths. |
| 57 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; | 58 const char kWebKitFontPrefFormat[] = "webkit.webprefs.fonts.%s.%s"; |
| 58 const char kWebKitFontPrefPrefix[] = "webkit.webprefs.fonts."; | |
| 59 | 59 |
| 60 // 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 |
| 61 // |script| is NULL, uses prefs::kWebKitCommonScript. | 61 // |script| is NULL, uses prefs::kWebKitCommonScript. |
| 62 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, | 62 std::string GetFontNamePrefPath(fonts::GenericFamily generic_family_enum, |
| 63 fonts::ScriptCode script_enum) { | 63 fonts::ScriptCode script_enum) { |
| 64 std::string script = fonts::ToString(script_enum); | 64 std::string script = fonts::ToString(script_enum); |
| 65 if (script.empty()) | 65 if (script.empty()) |
| 66 script = prefs::kWebKitCommonScript; | 66 script = prefs::kWebKitCommonScript; |
| 67 std::string generic_family = fonts::ToString(generic_family_enum); | 67 std::string generic_family = fonts::ToString(generic_family_enum); |
| 68 return StringPrintf(kWebKitFontPrefFormat, | 68 return StringPrintf(kWebKitFontPrefFormat, |
| 69 generic_family.c_str(), | 69 generic_family.c_str(), |
| 70 script.c_str()); | 70 script.c_str()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Extracts the generic family and script from font name pref path |pref_path|. | |
| 74 bool ParseFontNamePrefPath(std::string pref_path, | |
| 75 std::string* generic_family, | |
| 76 std::string* script) { | |
| 77 if (!StartsWithASCII(pref_path, kWebKitFontPrefPrefix, true)) | |
| 78 return false; | |
| 79 | |
| 80 size_t start = strlen(kWebKitFontPrefPrefix); | |
| 81 size_t pos = pref_path.find('.', start); | |
| 82 if (pos == std::string::npos || pos + 1 == pref_path.length()) | |
| 83 return false; | |
| 84 *generic_family = pref_path.substr(start, pos - start); | |
| 85 *script = pref_path.substr(pos + 1); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 // Returns the localized name of a font so that it can be matched within the | 73 // Returns the localized name of a font so that it can be matched within the |
| 90 // list of system fonts. On Windows, the list of system fonts has names only | 74 // list of system fonts. On Windows, the list of system fonts has names only |
| 91 // for the system locale, but the pref value may be in the English name. | 75 // for the system locale, but the pref value may be in the English name. |
| 92 std::string MaybeGetLocalizedFontName(const std::string& font_name) { | 76 std::string MaybeGetLocalizedFontName(const std::string& font_name) { |
| 93 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
| 94 if (!font_name.empty()) { | 78 if (!font_name.empty()) { |
| 95 gfx::Font font(font_name, 12); // dummy font size | 79 gfx::Font font(font_name, 12); // dummy font size |
| 96 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 80 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> |
| 97 GetLocalizedFontName(); | 81 GetLocalizedFontName(); |
| 98 } | 82 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 PrefEventMap::iterator iter = pref_event_map_.find(pref_name); | 154 PrefEventMap::iterator iter = pref_event_map_.find(pref_name); |
| 171 if (iter != pref_event_map_.end()) { | 155 if (iter != pref_event_map_.end()) { |
| 172 const std::string& event_name = iter->second.first; | 156 const std::string& event_name = iter->second.first; |
| 173 const std::string& key = iter->second.second; | 157 const std::string& key = iter->second.second; |
| 174 OnFontPrefChanged(pref_service, pref_name, event_name, key, incognito); | 158 OnFontPrefChanged(pref_service, pref_name, event_name, key, incognito); |
| 175 return; | 159 return; |
| 176 } | 160 } |
| 177 | 161 |
| 178 std::string generic_family; | 162 std::string generic_family; |
| 179 std::string script; | 163 std::string script; |
| 180 if (ParseFontNamePrefPath(pref_name, &generic_family, &script)) { | 164 if (pref_names_util::ParseFontNamePrefPath(pref_name, &generic_family, |
| 165 &script)) { |
| 181 OnFontNamePrefChanged(pref_service, pref_name, generic_family, script, | 166 OnFontNamePrefChanged(pref_service, pref_name, generic_family, script, |
| 182 incognito); | 167 incognito); |
| 183 return; | 168 return; |
| 184 } | 169 } |
| 185 | 170 |
| 186 NOTREACHED(); | 171 NOTREACHED(); |
| 187 } | 172 } |
| 188 | 173 |
| 189 void FontSettingsEventRouter::OnFontNamePrefChanged( | 174 void FontSettingsEventRouter::OnFontNamePrefChanged( |
| 190 PrefService* pref_service, | 175 PrefService* pref_service, |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 | 462 |
| 478 const char* SetMinimumFontSizeFunction::GetPrefName() { | 463 const char* SetMinimumFontSizeFunction::GetPrefName() { |
| 479 return prefs::kWebKitMinimumFontSize; | 464 return prefs::kWebKitMinimumFontSize; |
| 480 } | 465 } |
| 481 | 466 |
| 482 const char* SetMinimumFontSizeFunction::GetKey() { | 467 const char* SetMinimumFontSizeFunction::GetKey() { |
| 483 return kPixelSizeKey; | 468 return kPixelSizeKey; |
| 484 } | 469 } |
| 485 | 470 |
| 486 } // namespace extensions | 471 } // namespace extensions |
| OLD | NEW |