| 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 = | 
 |  116       base::Bind(&FontSettingsEventRouter::OnFontFamilyMapPrefChanged, | 
 |  117                  base::Unretained(this)); | 
|  114   RegisterFontFamilyMapObserver(®istrar_, |  118   RegisterFontFamilyMapObserver(®istrar_, | 
|  115                                 prefs::kWebKitStandardFontFamilyMap, this); |  119                                 prefs::kWebKitStandardFontFamilyMap, callback); | 
|  116   RegisterFontFamilyMapObserver(®istrar_, |  120   RegisterFontFamilyMapObserver(®istrar_, | 
|  117                                 prefs::kWebKitSerifFontFamilyMap, this); |  121                                 prefs::kWebKitSerifFontFamilyMap, callback); | 
|  118   RegisterFontFamilyMapObserver(®istrar_, |  122   RegisterFontFamilyMapObserver(®istrar_, | 
|  119                                 prefs::kWebKitSansSerifFontFamilyMap, this); |  123                                 prefs::kWebKitSansSerifFontFamilyMap, callback); | 
|  120   RegisterFontFamilyMapObserver(®istrar_, |  124   RegisterFontFamilyMapObserver(®istrar_, | 
|  121                                 prefs::kWebKitFixedFontFamilyMap, this); |  125                                 prefs::kWebKitFixedFontFamilyMap, callback); | 
|  122   RegisterFontFamilyMapObserver(®istrar_, |  126   RegisterFontFamilyMapObserver(®istrar_, | 
|  123                                 prefs::kWebKitCursiveFontFamilyMap, this); |  127                                 prefs::kWebKitCursiveFontFamilyMap, callback); | 
|  124   RegisterFontFamilyMapObserver(®istrar_, |  128   RegisterFontFamilyMapObserver(®istrar_, | 
|  125                                 prefs::kWebKitFantasyFontFamilyMap, this); |  129                                 prefs::kWebKitFantasyFontFamilyMap, callback); | 
|  126   RegisterFontFamilyMapObserver(®istrar_, |  130   RegisterFontFamilyMapObserver(®istrar_, | 
|  127                                 prefs::kWebKitPictographFontFamilyMap, this); |  131                                 prefs::kWebKitPictographFontFamilyMap, | 
 |  132                                 callback); | 
|  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, this); |  140   registrar_.Add(pref_name, | 
|  136   pref_event_map_[pref_name] = std::make_pair(event_name, key); |  141                  base::Bind(&FontSettingsEventRouter::OnFontPrefChanged, | 
 |  142                             base::Unretained(this), | 
 |  143                             event_name, key)); | 
