| 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 #include "chrome/browser/extensions/extension_font_settings_api.h" | 5 #include "chrome/browser/extensions/extension_font_settings_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 24 | 24 |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 #include "ui/gfx/font.h" | 26 #include "ui/gfx/font.h" |
| 27 #include "ui/gfx/platform_font_win.h" | 27 #include "ui/gfx/platform_font_win.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const char kCharsetKey[] = "charset"; |
| 33 const char kFontNameKey[] = "fontName"; |
| 32 const char kGenericFamilyKey[] = "genericFamily"; | 34 const char kGenericFamilyKey[] = "genericFamily"; |
| 33 const char kFontNameKey[] = "fontName"; | |
| 34 const char kLocalizedNameKey[] = "localizedName"; | 35 const char kLocalizedNameKey[] = "localizedName"; |
| 35 const char kPixelSizeKey[] = "pixelSize"; | 36 const char kPixelSizeKey[] = "pixelSize"; |
| 36 const char kScriptKey[] = "script"; | 37 const char kScriptKey[] = "script"; |
| 37 | 38 |
| 39 const char kOnDefaultCharacterSetChanged[] = |
| 40 "experimental.fontSettings.onDefaultCharacterSetChanged"; |
| 38 const char kOnDefaultFixedFontSizeChanged[] = | 41 const char kOnDefaultFixedFontSizeChanged[] = |
| 39 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; | 42 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; |
| 40 const char kOnDefaultFontSizeChanged[] = | 43 const char kOnDefaultFontSizeChanged[] = |
| 41 "experimental.fontSettings.onDefaultFontSizeChanged"; | 44 "experimental.fontSettings.onDefaultFontSizeChanged"; |
| 42 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; | 45 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; |
| 43 const char kOnMinimumFontSizeChanged[] = | 46 const char kOnMinimumFontSizeChanged[] = |
| 44 "experimental.fontSettings.onMinimumFontSizeChanged"; | 47 "experimental.fontSettings.onMinimumFontSizeChanged"; |
| 45 | 48 |
| 46 // Format for per-script font preference keys. | 49 // Format for per-script font preference keys. |
| 47 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 50 // E.g., "webkit.webprefs.fonts.standard.Hrkt" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } // namespace | 136 } // namespace |
| 134 | 137 |
| 135 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( | 138 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( |
| 136 Profile* profile) : profile_(profile) {} | 139 Profile* profile) : profile_(profile) {} |
| 137 | 140 |
| 138 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} | 141 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} |
| 139 | 142 |
| 140 void ExtensionFontSettingsEventRouter::Init() { | 143 void ExtensionFontSettingsEventRouter::Init() { |
| 141 registrar_.Init(profile_->GetPrefs()); | 144 registrar_.Init(profile_->GetPrefs()); |
| 142 | 145 |
| 143 registrar_.Add(prefs::kWebKitGlobalDefaultFixedFontSize, this); | 146 AddPrefToObserve(prefs::kWebKitGlobalDefaultFixedFontSize, |
| 144 pref_event_map_[prefs::kWebKitGlobalDefaultFixedFontSize] = | 147 kOnDefaultFixedFontSizeChanged, |
| 145 kOnDefaultFixedFontSizeChanged; | 148 kPixelSizeKey); |
| 146 | 149 AddPrefToObserve(prefs::kWebKitGlobalDefaultFontSize, |
| 147 registrar_.Add(prefs::kWebKitGlobalDefaultFontSize, this); | 150 kOnDefaultFontSizeChanged, |
| 148 pref_event_map_[prefs::kWebKitGlobalDefaultFontSize] = | 151 kPixelSizeKey); |
| 149 kOnDefaultFontSizeChanged; | 152 AddPrefToObserve(prefs::kWebKitGlobalMinimumFontSize, |
| 150 | 153 kOnMinimumFontSizeChanged, |
| 151 registrar_.Add(prefs::kWebKitGlobalMinimumFontSize, this); | 154 kPixelSizeKey); |
| 152 pref_event_map_[prefs::kWebKitGlobalMinimumFontSize] = | 155 AddPrefToObserve(prefs::kGlobalDefaultCharset, |
| 153 kOnMinimumFontSizeChanged; | 156 kOnDefaultCharacterSetChanged, |
| 157 kCharsetKey); |
| 154 | 158 |
| 155 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); | 159 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); |
| 156 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); | 160 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); |
| 157 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); | 161 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); |
| 158 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); | 162 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); |
| 159 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); | 163 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); |
| 160 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); | 164 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); |
| 161 RegisterFontFamilyMapObserver(®istrar_, | 165 RegisterFontFamilyMapObserver(®istrar_, |
| 162 prefs::kWebKitStandardFontFamilyMap, this); | 166 prefs::kWebKitStandardFontFamilyMap, this); |
| 163 RegisterFontFamilyMapObserver(®istrar_, | 167 RegisterFontFamilyMapObserver(®istrar_, |
| 164 prefs::kWebKitSerifFontFamilyMap, this); | 168 prefs::kWebKitSerifFontFamilyMap, this); |
| 165 RegisterFontFamilyMapObserver(®istrar_, | 169 RegisterFontFamilyMapObserver(®istrar_, |
| 166 prefs::kWebKitSansSerifFontFamilyMap, this); | 170 prefs::kWebKitSansSerifFontFamilyMap, this); |
| 167 RegisterFontFamilyMapObserver(®istrar_, | 171 RegisterFontFamilyMapObserver(®istrar_, |
| 168 prefs::kWebKitFixedFontFamilyMap, this); | 172 prefs::kWebKitFixedFontFamilyMap, this); |
| 169 RegisterFontFamilyMapObserver(®istrar_, | 173 RegisterFontFamilyMapObserver(®istrar_, |
| 170 prefs::kWebKitCursiveFontFamilyMap, this); | 174 prefs::kWebKitCursiveFontFamilyMap, this); |
| 171 RegisterFontFamilyMapObserver(®istrar_, | 175 RegisterFontFamilyMapObserver(®istrar_, |
| 172 prefs::kWebKitFantasyFontFamilyMap, this); | 176 prefs::kWebKitFantasyFontFamilyMap, this); |
| 173 } | 177 } |
| 174 | 178 |
| 179 void ExtensionFontSettingsEventRouter::AddPrefToObserve(const char* pref_name, |
| 180 const char* event_name, |
| 181 const char* key) { |
| 182 registrar_.Add(pref_name, this); |
| 183 pref_event_map_[pref_name] = std::make_pair(event_name, key); |
| 184 } |
| 185 |
| 175 void ExtensionFontSettingsEventRouter::Observe( | 186 void ExtensionFontSettingsEventRouter::Observe( |
| 176 int type, | 187 int type, |
| 177 const content::NotificationSource& source, | 188 const content::NotificationSource& source, |
| 178 const content::NotificationDetails& details) { | 189 const content::NotificationDetails& details) { |
| 179 if (type != chrome::NOTIFICATION_PREF_CHANGED) { | 190 if (type != chrome::NOTIFICATION_PREF_CHANGED) { |
| 180 NOTREACHED(); | 191 NOTREACHED(); |
| 181 return; | 192 return; |
| 182 } | 193 } |
| 183 | 194 |
| 184 PrefService* pref_service = content::Source<PrefService>(source).ptr(); | 195 PrefService* pref_service = content::Source<PrefService>(source).ptr(); |
| 185 bool incognito = (pref_service != profile_->GetPrefs()); | 196 bool incognito = (pref_service != profile_->GetPrefs()); |
| 186 // We're only observing pref changes on the regular profile. | 197 // We're only observing pref changes on the regular profile. |
| 187 DCHECK(!incognito); | 198 DCHECK(!incognito); |
| 188 const std::string* pref_key = | 199 const std::string* pref_name = |
| 189 content::Details<const std::string>(details).ptr(); | 200 content::Details<const std::string>(details).ptr(); |
| 190 | 201 |
| 191 PrefEventMap::iterator iter = pref_event_map_.find(*pref_key); | 202 PrefEventMap::iterator iter = pref_event_map_.find(*pref_name); |
| 192 if (iter != pref_event_map_.end()) { | 203 if (iter != pref_event_map_.end()) { |
| 193 OnFontSizePrefChanged(pref_service, *pref_key, iter->second, incognito); | 204 const std::string& event_name = iter->second.first; |
| 205 const std::string& key = iter->second.second; |
| 206 OnFontPrefChanged(pref_service, *pref_name, event_name, key, incognito); |
| 194 return; | 207 return; |
| 195 } | 208 } |
| 196 | 209 |
| 197 std::string generic_family; | 210 std::string generic_family; |
| 198 std::string script; | 211 std::string script; |
| 199 if (ParseFontNamePrefPath(*pref_key, &generic_family, &script)) { | 212 if (ParseFontNamePrefPath(*pref_name, &generic_family, &script)) { |
| 200 OnFontNamePrefChanged(pref_service, *pref_key, generic_family, script, | 213 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script, |
| 201 incognito); | 214 incognito); |
| 202 return; | 215 return; |
| 203 } | 216 } |
| 204 | 217 |
| 205 NOTREACHED(); | 218 NOTREACHED(); |
| 206 } | 219 } |
| 207 | 220 |
| 208 void ExtensionFontSettingsEventRouter::OnFontNamePrefChanged( | 221 void ExtensionFontSettingsEventRouter::OnFontNamePrefChanged( |
| 209 PrefService* pref_service, | 222 PrefService* pref_service, |
| 210 const std::string& pref_key, | 223 const std::string& pref_name, |
| 211 const std::string& generic_family, | 224 const std::string& generic_family, |
| 212 const std::string& script, | 225 const std::string& script, |
| 213 bool incognito) { | 226 bool incognito) { |
| 214 const PrefService::Preference* pref = pref_service->FindPreference( | 227 const PrefService::Preference* pref = pref_service->FindPreference( |
| 215 pref_key.c_str()); | 228 pref_name.c_str()); |
| 216 CHECK(pref); | 229 CHECK(pref); |
| 217 | 230 |
| 218 std::string font_name; | 231 std::string font_name; |
| 219 if (!pref->GetValue()->GetAsString(&font_name)) { | 232 if (!pref->GetValue()->GetAsString(&font_name)) { |
| 220 NOTREACHED(); | 233 NOTREACHED(); |
| 221 return; | 234 return; |
| 222 } | 235 } |
| 223 font_name = MaybeGetLocalizedFontName(font_name); | 236 font_name = MaybeGetLocalizedFontName(font_name); |
| 224 | 237 |
| 225 ListValue args; | 238 ListValue args; |
| 226 DictionaryValue* dict = new DictionaryValue(); | 239 DictionaryValue* dict = new DictionaryValue(); |
| 227 args.Append(dict); | 240 args.Append(dict); |
| 228 dict->SetString(kFontNameKey, font_name); | 241 dict->SetString(kFontNameKey, font_name); |
| 229 dict->SetString(kGenericFamilyKey, generic_family); | 242 dict->SetString(kGenericFamilyKey, generic_family); |
| 230 if (!script.empty()) | 243 if (!script.empty()) |
| 231 dict->SetString(kScriptKey, script); | 244 dict->SetString(kScriptKey, script); |
| 232 | 245 |
| 233 extension_preference_helpers::DispatchEventToExtensions( | 246 extension_preference_helpers::DispatchEventToExtensions( |
| 234 profile_, | 247 profile_, |
| 235 kOnFontChanged, | 248 kOnFontChanged, |
| 236 &args, | 249 &args, |
| 237 ExtensionAPIPermission::kExperimental, | 250 ExtensionAPIPermission::kExperimental, |
| 238 incognito, | 251 incognito, |
| 239 pref_key); | 252 pref_name); |
| 240 } | 253 } |
| 241 | 254 |
| 242 void ExtensionFontSettingsEventRouter::OnFontSizePrefChanged( | 255 void ExtensionFontSettingsEventRouter::OnFontPrefChanged( |
| 243 PrefService* pref_service, | 256 PrefService* pref_service, |
| 244 const std::string& pref_key, | 257 const std::string& pref_name, |
| 245 const std::string& event_name, | 258 const std::string& event_name, |
| 259 const std::string& key, |
| 246 bool incognito) { | 260 bool incognito) { |
| 247 const PrefService::Preference* pref = pref_service->FindPreference( | 261 const PrefService::Preference* pref = pref_service->FindPreference( |
| 248 pref_key.c_str()); | 262 pref_name.c_str()); |
| 249 CHECK(pref); | 263 CHECK(pref); |
| 250 | 264 |
| 251 int size; | |
| 252 if (!pref->GetValue()->GetAsInteger(&size)) { | |
| 253 NOTREACHED(); | |
| 254 return; | |
| 255 } | |
| 256 | |
| 257 ListValue args; | 265 ListValue args; |
| 258 DictionaryValue* dict = new DictionaryValue(); | 266 DictionaryValue* dict = new DictionaryValue(); |
| 259 args.Append(dict); | 267 args.Append(dict); |
| 260 dict->SetInteger(kPixelSizeKey, size); | 268 dict->Set(key, pref->GetValue()->DeepCopy()); |
| 261 | 269 |
| 262 extension_preference_helpers::DispatchEventToExtensions( | 270 extension_preference_helpers::DispatchEventToExtensions( |
| 263 profile_, | 271 profile_, |
| 264 event_name, | 272 event_name, |
| 265 &args, | 273 &args, |
| 266 ExtensionAPIPermission::kExperimental, | 274 ExtensionAPIPermission::kExperimental, |
| 267 incognito, | 275 incognito, |
| 268 pref_key); | 276 pref_name); |
| 269 } | 277 } |
| 270 | 278 |
| 271 bool GetFontFunction::RunImpl() { | 279 bool GetFontFunction::RunImpl() { |
| 272 DictionaryValue* details = NULL; | 280 DictionaryValue* details = NULL; |
| 273 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 281 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 274 | 282 |
| 275 std::string pref_path; | 283 std::string pref_path; |
| 276 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); | 284 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); |
| 277 | 285 |
| 278 PrefService* prefs = profile_->GetPrefs(); | 286 PrefService* prefs = profile_->GetPrefs(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 DictionaryValue* font_name = new DictionaryValue(); | 354 DictionaryValue* font_name = new DictionaryValue(); |
| 347 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); | 355 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); |
| 348 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); | 356 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); |
| 349 result->Append(font_name); | 357 result->Append(font_name); |
| 350 } | 358 } |
| 351 | 359 |
| 352 result_.reset(result.release()); | 360 result_.reset(result.release()); |
| 353 return true; | 361 return true; |
| 354 } | 362 } |
| 355 | 363 |
| 356 bool GetFontSizeExtensionFunction::RunImpl() { | 364 bool GetFontPrefExtensionFunction::RunImpl() { |
| 357 PrefService* prefs = profile_->GetPrefs(); | 365 PrefService* prefs = profile_->GetPrefs(); |
| 358 int size = prefs->GetInteger(GetPrefName()); | 366 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); |
| 367 EXTENSION_FUNCTION_VALIDATE(pref); |
| 359 | 368 |
| 360 DictionaryValue* result = new DictionaryValue(); | 369 DictionaryValue* result = new DictionaryValue(); |
| 361 result->SetInteger(kPixelSizeKey, size); | 370 result->Set(GetKey(), pref->GetValue()->DeepCopy()); |
| 362 result_.reset(result); | 371 result_.reset(result); |
| 363 return true; | 372 return true; |
| 364 } | 373 } |
| 365 | 374 |
| 366 bool SetFontSizeExtensionFunction::RunImpl() { | 375 bool SetFontPrefExtensionFunction::RunImpl() { |
| 367 DictionaryValue* details = NULL; | 376 DictionaryValue* details = NULL; |
| 368 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 377 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 369 | 378 |
| 370 int size; | 379 Value* value; |
| 371 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kPixelSizeKey, &size)); | 380 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value)); |
| 372 | 381 |
| 373 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 382 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
| 374 prefs->SetExtensionControlledPref(extension_id(), | 383 prefs->SetExtensionControlledPref(extension_id(), |
| 375 GetPrefName(), | 384 GetPrefName(), |
| 376 kExtensionPrefsScopeRegular, | 385 kExtensionPrefsScopeRegular, |
| 377 Value::CreateIntegerValue(size)); | 386 value->DeepCopy()); |
| 378 return true; | 387 return true; |
| 379 } | 388 } |
| 380 | 389 |
| 381 const char* GetDefaultFontSizeFunction::GetPrefName() { | 390 const char* GetDefaultFontSizeFunction::GetPrefName() { |
| 382 return prefs::kWebKitGlobalDefaultFontSize; | 391 return prefs::kWebKitGlobalDefaultFontSize; |
| 383 } | 392 } |
| 384 | 393 |
| 394 const char* GetDefaultFontSizeFunction::GetKey() { |
| 395 return kPixelSizeKey; |
| 396 } |
| 397 |
| 385 const char* SetDefaultFontSizeFunction::GetPrefName() { | 398 const char* SetDefaultFontSizeFunction::GetPrefName() { |
| 386 return prefs::kWebKitGlobalDefaultFontSize; | 399 return prefs::kWebKitGlobalDefaultFontSize; |
| 387 } | 400 } |
| 388 | 401 |
| 402 const char* SetDefaultFontSizeFunction::GetKey() { |
| 403 return kPixelSizeKey; |
| 404 } |
| 405 |
| 389 const char* GetDefaultFixedFontSizeFunction::GetPrefName() { | 406 const char* GetDefaultFixedFontSizeFunction::GetPrefName() { |
| 390 return prefs::kWebKitGlobalDefaultFixedFontSize; | 407 return prefs::kWebKitGlobalDefaultFixedFontSize; |
| 391 } | 408 } |
| 392 | 409 |
| 410 const char* GetDefaultFixedFontSizeFunction::GetKey() { |
| 411 return kPixelSizeKey; |
| 412 } |
| 413 |
| 393 const char* SetDefaultFixedFontSizeFunction::GetPrefName() { | 414 const char* SetDefaultFixedFontSizeFunction::GetPrefName() { |
| 394 return prefs::kWebKitGlobalDefaultFixedFontSize; | 415 return prefs::kWebKitGlobalDefaultFixedFontSize; |
| 395 } | 416 } |
| 396 | 417 |
| 418 const char* SetDefaultFixedFontSizeFunction::GetKey() { |
| 419 return kPixelSizeKey; |
| 420 } |
| 421 |
| 397 const char* GetMinimumFontSizeFunction::GetPrefName() { | 422 const char* GetMinimumFontSizeFunction::GetPrefName() { |
| 398 return prefs::kWebKitGlobalMinimumFontSize; | 423 return prefs::kWebKitGlobalMinimumFontSize; |
| 399 } | 424 } |
| 400 | 425 |
| 426 const char* GetMinimumFontSizeFunction::GetKey() { |
| 427 return kPixelSizeKey; |
| 428 } |
| 429 |
| 401 const char* SetMinimumFontSizeFunction::GetPrefName() { | 430 const char* SetMinimumFontSizeFunction::GetPrefName() { |
| 402 return prefs::kWebKitGlobalMinimumFontSize; | 431 return prefs::kWebKitGlobalMinimumFontSize; |
| 403 } | 432 } |
| 433 |
| 434 const char* SetMinimumFontSizeFunction::GetKey() { |
| 435 return kPixelSizeKey; |
| 436 } |
| 437 |
| 438 const char* GetDefaultCharacterSetFunction::GetPrefName() { |
| 439 return prefs::kGlobalDefaultCharset; |
| 440 } |
| 441 |
| 442 const char* GetDefaultCharacterSetFunction::GetKey() { |
| 443 return kCharsetKey; |
| 444 } |
| 445 |
| 446 const char* SetDefaultCharacterSetFunction::GetPrefName() { |
| 447 return prefs::kGlobalDefaultCharset; |
| 448 } |
| 449 |
| 450 const char* SetDefaultCharacterSetFunction::GetKey() { |
| 451 return kCharsetKey; |
| 452 } |
| OLD | NEW |