1// Copyright 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.
4
5'use strict';
6
7(function() {
8  function SwipeGestureOptions(opt_options) {
9    if (opt_options) {
10      this.element_ = opt_options.element;
11      this.left_start_ratio_ = opt_options.left_start_ratio;
12      this.top_start_ratio_ = opt_options.top_start_ratio;
13      this.direction_ = opt_options.direction;
14      this.distance_ = opt_options.distance;
15      this.speed_ = opt_options.speed;
16    } else {
17      this.element_ = document.body;
18      this.left_start_ratio_ = 0.5;
19      this.top_start_ratio_ = 0.5;
20      this.direction_ = 'left';
21      this.distance_ = 0;
22      this.speed_ = 800;
23    }
24  }
25
26  function supportedByBrowser() {
27    return !!(window.chrome &&
28              chrome.gpuBenchmarking &&
29              chrome.gpuBenchmarking.swipe);
30  }
31
32  // This class swipes a page for a specified distance.
33  function SwipeAction(opt_callback) {
34    var self = this;
35
36    this.beginMeasuringHook = function() {}
37    this.endMeasuringHook = function() {}
38
39    this.callback_ = opt_callback;
40  }
41
42  SwipeAction.prototype.start = function(opt_options) {
43    this.options_ = new SwipeGestureOptions(opt_options);
44    // Assign this.element_ here instead of constructor, because the constructor
45    // ensures this method will be called after the document is loaded.
46    this.element_ = this.options_.element_;
47    requestAnimationFrame(this.startGesture_.bind(this));
48  };
49
50  SwipeAction.prototype.startGesture_ = function() {
51    this.beginMeasuringHook();
52
53    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
54    var start_left =
55        rect.left + rect.width * this.options_.left_start_ratio_;
56    var start_top =
57        rect.top + rect.height * this.options_.top_start_ratio_;
58    chrome.gpuBenchmarking.swipe(this.options_.direction_,
59                                 this.options_.distance_,
60                                 this.onGestureComplete_.bind(this),
61                                 start_left, start_top,
62                                 this.options_.speed_);
63  };
64
65  SwipeAction.prototype.onGestureComplete_ = function() {
66    this.endMeasuringHook();
67
68    // We're done.
69    if (this.callback_)
70      this.callback_();
71  };
72
73  window.__SwipeAction = SwipeAction;
74  window.__SwipeAction_SupportedByBrowser = supportedByBrowser;
75})();
76