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'use strict';
6
7(function() {
8  function supportedByBrowser() {
9    return !!(window.chrome &&
10              chrome.gpuBenchmarking &&
11              chrome.gpuBenchmarking.scrollBounce);
12  }
13
14  function ScrollBounceAction(opt_callback) {
15    var self = this;
16
17    this.beginMeasuringHook = function() {}
18    this.endMeasuringHook = function() {}
19
20    this.callback_ = opt_callback;
21  }
22
23  ScrollBounceAction.prototype.start = function(options) {
24    this.options_ = options;
25    // Assign this.element_ here instead of constructor, because the constructor
26    // ensures this method will be called after the document is loaded.
27    this.element_ = this.options_.element;
28    requestAnimationFrame(this.startGesture_.bind(this));
29  };
30
31  ScrollBounceAction.prototype.startGesture_ = function() {
32    this.beginMeasuringHook();
33
34    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element);
35    var start_left =
36        rect.left + rect.width * this.options_.left_start_ratio;
37    var start_top =
38        rect.top + rect.height * this.options_.top_start_ratio;
39    chrome.gpuBenchmarking.scrollBounce(this.options_.direction,
40                                        this.options_.distance,
41                                        this.options_.overscroll,
42                                        this.options_.repeat_count,
43                                        this.onGestureComplete_.bind(this),
44                                        start_left, start_top,
45                                        this.options_.speed);
46  };
47
48  ScrollBounceAction.prototype.onGestureComplete_ = function() {
49    this.endMeasuringHook();
50
51    // We're done.
52    if (this.callback_)
53      this.callback_();
54  };
55
56  window.__ScrollBounceAction = ScrollBounceAction;
57  window.__ScrollBounceAction_SupportedByBrowser = supportedByBrowser;
58})();
59