summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/sync_internals/chrome_sync.js
blob: f8d9a2f97dda939ee4c278d646e1381d0168edad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// require cr.js
// require cr/event_target.js
// require cr/util.js

cr.define('chrome.sync', function() {
  'use strict';

  function makeSyncFunction(name) {
    var callbacks = [];

    // Calls the function, assuming the last argument is a callback to be
    // called with the return value.
    var fn = function() {
      var args = Array.prototype.slice.call(arguments);
      callbacks.push(args.pop());
      chrome.send(name, args);
    };

    // Handle a reply, assuming that messages are processed in FIFO order.
    // Called by SyncInternalsUI::HandleJsReply().
    fn.handleReply = function() {
      var args = Array.prototype.slice.call(arguments);
      // Remove the callback before we call it since the callback may
      // throw.
      var callback = callbacks.shift();
      callback.apply(null, args);
    };

    return fn;
  }

  var syncFunctions = [
    // Get an array containing a JSON representations of all known sync nodes.
    'getAllNodes',
  ];

  for (var i = 0; i < syncFunctions.length; ++i) {
    var syncFunction = syncFunctions[i];
    chrome.sync[syncFunction] = makeSyncFunction(syncFunction);
  }

  /**
   * A simple timer to measure elapsed time.
   * @constructor
   */
  function Timer() {
    /**
     * The time that this Timer was created.
     * @type {number}
     * @private
     * @const
     */
    this.start_ = Date.now();
  }

  /**
   * @return {number} The elapsed seconds since this Timer was created.
   */
  Timer.prototype.getElapsedSeconds = function() {
    return (Date.now() - this.start_) / 1000;
  };

  /** @return {!Timer} An object which measures elapsed time. */
  var makeTimer = function() {
    return new Timer;
  };

  /**
   * @param {string} name The name of the event type.
   * @param {!Object} details A collection of event-specific details.
   */
  var dispatchEvent = function(name, details) {
    var e = new Event(name);
    e.details = details;
    chrome.sync.events.dispatchEvent(e);
  };

  /**
   * Registers to receive a stream of events through
   * chrome.sync.dispatchEvent().
   */
  var registerForEvents = function() {
    chrome.send('registerForEvents');
  };

  /**
   * Asks the browser to refresh our snapshot of sync state.  Should result
   * in an onAboutInfoUpdated event being emitted.
   */
  var requestUpdatedAboutInfo = function() {
    chrome.send('requestUpdatedAboutInfo');
  };

  /**
   * Asks the browser to send us the list of registered types.  Should result
   * in an onReceivedListOfTypes event being emitted.
   */
  var requestListOfTypes = function() {
    chrome.send('requestListOfTypes');
  };

  return {
    makeTimer: makeTimer,
    dispatchEvent: dispatchEvent,
    events: new cr.EventTarget(),

    registerForEvents: registerForEvents,
    requestUpdatedAboutInfo: requestUpdatedAboutInfo,
    requestListOfTypes: requestListOfTypes,
  };
});