Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: chrome/browser/extensions/api/font_settings/font_settings_api.cc

Issue 11336008: When a font family pref changes to the empty string, pass it to WebKit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add unit test Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 PrefEventMap::iterator iter = pref_event_map_.find(*pref_name); 157 PrefEventMap::iterator iter = pref_event_map_.find(*pref_name);
174 if (iter != pref_event_map_.end()) { 158 if (iter != pref_event_map_.end()) {
175 const std::string& event_name = iter->second.first; 159 const std::string& event_name = iter->second.first;
176 const std::string& key = iter->second.second; 160 const std::string& key = iter->second.second;
177 OnFontPrefChanged(pref_service, *pref_name, event_name, key, incognito); 161 OnFontPrefChanged(pref_service, *pref_name, event_name, key, incognito);
178 return; 162 return;
179 } 163 }
180 164
181 std::string generic_family; 165 std::string generic_family;
182 std::string script; 166 std::string script;
183 if (ParseFontNamePrefPath(*pref_name, &generic_family, &script)) { 167 if (pref_names_util::ParseFontNamePrefPath(*pref_name, &generic_family,
168 &script)) {
184 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script, 169 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script,
185 incognito); 170 incognito);
186 return; 171 return;
187 } 172 }
188 173
189 NOTREACHED(); 174 NOTREACHED();
190 } 175 }
191 176
192 void FontSettingsEventRouter::OnFontNamePrefChanged( 177 void FontSettingsEventRouter::OnFontNamePrefChanged(
193 PrefService* pref_service, 178 PrefService* pref_service,
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 465
481 const char* SetMinimumFontSizeFunction::GetPrefName() { 466 const char* SetMinimumFontSizeFunction::GetPrefName() {
482 return prefs::kWebKitMinimumFontSize; 467 return prefs::kWebKitMinimumFontSize;
483 } 468 }
484 469
485 const char* SetMinimumFontSizeFunction::GetKey() { 470 const char* SetMinimumFontSizeFunction::GetKey() {
486 return kPixelSizeKey; 471 return kPixelSizeKey;
487 } 472 }
488 473
489 } // namespace extensions 474 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/prefs/prefs_tab_helper.h » ('j') | chrome/browser/ui/prefs/prefs_tab_helper.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698