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/extension_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/extension_preference_helpers.h" | 15 #include "chrome/browser/extensions/extension_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 "content/public/browser/font_list_async.h" | 23 #include "content/public/browser/font_list_async.h" |
24 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
26 | 26 |
27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
28 #include "ui/gfx/font.h" | 28 #include "ui/gfx/font.h" |
29 #include "ui/gfx/platform_font_win.h" | 29 #include "ui/gfx/platform_font_win.h" |
30 #endif | 30 #endif |
31 | 31 |
32 using extensions::APIPermission; | 32 namespace extensions { |
33 | 33 |
34 namespace fonts = extensions::api::font_settings; | 34 namespace fonts = api::font_settings; |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 const char kFontIdKey[] = "fontId"; | 38 const char kFontIdKey[] = "fontId"; |
39 const char kGenericFamilyKey[] = "genericFamily"; | 39 const char kGenericFamilyKey[] = "genericFamily"; |
40 const char kLevelOfControlKey[] = "levelOfControl"; | 40 const char kLevelOfControlKey[] = "levelOfControl"; |
41 const char kDisplayNameKey[] = "displayName"; | 41 const char kDisplayNameKey[] = "displayName"; |
42 const char kPixelSizeKey[] = "pixelSize"; | 42 const char kPixelSizeKey[] = "pixelSize"; |
43 const char kScriptKey[] = "script"; | 43 const char kScriptKey[] = "script"; |
44 | 44 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 content::NotificationObserver* obs) { | 102 content::NotificationObserver* obs) { |
103 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 103 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
104 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 104 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
105 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 105 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); |
106 registrar->Add(pref_name.c_str(), obs); | 106 registrar->Add(pref_name.c_str(), obs); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 } // namespace | 110 } // namespace |
111 | 111 |
112 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( | 112 FontSettingsEventRouter::FontSettingsEventRouter( |
113 Profile* profile) : profile_(profile) {} | 113 Profile* profile) : profile_(profile) {} |
114 | 114 |
115 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} | 115 FontSettingsEventRouter::~FontSettingsEventRouter() {} |
116 | 116 |
117 void ExtensionFontSettingsEventRouter::Init() { | 117 void FontSettingsEventRouter::Init() { |
118 registrar_.Init(profile_->GetPrefs()); | 118 registrar_.Init(profile_->GetPrefs()); |
119 | 119 |
120 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, | 120 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, |
121 kOnDefaultFixedFontSizeChanged, | 121 kOnDefaultFixedFontSizeChanged, |
122 kPixelSizeKey); | 122 kPixelSizeKey); |
123 AddPrefToObserve(prefs::kWebKitDefaultFontSize, | 123 AddPrefToObserve(prefs::kWebKitDefaultFontSize, |
124 kOnDefaultFontSizeChanged, | 124 kOnDefaultFontSizeChanged, |
125 kPixelSizeKey); | 125 kPixelSizeKey); |
126 AddPrefToObserve(prefs::kWebKitMinimumFontSize, | 126 AddPrefToObserve(prefs::kWebKitMinimumFontSize, |
127 kOnMinimumFontSizeChanged, | 127 kOnMinimumFontSizeChanged, |
128 kPixelSizeKey); | 128 kPixelSizeKey); |
129 | 129 |
130 RegisterFontFamilyMapObserver(®istrar_, | 130 RegisterFontFamilyMapObserver(®istrar_, |
131 prefs::kWebKitStandardFontFamilyMap, this); | 131 prefs::kWebKitStandardFontFamilyMap, this); |
132 RegisterFontFamilyMapObserver(®istrar_, | 132 RegisterFontFamilyMapObserver(®istrar_, |
133 prefs::kWebKitSerifFontFamilyMap, this); | 133 prefs::kWebKitSerifFontFamilyMap, this); |
134 RegisterFontFamilyMapObserver(®istrar_, | 134 RegisterFontFamilyMapObserver(®istrar_, |
135 prefs::kWebKitSansSerifFontFamilyMap, this); | 135 prefs::kWebKitSansSerifFontFamilyMap, this); |
136 RegisterFontFamilyMapObserver(®istrar_, | 136 RegisterFontFamilyMapObserver(®istrar_, |
137 prefs::kWebKitFixedFontFamilyMap, this); | 137 prefs::kWebKitFixedFontFamilyMap, this); |
138 RegisterFontFamilyMapObserver(®istrar_, | 138 RegisterFontFamilyMapObserver(®istrar_, |
139 prefs::kWebKitCursiveFontFamilyMap, this); | 139 prefs::kWebKitCursiveFontFamilyMap, this); |
140 RegisterFontFamilyMapObserver(®istrar_, | 140 RegisterFontFamilyMapObserver(®istrar_, |
141 prefs::kWebKitFantasyFontFamilyMap, this); | 141 prefs::kWebKitFantasyFontFamilyMap, this); |
142 } | 142 } |
143 | 143 |
144 void ExtensionFontSettingsEventRouter::AddPrefToObserve(const char* pref_name, | 144 void FontSettingsEventRouter::AddPrefToObserve(const char* pref_name, |
145 const char* event_name, | 145 const char* event_name, |
146 const char* key) { | 146 const char* key) { |
147 registrar_.Add(pref_name, this); | 147 registrar_.Add(pref_name, this); |
148 pref_event_map_[pref_name] = std::make_pair(event_name, key); | 148 pref_event_map_[pref_name] = std::make_pair(event_name, key); |
149 } | 149 } |
150 | 150 |
151 void ExtensionFontSettingsEventRouter::Observe( | 151 void FontSettingsEventRouter::Observe( |
152 int type, | 152 int type, |
153 const content::NotificationSource& source, | 153 const content::NotificationSource& source, |
154 const content::NotificationDetails& details) { | 154 const content::NotificationDetails& details) { |
155 if (type != chrome::NOTIFICATION_PREF_CHANGED) { | 155 if (type != chrome::NOTIFICATION_PREF_CHANGED) { |
156 NOTREACHED(); | 156 NOTREACHED(); |
157 return; | 157 return; |
158 } | 158 } |
159 | 159 |
160 PrefService* pref_service = content::Source<PrefService>(source).ptr(); | 160 PrefService* pref_service = content::Source<PrefService>(source).ptr(); |
161 bool incognito = (pref_service != profile_->GetPrefs()); | 161 bool incognito = (pref_service != profile_->GetPrefs()); |
(...skipping 14 matching lines...) Expand all Loading... |
176 std::string script; | 176 std::string script; |
177 if (ParseFontNamePrefPath(*pref_name, &generic_family, &script)) { | 177 if (ParseFontNamePrefPath(*pref_name, &generic_family, &script)) { |
178 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script, | 178 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script, |
179 incognito); | 179 incognito); |
180 return; | 180 return; |
181 } | 181 } |
182 | 182 |
183 NOTREACHED(); | 183 NOTREACHED(); |
184 } | 184 } |
185 | 185 |
186 void ExtensionFontSettingsEventRouter::OnFontNamePrefChanged( | 186 void FontSettingsEventRouter::OnFontNamePrefChanged( |
187 PrefService* pref_service, | 187 PrefService* pref_service, |
188 const std::string& pref_name, | 188 const std::string& pref_name, |
189 const std::string& generic_family, | 189 const std::string& generic_family, |
190 const std::string& script, | 190 const std::string& script, |
191 bool incognito) { | 191 bool incognito) { |
192 const PrefService::Preference* pref = pref_service->FindPreference( | 192 const PrefService::Preference* pref = pref_service->FindPreference( |
193 pref_name.c_str()); | 193 pref_name.c_str()); |
194 CHECK(pref); | 194 CHECK(pref); |
195 | 195 |
196 std::string font_name; | 196 std::string font_name; |
(...skipping 12 matching lines...) Expand all Loading... |
209 | 209 |
210 extension_preference_helpers::DispatchEventToExtensions( | 210 extension_preference_helpers::DispatchEventToExtensions( |
211 profile_, | 211 profile_, |
212 kOnFontChanged, | 212 kOnFontChanged, |
213 &args, | 213 &args, |
214 APIPermission::kFontSettings, | 214 APIPermission::kFontSettings, |
215 incognito, | 215 incognito, |
216 pref_name); | 216 pref_name); |
217 } | 217 } |
218 | 218 |
219 void ExtensionFontSettingsEventRouter::OnFontPrefChanged( | 219 void FontSettingsEventRouter::OnFontPrefChanged( |
220 PrefService* pref_service, | 220 PrefService* pref_service, |
221 const std::string& pref_name, | 221 const std::string& pref_name, |
222 const std::string& event_name, | 222 const std::string& event_name, |
223 const std::string& key, | 223 const std::string& key, |
224 bool incognito) { | 224 bool incognito) { |
225 const PrefService::Preference* pref = pref_service->FindPreference( | 225 const PrefService::Preference* pref = pref_service->FindPreference( |
226 pref_name.c_str()); | 226 pref_name.c_str()); |
227 CHECK(pref); | 227 CHECK(pref); |
228 | 228 |
229 ListValue args; | 229 ListValue args; |
(...skipping 20 matching lines...) Expand all Loading... |
250 fonts::ClearFont::Params::Create(*args_)); | 250 fonts::ClearFont::Params::Create(*args_)); |
251 EXTENSION_FUNCTION_VALIDATE(params.get()); | 251 EXTENSION_FUNCTION_VALIDATE(params.get()); |
252 | 252 |
253 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 253 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
254 params->details.script.get()); | 254 params->details.script.get()); |
255 | 255 |
256 // Ensure |pref_path| really is for a registered per-script font pref. | 256 // Ensure |pref_path| really is for a registered per-script font pref. |
257 EXTENSION_FUNCTION_VALIDATE( | 257 EXTENSION_FUNCTION_VALIDATE( |
258 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 258 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
259 | 259 |
260 extensions::ExtensionPrefs* prefs = | 260 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
261 profile_->GetExtensionService()->extension_prefs(); | |
262 prefs->RemoveExtensionControlledPref(extension_id(), | 261 prefs->RemoveExtensionControlledPref(extension_id(), |
263 pref_path.c_str(), | 262 pref_path.c_str(), |
264 extensions::kExtensionPrefsScopeRegular); | 263 kExtensionPrefsScopeRegular); |
265 return true; | 264 return true; |
266 } | 265 } |
267 | 266 |
268 bool GetFontFunction::RunImpl() { | 267 bool GetFontFunction::RunImpl() { |
269 scoped_ptr<fonts::GetFont::Params> params( | 268 scoped_ptr<fonts::GetFont::Params> params( |
270 fonts::GetFont::Params::Create(*args_)); | 269 fonts::GetFont::Params::Create(*args_)); |
271 EXTENSION_FUNCTION_VALIDATE(params.get()); | 270 EXTENSION_FUNCTION_VALIDATE(params.get()); |
272 | 271 |
273 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 272 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
274 params->details.script.get()); | 273 params->details.script.get()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 scoped_ptr<fonts::SetFont::Params> params( | 305 scoped_ptr<fonts::SetFont::Params> params( |
307 fonts::SetFont::Params::Create(*args_)); | 306 fonts::SetFont::Params::Create(*args_)); |
308 EXTENSION_FUNCTION_VALIDATE(params.get()); | 307 EXTENSION_FUNCTION_VALIDATE(params.get()); |
309 | 308 |
310 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, | 309 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, |
311 params->details.script.get()); | 310 params->details.script.get()); |
312 // Ensure |pref_path| really is for a registered font pref. | 311 // Ensure |pref_path| really is for a registered font pref. |
313 EXTENSION_FUNCTION_VALIDATE( | 312 EXTENSION_FUNCTION_VALIDATE( |
314 profile_->GetPrefs()->FindPreference(pref_path.c_str())); | 313 profile_->GetPrefs()->FindPreference(pref_path.c_str())); |
315 | 314 |
316 extensions::ExtensionPrefs* prefs = | 315 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
317 profile_->GetExtensionService()->extension_prefs(); | |
318 prefs->SetExtensionControlledPref( | 316 prefs->SetExtensionControlledPref( |
319 extension_id(), | 317 extension_id(), |
320 pref_path.c_str(), | 318 pref_path.c_str(), |
321 extensions::kExtensionPrefsScopeRegular, | 319 kExtensionPrefsScopeRegular, |
322 Value::CreateStringValue(params->details.font_id)); | 320 Value::CreateStringValue(params->details.font_id)); |
323 return true; | 321 return true; |
324 } | 322 } |
325 | 323 |
326 bool GetFontListFunction::RunImpl() { | 324 bool GetFontListFunction::RunImpl() { |
327 content::GetFontListAsync( | 325 content::GetFontListAsync( |
328 Bind(&GetFontListFunction::FontListHasLoaded, this)); | 326 Bind(&GetFontListFunction::FontListHasLoaded, this)); |
329 return true; | 327 return true; |
330 } | 328 } |
331 | 329 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 SetResult(result.release()); | 362 SetResult(result.release()); |
365 return true; | 363 return true; |
366 } | 364 } |
367 | 365 |
368 bool ClearFontPrefExtensionFunction::RunImpl() { | 366 bool ClearFontPrefExtensionFunction::RunImpl() { |
369 if (profile_->IsOffTheRecord()) { | 367 if (profile_->IsOffTheRecord()) { |
370 error_ = kSetFromIncognitoError; | 368 error_ = kSetFromIncognitoError; |
371 return false; | 369 return false; |
372 } | 370 } |
373 | 371 |
374 extensions::ExtensionPrefs* prefs = | 372 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
375 profile_->GetExtensionService()->extension_prefs(); | |
376 prefs->RemoveExtensionControlledPref(extension_id(), | 373 prefs->RemoveExtensionControlledPref(extension_id(), |
377 GetPrefName(), | 374 GetPrefName(), |
378 extensions::kExtensionPrefsScopeRegular); | 375 kExtensionPrefsScopeRegular); |
379 return true; | 376 return true; |
380 } | 377 } |
381 | 378 |
382 bool GetFontPrefExtensionFunction::RunImpl() { | 379 bool GetFontPrefExtensionFunction::RunImpl() { |
383 PrefService* prefs = profile_->GetPrefs(); | 380 PrefService* prefs = profile_->GetPrefs(); |
384 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); | 381 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); |
385 EXTENSION_FUNCTION_VALIDATE(pref); | 382 EXTENSION_FUNCTION_VALIDATE(pref); |
386 | 383 |
387 // We don't support incognito-specific font prefs, so don't consider them when | 384 // We don't support incognito-specific font prefs, so don't consider them when |
388 // getting level of control. | 385 // getting level of control. |
(...skipping 17 matching lines...) Expand all Loading... |
406 error_ = kSetFromIncognitoError; | 403 error_ = kSetFromIncognitoError; |
407 return false; | 404 return false; |
408 } | 405 } |
409 | 406 |
410 DictionaryValue* details = NULL; | 407 DictionaryValue* details = NULL; |
411 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 408 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
412 | 409 |
413 Value* value; | 410 Value* value; |
414 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value)); | 411 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value)); |
415 | 412 |
416 extensions::ExtensionPrefs* prefs = | 413 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
417 profile_->GetExtensionService()->extension_prefs(); | |
418 prefs->SetExtensionControlledPref(extension_id(), | 414 prefs->SetExtensionControlledPref(extension_id(), |
419 GetPrefName(), | 415 GetPrefName(), |
420 extensions::kExtensionPrefsScopeRegular, | 416 kExtensionPrefsScopeRegular, |
421 value->DeepCopy()); | 417 value->DeepCopy()); |
422 return true; | 418 return true; |
423 } | 419 } |
424 | 420 |
425 const char* ClearDefaultFontSizeFunction::GetPrefName() { | 421 const char* ClearDefaultFontSizeFunction::GetPrefName() { |
426 return prefs::kWebKitDefaultFontSize; | 422 return prefs::kWebKitDefaultFontSize; |
427 } | 423 } |
428 | 424 |
429 const char* GetDefaultFontSizeFunction::GetPrefName() { | 425 const char* GetDefaultFontSizeFunction::GetPrefName() { |
430 return prefs::kWebKitDefaultFontSize; | 426 return prefs::kWebKitDefaultFontSize; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 return kPixelSizeKey; | 470 return kPixelSizeKey; |
475 } | 471 } |
476 | 472 |
477 const char* SetMinimumFontSizeFunction::GetPrefName() { | 473 const char* SetMinimumFontSizeFunction::GetPrefName() { |
478 return prefs::kWebKitMinimumFontSize; | 474 return prefs::kWebKitMinimumFontSize; |
479 } | 475 } |
480 | 476 |
481 const char* SetMinimumFontSizeFunction::GetKey() { | 477 const char* SetMinimumFontSizeFunction::GetKey() { |
482 return kPixelSizeKey; | 478 return kPixelSizeKey; |
483 } | 479 } |
| 480 |
| 481 } // namespace extensions |
OLD | NEW |