OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/dom_distiller/core/distilled_page_prefs.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/observer_list.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "components/pref_registry/pref_registry_syncable.h" | |
13 | |
14 namespace { | |
15 | |
16 // Path to the integer corresponding to user's preference theme. | |
17 const char kThemePref[] = "dom_distiller.theme"; | |
18 | |
19 } | |
20 | |
21 namespace dom_distiller { | |
22 | |
23 DistilledPagePrefs::DistilledPagePrefs(PrefService* pref_service) | |
24 : pref_service_(pref_service), | |
25 weak_ptr_factory_(this) { | |
26 } | |
27 | |
28 DistilledPagePrefs::~DistilledPagePrefs() { | |
29 } | |
30 | |
31 // static | |
32 void DistilledPagePrefs::RegisterProfilePrefs( | |
33 user_prefs::PrefRegistrySyncable* registry) { | |
34 registry->RegisterIntegerPref(kThemePref, | |
35 Theme::kLight, | |
36 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
37 } | |
38 | |
39 void DistilledPagePrefs::SetTheme(Theme new_theme) { | |
40 if (new_theme >= Theme::kThemeCount) { | |
nyquist
2014/07/08 22:26:47
When a parameter is already an enum you don't have
smaslo
2014/07/09 19:42:25
Done.
| |
41 new_theme = Theme::kLight; | |
42 } | |
43 pref_service_->SetInteger(kThemePref, new_theme); | |
44 base::MessageLoop::current()->PostTask(FROM_HERE, | |
45 base::Bind(&DistilledPagePrefs::NotifyOnChangeTheme, | |
46 weak_ptr_factory_.GetWeakPtr(), new_theme)); | |
47 } | |
48 | |
49 const DistilledPagePrefs::Theme DistilledPagePrefs::GetTheme() { | |
50 if (pref_service_->GetInteger(kThemePref) >= Theme::kThemeCount) { | |
nyquist
2014/07/08 22:26:48
You also need to check for negative values. How ab
smaslo
2014/07/09 19:42:25
Done.
| |
51 SetTheme(Theme::kLight); | |
nyquist
2014/07/08 22:26:48
I think you could:
1) read to local variable
2) bo
smaslo
2014/07/09 19:42:25
Done.
| |
52 } | |
53 return (Theme) pref_service_->GetInteger(kThemePref); | |
nyquist
2014/07/08 22:26:48
I don't think you want to read from disk again her
smaslo
2014/07/09 19:42:25
Done.
| |
54 } | |
55 | |
56 void DistilledPagePrefs::AddObserver(Observer* obs) { | |
57 observers_.AddObserver(obs); | |
58 } | |
59 | |
60 void DistilledPagePrefs::RemoveObserver(Observer* obs) { | |
61 observers_.RemoveObserver(obs); | |
62 } | |
63 | |
64 void DistilledPagePrefs::NotifyOnChangeTheme(Theme new_theme) { | |
65 FOR_EACH_OBSERVER(Observer, observers_, OnChangeTheme(new_theme)); | |
66 } | |
67 | |
68 } // namespace dom_distiller | |
69 | |
OLD | NEW |