|  137 } |  144 } | 
|  138  |  145  | 
|  139 void FontSettingsEventRouter::OnPreferenceChanged( |  146 void FontSettingsEventRouter::OnFontFamilyMapPrefChanged( | 
|  140     PrefServiceBase* pref_service, |  | 
|  141     const std::string& pref_name) { |  147     const std::string& pref_name) { | 
|  142   bool incognito = (pref_service != profile_->GetPrefs()); |  | 
|  143   // We're only observing pref changes on the regular profile. |  | 
|  144   DCHECK(!incognito); |  | 
|  145  |  | 
|  146   PrefEventMap::iterator iter = pref_event_map_.find(pref_name); |  | 
|  147   if (iter != pref_event_map_.end()) { |  | 
|  148     const std::string& event_name = iter->second.first; |  | 
|  149     const std::string& key = iter->second.second; |  | 
|  150     OnFontPrefChanged(pref_service, pref_name, event_name, key, incognito); |  | 
|  151     return; |  | 
|  152   } |  | 
|  153  |  | 
|  154   std::string generic_family; |  148   std::string generic_family; | 
|  155   std::string script; |  149   std::string script; | 
|  156   if (pref_names_util::ParseFontNamePrefPath(pref_name, &generic_family, |  150   if (pref_names_util::ParseFontNamePrefPath(pref_name, &generic_family, | 
|  157                                              &script)) { |  151                                              &script)) { | 
|  158     OnFontNamePrefChanged(pref_service, pref_name, generic_family, script, |  152     OnFontNamePrefChanged(pref_name, generic_family, script); | 
|  159                           incognito); |  | 
|  160     return; |  153     return; | 
|  161   } |  154   } | 
|  162  |  155  | 
|  163   NOTREACHED(); |  156   NOTREACHED(); | 
|  164 } |  157 } | 
|  165  |  158  | 
|  166 void FontSettingsEventRouter::OnFontNamePrefChanged( |  159 void FontSettingsEventRouter::OnFontNamePrefChanged( | 
|  167     PrefServiceBase* pref_service, |  | 
|  168     const std::string& pref_name, |  160     const std::string& pref_name, | 
|  169     const std::string& generic_family, |  161     const std::string& generic_family, | 
|  170     const std::string& script, |  162     const std::string& script) { | 
|  171     bool incognito) { |  163   const PrefServiceBase::Preference* pref = registrar_.prefs()->FindPreference( | 
|  172   const PrefServiceBase::Preference* pref = pref_service->FindPreference( |  | 
|  173       pref_name.c_str()); |  164       pref_name.c_str()); | 
|  174   CHECK(pref); |  165   CHECK(pref); | 
|  175  |  166  | 
|  176   std::string font_name; |  167   std::string font_name; | 
|  177   if (!pref->GetValue()->GetAsString(&font_name)) { |  168   if (!pref->GetValue()->GetAsString(&font_name)) { | 
|  178     NOTREACHED(); |  169     NOTREACHED(); | 
|  179     return; |  170     return; | 
|  180   } |  171   } | 
|  181   font_name = MaybeGetLocalizedFontName(font_name); |  172   font_name = MaybeGetLocalizedFontName(font_name); | 
|  182  |  173  | 
|  183   ListValue args; |  174   ListValue args; | 
|  184   DictionaryValue* dict = new DictionaryValue(); |  175   DictionaryValue* dict = new DictionaryValue(); | 
|  185   args.Append(dict); |  176   args.Append(dict); | 
|  186   dict->SetString(kFontIdKey, font_name); |  177   dict->SetString(kFontIdKey, font_name); | 
|  187   dict->SetString(kGenericFamilyKey, generic_family); |  178   dict->SetString(kGenericFamilyKey, generic_family); | 
|  188   dict->SetString(kScriptKey, script); |  179   dict->SetString(kScriptKey, script); | 
|  189  |  180  | 
|  190   extensions::preference_helpers::DispatchEventToExtensions( |  181   extensions::preference_helpers::DispatchEventToExtensions( | 
|  191       profile_, |  182       profile_, | 
|  192       kOnFontChanged, |  183       kOnFontChanged, | 
|  193       &args, |  184       &args, | 
|  194       APIPermission::kFontSettings, |  185       APIPermission::kFontSettings, | 
|  195       incognito, |  186       false, | 
|  196       pref_name); |  187       pref_name); | 
|  197 } |  188 } | 
|  198  |  189  | 
|  199 void FontSettingsEventRouter::OnFontPrefChanged( |  190 void FontSettingsEventRouter::OnFontPrefChanged( | 
|  200     PrefServiceBase* pref_service, |  | 
|  201     const std::string& pref_name, |  | 
|  202     const std::string& event_name, |  191     const std::string& event_name, | 
|  203     const std::string& key, |  192     const std::string& key, | 
|  204     bool incognito) { |  193     const std::string& pref_name) { | 
|  205   const PrefServiceBase::Preference* pref = pref_service->FindPreference( |  194   const PrefServiceBase::Preference* pref = registrar_.prefs()->FindPreference( | 
|  206       pref_name.c_str()); |  195       pref_name.c_str()); | 
|  207   CHECK(pref); |  196   CHECK(pref); | 
|  208  |  197  | 
|  209   ListValue args; |  198   ListValue args; | 
|  210   DictionaryValue* dict = new DictionaryValue(); |  199   DictionaryValue* dict = new DictionaryValue(); | 
|  211   args.Append(dict); |  200   args.Append(dict); | 
|  212   dict->Set(key, pref->GetValue()->DeepCopy()); |  201   dict->Set(key, pref->GetValue()->DeepCopy()); | 
|  213  |  202  | 
|  214   extensions::preference_helpers::DispatchEventToExtensions( |  203   extensions::preference_helpers::DispatchEventToExtensions( | 
|  215       profile_, |  204       profile_, | 
|  216       event_name, |  205       event_name, | 
|  217       &args, |  206       &args, | 
|  218       APIPermission::kFontSettings, |  207       APIPermission::kFontSettings, | 
|  219       incognito, |  208       false, | 
|  220       pref_name); |  209       pref_name); | 
|  221 } |  210 } | 
|  222  |  211  | 
|  223 bool ClearFontFunction::RunImpl() { |  212 bool ClearFontFunction::RunImpl() { | 
|  224   if (profile_->IsOffTheRecord()) { |  213   if (profile_->IsOffTheRecord()) { | 
|  225     error_ = kSetFromIncognitoError; |  214     error_ = kSetFromIncognitoError; | 
|  226     return false; |  215     return false; | 
|  227   } |  216   } | 
|  228  |  217  | 
|  229   scoped_ptr<fonts::ClearFont::Params> params( |  218   scoped_ptr<fonts::ClearFont::Params> params( | 
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  454  |  443  | 
|  455 const char* SetMinimumFontSizeFunction::GetPrefName() { |  444 const char* SetMinimumFontSizeFunction::GetPrefName() { | 
|  456   return prefs::kWebKitMinimumFontSize; |  445   return prefs::kWebKitMinimumFontSize; | 
|  457 } |  446 } | 
|  458  |  447  | 
|  459 const char* SetMinimumFontSizeFunction::GetKey() { |  448 const char* SetMinimumFontSizeFunction::GetKey() { | 
|  460   return kPixelSizeKey; |  449   return kPixelSizeKey; | 
|  461 } |  450 } | 
|  462  |  451  | 
|  463 }  // namespace extensions |  452 }  // namespace extensions | 
| OLD | NEW |