Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / font_smoothing_win.cc
bloba4be72930a4365e1dc4c5c6fac15337defa49239
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
3 // found in the LICENSE file.
5 #include "ui/gfx/font_smoothing_win.h"
7 #include "base/memory/singleton.h"
8 #include "ui/base/win/singleton_hwnd.h"
10 namespace {
12 // Helper class to cache font smoothing settings and listen for notifications
13 // to re-query them from the system.
14 class CachedFontSmoothingSettings : public ui::SingletonHwnd::Observer {
15 public:
16 static CachedFontSmoothingSettings* GetInstance();
18 // Returns the cached Windows font smoothing settings. Queries the settings
19 // via Windows APIs and begins listening for changes when called for the
20 // first time.
21 void GetFontSmoothingSettings(bool* smoothing_enabled,
22 bool* cleartype_enabled);
24 private:
25 friend struct DefaultSingletonTraits<CachedFontSmoothingSettings>;
27 CachedFontSmoothingSettings();
28 virtual ~CachedFontSmoothingSettings();
30 // Listener for WM_SETTINGCHANGE notifications.
31 virtual void OnWndProc(HWND hwnd,
32 UINT message,
33 WPARAM wparam,
34 LPARAM lparam) OVERRIDE;
36 // Queries the font settings from the system.
37 void QueryFontSettings();
39 // Indicates whether the MessagePumpObserver has been registered.
40 bool observer_added_;
42 // Indicates whether |smoothing_enabled_| and |cleartype_enabled_| are valid
43 // or need to be re-queried from the system.
44 bool need_to_query_settings_;
46 // Indicates that font smoothing is enabled.
47 bool smoothing_enabled_;
49 // Indicates that the ClearType font smoothing is enabled.
50 bool cleartype_enabled_;
52 DISALLOW_COPY_AND_ASSIGN(CachedFontSmoothingSettings);
55 // static
56 CachedFontSmoothingSettings* CachedFontSmoothingSettings::GetInstance() {
57 return Singleton<CachedFontSmoothingSettings>::get();
60 void CachedFontSmoothingSettings::GetFontSmoothingSettings(
61 bool* smoothing_enabled,
62 bool* cleartype_enabled) {
63 // If cached settings are stale, query them from the OS.
64 if (need_to_query_settings_) {
65 QueryFontSettings();
66 need_to_query_settings_ = false;
68 if (!observer_added_) {
69 ui::SingletonHwnd::GetInstance()->AddObserver(this);
70 observer_added_ = true;
72 *smoothing_enabled = smoothing_enabled_;
73 *cleartype_enabled = cleartype_enabled_;
76 CachedFontSmoothingSettings::CachedFontSmoothingSettings()
77 : observer_added_(false),
78 need_to_query_settings_(true),
79 smoothing_enabled_(false),
80 cleartype_enabled_(false) {
83 CachedFontSmoothingSettings::~CachedFontSmoothingSettings() {
84 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
85 // been destroyed already (both singletons).
88 void CachedFontSmoothingSettings::OnWndProc(HWND hwnd,
89 UINT message,
90 WPARAM wparam,
91 LPARAM lparam) {
92 if (message == WM_SETTINGCHANGE)
93 need_to_query_settings_ = true;
96 void CachedFontSmoothingSettings::QueryFontSettings() {
97 smoothing_enabled_ = false;
98 cleartype_enabled_ = false;
100 BOOL enabled = false;
101 if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) {
102 smoothing_enabled_ = true;
104 UINT smooth_type = 0;
105 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smooth_type, 0))
106 cleartype_enabled_ = (smooth_type == FE_FONTSMOOTHINGCLEARTYPE);
110 } // namespace
112 namespace gfx {
114 void GetCachedFontSmoothingSettings(bool* smoothing_enabled,
115 bool* cleartype_enabled) {
116 CachedFontSmoothingSettings::GetInstance()->GetFontSmoothingSettings(
117 smoothing_enabled,
118 cleartype_enabled);
121 } // namespace gfx