OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // require cr.js | 5 // require cr.js |
6 // require cr/event_target.js | 6 // require cr/event_target.js |
7 // require cr/util.js | 7 // require cr/util.js |
8 | 8 |
9 cr.define('chrome.sync', function() { | 9 cr.define('chrome.sync', function() { |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 function makeSyncFunction(name) { | |
13 var callbacks = []; | |
14 | |
15 // Calls the function, assuming the last argument is a callback to be | |
16 // called with the return value. | |
17 var fn = function() { | |
18 var args = Array.prototype.slice.call(arguments); | |
19 callbacks.push(args.pop()); | |
20 chrome.send(name, args); | |
21 }; | |
22 | |
23 // Handle a reply, assuming that messages are processed in FIFO order. | |
24 // Called by SyncInternalsUI::HandleJsReply(). | |
25 fn.handleReply = function() { | |
26 var args = Array.prototype.slice.call(arguments); | |
27 // Remove the callback before we call it since the callback may | |
28 // throw. | |
29 var callback = callbacks.shift(); | |
30 callback.apply(null, args); | |
31 }; | |
32 | |
33 return fn; | |
34 } | |
35 | |
36 var syncFunctions = [ | |
37 // Get an array containing a JSON representations of all known sync nodes. | |
38 'getAllNodes', | |
39 ]; | |
40 | |
41 for (var i = 0; i < syncFunctions.length; ++i) { | |
42 var syncFunction = syncFunctions[i]; | |
43 chrome.sync[syncFunction] = makeSyncFunction(syncFunction); | |
44 } | |
45 | |
46 /** | 12 /** |
47 * A simple timer to measure elapsed time. | 13 * A simple timer to measure elapsed time. |
48 * @constructor | 14 * @constructor |
49 */ | 15 */ |
50 function Timer() { | 16 function Timer() { |
51 /** | 17 /** |
52 * The time that this Timer was created. | 18 * The time that this Timer was created. |
53 * @type {number} | 19 * @type {number} |
54 * @private | 20 * @private |
55 * @const | 21 * @const |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 }; | 62 }; |
97 | 63 |
98 /** | 64 /** |
99 * Asks the browser to send us the list of registered types. Should result | 65 * Asks the browser to send us the list of registered types. Should result |
100 * in an onReceivedListOfTypes event being emitted. | 66 * in an onReceivedListOfTypes event being emitted. |
101 */ | 67 */ |
102 var requestListOfTypes = function() { | 68 var requestListOfTypes = function() { |
103 chrome.send('requestListOfTypes'); | 69 chrome.send('requestListOfTypes'); |
104 }; | 70 }; |
105 | 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. | |
Dan Beam
2014/04/05 00:19:25
nit: @type {{number: !Function}} (though technical
rlarocque
2014/04/05 00:27:29
Done.
| |
81 */ | |
82 var requestCallbacks = {}; | |
83 | |
84 /** | |
85 * Asks the browser to send us a copy of all existing sync nodes. | |
86 * Will eventually invoke the given callback with the results. | |
87 * | |
88 * @param {function(!Object)} callback The function to call with the response. | |
89 */ | |
90 var getAllNodes = function(callback) { | |
91 requestId++; | |
92 requestCallbacks[requestId] = callback; | |
93 chrome.send('getAllNodes', [requestId]); | |
94 }; | |
95 | |
96 /** | |
97 * Called from C++ with the response to a getAllNodes request. | |
98 * @param {number} id The requestId passed in with the request. | |
99 * @param {Object} response The response to the request. | |
100 */ | |
101 var getAllNodesCallback = function(id, response) { | |
102 requestCallbacks[id](response); | |
103 requestCallbacks[id] = undefined; | |
104 }; | |
105 | |
106 return { | 106 return { |
107 makeTimer: makeTimer, | 107 makeTimer: makeTimer, |
108 dispatchEvent: dispatchEvent, | 108 dispatchEvent: dispatchEvent, |
109 events: new cr.EventTarget(), | 109 events: new cr.EventTarget(), |
110 | 110 getAllNodes: getAllNodes, |
111 getAllNodesCallback: getAllNodesCallback, | |
111 registerForEvents: registerForEvents, | 112 registerForEvents: registerForEvents, |
112 requestUpdatedAboutInfo: requestUpdatedAboutInfo, | 113 requestUpdatedAboutInfo: requestUpdatedAboutInfo, |
113 requestListOfTypes: requestListOfTypes, | 114 requestListOfTypes: requestListOfTypes, |
114 }; | 115 }; |
115 }); | 116 }); |
OLD | NEW |