1 // Copyright (c) 2012 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.
6 // require cr/event_target.js
9 cr.define('chrome.sync', function() {
13 * A simple timer to measure elapsed time.
18 * The time that this Timer was created.
23 this.start_ = Date.now();
27 * @return {number} The elapsed seconds since this Timer was created.
29 Timer.prototype.getElapsedSeconds = function() {
30 return (Date.now() - this.start_) / 1000;
33 /** @return {!Timer} An object which measures elapsed time. */
34 var makeTimer = function() {
39 * @param {string} name The name of the event type.
40 * @param {!Object} details A collection of event-specific details.
42 var dispatchEvent = function(name, details) {
43 var e = new Event(name);
45 chrome.sync.events.dispatchEvent(e);
49 * Registers to receive a stream of events through
50 * chrome.sync.dispatchEvent().
52 var registerForEvents = function() {
53 chrome.send('registerForEvents');
57 * Registers to receive a stream of status counter update events
58 * chrome.sync.dispatchEvent().
60 var registerForPerTypeCounters = function() {
61 chrome.send('registerForPerTypeCounters');
65 * Asks the browser to refresh our snapshot of sync state. Should result
66 * in an onAboutInfoUpdated event being emitted.
68 var requestUpdatedAboutInfo = function() {
69 chrome.send('requestUpdatedAboutInfo');
73 * Asks the browser to send us the list of registered types. Should result
74 * in an onReceivedListOfTypes event being emitted.
76 var requestListOfTypes = function() {
77 chrome.send('requestListOfTypes');
81 * Counter to uniquely identify requests while they're in progress.
82 * Used in the implementation of GetAllNodes.
87 * A map from counter values to asynchronous request callbacks.
88 * Used in the implementation of GetAllNodes.
89 * @type {{number: !Function}}
91 var requestCallbacks = {};
94 * Asks the browser to send us a copy of all existing sync nodes.
95 * Will eventually invoke the given callback with the results.
97 * @param {function(!Object)} callback The function to call with the response.
99 var getAllNodes = function(callback) {
101 requestCallbacks[requestId] = callback;
102 chrome.send('getAllNodes', [requestId]);
106 * Called from C++ with the response to a getAllNodes request.
107 * @param {number} id The requestId passed in with the request.
108 * @param {Object} response The response to the request.
110 var getAllNodesCallback = function(id, response) {
111 requestCallbacks[id](response);
112 requestCallbacks[id] = undefined;
116 makeTimer: makeTimer,
117 dispatchEvent: dispatchEvent,
118 events: new cr.EventTarget(),
119 getAllNodes: getAllNodes,
120 getAllNodesCallback: getAllNodesCallback,
121 registerForEvents: registerForEvents,
122 registerForPerTypeCounters: registerForPerTypeCounters,
123 requestUpdatedAboutInfo: requestUpdatedAboutInfo,
124 requestListOfTypes: requestListOfTypes,