Fix includes in //components/keyed_service/content
[chromium-blink-merge.git] / chrome / browser / ui / gesture_prefs_observer_factory_aura.cc
blobd5f3d238212757375627778cd87eed89632b34e2
1 // Copyright (c) 2013 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 "chrome/browser/ui/gesture_prefs_observer_factory_aura.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/incognito_helpers.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/keyed_service/content/browser_context_dependency_manager.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/overscroll_configuration.h"
24 #include "content/public/common/renderer_preferences.h"
25 #include "ui/events/gestures/gesture_configuration.h"
27 using ui::GestureConfiguration;
29 namespace {
31 struct OverscrollPref {
32 const char* pref_name;
33 content::OverscrollConfig config;
36 const std::vector<OverscrollPref>& GetOverscrollPrefs() {
37 CR_DEFINE_STATIC_LOCAL(std::vector<OverscrollPref>, overscroll_prefs, ());
38 if (overscroll_prefs.empty()) {
39 using namespace content;
40 const OverscrollPref kOverscrollPrefs[] = {
41 { prefs::kOverscrollHorizontalThresholdComplete,
42 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE },
43 { prefs::kOverscrollVerticalThresholdComplete,
44 OVERSCROLL_CONFIG_VERT_THRESHOLD_COMPLETE },
45 { prefs::kOverscrollMinimumThresholdStart,
46 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN },
47 { prefs::kOverscrollMinimumThresholdStartTouchpad,
48 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD },
49 { prefs::kOverscrollVerticalThresholdStart,
50 OVERSCROLL_CONFIG_VERT_THRESHOLD_START },
51 { prefs::kOverscrollHorizontalResistThreshold,
52 OVERSCROLL_CONFIG_HORIZ_RESIST_AFTER },
53 { prefs::kOverscrollVerticalResistThreshold,
54 OVERSCROLL_CONFIG_VERT_RESIST_AFTER },
56 overscroll_prefs.assign(kOverscrollPrefs,
57 kOverscrollPrefs + arraysize(kOverscrollPrefs));
59 return overscroll_prefs;
62 // This class manages gesture configuration preferences.
63 class GesturePrefsObserver : public KeyedService {
64 public:
65 explicit GesturePrefsObserver(PrefService* prefs);
66 virtual ~GesturePrefsObserver();
68 // KeyedService implementation.
69 virtual void Shutdown() override;
71 private:
72 // Notification callback invoked when browser-side preferences
73 // are updated and need to be pushed into ui::GesturePreferences.
74 void Update();
76 // Notification callback invoked when the fling deacceleration
77 // gesture preferences are changed from chrome://gesture.
78 // Broadcasts the changes all renderers where they are used.
79 void Notify();
81 // Notification helper to push overscroll preferences into
82 // content.
83 void UpdateOverscrollPrefs();
85 PrefChangeRegistrar registrar_;
86 PrefService* prefs_;
88 DISALLOW_COPY_AND_ASSIGN(GesturePrefsObserver);
91 // The list of prefs we want to observe.
92 // Note that this collection of settings should correspond to the settings used
93 // in ui/events/gestures/gesture_configuration.h
94 const char* kPrefsToObserve[] = {
95 prefs::kFlingMaxCancelToDownTimeInMs,
96 prefs::kFlingMaxTapGapTimeInMs,
97 prefs::kTabScrubActivationDelayInMs,
98 prefs::kMaxSeparationForGestureTouchesInPixels,
99 prefs::kSemiLongPressTimeInMs,
102 const char* kPrefsToRemove[] = {
103 "gesture.fling_acceleration_curve_coefficient_0",
104 "gesture.fling_acceleration_curve_coefficient_1",
105 "gesture.fling_acceleration_curve_coefficient_2",
106 "gesture.fling_acceleration_curve_coefficient_3",
107 "gesture.semi_long_press_time_in_seconds",
108 "flingcurve.touchpad_alpha",
109 "flingcurve.touchpad_beta",
110 "flingcurve.touchpad_gamma",
111 "flingcurve.touchscreen_alpha",
112 "flingcurve.touchscreen_beta",
113 "flingcurve.touchscreen_gamma",
116 GesturePrefsObserver::GesturePrefsObserver(PrefService* prefs)
117 : prefs_(prefs) {
118 for (size_t i = 0; i < arraysize(kPrefsToRemove); ++i) {
119 if (prefs->FindPreference(kPrefsToRemove[i]))
120 prefs->ClearPref(kPrefsToRemove[i]);
123 registrar_.Init(prefs);
124 registrar_.RemoveAll();
125 base::Closure callback = base::Bind(&GesturePrefsObserver::Update,
126 base::Unretained(this));
128 base::Closure notify_callback = base::Bind(&GesturePrefsObserver::Notify,
129 base::Unretained(this));
131 for (size_t i = 0; i < arraysize(kPrefsToObserve); ++i)
132 registrar_.Add(kPrefsToObserve[i], callback);
134 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
135 for (size_t i = 0; i < overscroll_prefs.size(); ++i)
136 registrar_.Add(overscroll_prefs[i].pref_name, callback);
138 Update();
141 GesturePrefsObserver::~GesturePrefsObserver() {}
143 void GesturePrefsObserver::Shutdown() {
144 registrar_.RemoveAll();
147 void GesturePrefsObserver::Update() {
148 GestureConfiguration::set_fling_max_cancel_to_down_time_in_ms(
149 prefs_->GetInteger(prefs::kFlingMaxCancelToDownTimeInMs));
150 GestureConfiguration::set_fling_max_tap_gap_time_in_ms(
151 prefs_->GetInteger(prefs::kFlingMaxTapGapTimeInMs));
152 GestureConfiguration::set_tab_scrub_activation_delay_in_ms(
153 prefs_->GetInteger(prefs::kTabScrubActivationDelayInMs));
154 GestureConfiguration::set_semi_long_press_time_in_ms(
155 prefs_->GetInteger(prefs::kSemiLongPressTimeInMs));
156 GestureConfiguration::set_max_separation_for_gesture_touches_in_pixels(
157 static_cast<float>(
158 prefs_->GetDouble(prefs::kMaxSeparationForGestureTouchesInPixels)));
160 UpdateOverscrollPrefs();
163 void GesturePrefsObserver::UpdateOverscrollPrefs() {
164 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
165 for (size_t i = 0; i < overscroll_prefs.size(); ++i) {
166 content::SetOverscrollConfig(overscroll_prefs[i].config,
167 static_cast<float>(prefs_->GetDouble(overscroll_prefs[i].pref_name)));
171 void GesturePrefsObserver::Notify() {
172 // Must do a notify to distribute the changes to all renderers.
173 content::NotificationService* service =
174 content::NotificationService::current();
175 service->Notify(chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED,
176 content::Source<GesturePrefsObserver>(this),
177 content::NotificationService::NoDetails());
180 } // namespace
182 // static
183 GesturePrefsObserverFactoryAura*
184 GesturePrefsObserverFactoryAura::GetInstance() {
185 return Singleton<GesturePrefsObserverFactoryAura>::get();
188 GesturePrefsObserverFactoryAura::GesturePrefsObserverFactoryAura()
189 : BrowserContextKeyedServiceFactory(
190 "GesturePrefsObserverAura",
191 BrowserContextDependencyManager::GetInstance()) {}
193 GesturePrefsObserverFactoryAura::~GesturePrefsObserverFactoryAura() {}
195 KeyedService* GesturePrefsObserverFactoryAura::BuildServiceInstanceFor(
196 content::BrowserContext* profile) const {
197 return new GesturePrefsObserver(static_cast<Profile*>(profile)->GetPrefs());
200 void GesturePrefsObserverFactoryAura::RegisterOverscrollPrefs(
201 user_prefs::PrefRegistrySyncable* registry) {
202 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
204 for (size_t i = 0; i < overscroll_prefs.size(); ++i) {
205 registry->RegisterDoublePref(
206 overscroll_prefs[i].pref_name,
207 content::GetOverscrollConfig(overscroll_prefs[i].config),
208 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
212 void GesturePrefsObserverFactoryAura::RegisterProfilePrefs(
213 user_prefs::PrefRegistrySyncable* registry) {
214 registry->RegisterIntegerPref(
215 prefs::kFlingMaxCancelToDownTimeInMs,
216 GestureConfiguration::fling_max_cancel_to_down_time_in_ms(),
217 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
218 registry->RegisterIntegerPref(
219 prefs::kFlingMaxTapGapTimeInMs,
220 GestureConfiguration::fling_max_tap_gap_time_in_ms(),
221 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
222 registry->RegisterIntegerPref(
223 prefs::kTabScrubActivationDelayInMs,
224 GestureConfiguration::tab_scrub_activation_delay_in_ms(),
225 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
226 registry->RegisterIntegerPref(
227 prefs::kSemiLongPressTimeInMs,
228 GestureConfiguration::semi_long_press_time_in_ms(),
229 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
230 registry->RegisterDoublePref(
231 prefs::kMaxSeparationForGestureTouchesInPixels,
232 GestureConfiguration::max_separation_for_gesture_touches_in_pixels(),
233 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
234 RegisterOverscrollPrefs(registry);
237 bool
238 GesturePrefsObserverFactoryAura::ServiceIsCreatedWithBrowserContext() const {
239 // Create the observer as soon as the profile is created.
240 return true;
243 content::BrowserContext*
244 GesturePrefsObserverFactoryAura::GetBrowserContextToUse(
245 content::BrowserContext* context) const {
246 // Use same gesture preferences on incognito windows.
247 return chrome::GetBrowserContextRedirectedInIncognito(context);
250 bool GesturePrefsObserverFactoryAura::ServiceIsNULLWhileTesting() const {
251 // Some tests replace the PrefService of the TestingProfile after the
252 // GesturePrefsObserver has been created, which makes Shutdown()
253 // remove the registrar from a non-existent PrefService.
254 return true;