chrome_sync.js revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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. 4 5// require cr.js 6// require cr/event_target.js 7// require cr/util.js 8 9cr.define('chrome.sync', function() { 10 'use strict'; 11 12 /** 13 * A simple timer to measure elapsed time. 14 * @constructor 15 */ 16 function Timer() { 17 /** 18 * The time that this Timer was created. 19 * @type {number} 20 * @private 21 * @const 22 */ 23 this.start_ = Date.now(); 24 } 25 26 /** 27 * @return {number} The elapsed seconds since this Timer was created. 28 */ 29 Timer.prototype.getElapsedSeconds = function() { 30 return (Date.now() - this.start_) / 1000; 31 }; 32 33 /** @return {!Timer} An object which measures elapsed time. */ 34 var makeTimer = function() { 35 return new Timer; 36 }; 37 38 /** 39 * @param {string} name The name of the event type. 40 * @param {!Object} details A collection of event-specific details. 41 */ 42 var dispatchEvent = function(name, details) { 43 var e = new Event(name); 44 e.details = details; 45 chrome.sync.events.dispatchEvent(e); 46 }; 47 48 /** 49 * Registers to receive a stream of events through 50 * chrome.sync.dispatchEvent(). 51 */ 52 var registerForEvents = function() { 53 chrome.send('registerForEvents'); 54 }; 55 56 /** 57 * Asks the browser to refresh our snapshot of sync state. Should result 58 * in an onAboutInfoUpdated event being emitted. 59 */ 60 var requestUpdatedAboutInfo = function() { 61 chrome.send('requestUpdatedAboutInfo'); 62 }; 63 64 /** 65 * Asks the browser to send us the list of registered types. Should result 66 * in an onReceivedListOfTypes event being emitted. 67 */ 68 var requestListOfTypes = function() { 69 chrome.send('requestListOfTypes'); 70 }; 71 72 /** 73 * Counter to uniquely identify requests while they're in progress. 74 * Used in the implementation of GetAllNodes. 75 */ 76 var requestId = 0; 77 78 /** 79 * A map from counter values to asynchronous request callbacks. 80 * Used in the implementation of GetAllNodes. 81 * @type {{number: !Function}} 82 */ 83 var requestCallbacks = {}; 84 85 /** 86 * Asks the browser to send us a copy of all existing sync nodes. 87 * Will eventually invoke the given callback with the results. 88 * 89 * @param {function(!Object)} callback The function to call with the response. 90 */ 91 var getAllNodes = function(callback) { 92 requestId++; 93 requestCallbacks[requestId] = callback; 94 chrome.send('getAllNodes', [requestId]); 95 }; 96 97 /** 98 * Called from C++ with the response to a getAllNodes request. 99 * @param {number} id The requestId passed in with the request. 100 * @param {Object} response The response to the request. 101 */ 102 var getAllNodesCallback = function(id, response) { 103 requestCallbacks[id](response); 104 requestCallbacks[id] = undefined; 105 }; 106 107 return { 108 makeTimer: makeTimer, 109 dispatchEvent: dispatchEvent, 110 events: new cr.EventTarget(), 111 getAllNodes: getAllNodes, 112 getAllNodesCallback: getAllNodesCallback, 113 registerForEvents: registerForEvents, 114 requestUpdatedAboutInfo: requestUpdatedAboutInfo, 115 requestListOfTypes: requestListOfTypes, 116 }; 117}); 118