Chromium Code Reviews| 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" | 
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 if (!font_name.empty()) { | 78 if (!font_name.empty()) { | 
| 79 gfx::Font font(font_name, 12); // dummy font size | 79 gfx::Font font(font_name, 12); // dummy font size | 
| 80 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 80 return static_cast<gfx::PlatformFontWin*>(font.platform_font())-> | 
| 81 GetLocalizedFontName(); | 81 GetLocalizedFontName(); | 
| 82 } | 82 } | 
| 83 #endif | 83 #endif | 
| 84 return font_name; | 84 return font_name; | 
| 85 } | 85 } | 
| 86 | 86 | 
| 87 // Registers |obs| to observe per-script font prefs under the path |map_name|. | 87 // Registers |obs| to observe per-script font prefs under the path |map_name|. | 
| 88 void RegisterFontFamilyMapObserver(PrefChangeRegistrar* registrar, | 88 void RegisterFontFamilyMapObserver( | 
| 89 const char* map_name, | 89 PrefChangeRegistrar* registrar, | 
| 90 PrefObserver* obs) { | 90 const char* map_name, | 
| 91 const PrefChangeRegistrar::NamedChangeCallback& callback) { | |
| 91 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 92 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 
| 92 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 93 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 
| 93 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 94 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 
| 94 registrar->Add(pref_name.c_str(), obs); | 95 registrar->Add(pref_name.c_str(), callback); | 
| 95 } | 96 } | 
| 96 } | 97 } | 
| 97 | 98 | 
| 98 } // namespace | 99 } // namespace | 
| 99 | 100 | 
| 100 FontSettingsEventRouter::FontSettingsEventRouter( | 101 FontSettingsEventRouter::FontSettingsEventRouter( | 
| 101 Profile* profile) : profile_(profile) { | 102 Profile* profile) : profile_(profile) { | 
| 102 registrar_.Init(profile_->GetPrefs()); | 103 registrar_.Init(profile_->GetPrefs()); | 
| 103 | 104 | 
| 104 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, | 105 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize, | 
| 105 kOnDefaultFixedFontSizeChanged, | 106 kOnDefaultFixedFontSizeChanged, | 
| 106 kPixelSizeKey); | 107 kPixelSizeKey); | 
| 107 AddPrefToObserve(prefs::kWebKitDefaultFontSize, | 108 AddPrefToObserve(prefs::kWebKitDefaultFontSize, | 
| 108 kOnDefaultFontSizeChanged, | 109 kOnDefaultFontSizeChanged, | 
| 109 kPixelSizeKey); | 110 kPixelSizeKey); | 
| 110 AddPrefToObserve(prefs::kWebKitMinimumFontSize, | 111 AddPrefToObserve(prefs::kWebKitMinimumFontSize, | 
| 111 kOnMinimumFontSizeChanged, | 112 kOnMinimumFontSizeChanged, | 
| 112 kPixelSizeKey); | 113 kPixelSizeKey); | 
| 113 | 114 | 
| 115 PrefChangeRegistrar::NamedChangeCallback callback = base::Bind( | |
| 116 &FontSettingsEventRouter::OnPreferenceChanged, base::Unretained(this)); | |
| 114 RegisterFontFamilyMapObserver(®istrar_, | 117 RegisterFontFamilyMapObserver(®istrar_, | 
| 115 prefs::kWebKitStandardFontFamilyMap, this); | 118 prefs::kWebKitStandardFontFamilyMap, callback); | 
| 116 RegisterFontFamilyMapObserver(®istrar_, | 119 RegisterFontFamilyMapObserver(®istrar_, | 
| 117 prefs::kWebKitSerifFontFamilyMap, this); | 120 prefs::kWebKitSerifFontFamilyMap, callback); | 
| 118 RegisterFontFamilyMapObserver(®istrar_, | 121 RegisterFontFamilyMapObserver(®istrar_, | 
| 119 prefs::kWebKitSansSerifFontFamilyMap, this); | 122 prefs::kWebKitSansSerifFontFamilyMap, callback); | 
| 120 RegisterFontFamilyMapObserver(®istrar_, | 123 RegisterFontFamilyMapObserver(®istrar_, | 
| 121 prefs::kWebKitFixedFontFamilyMap, this); | 124 prefs::kWebKitFixedFontFamilyMap, callback); | 
| 122 RegisterFontFamilyMapObserver(®istrar_, | 125 RegisterFontFamilyMapObserver(®istrar_, | 
| 123 prefs::kWebKitCursiveFontFamilyMap, this); | 126 prefs::kWebKitCursiveFontFamilyMap, callback); | 
| 124 RegisterFontFamilyMapObserver(®istrar_, | 127 RegisterFontFamilyMapObserver(®istrar_, | 
| 125 prefs::kWebKitFantasyFontFamilyMap, this); | 128 prefs::kWebKitFantasyFontFamilyMap, callback); | 
| 126 RegisterFontFamilyMapObserver(®istrar_, | 129 RegisterFontFamilyMapObserver(®istrar_, | 
| 127 prefs::kWebKitPictographFontFamilyMap, this); | 130 prefs::kWebKitPictographFontFamilyMap, | 
| 131 callback); | |
| 
 
