OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/dom_distiller/core/distilled_page_prefs.h" | 5 #include "components/dom_distiller/core/distilled_page_prefs.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "components/pref_registry/pref_registry_syncable.h" | 12 #include "components/pref_registry/pref_registry_syncable.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // Path to the integer corresponding to user's preference theme. | 16 // Path to the integer corresponding to user's preference theme. |
| 17 const char kFontPref[] = "dom_distiller.font_family"; |
| 18 // Path to the integer corresponding to user's preference font family. |
17 const char kThemePref[] = "dom_distiller.theme"; | 19 const char kThemePref[] = "dom_distiller.theme"; |
18 } | 20 } |
19 | 21 |
20 namespace dom_distiller { | 22 namespace dom_distiller { |
21 | 23 |
22 DistilledPagePrefs::DistilledPagePrefs(PrefService* pref_service) | 24 DistilledPagePrefs::DistilledPagePrefs(PrefService* pref_service) |
23 : pref_service_(pref_service), weak_ptr_factory_(this) { | 25 : pref_service_(pref_service), weak_ptr_factory_(this) { |
24 } | 26 } |
25 | 27 |
26 DistilledPagePrefs::~DistilledPagePrefs() { | 28 DistilledPagePrefs::~DistilledPagePrefs() { |
27 } | 29 } |
28 | 30 |
29 // static | 31 // static |
30 void DistilledPagePrefs::RegisterProfilePrefs( | 32 void DistilledPagePrefs::RegisterProfilePrefs( |
31 user_prefs::PrefRegistrySyncable* registry) { | 33 user_prefs::PrefRegistrySyncable* registry) { |
32 registry->RegisterIntegerPref( | 34 registry->RegisterIntegerPref( |
33 kThemePref, | 35 kThemePref, |
34 DistilledPagePrefs::LIGHT, | 36 DistilledPagePrefs::LIGHT, |
35 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 37 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 38 registry->RegisterIntegerPref( |
| 39 kFontPref, |
| 40 DistilledPagePrefs::SANS_SERIF, |
| 41 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 42 } |
| 43 |
| 44 void DistilledPagePrefs::SetFontFamily( |
| 45 DistilledPagePrefs::FontFamily new_font_family) { |
| 46 pref_service_->SetInteger(kFontPref, new_font_family); |
| 47 base::MessageLoop::current()->PostTask( |
| 48 FROM_HERE, |
| 49 base::Bind(&DistilledPagePrefs::NotifyOnChangeFontFamily, |
| 50 weak_ptr_factory_.GetWeakPtr(), |
| 51 new_font_family)); |
| 52 } |
| 53 |
| 54 DistilledPagePrefs::FontFamily DistilledPagePrefs::GetFontFamily() { |
| 55 int font_family = pref_service_->GetInteger(kFontPref); |
| 56 if (font_family < 0 || font_family >= DistilledPagePrefs::FONT_FAMILY_COUNT) { |
| 57 // Persisted data was incorrect, trying to clean it up by storing the |
| 58 // default. |
| 59 SetFontFamily(DistilledPagePrefs::SANS_SERIF); |
| 60 return DistilledPagePrefs::SANS_SERIF; |
| 61 } |
| 62 return static_cast<FontFamily>(font_family); |
36 } | 63 } |
37 | 64 |
38 void DistilledPagePrefs::SetTheme(DistilledPagePrefs::Theme new_theme) { | 65 void DistilledPagePrefs::SetTheme(DistilledPagePrefs::Theme new_theme) { |
39 pref_service_->SetInteger(kThemePref, new_theme); | 66 pref_service_->SetInteger(kThemePref, new_theme); |
40 base::MessageLoop::current()->PostTask( | 67 base::MessageLoop::current()->PostTask( |
41 FROM_HERE, | 68 FROM_HERE, |
42 base::Bind(&DistilledPagePrefs::NotifyOnChangeTheme, | 69 base::Bind(&DistilledPagePrefs::NotifyOnChangeTheme, |
43 weak_ptr_factory_.GetWeakPtr(), | 70 weak_ptr_factory_.GetWeakPtr(), |
44 new_theme)); | 71 new_theme)); |
45 } | 72 } |
46 | 73 |
47 DistilledPagePrefs::Theme DistilledPagePrefs::GetTheme() { | 74 DistilledPagePrefs::Theme DistilledPagePrefs::GetTheme() { |
48 int theme = pref_service_->GetInteger(kThemePref); | 75 int theme = pref_service_->GetInteger(kThemePref); |
49 if (theme < 0 || theme >= DistilledPagePrefs::THEME_COUNT) { | 76 if (theme < 0 || theme >= DistilledPagePrefs::THEME_COUNT) { |
50 // Persisted data was incorrect, trying to clean it up by storing the | 77 // Persisted data was incorrect, trying to clean it up by storing the |
51 // default. | 78 // default. |
52 SetTheme(DistilledPagePrefs::LIGHT); | 79 SetTheme(DistilledPagePrefs::LIGHT); |
53 return DistilledPagePrefs::LIGHT; | 80 return DistilledPagePrefs::LIGHT; |
54 } | 81 } |
55 return (Theme) theme; | 82 return static_cast<Theme>(theme); |
56 } | 83 } |
57 | 84 |
58 void DistilledPagePrefs::AddObserver(Observer* obs) { | 85 void DistilledPagePrefs::AddObserver(Observer* obs) { |
59 observers_.AddObserver(obs); | 86 observers_.AddObserver(obs); |
60 } | 87 } |
61 | 88 |
62 void DistilledPagePrefs::RemoveObserver(Observer* obs) { | 89 void DistilledPagePrefs::RemoveObserver(Observer* obs) { |
63 observers_.RemoveObserver(obs); | 90 observers_.RemoveObserver(obs); |
64 } | 91 } |
65 | 92 |
| 93 void DistilledPagePrefs::NotifyOnChangeFontFamily( |
| 94 DistilledPagePrefs::FontFamily new_font_family) { |
| 95 FOR_EACH_OBSERVER(Observer, observers_, OnChangeFontFamily(new_font_family)); |
| 96 } |
| 97 |
66 void DistilledPagePrefs::NotifyOnChangeTheme( | 98 void DistilledPagePrefs::NotifyOnChangeTheme( |
67 DistilledPagePrefs::Theme new_theme) { | 99 DistilledPagePrefs::Theme new_theme) { |
68 FOR_EACH_OBSERVER(Observer, observers_, OnChangeTheme(new_theme)); | 100 FOR_EACH_OBSERVER(Observer, observers_, OnChangeTheme(new_theme)); |
69 } | 101 } |
70 | 102 |
71 } // namespace dom_distiller | 103 } // namespace dom_distiller |
OLD | NEW |