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
3 // found in the LICENSE file.
5 #include "components/dom_distiller/core/distilled_page_prefs.h"
8 #include "base/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
18 // Path to the integer corresponding to user's preference theme.
19 const char kFontPref
[] = "dom_distiller.font_family";
20 // Path to the integer corresponding to user's preference font family.
21 const char kThemePref
[] = "dom_distiller.theme";
24 namespace dom_distiller
{
26 DistilledPagePrefs::DistilledPagePrefs(PrefService
* pref_service
)
27 : pref_service_(pref_service
), weak_ptr_factory_(this) {
30 DistilledPagePrefs::~DistilledPagePrefs() {
34 void DistilledPagePrefs::RegisterProfilePrefs(
35 user_prefs::PrefRegistrySyncable
* registry
) {
36 registry
->RegisterIntegerPref(
38 DistilledPagePrefs::LIGHT
,
39 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
40 registry
->RegisterIntegerPref(
42 DistilledPagePrefs::SANS_SERIF
,
43 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
46 void DistilledPagePrefs::SetFontFamily(
47 DistilledPagePrefs::FontFamily new_font_family
) {
48 pref_service_
->SetInteger(kFontPref
, new_font_family
);
49 base::ThreadTaskRunnerHandle::Get()->PostTask(
50 FROM_HERE
, base::Bind(&DistilledPagePrefs::NotifyOnChangeFontFamily
,
51 weak_ptr_factory_
.GetWeakPtr(), new_font_family
));
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
59 SetFontFamily(DistilledPagePrefs::SANS_SERIF
);
60 return DistilledPagePrefs::SANS_SERIF
;
62 return static_cast<FontFamily
>(font_family
);
65 void DistilledPagePrefs::SetTheme(DistilledPagePrefs::Theme new_theme
) {
66 pref_service_
->SetInteger(kThemePref
, new_theme
);
67 base::ThreadTaskRunnerHandle::Get()->PostTask(
68 FROM_HERE
, base::Bind(&DistilledPagePrefs::NotifyOnChangeTheme
,
69 weak_ptr_factory_
.GetWeakPtr(), new_theme
));
72 DistilledPagePrefs::Theme
DistilledPagePrefs::GetTheme() {
73 int theme
= pref_service_
->GetInteger(kThemePref
);
74 if (theme
< 0 || theme
>= DistilledPagePrefs::THEME_COUNT
) {
75 // Persisted data was incorrect, trying to clean it up by storing the
77 SetTheme(DistilledPagePrefs::LIGHT
);
78 return DistilledPagePrefs::LIGHT
;
80 return static_cast<Theme
>(theme
);
83 void DistilledPagePrefs::AddObserver(Observer
* obs
) {
84 observers_
.AddObserver(obs
);
87 void DistilledPagePrefs::RemoveObserver(Observer
* obs
) {
88 observers_
.RemoveObserver(obs
);
91 void DistilledPagePrefs::NotifyOnChangeFontFamily(
92 DistilledPagePrefs::FontFamily new_font_family
) {
93 FOR_EACH_OBSERVER(Observer
, observers_
, OnChangeFontFamily(new_font_family
));
96 void DistilledPagePrefs::NotifyOnChangeTheme(
97 DistilledPagePrefs::Theme new_theme
) {
98 FOR_EACH_OBSERVER(Observer
, observers_
, OnChangeTheme(new_theme
));
101 } // namespace dom_distiller