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"
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/user_prefs/pref_registry_syncable.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/overscroll_configuration.h"
23 #include "content/public/common/renderer_preferences.h"
24 #include "ui/events/gestures/gesture_configuration.h"
26 using ui::GestureConfiguration
;
30 struct OverscrollPref
{
31 const char* pref_name
;
32 content::OverscrollConfig config
;
35 const std::vector
<OverscrollPref
>& GetOverscrollPrefs() {
36 CR_DEFINE_STATIC_LOCAL(std::vector
<OverscrollPref
>, overscroll_prefs
, ());
37 if (overscroll_prefs
.empty()) {
38 using namespace content
;
39 const OverscrollPref kOverscrollPrefs
[] = {
40 { prefs::kOverscrollHorizontalThresholdComplete
,
41 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE
},
42 { prefs::kOverscrollVerticalThresholdComplete
,
43 OVERSCROLL_CONFIG_VERT_THRESHOLD_COMPLETE
},
44 { prefs::kOverscrollMinimumThresholdStart
,
45 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN
},
46 { prefs::kOverscrollMinimumThresholdStartTouchpad
,
47 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD
},
48 { prefs::kOverscrollVerticalThresholdStart
,
49 OVERSCROLL_CONFIG_VERT_THRESHOLD_START
},
50 { prefs::kOverscrollHorizontalResistThreshold
,
51 OVERSCROLL_CONFIG_HORIZ_RESIST_AFTER
},
52 { prefs::kOverscrollVerticalResistThreshold
,
53 OVERSCROLL_CONFIG_VERT_RESIST_AFTER
},
55 overscroll_prefs
.assign(kOverscrollPrefs
,
56 kOverscrollPrefs
+ arraysize(kOverscrollPrefs
));
58 return overscroll_prefs
;
61 // This class manages gesture configuration preferences.
62 class GesturePrefsObserver
: public KeyedService
{
64 explicit GesturePrefsObserver(PrefService
* prefs
);
65 virtual ~GesturePrefsObserver();
67 // KeyedService implementation.
68 virtual void Shutdown() OVERRIDE
;
71 // Notification callback invoked when browser-side preferences
72 // are updated and need to be pushed into ui::GesturePreferences.
75 // Notification callback invoked when the fling deacceleration
76 // gesture preferences are changed from chrome://gesture.
77 // Broadcasts the changes all renderers where they are used.
80 // Notification helper to push overscroll preferences into
82 void UpdateOverscrollPrefs();
84 PrefChangeRegistrar registrar_
;
87 DISALLOW_COPY_AND_ASSIGN(GesturePrefsObserver
);
90 // The list of prefs we want to observe.
91 // Note that this collection of settings should correspond to the settings used
92 // in ui/events/gestures/gesture_configuration.h
93 const char* kPrefsToObserve
[] = {
94 prefs::kFlingAccelerationCurveCoefficient0
,
95 prefs::kFlingAccelerationCurveCoefficient1
,
96 prefs::kFlingAccelerationCurveCoefficient2
,
97 prefs::kFlingAccelerationCurveCoefficient3
,
98 prefs::kFlingMaxCancelToDownTimeInMs
,
99 prefs::kFlingMaxTapGapTimeInMs
,
100 prefs::kTabScrubActivationDelayInMS
,
101 prefs::kFlingVelocityCap
,
102 prefs::kLongPressTimeInSeconds
,
103 prefs::kMaxDistanceForTwoFingerTapInPixels
,
104 prefs::kMaxSecondsBetweenDoubleClick
,
105 prefs::kMaxSeparationForGestureTouchesInPixels
,
106 prefs::kMaxSwipeDeviationRatio
,
107 prefs::kMaxTouchDownDurationInSecondsForClick
,
108 prefs::kMaxTouchMoveInPixelsForClick
,
109 prefs::kMinDistanceForPinchScrollInPixels
,
110 prefs::kMinFlickSpeedSquared
,
111 prefs::kMinPinchUpdateDistanceInPixels
,
112 prefs::kMinRailBreakVelocity
,
113 prefs::kMinScrollDeltaSquared
,
114 prefs::kMinSwipeSpeed
,
115 prefs::kMinTouchDownDurationInSecondsForClick
,
116 prefs::kPointsBufferedForVelocity
,
117 prefs::kRailBreakProportion
,
118 prefs::kRailStartProportion
,
119 prefs::kSemiLongPressTimeInSeconds
,
120 prefs::kShowPressDelayInMS
,
123 const char* kFlingTouchpadPrefs
[] = {
124 prefs::kFlingCurveTouchpadAlpha
,
125 prefs::kFlingCurveTouchpadBeta
,
126 prefs::kFlingCurveTouchpadGamma
129 const char* kFlingTouchscreenPrefs
[] = {
130 prefs::kFlingCurveTouchscreenAlpha
,
131 prefs::kFlingCurveTouchscreenBeta
,
132 prefs::kFlingCurveTouchscreenGamma
,
135 GesturePrefsObserver::GesturePrefsObserver(PrefService
* prefs
)
137 registrar_
.Init(prefs
);
138 registrar_
.RemoveAll();
139 base::Closure callback
= base::Bind(&GesturePrefsObserver::Update
,
140 base::Unretained(this));
142 base::Closure notify_callback
= base::Bind(&GesturePrefsObserver::Notify
,
143 base::Unretained(this));
145 for (size_t i
= 0; i
< arraysize(kPrefsToObserve
); ++i
)
146 registrar_
.Add(kPrefsToObserve
[i
], callback
);
148 const std::vector
<OverscrollPref
>& overscroll_prefs
= GetOverscrollPrefs();
149 for (size_t i
= 0; i
< overscroll_prefs
.size(); ++i
)
150 registrar_
.Add(overscroll_prefs
[i
].pref_name
, callback
);
152 for (size_t i
= 0; i
< arraysize(kFlingTouchpadPrefs
); ++i
)
153 registrar_
.Add(kFlingTouchpadPrefs
[i
], notify_callback
);
154 for (size_t i
= 0; i
< arraysize(kFlingTouchscreenPrefs
); ++i
)
155 registrar_
.Add(kFlingTouchscreenPrefs
[i
], notify_callback
);
160 GesturePrefsObserver::~GesturePrefsObserver() {}
162 void GesturePrefsObserver::Shutdown() {
163 registrar_
.RemoveAll();
166 void GesturePrefsObserver::Update() {
167 GestureConfiguration::set_fling_acceleration_curve_coefficients(0,
168 prefs_
->GetDouble(prefs::kFlingAccelerationCurveCoefficient0
));
169 GestureConfiguration::set_fling_acceleration_curve_coefficients(1,
170 prefs_
->GetDouble(prefs::kFlingAccelerationCurveCoefficient1
));
171 GestureConfiguration::set_fling_acceleration_curve_coefficients(2,
172 prefs_
->GetDouble(prefs::kFlingAccelerationCurveCoefficient2
));
173 GestureConfiguration::set_fling_acceleration_curve_coefficients(3,
174 prefs_
->GetDouble(prefs::kFlingAccelerationCurveCoefficient3
));
175 GestureConfiguration::set_fling_max_cancel_to_down_time_in_ms(
176 prefs_
->GetInteger(prefs::kFlingMaxCancelToDownTimeInMs
));
177 GestureConfiguration::set_fling_max_tap_gap_time_in_ms(
178 prefs_
->GetInteger(prefs::kFlingMaxTapGapTimeInMs
));
179 GestureConfiguration::set_tab_scrub_activation_delay_in_ms(
180 prefs_
->GetInteger(prefs::kTabScrubActivationDelayInMS
));
181 GestureConfiguration::set_fling_velocity_cap(
182 prefs_
->GetDouble(prefs::kFlingVelocityCap
));
183 GestureConfiguration::set_long_press_time_in_seconds(
185 prefs::kLongPressTimeInSeconds
));
186 GestureConfiguration::set_semi_long_press_time_in_seconds(
188 prefs::kSemiLongPressTimeInSeconds
));
189 GestureConfiguration::set_max_distance_for_two_finger_tap_in_pixels(
191 prefs::kMaxDistanceForTwoFingerTapInPixels
));
192 GestureConfiguration::set_max_seconds_between_double_click(
194 prefs::kMaxSecondsBetweenDoubleClick
));
195 GestureConfiguration::set_max_separation_for_gesture_touches_in_pixels(
197 prefs::kMaxSeparationForGestureTouchesInPixels
));
198 GestureConfiguration::set_max_swipe_deviation_ratio(
200 prefs::kMaxSwipeDeviationRatio
));
201 GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
203 prefs::kMaxTouchDownDurationInSecondsForClick
));
204 GestureConfiguration::set_max_touch_move_in_pixels_for_click(
206 prefs::kMaxTouchMoveInPixelsForClick
));
207 GestureConfiguration::set_max_distance_between_taps_for_double_tap(
209 prefs::kMaxDistanceBetweenTapsForDoubleTap
));
210 GestureConfiguration::set_min_distance_for_pinch_scroll_in_pixels(
212 prefs::kMinDistanceForPinchScrollInPixels
));
213 GestureConfiguration::set_min_flick_speed_squared(
215 prefs::kMinFlickSpeedSquared
));
216 GestureConfiguration::set_min_pinch_update_distance_in_pixels(
218 prefs::kMinPinchUpdateDistanceInPixels
));
219 GestureConfiguration::set_min_rail_break_velocity(
221 prefs::kMinRailBreakVelocity
));
222 GestureConfiguration::set_min_scroll_delta_squared(
224 prefs::kMinScrollDeltaSquared
));
225 GestureConfiguration::set_min_swipe_speed(
227 prefs::kMinSwipeSpeed
));
228 GestureConfiguration::set_min_touch_down_duration_in_seconds_for_click(
230 prefs::kMinTouchDownDurationInSecondsForClick
));
231 GestureConfiguration::set_points_buffered_for_velocity(
233 prefs::kPointsBufferedForVelocity
));
234 GestureConfiguration::set_rail_break_proportion(
236 prefs::kRailBreakProportion
));
237 GestureConfiguration::set_rail_start_proportion(
239 prefs::kRailStartProportion
));
240 GestureConfiguration::set_scroll_prediction_seconds(
241 prefs_
->GetDouble(prefs::kScrollPredictionSeconds
));
242 GestureConfiguration::set_show_press_delay_in_ms(
243 prefs_
->GetInteger(prefs::kShowPressDelayInMS
));
245 UpdateOverscrollPrefs();
248 void GesturePrefsObserver::UpdateOverscrollPrefs() {
249 const std::vector
<OverscrollPref
>& overscroll_prefs
= GetOverscrollPrefs();
250 for (size_t i
= 0; i
< overscroll_prefs
.size(); ++i
) {
251 content::SetOverscrollConfig(overscroll_prefs
[i
].config
,
252 static_cast<float>(prefs_
->GetDouble(overscroll_prefs
[i
].pref_name
)));
256 void GesturePrefsObserver::Notify() {
257 // Must do a notify to distribute the changes to all renderers.
258 content::NotificationService
* service
=
259 content::NotificationService::current();
260 service
->Notify(chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED
,
261 content::Source
<GesturePrefsObserver
>(this),
262 content::NotificationService::NoDetails());
268 GesturePrefsObserverFactoryAura
*
269 GesturePrefsObserverFactoryAura::GetInstance() {
270 return Singleton
<GesturePrefsObserverFactoryAura
>::get();
273 GesturePrefsObserverFactoryAura::GesturePrefsObserverFactoryAura()
274 : BrowserContextKeyedServiceFactory(
275 "GesturePrefsObserverAura",
276 BrowserContextDependencyManager::GetInstance()) {}
278 GesturePrefsObserverFactoryAura::~GesturePrefsObserverFactoryAura() {}
280 KeyedService
* GesturePrefsObserverFactoryAura::BuildServiceInstanceFor(
281 content::BrowserContext
* profile
) const {
282 return new GesturePrefsObserver(static_cast<Profile
*>(profile
)->GetPrefs());
285 void GesturePrefsObserverFactoryAura::RegisterOverscrollPrefs(
286 user_prefs::PrefRegistrySyncable
* registry
) {
287 const std::vector
<OverscrollPref
>& overscroll_prefs
= GetOverscrollPrefs();
289 for (size_t i
= 0; i
< overscroll_prefs
.size(); ++i
) {
290 registry
->RegisterDoublePref(
291 overscroll_prefs
[i
].pref_name
,
292 content::GetOverscrollConfig(overscroll_prefs
[i
].config
),
293 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
297 void GesturePrefsObserverFactoryAura::RegisterFlingCurveParameters(
298 user_prefs::PrefRegistrySyncable
* registry
) {
299 content::RendererPreferences def_prefs
;
301 for (size_t i
= 0; i
< arraysize(kFlingTouchpadPrefs
); i
++)
302 registry
->RegisterDoublePref(
303 kFlingTouchpadPrefs
[i
],
304 def_prefs
.touchpad_fling_profile
[i
],
305 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
307 for (size_t i
= 0; i
< arraysize(kFlingTouchscreenPrefs
); i
++)
308 registry
->RegisterDoublePref(
309 kFlingTouchscreenPrefs
[i
],
310 def_prefs
.touchscreen_fling_profile
[i
],
311 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
314 void GesturePrefsObserverFactoryAura::RegisterProfilePrefs(
315 user_prefs::PrefRegistrySyncable
* registry
) {
316 registry
->RegisterDoublePref(
317 prefs::kFlingAccelerationCurveCoefficient0
,
318 GestureConfiguration::fling_acceleration_curve_coefficients(0),
319 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
320 registry
->RegisterDoublePref(
321 prefs::kFlingAccelerationCurveCoefficient1
,
322 GestureConfiguration::fling_acceleration_curve_coefficients(1),
323 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
324 registry
->RegisterDoublePref(
325 prefs::kFlingAccelerationCurveCoefficient2
,
326 GestureConfiguration::fling_acceleration_curve_coefficients(2),
327 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
328 registry
->RegisterDoublePref(
329 prefs::kFlingAccelerationCurveCoefficient3
,
330 GestureConfiguration::fling_acceleration_curve_coefficients(3),
331 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
332 registry
->RegisterIntegerPref(
333 prefs::kFlingMaxCancelToDownTimeInMs
,
334 GestureConfiguration::fling_max_cancel_to_down_time_in_ms(),
335 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
336 registry
->RegisterIntegerPref(
337 prefs::kFlingMaxTapGapTimeInMs
,
338 GestureConfiguration::fling_max_tap_gap_time_in_ms(),
339 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
340 registry
->RegisterIntegerPref(
341 prefs::kTabScrubActivationDelayInMS
,
342 GestureConfiguration::tab_scrub_activation_delay_in_ms(),
343 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
344 registry
->RegisterDoublePref(
345 prefs::kFlingVelocityCap
,
346 GestureConfiguration::fling_velocity_cap(),
347 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
348 registry
->RegisterDoublePref(
349 prefs::kLongPressTimeInSeconds
,
350 GestureConfiguration::long_press_time_in_seconds(),
351 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
352 registry
->RegisterDoublePref(
353 prefs::kSemiLongPressTimeInSeconds
,
354 GestureConfiguration::semi_long_press_time_in_seconds(),
355 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
356 registry
->RegisterDoublePref(
357 prefs::kMaxDistanceForTwoFingerTapInPixels
,
358 GestureConfiguration::max_distance_for_two_finger_tap_in_pixels(),
359 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
360 registry
->RegisterDoublePref(
361 prefs::kMaxSecondsBetweenDoubleClick
,
362 GestureConfiguration::max_seconds_between_double_click(),
363 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
364 registry
->RegisterDoublePref(
365 prefs::kMaxSeparationForGestureTouchesInPixels
,
366 GestureConfiguration::max_separation_for_gesture_touches_in_pixels(),
367 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
368 registry
->RegisterDoublePref(
369 prefs::kMaxSwipeDeviationRatio
,
370 GestureConfiguration::max_swipe_deviation_ratio(),
371 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
372 registry
->RegisterDoublePref(
373 prefs::kMaxTouchDownDurationInSecondsForClick
,
374 GestureConfiguration::max_touch_down_duration_in_seconds_for_click(),
375 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
376 registry
->RegisterDoublePref(
377 prefs::kMaxTouchMoveInPixelsForClick
,
378 GestureConfiguration::max_touch_move_in_pixels_for_click(),
379 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
380 registry
->RegisterDoublePref(
381 prefs::kMaxDistanceBetweenTapsForDoubleTap
,
382 GestureConfiguration::max_distance_between_taps_for_double_tap(),
383 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
384 registry
->RegisterDoublePref(
385 prefs::kMinDistanceForPinchScrollInPixels
,
386 GestureConfiguration::min_distance_for_pinch_scroll_in_pixels(),
387 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
388 registry
->RegisterDoublePref(
389 prefs::kMinFlickSpeedSquared
,
390 GestureConfiguration::min_flick_speed_squared(),
391 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
392 registry
->RegisterDoublePref(
393 prefs::kMinPinchUpdateDistanceInPixels
,
394 GestureConfiguration::min_pinch_update_distance_in_pixels(),
395 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
396 registry
->RegisterDoublePref(
397 prefs::kMinRailBreakVelocity
,
398 GestureConfiguration::min_rail_break_velocity(),
399 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
400 registry
->RegisterDoublePref(
401 prefs::kMinScrollDeltaSquared
,
402 GestureConfiguration::min_scroll_delta_squared(),
403 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
404 registry
->RegisterDoublePref(
405 prefs::kMinSwipeSpeed
,
406 GestureConfiguration::min_swipe_speed(),
407 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
408 registry
->RegisterDoublePref(
409 prefs::kMinTouchDownDurationInSecondsForClick
,
410 GestureConfiguration::min_touch_down_duration_in_seconds_for_click(),
411 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
412 registry
->RegisterIntegerPref(
413 prefs::kPointsBufferedForVelocity
,
414 GestureConfiguration::points_buffered_for_velocity(),
415 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
416 registry
->RegisterDoublePref(
417 prefs::kRailBreakProportion
,
418 GestureConfiguration::rail_break_proportion(),
419 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
420 registry
->RegisterDoublePref(
421 prefs::kRailStartProportion
,
422 GestureConfiguration::rail_start_proportion(),
423 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
424 registry
->RegisterDoublePref(
425 prefs::kScrollPredictionSeconds
,
426 GestureConfiguration::scroll_prediction_seconds(),
427 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
428 registry
->RegisterIntegerPref(
429 prefs::kShowPressDelayInMS
,
430 GestureConfiguration::show_press_delay_in_ms(),
431 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
432 RegisterOverscrollPrefs(registry
);
433 RegisterFlingCurveParameters(registry
);
437 GesturePrefsObserverFactoryAura::ServiceIsCreatedWithBrowserContext() const {
438 // Create the observer as soon as the profile is created.
442 content::BrowserContext
*
443 GesturePrefsObserverFactoryAura::GetBrowserContextToUse(
444 content::BrowserContext
* context
) const {
445 // Use same gesture preferences on incognito windows.
446 return chrome::GetBrowserContextRedirectedInIncognito(context
);
449 bool GesturePrefsObserverFactoryAura::ServiceIsNULLWhileTesting() const {
450 // Some tests replace the PrefService of the TestingProfile after the
451 // GesturePrefsObserver has been created, which makes Shutdown()
452 // remove the registrar from a non-existent PrefService.