NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / gesture_prefs_observer_factory_aura.cc
blob36e7e4ffb23fad62f06eac0158817a86da59b5ee
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/browser_context_keyed_service/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;
28 namespace {
30 // TODO(tdresser): Remove this deprecated pref. See crbug.com/339486.
31 const char kMinScrollSuccessiveVelocityEvents[] =
32 "gesture.min_scroll_successive_velocity_events";
34 struct OverscrollPref {
35 const char* pref_name;
36 content::OverscrollConfig config;
39 const std::vector<OverscrollPref>& GetOverscrollPrefs() {
40 CR_DEFINE_STATIC_LOCAL(std::vector<OverscrollPref>, overscroll_prefs, ());
41 if (overscroll_prefs.empty()) {
42 using namespace content;
43 const OverscrollPref kOverscrollPrefs[] = {
44 { prefs::kOverscrollHorizontalThresholdComplete,
45 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE },
46 { prefs::kOverscrollVerticalThresholdComplete,
47 OVERSCROLL_CONFIG_VERT_THRESHOLD_COMPLETE },
48 { prefs::kOverscrollMinimumThresholdStart,
49 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN },
50 { prefs::kOverscrollMinimumThresholdStartTouchpad,
51 OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD },
52 { prefs::kOverscrollVerticalThresholdStart,
53 OVERSCROLL_CONFIG_VERT_THRESHOLD_START },
54 { prefs::kOverscrollHorizontalResistThreshold,
55 OVERSCROLL_CONFIG_HORIZ_RESIST_AFTER },
56 { prefs::kOverscrollVerticalResistThreshold,
57 OVERSCROLL_CONFIG_VERT_RESIST_AFTER },
59 overscroll_prefs.assign(kOverscrollPrefs,
60 kOverscrollPrefs + arraysize(kOverscrollPrefs));
62 return overscroll_prefs;
65 // This class manages gesture configuration preferences.
66 class GesturePrefsObserver : public BrowserContextKeyedService {
67 public:
68 explicit GesturePrefsObserver(PrefService* prefs);
69 virtual ~GesturePrefsObserver();
71 // BrowserContextKeyedService implementation.
72 virtual void Shutdown() OVERRIDE;
74 private:
75 // Notification callback invoked when browser-side preferences
76 // are updated and need to be pushed into ui::GesturePreferences.
77 void Update();
79 // Notification callback invoked when the fling deacceleration
80 // gesture preferences are changed from chrome://gesture.
81 // Broadcasts the changes all renderers where they are used.
82 void Notify();
84 // Notification helper to push overscroll preferences into
85 // content.
86 void UpdateOverscrollPrefs();
88 PrefChangeRegistrar registrar_;
89 PrefService* prefs_;
91 DISALLOW_COPY_AND_ASSIGN(GesturePrefsObserver);
94 // The list of prefs we want to observe.
95 // Note that this collection of settings should correspond to the settings used
96 // in ui/events/gestures/gesture_configuration.h
97 const char* kPrefsToObserve[] = {
98 prefs::kFlingAccelerationCurveCoefficient0,
99 prefs::kFlingAccelerationCurveCoefficient1,
100 prefs::kFlingAccelerationCurveCoefficient2,
101 prefs::kFlingAccelerationCurveCoefficient3,
102 prefs::kFlingMaxCancelToDownTimeInMs,
103 prefs::kFlingMaxTapGapTimeInMs,
104 prefs::kTabScrubActivationDelayInMS,
105 prefs::kFlingVelocityCap,
106 prefs::kLongPressTimeInSeconds,
107 prefs::kMaxDistanceForTwoFingerTapInPixels,
108 prefs::kMaxSecondsBetweenDoubleClick,
109 prefs::kMaxSeparationForGestureTouchesInPixels,
110 prefs::kMaxSwipeDeviationRatio,
111 prefs::kMaxTouchDownDurationInSecondsForClick,
112 prefs::kMaxTouchMoveInPixelsForClick,
113 prefs::kMinDistanceForPinchScrollInPixels,
114 prefs::kMinFlickSpeedSquared,
115 prefs::kMinPinchUpdateDistanceInPixels,
116 prefs::kMinRailBreakVelocity,
117 prefs::kMinScrollDeltaSquared,
118 prefs::kMinSwipeSpeed,
119 prefs::kMinTouchDownDurationInSecondsForClick,
120 prefs::kPointsBufferedForVelocity,
121 prefs::kRailBreakProportion,
122 prefs::kRailStartProportion,
123 prefs::kSemiLongPressTimeInSeconds,
126 const char* kFlingTouchpadPrefs[] = {
127 prefs::kFlingCurveTouchpadAlpha,
128 prefs::kFlingCurveTouchpadBeta,
129 prefs::kFlingCurveTouchpadGamma
132 const char* kFlingTouchscreenPrefs[] = {
133 prefs::kFlingCurveTouchscreenAlpha,
134 prefs::kFlingCurveTouchscreenBeta,
135 prefs::kFlingCurveTouchscreenGamma,
138 GesturePrefsObserver::GesturePrefsObserver(PrefService* prefs)
139 : prefs_(prefs) {
140 // Clear for migration.
141 prefs->ClearPref(kMinScrollSuccessiveVelocityEvents);
143 registrar_.Init(prefs);
144 registrar_.RemoveAll();
145 base::Closure callback = base::Bind(&GesturePrefsObserver::Update,
146 base::Unretained(this));
148 base::Closure notify_callback = base::Bind(&GesturePrefsObserver::Notify,
149 base::Unretained(this));
151 for (size_t i = 0; i < arraysize(kPrefsToObserve); ++i)
152 registrar_.Add(kPrefsToObserve[i], callback);
154 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
155 for (size_t i = 0; i < overscroll_prefs.size(); ++i)
156 registrar_.Add(overscroll_prefs[i].pref_name, callback);
158 for (size_t i = 0; i < arraysize(kFlingTouchpadPrefs); ++i)
159 registrar_.Add(kFlingTouchpadPrefs[i], notify_callback);
160 for (size_t i = 0; i < arraysize(kFlingTouchscreenPrefs); ++i)
161 registrar_.Add(kFlingTouchscreenPrefs[i], notify_callback);
163 Update();
166 GesturePrefsObserver::~GesturePrefsObserver() {}
168 void GesturePrefsObserver::Shutdown() {
169 registrar_.RemoveAll();
172 void GesturePrefsObserver::Update() {
173 GestureConfiguration::set_fling_acceleration_curve_coefficients(0,
174 prefs_->GetDouble(prefs::kFlingAccelerationCurveCoefficient0));
175 GestureConfiguration::set_fling_acceleration_curve_coefficients(1,
176 prefs_->GetDouble(prefs::kFlingAccelerationCurveCoefficient1));
177 GestureConfiguration::set_fling_acceleration_curve_coefficients(2,
178 prefs_->GetDouble(prefs::kFlingAccelerationCurveCoefficient2));
179 GestureConfiguration::set_fling_acceleration_curve_coefficients(3,
180 prefs_->GetDouble(prefs::kFlingAccelerationCurveCoefficient3));
181 GestureConfiguration::set_fling_max_cancel_to_down_time_in_ms(
182 prefs_->GetInteger(prefs::kFlingMaxCancelToDownTimeInMs));
183 GestureConfiguration::set_fling_max_tap_gap_time_in_ms(
184 prefs_->GetInteger(prefs::kFlingMaxTapGapTimeInMs));
185 GestureConfiguration::set_tab_scrub_activation_delay_in_ms(
186 prefs_->GetInteger(prefs::kTabScrubActivationDelayInMS));
187 GestureConfiguration::set_fling_velocity_cap(
188 prefs_->GetDouble(prefs::kFlingVelocityCap));
189 GestureConfiguration::set_long_press_time_in_seconds(
190 prefs_->GetDouble(
191 prefs::kLongPressTimeInSeconds));
192 GestureConfiguration::set_semi_long_press_time_in_seconds(
193 prefs_->GetDouble(
194 prefs::kSemiLongPressTimeInSeconds));
195 GestureConfiguration::set_max_distance_for_two_finger_tap_in_pixels(
196 prefs_->GetDouble(
197 prefs::kMaxDistanceForTwoFingerTapInPixels));
198 GestureConfiguration::set_max_seconds_between_double_click(
199 prefs_->GetDouble(
200 prefs::kMaxSecondsBetweenDoubleClick));
201 GestureConfiguration::set_max_separation_for_gesture_touches_in_pixels(
202 prefs_->GetDouble(
203 prefs::kMaxSeparationForGestureTouchesInPixels));
204 GestureConfiguration::set_max_swipe_deviation_ratio(
205 prefs_->GetDouble(
206 prefs::kMaxSwipeDeviationRatio));
207 GestureConfiguration::set_max_touch_down_duration_in_seconds_for_click(
208 prefs_->GetDouble(
209 prefs::kMaxTouchDownDurationInSecondsForClick));
210 GestureConfiguration::set_max_touch_move_in_pixels_for_click(
211 prefs_->GetDouble(
212 prefs::kMaxTouchMoveInPixelsForClick));
213 GestureConfiguration::set_max_distance_between_taps_for_double_tap(
214 prefs_->GetDouble(
215 prefs::kMaxDistanceBetweenTapsForDoubleTap));
216 GestureConfiguration::set_min_distance_for_pinch_scroll_in_pixels(
217 prefs_->GetDouble(
218 prefs::kMinDistanceForPinchScrollInPixels));
219 GestureConfiguration::set_min_flick_speed_squared(
220 prefs_->GetDouble(
221 prefs::kMinFlickSpeedSquared));
222 GestureConfiguration::set_min_pinch_update_distance_in_pixels(
223 prefs_->GetDouble(
224 prefs::kMinPinchUpdateDistanceInPixels));
225 GestureConfiguration::set_min_rail_break_velocity(
226 prefs_->GetDouble(
227 prefs::kMinRailBreakVelocity));
228 GestureConfiguration::set_min_scroll_delta_squared(
229 prefs_->GetDouble(
230 prefs::kMinScrollDeltaSquared));
231 GestureConfiguration::set_min_swipe_speed(
232 prefs_->GetDouble(
233 prefs::kMinSwipeSpeed));
234 GestureConfiguration::set_min_touch_down_duration_in_seconds_for_click(
235 prefs_->GetDouble(
236 prefs::kMinTouchDownDurationInSecondsForClick));
237 GestureConfiguration::set_points_buffered_for_velocity(
238 prefs_->GetInteger(
239 prefs::kPointsBufferedForVelocity));
240 GestureConfiguration::set_rail_break_proportion(
241 prefs_->GetDouble(
242 prefs::kRailBreakProportion));
243 GestureConfiguration::set_rail_start_proportion(
244 prefs_->GetDouble(
245 prefs::kRailStartProportion));
246 GestureConfiguration::set_scroll_prediction_seconds(
247 prefs_->GetDouble(prefs::kScrollPredictionSeconds));
248 GestureConfiguration::set_show_press_delay_in_ms(
249 prefs_->GetInteger(prefs::kShowPressDelayInMS));
251 UpdateOverscrollPrefs();
254 void GesturePrefsObserver::UpdateOverscrollPrefs() {
255 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
256 for (size_t i = 0; i < overscroll_prefs.size(); ++i) {
257 content::SetOverscrollConfig(overscroll_prefs[i].config,
258 static_cast<float>(prefs_->GetDouble(overscroll_prefs[i].pref_name)));
262 void GesturePrefsObserver::Notify() {
263 // Must do a notify to distribute the changes to all renderers.
264 content::NotificationService* service =
265 content::NotificationService::current();
266 service->Notify(chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED,
267 content::Source<GesturePrefsObserver>(this),
268 content::NotificationService::NoDetails());
271 } // namespace
273 // static
274 GesturePrefsObserverFactoryAura*
275 GesturePrefsObserverFactoryAura::GetInstance() {
276 return Singleton<GesturePrefsObserverFactoryAura>::get();
279 GesturePrefsObserverFactoryAura::GesturePrefsObserverFactoryAura()
280 : BrowserContextKeyedServiceFactory(
281 "GesturePrefsObserverAura",
282 BrowserContextDependencyManager::GetInstance()) {}
284 GesturePrefsObserverFactoryAura::~GesturePrefsObserverFactoryAura() {}
286 BrowserContextKeyedService*
287 GesturePrefsObserverFactoryAura::BuildServiceInstanceFor(
288 content::BrowserContext* profile) const {
289 return new GesturePrefsObserver(static_cast<Profile*>(profile)->GetPrefs());
292 void GesturePrefsObserverFactoryAura::RegisterOverscrollPrefs(
293 user_prefs::PrefRegistrySyncable* registry) {
294 const std::vector<OverscrollPref>& overscroll_prefs = GetOverscrollPrefs();
296 for (size_t i = 0; i < overscroll_prefs.size(); ++i) {
297 registry->RegisterDoublePref(
298 overscroll_prefs[i].pref_name,
299 content::GetOverscrollConfig(overscroll_prefs[i].config),
300 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
304 void GesturePrefsObserverFactoryAura::RegisterFlingCurveParameters(
305 user_prefs::PrefRegistrySyncable* registry) {
306 content::RendererPreferences def_prefs;
308 for (size_t i = 0; i < arraysize(kFlingTouchpadPrefs); i++)
309 registry->RegisterDoublePref(
310 kFlingTouchpadPrefs[i],
311 def_prefs.touchpad_fling_profile[i],
312 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
314 for (size_t i = 0; i < arraysize(kFlingTouchscreenPrefs); i++)
315 registry->RegisterDoublePref(
316 kFlingTouchscreenPrefs[i],
317 def_prefs.touchscreen_fling_profile[i],
318 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
321 void GesturePrefsObserverFactoryAura::RegisterProfilePrefs(
322 user_prefs::PrefRegistrySyncable* registry) {
323 registry->RegisterDoublePref(
324 prefs::kFlingAccelerationCurveCoefficient0,
325 GestureConfiguration::fling_acceleration_curve_coefficients(0),
326 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
327 registry->RegisterDoublePref(
328 prefs::kFlingAccelerationCurveCoefficient1,
329 GestureConfiguration::fling_acceleration_curve_coefficients(1),
330 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
331 registry->RegisterDoublePref(
332 prefs::kFlingAccelerationCurveCoefficient2,
333 GestureConfiguration::fling_acceleration_curve_coefficients(2),
334 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
335 registry->RegisterDoublePref(
336 prefs::kFlingAccelerationCurveCoefficient3,
337 GestureConfiguration::fling_acceleration_curve_coefficients(3),
338 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
339 registry->RegisterIntegerPref(
340 prefs::kFlingMaxCancelToDownTimeInMs,
341 GestureConfiguration::fling_max_cancel_to_down_time_in_ms(),
342 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
343 registry->RegisterIntegerPref(
344 prefs::kFlingMaxTapGapTimeInMs,
345 GestureConfiguration::fling_max_tap_gap_time_in_ms(),
346 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
347 registry->RegisterIntegerPref(
348 prefs::kTabScrubActivationDelayInMS,
349 GestureConfiguration::tab_scrub_activation_delay_in_ms(),
350 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
351 registry->RegisterDoublePref(
352 prefs::kFlingVelocityCap,
353 GestureConfiguration::fling_velocity_cap(),
354 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
355 registry->RegisterDoublePref(
356 prefs::kLongPressTimeInSeconds,
357 GestureConfiguration::long_press_time_in_seconds(),
358 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
359 registry->RegisterDoublePref(
360 prefs::kSemiLongPressTimeInSeconds,
361 GestureConfiguration::semi_long_press_time_in_seconds(),
362 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
363 registry->RegisterDoublePref(
364 prefs::kMaxDistanceForTwoFingerTapInPixels,
365 GestureConfiguration::max_distance_for_two_finger_tap_in_pixels(),
366 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
367 registry->RegisterDoublePref(
368 prefs::kMaxSecondsBetweenDoubleClick,
369 GestureConfiguration::max_seconds_between_double_click(),
370 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
371 registry->RegisterDoublePref(
372 prefs::kMaxSeparationForGestureTouchesInPixels,
373 GestureConfiguration::max_separation_for_gesture_touches_in_pixels(),
374 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
375 registry->RegisterDoublePref(
376 prefs::kMaxSwipeDeviationRatio,
377 GestureConfiguration::max_swipe_deviation_ratio(),
378 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
379 registry->RegisterDoublePref(
380 prefs::kMaxTouchDownDurationInSecondsForClick,
381 GestureConfiguration::max_touch_down_duration_in_seconds_for_click(),
382 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
383 registry->RegisterDoublePref(
384 prefs::kMaxTouchMoveInPixelsForClick,
385 GestureConfiguration::max_touch_move_in_pixels_for_click(),
386 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
387 registry->RegisterDoublePref(
388 prefs::kMaxDistanceBetweenTapsForDoubleTap,
389 GestureConfiguration::max_distance_between_taps_for_double_tap(),
390 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
391 registry->RegisterDoublePref(
392 prefs::kMinDistanceForPinchScrollInPixels,
393 GestureConfiguration::min_distance_for_pinch_scroll_in_pixels(),
394 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
395 registry->RegisterDoublePref(
396 prefs::kMinFlickSpeedSquared,
397 GestureConfiguration::min_flick_speed_squared(),
398 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
399 registry->RegisterDoublePref(
400 prefs::kMinPinchUpdateDistanceInPixels,
401 GestureConfiguration::min_pinch_update_distance_in_pixels(),
402 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
403 registry->RegisterDoublePref(
404 prefs::kMinRailBreakVelocity,
405 GestureConfiguration::min_rail_break_velocity(),
406 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
407 registry->RegisterDoublePref(
408 prefs::kMinScrollDeltaSquared,
409 GestureConfiguration::min_scroll_delta_squared(),
410 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
411 registry->RegisterDoublePref(
412 prefs::kMinSwipeSpeed,
413 GestureConfiguration::min_swipe_speed(),
414 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
415 registry->RegisterDoublePref(
416 prefs::kMinTouchDownDurationInSecondsForClick,
417 GestureConfiguration::min_touch_down_duration_in_seconds_for_click(),
418 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
419 registry->RegisterIntegerPref(
420 prefs::kPointsBufferedForVelocity,
421 GestureConfiguration::points_buffered_for_velocity(),
422 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
423 registry->RegisterDoublePref(
424 prefs::kRailBreakProportion,
425 GestureConfiguration::rail_break_proportion(),
426 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
427 registry->RegisterDoublePref(
428 prefs::kRailStartProportion,
429 GestureConfiguration::rail_start_proportion(),
430 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
431 registry->RegisterDoublePref(
432 prefs::kScrollPredictionSeconds,
433 GestureConfiguration::scroll_prediction_seconds(),
434 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
435 registry->RegisterIntegerPref(
436 prefs::kShowPressDelayInMS,
437 GestureConfiguration::show_press_delay_in_ms(),
438 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
440 // Register for migration.
441 registry->RegisterIntegerPref(
442 kMinScrollSuccessiveVelocityEvents,
444 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
446 RegisterOverscrollPrefs(registry);
447 RegisterFlingCurveParameters(registry);
450 bool
451 GesturePrefsObserverFactoryAura::ServiceIsCreatedWithBrowserContext() const {
452 // Create the observer as soon as the profile is created.
453 return true;
456 content::BrowserContext*
457 GesturePrefsObserverFactoryAura::GetBrowserContextToUse(
458 content::BrowserContext* context) const {
459 // Use same gesture preferences on incognito windows.
460 return chrome::GetBrowserContextRedirectedInIncognito(context);
463 bool GesturePrefsObserverFactoryAura::ServiceIsNULLWhileTesting() const {
464 // Some tests replace the PrefService of the TestingProfile after the
465 // GesturePrefsObserver has been created, which makes Shutdown()
466 // remove the registrar from a non-existent PrefService.
467 return true;