Mattias Nissler (ping if slow)
2012/11/13 15:09:49
All these could bind directly to the second half o
 
Jói
2012/11/13 15:16:45
They could, but we would lose the DCHECK at the en
 
 | |
| 128 } | 132 } | 
| 129 | 133 | 
| 130 FontSettingsEventRouter::~FontSettingsEventRouter() {} | 134 FontSettingsEventRouter::~FontSettingsEventRouter() {} | 
| 131 | 135 | 
| 132 void FontSettingsEventRouter::AddPrefToObserve(const char* pref_name, | 136 void FontSettingsEventRouter::AddPrefToObserve(const char* pref_name, | 
| 133 const char* event_name, | 137 const char* event_name, | 
| 134 const char* key) { | 138 const char* key) { | 
| 135 registrar_.Add(pref_name, this); | 139 registrar_.Add(pref_name, | 
| 140 base::Bind(&FontSettingsEventRouter::OnPreferenceChanged, | |
| 141 base::Unretained(this))); | |
| 136 pref_event_map_[pref_name] = std::make_pair(event_name, key); | 142 pref_event_map_[pref_name] = std::make_pair(event_name, key); | 
| 
 
Mattias Nissler (ping if slow)
2012/11/13 15:09:49
it seems weird to keep this separate map and the l
 
Jói
2012/11/13 15:16:45
I think you're probably right.  Am looping in falk
 
falken
2012/11/14 03:35:31
I think it's a good idea to bind directly to OnFon
 
 | |
| 137 } | 143 } | 
| 138 | 144 | 
| 139 void FontSettingsEventRouter::OnPreferenceChanged( | 145 void FontSettingsEventRouter::OnPreferenceChanged( | 
| 140 PrefServiceBase* pref_service, | |
| 141 const std::string& pref_name) { | 146 const std::string& pref_name) { | 
| 142 bool incognito = (pref_service != profile_->GetPrefs()); | 147 bool incognito = (registrar_.prefs() != profile_->GetPrefs()); | 
| 143 // We're only observing pref changes on the regular profile. | 148 // We're only observing pref changes on the regular profile. | 
| 144 DCHECK(!incognito); | 149 DCHECK(!incognito); | 
| 145 | 150 | 
| 146 PrefEventMap::iterator iter = pref_event_map_.find(pref_name); | 151 PrefEventMap::iterator iter = pref_event_map_.find(pref_name); | 
| 147 if (iter != pref_event_map_.end()) { | 152 if (iter != pref_event_map_.end()) { | 
| 148 const std::string& event_name = iter->second.first; | 153 const std::string& event_name = iter->second.first; | 
| 149 const std::string& key = iter->second.second; | 154 const std::string& key = iter->second.second; | 
| 150 OnFontPrefChanged(pref_service, pref_name, event_name, key, incognito); | 155 OnFontPrefChanged(pref_name, event_name, key, incognito); | 
| 151 return; | 156 return; | 
| 152 } | 157 } | 
| 153 | 158 | 
| 154 std::string generic_family; | 159 std::string generic_family; | 
| 155 std::string script; | 160 std::string script; | 
| 156 if (pref_names_util::ParseFontNamePrefPath(pref_name, &generic_family, | 161 if (pref_names_util::ParseFontNamePrefPath(pref_name, &generic_family, | 
| 157 &script)) { | 162 &script)) { | 
| 158 OnFontNamePrefChanged(pref_service, pref_name, generic_family, script, | 163 OnFontNamePrefChanged(pref_name, generic_family, script, incognito); | 
| 159 incognito); | |
| 160 return; | 164 return; | 
| 161 } | 165 } | 
| 162 | 166 | 
| 163 NOTREACHED(); | 167 NOTREACHED(); | 
| 164 } | 168 } | 
| 165 | 169 | 
| 166 void FontSettingsEventRouter::OnFontNamePrefChanged( | 170 void FontSettingsEventRouter::OnFontNamePrefChanged( | 
| 167 PrefServiceBase* pref_service, | |
| 168 const std::string& pref_name, | 171 const std::string& pref_name, | 
| 169 const std::string& generic_family, | 172 const std::string& generic_family, | 
| 170 const std::string& script, | 173 const std::string& script, | 
| 171 bool incognito) { | 174 bool incognito) { | 
| 172 const PrefServiceBase::Preference* pref = pref_service->FindPreference( | 175 const PrefServiceBase::Preference* pref = registrar_.prefs()->FindPreference( | 
| 173 pref_name.c_str()); | 176 pref_name.c_str()); | 
| 174 CHECK(pref); | 177 CHECK(pref); | 
| 175 | 178 | 
| 176 std::string font_name; | 179 std::string font_name; | 
| 177 if (!pref->GetValue()->GetAsString(&font_name)) { | 180 if (!pref->GetValue()->GetAsString(&font_name)) { | 
| 178 NOTREACHED(); | 181 NOTREACHED(); | 
| 179 return; | 182 return; | 
| 180 } | 183 } | 
| 181 font_name = MaybeGetLocalizedFontName(font_name); | 184 font_name = MaybeGetLocalizedFontName(font_name); | 
| 182 | 185 | 
| 183 ListValue args; | 186 ListValue args; | 
| 184 DictionaryValue* dict = new DictionaryValue(); | 187 DictionaryValue* dict = new DictionaryValue(); | 
| 185 args.Append(dict); | 188 args.Append(dict); | 
| 186 dict->SetString(kFontIdKey, font_name); | 189 dict->SetString(kFontIdKey, font_name); | 
| 187 dict->SetString(kGenericFamilyKey, generic_family); | 190 dict->SetString(kGenericFamilyKey, generic_family); | 
| 188 dict->SetString(kScriptKey, script); | 191 dict->SetString(kScriptKey, script); | 
| 189 | 192 | 
| 190 extensions::preference_helpers::DispatchEventToExtensions( | 193 extensions::preference_helpers::DispatchEventToExtensions( | 
| 191 profile_, | 194 profile_, | 
| 192 kOnFontChanged, | 195 kOnFontChanged, | 
| 193 &args, | 196 &args, | 
| 194 APIPermission::kFontSettings, | 197 APIPermission::kFontSettings, | 
| 195 incognito, | 198 incognito, | 
| 196 pref_name); | 199 pref_name); | 
| 197 } | 200 } | 
| 198 | 201 | 
| 199 void FontSettingsEventRouter::OnFontPrefChanged( | 202 void FontSettingsEventRouter::OnFontPrefChanged( | 
| 200 PrefServiceBase* pref_service, | |
| 201 const std::string& pref_name, | 203 const std::string& pref_name, | 
| 202 const std::string& event_name, | 204 const std::string& event_name, | 
| 203 const std::string& key, | 205 const std::string& key, | 
| 204 bool incognito) { | 206 bool incognito) { | 
| 205 const PrefServiceBase::Preference* pref = pref_service->FindPreference( | 207 const PrefServiceBase::Preference* pref = registrar_.prefs()->FindPreference( | 
| 206 pref_name.c_str()); | 208 pref_name.c_str()); | 
| 207 CHECK(pref); | 209 CHECK(pref); | 
| 208 | 210 | 
| 209 ListValue args; | 211 ListValue args; | 
| 210 DictionaryValue* dict = new DictionaryValue(); | 212 DictionaryValue* dict = new DictionaryValue(); | 
| 211 args.Append(dict); | 213 args.Append(dict); | 
| 212 dict->Set(key, pref->GetValue()->DeepCopy()); | 214 dict->Set(key, pref->GetValue()->DeepCopy()); | 
| 213 | 215 | 
| 214 extensions::preference_helpers::DispatchEventToExtensions( | 216 extensions::preference_helpers::DispatchEventToExtensions( | 
| 215 profile_, | 217 profile_, | 
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 | 456 | 
| 455 const char* SetMinimumFontSizeFunction::GetPrefName() { | 457 const char* SetMinimumFontSizeFunction::GetPrefName() { | 
| 456 return prefs::kWebKitMinimumFontSize; | 458 return prefs::kWebKitMinimumFontSize; | 
| 457 } | 459 } | 
| 458 | 460 | 
| 459 const char* SetMinimumFontSizeFunction::GetKey() { | 461 const char* SetMinimumFontSizeFunction::GetKey() { | 
| 460 return kPixelSizeKey; | 462 return kPixelSizeKey; | 
| 461 } | 463 } | 
| 462 | 464 | 
| 463 } // namespace extensions | 465 } // namespace extensions | 
| OLD | NEW |