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

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

Issue 747013003: Various optimizations to reduce the number of temporary allocations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
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"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
87 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { 88 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
88 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; 89 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i];
89 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); 90 pref_name->assign(map_name);
90 registrar->Add(pref_name.c_str(), callback); 91 pref_name->append(1, '.');
92 pref_name->append(script);
Nico 2014/12/01 17:05:15 This kind of change does not lgtm. The previous co
Peter Kasting 2014/12/01 23:09:17 FWIW, I actually find the new code significantly m
Nico 2014/12/01 23:19:06 Huh, this is very surprising to me. I thought prin
Peter Kasting 2014/12/01 23:35:30 We have quite a few in our codebase, but I think i
cmumford 2014/12/03 17:28:29 I wonder if creating a variadic template function
93 registrar->Add(*pref_name, callback);
91 } 94 }
92 } 95 }
93 96
94 } // namespace 97 } // namespace
95 98
96 FontSettingsEventRouter::FontSettingsEventRouter( 99 FontSettingsEventRouter::FontSettingsEventRouter(
97 Profile* profile) : profile_(profile) { 100 Profile* profile) : profile_(profile) {
98 registrar_.Init(profile_->GetPrefs()); 101 registrar_.Init(profile_->GetPrefs());
99 102
100 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, 103 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize,
101 fonts::OnDefaultFixedFontSizeChanged::kEventName, 104 fonts::OnDefaultFixedFontSizeChanged::kEventName,
102 kPixelSizeKey); 105 kPixelSizeKey);
103 AddPrefToObserve(prefs::kWebKitDefaultFontSize, 106 AddPrefToObserve(prefs::kWebKitDefaultFontSize,
104 fonts::OnDefaultFontSizeChanged::kEventName, 107 fonts::OnDefaultFontSizeChanged::kEventName,
105 kPixelSizeKey); 108 kPixelSizeKey);
106 AddPrefToObserve(prefs::kWebKitMinimumFontSize, 109 AddPrefToObserve(prefs::kWebKitMinimumFontSize,
107 fonts::OnMinimumFontSizeChanged::kEventName, 110 fonts::OnMinimumFontSizeChanged::kEventName,
108 kPixelSizeKey); 111 kPixelSizeKey);
109 112
110 PrefChangeRegistrar::NamedChangeCallback callback = 113 PrefChangeRegistrar::NamedChangeCallback callback =
111 base::Bind(&FontSettingsEventRouter::OnFontFamilyMapPrefChanged, 114 base::Bind(&FontSettingsEventRouter::OnFontFamilyMapPrefChanged,
112 base::Unretained(this)); 115 base::Unretained(this));
113 RegisterFontFamilyMapObserver(&registrar_, 116
114 prefs::kWebKitStandardFontFamilyMap, callback); 117 std::string pref_name;
115 RegisterFontFamilyMapObserver(&registrar_, 118 pref_name.reserve(500);
116 prefs::kWebKitSerifFontFamilyMap, callback); 119 RegisterFontFamilyMapObserver(
117 RegisterFontFamilyMapObserver(&registrar_, 120 &registrar_, prefs::kWebKitStandardFontFamilyMap, callback, &pref_name);
118 prefs::kWebKitSansSerifFontFamilyMap, callback); 121 RegisterFontFamilyMapObserver(&registrar_, prefs::kWebKitSerifFontFamilyMap,
119 RegisterFontFamilyMapObserver(&registrar_, 122 callback, &pref_name);
120 prefs::kWebKitFixedFontFamilyMap, callback); 123 RegisterFontFamilyMapObserver(
121 RegisterFontFamilyMapObserver(&registrar_, 124 &registrar_, prefs::kWebKitSansSerifFontFamilyMap, callback, &pref_name);
122 prefs::kWebKitCursiveFontFamilyMap, callback); 125 RegisterFontFamilyMapObserver(&registrar_, prefs::kWebKitFixedFontFamilyMap,
123 RegisterFontFamilyMapObserver(&registrar_, 126 callback, &pref_name);
124 prefs::kWebKitFantasyFontFamilyMap, callback); 127 RegisterFontFamilyMapObserver(&registrar_, prefs::kWebKitCursiveFontFamilyMap,
125 RegisterFontFamilyMapObserver(&registrar_, 128 callback, &pref_name);
126 prefs::kWebKitPictographFontFamilyMap, 129 RegisterFontFamilyMapObserver(&registrar_, prefs::kWebKitFantasyFontFamilyMap,
127 callback); 130 callback, &pref_name);
131 RegisterFontFamilyMapObserver(
132 &registrar_, prefs::kWebKitPictographFontFamilyMap, callback, &pref_name);
128 } 133 }
129 134
130 FontSettingsEventRouter::~FontSettingsEventRouter() {} 135 FontSettingsEventRouter::~FontSettingsEventRouter() {}
131 136
132 void FontSettingsEventRouter::AddPrefToObserve(const char* pref_name, 137 void FontSettingsEventRouter::AddPrefToObserve(const char* pref_name,
133 const char* event_name, 138 const char* event_name,
134 const char* key) { 139 const char* key) {
135 registrar_.Add(pref_name, 140 registrar_.Add(pref_name,
136 base::Bind(&FontSettingsEventRouter::OnFontPrefChanged, 141 base::Bind(&FontSettingsEventRouter::OnFontPrefChanged,
137 base::Unretained(this), 142 base::Unretained(this),
(...skipping 11 matching lines...) Expand all
149 } 154 }
150 155
151 NOTREACHED(); 156 NOTREACHED();
152 } 157 }
153 158
154 void FontSettingsEventRouter::OnFontNamePrefChanged( 159 void FontSettingsEventRouter::OnFontNamePrefChanged(
155 const std::string& pref_name, 160 const std::string& pref_name,
156 const std::string& generic_family, 161 const std::string& generic_family,
157 const std::string& script) { 162 const std::string& script) {
158 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( 163 const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
159 pref_name.c_str()); 164 pref_name);
160 CHECK(pref); 165 CHECK(pref);
161 166
162 std::string font_name; 167 std::string font_name;
163 if (!pref->GetValue()->GetAsString(&font_name)) { 168 if (!pref->GetValue()->GetAsString(&font_name)) {
164 NOTREACHED(); 169 NOTREACHED();
165 return; 170 return;
166 } 171 }
167 font_name = MaybeGetLocalizedFontName(font_name); 172 font_name = MaybeGetLocalizedFontName(font_name);
168 173
169 base::ListValue args; 174 base::ListValue args;
(...skipping 10 matching lines...) Expand all
180 APIPermission::kFontSettings, 185 APIPermission::kFontSettings,
181 false, 186 false,
182 pref_name); 187 pref_name);
183 } 188 }
184 189
185 void FontSettingsEventRouter::OnFontPrefChanged( 190 void FontSettingsEventRouter::OnFontPrefChanged(
186 const std::string& event_name, 191 const std::string& event_name,
187 const std::string& key, 192 const std::string& key,
188 const std::string& pref_name) { 193 const std::string& pref_name) {
189 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( 194 const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
190 pref_name.c_str()); 195 pref_name);
191 CHECK(pref); 196 CHECK(pref);
192 197
193 base::ListValue args; 198 base::ListValue args;
194 base::DictionaryValue* dict = new base::DictionaryValue(); 199 base::DictionaryValue* dict = new base::DictionaryValue();
195 args.Append(dict); 200 args.Append(dict);
196 dict->Set(key, pref->GetValue()->DeepCopy()); 201 dict->Set(key, pref->GetValue()->DeepCopy());
197 202
198 extensions::preference_helpers::DispatchEventToExtensions( 203 extensions::preference_helpers::DispatchEventToExtensions(
199 profile_, 204 profile_,
200 event_name, 205 event_name,
(...skipping 27 matching lines...) Expand all
228 233
229 scoped_ptr<fonts::ClearFont::Params> params( 234 scoped_ptr<fonts::ClearFont::Params> params(
230 fonts::ClearFont::Params::Create(*args_)); 235 fonts::ClearFont::Params::Create(*args_));
231 EXTENSION_FUNCTION_VALIDATE(params.get()); 236 EXTENSION_FUNCTION_VALIDATE(params.get());
232 237
233 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 238 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
234 params->details.script); 239 params->details.script);
235 240
236 // Ensure |pref_path| really is for a registered per-script font pref. 241 // Ensure |pref_path| really is for a registered per-script font pref.
237 EXTENSION_FUNCTION_VALIDATE( 242 EXTENSION_FUNCTION_VALIDATE(
238 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); 243 GetProfile()->GetPrefs()->FindPreference(pref_path));
239 244
240 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( 245 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref(
241 extension_id(), pref_path.c_str(), kExtensionPrefsScopeRegular); 246 extension_id(), pref_path, kExtensionPrefsScopeRegular);
242 return true; 247 return true;
243 } 248 }
244 249
245 bool FontSettingsGetFontFunction::RunSync() { 250 bool FontSettingsGetFontFunction::RunSync() {
246 scoped_ptr<fonts::GetFont::Params> params( 251 scoped_ptr<fonts::GetFont::Params> params(
247 fonts::GetFont::Params::Create(*args_)); 252 fonts::GetFont::Params::Create(*args_));
248 EXTENSION_FUNCTION_VALIDATE(params.get()); 253 EXTENSION_FUNCTION_VALIDATE(params.get());
249 254
250 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 255 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
251 params->details.script); 256 params->details.script);
252 257
253 PrefService* prefs = GetProfile()->GetPrefs(); 258 PrefService* prefs = GetProfile()->GetPrefs();
254 const PrefService::Preference* pref = 259 const PrefService::Preference* pref =
255 prefs->FindPreference(pref_path.c_str()); 260 prefs->FindPreference(pref_path);
256 261
257 std::string font_name; 262 std::string font_name;
258 EXTENSION_FUNCTION_VALIDATE( 263 EXTENSION_FUNCTION_VALIDATE(
259 pref && pref->GetValue()->GetAsString(&font_name)); 264 pref && pref->GetValue()->GetAsString(&font_name));
260 font_name = MaybeGetLocalizedFontName(font_name); 265 font_name = MaybeGetLocalizedFontName(font_name);
261 266
262 // We don't support incognito-specific font prefs, so don't consider them when 267 // We don't support incognito-specific font prefs, so don't consider them when
263 // getting level of control. 268 // getting level of control.
264 const bool kIncognito = false; 269 const bool kIncognito = false;
265 std::string level_of_control = 270 std::string level_of_control =
(...skipping 15 matching lines...) Expand all
281 286
282 scoped_ptr<fonts::SetFont::Params> params( 287 scoped_ptr<fonts::SetFont::Params> params(
283 fonts::SetFont::Params::Create(*args_)); 288 fonts::SetFont::Params::Create(*args_));
284 EXTENSION_FUNCTION_VALIDATE(params.get()); 289 EXTENSION_FUNCTION_VALIDATE(params.get());
285 290
286 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 291 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
287 params->details.script); 292 params->details.script);
288 293
289 // Ensure |pref_path| really is for a registered font pref. 294 // Ensure |pref_path| really is for a registered font pref.
290 EXTENSION_FUNCTION_VALIDATE( 295 EXTENSION_FUNCTION_VALIDATE(
291 GetProfile()->GetPrefs()->FindPreference(pref_path.c_str())); 296 GetProfile()->GetPrefs()->FindPreference(pref_path));
292 297
293 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( 298 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref(
294 extension_id(), 299 extension_id(),
295 pref_path.c_str(), 300 pref_path,
296 kExtensionPrefsScopeRegular, 301 kExtensionPrefsScopeRegular,
297 new base::StringValue(params->details.font_id)); 302 new base::StringValue(params->details.font_id));
298 return true; 303 return true;
299 } 304 }
300 305
301 bool FontSettingsGetFontListFunction::RunAsync() { 306 bool FontSettingsGetFontListFunction::RunAsync() {
302 content::GetFontListAsync( 307 content::GetFontListAsync(
303 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); 308 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this));
304 return true; 309 return true;
305 } 310 }
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 453
449 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { 454 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() {
450 return prefs::kWebKitMinimumFontSize; 455 return prefs::kWebKitMinimumFontSize;
451 } 456 }
452 457
453 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { 458 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() {
454 return kPixelSizeKey; 459 return kPixelSizeKey;
455 } 460 }
456 461
457 } // namespace extensions 462 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/themes/browser_theme_pack.h » ('j') | chrome/browser/themes/browser_theme_pack.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698