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/ui/prefs/prefs_tab_helper.h" | 5 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/prefs/overlay_user_pref_store.h" | 9 #include "base/prefs/overlay_user_pref_store.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 #endif // !defined(OS_ANDROID) | 124 #endif // !defined(OS_ANDROID) |
125 | 125 |
126 // Registers |obs| to observe per-script font prefs under the path |map_name|. | 126 // Registers |obs| to observe per-script font prefs under the path |map_name|. |
127 // On android, there's no exposed way to change these prefs, so we can save | 127 // On android, there's no exposed way to change these prefs, so we can save |
128 // ~715KB of heap and some startup cycles by avoiding observing these prefs | 128 // ~715KB of heap and some startup cycles by avoiding observing these prefs |
129 // since they will never change. | 129 // since they will never change. |
130 void RegisterFontFamilyMapObserver( | 130 void RegisterFontFamilyMapObserver( |
131 PrefChangeRegistrar* registrar, | 131 PrefChangeRegistrar* registrar, |
132 const char* map_name, | 132 const char* map_name, |
133 const PrefChangeRegistrar::NamedChangeCallback& obs) { | 133 const PrefChangeRegistrar::NamedChangeCallback& obs) { |
134 bool result = StartsWithASCII(map_name, "webkit.webprefs.", true); | 134 DCHECK(StartsWithASCII(map_name, "webkit.webprefs.", true)); |
135 DCHECK(result); | 135 |
| 136 std::string pref_name; |
| 137 pref_name.reserve(512); // Reduces number of grows. |
136 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { | 138 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
137 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; | 139 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
138 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); | 140 pref_name.assign(map_name); |
139 registrar->Add(pref_name.c_str(), obs); | 141 pref_name.append(1, '.'); |
| 142 pref_name.append(script); |
| 143 registrar->Add(pref_name, obs); |
140 } | 144 } |
141 } | 145 } |
142 | 146 |
143 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
144 // On Windows with antialising we want to use an alternate fixed font like | 148 // On Windows with antialising we want to use an alternate fixed font like |
145 // Consolas, which looks much better than Courier New. | 149 // Consolas, which looks much better than Courier New. |
146 bool ShouldUseAlternateDefaultFixedFont(const std::string& script) { | 150 bool ShouldUseAlternateDefaultFixedFont(const std::string& script) { |
147 if (!StartsWithASCII(script, "courier", false)) | 151 if (!StartsWithASCII(script, "courier", false)) |
148 return false; | 152 return false; |
149 UINT smooth_type = 0; | 153 UINT smooth_type = 0; |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 // is normally okay since WebKit does the desired fallback behavior regardless | 629 // is normally okay since WebKit does the desired fallback behavior regardless |
626 // of whether the empty string is passed or the pref is not passed at all. But | 630 // of whether the empty string is passed or the pref is not passed at all. But |
627 // if the pref has changed from non-empty to the empty string, we must let | 631 // if the pref has changed from non-empty to the empty string, we must let |
628 // WebKit know. | 632 // WebKit know. |
629 std::string generic_family; | 633 std::string generic_family; |
630 std::string script; | 634 std::string script; |
631 if (pref_names_util::ParseFontNamePrefPath(pref_name, | 635 if (pref_names_util::ParseFontNamePrefPath(pref_name, |
632 &generic_family, | 636 &generic_family, |
633 &script)) { | 637 &script)) { |
634 PrefService* prefs = GetProfile()->GetPrefs(); | 638 PrefService* prefs = GetProfile()->GetPrefs(); |
635 std::string pref_value = prefs->GetString(pref_name.c_str()); | 639 std::string pref_value = prefs->GetString(pref_name); |
636 if (pref_value.empty()) { | 640 if (pref_value.empty()) { |
637 WebPreferences web_prefs = | 641 WebPreferences web_prefs = |
638 web_contents_->GetRenderViewHost()->GetWebkitPreferences(); | 642 web_contents_->GetRenderViewHost()->GetWebkitPreferences(); |
639 OverrideFontFamily(&web_prefs, generic_family, script, std::string()); | 643 OverrideFontFamily(&web_prefs, generic_family, script, std::string()); |
640 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs); | 644 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs); |
641 return; | 645 return; |
642 } | 646 } |
643 } | 647 } |
644 } | 648 } |
645 | 649 |
646 void PrefsTabHelper::OnWebPrefChanged(const std::string& pref_name) { | 650 void PrefsTabHelper::OnWebPrefChanged(const std::string& pref_name) { |
647 #if !defined(OS_ANDROID) | 651 #if !defined(OS_ANDROID) |
648 OnFontFamilyPrefChanged(pref_name); | 652 OnFontFamilyPrefChanged(pref_name); |
649 #endif | 653 #endif |
650 | 654 |
651 web_contents_->GetRenderViewHost()->OnWebkitPreferencesChanged(); | 655 web_contents_->GetRenderViewHost()->OnWebkitPreferencesChanged(); |
652 } | 656 } |
OLD | NEW |