OLD | NEW |
| (Empty) |
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 | |
9 cr.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 * Registers to receive a stream of status counter update events | |
58 * chrome.sync.dispatchEvent(). | |
59 */ | |
60 var registerForPerTypeCounters = function() { | |
61 chrome.send('registerForPerTypeCounters'); | |
62 } | |
63 | |
64 /** | |
65 * Asks the browser to refresh our snapshot of sync state. Should result | |
66 * in an onAboutInfoUpdated event being emitted. | |
67 */ | |
68 var requestUpdatedAboutInfo = function() { | |
69 chrome.send('requestUpdatedAboutInfo'); | |
70 }; | |
71 | |
72 /** | |
73 * Asks the browser to send us the list of registered types. Should result | |
74 * in an onReceivedListOfTypes event being emitted. | |
75 */ | |
76 var requestListOfTypes = function() { | |
77 chrome.send('requestListOfTypes'); | |
78 }; | |
79 | |
80 /** | |
81 * Counter to uniquely identify requests while they're in progress. | |
82 * Used in the implementation of GetAllNodes. | |
83 */ | |
84 var requestId = 0; | |
85 | |
86 /** | |
87 * A map from counter values to asynchronous request callbacks. | |
88 * Used in the implementation of GetAllNodes. | |
89 * @type {{number: !Function}} | |
90 */ | |
91 var requestCallbacks = {}; | |
92 | |
93 /** | |
94 * Asks the browser to send us a copy of all existing sync nodes. | |
95 * Will eventually invoke the given callback with the results. | |
96 * | |
97 * @param {function(!Object)} callback The function to call with the response. | |
98 */ | |
99 var getAllNodes = function(callback) { | |
100 requestId++; | |
101 requestCallbacks[requestId] = callback; | |
102 chrome.send('getAllNodes', [requestId]); | |
103 }; | |
104 | |
105 /** | |
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. | |
109 */ | |
110 var getAllNodesCallback = function(id, response) { | |
111 requestCallbacks[id](response); | |
112 requestCallbacks[id] = undefined; | |
113 }; | |
114 | |
115 return { | |
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, | |
125 }; | |
126 }); | |
OLD | NEW |