| 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. |
| 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 |
| 106 return { | 107 return { |
| 107 makeTimer: makeTimer, | 108 makeTimer: makeTimer, |
| 108 dispatchEvent: dispatchEvent, | 109 dispatchEvent: dispatchEvent, |
| 109 events: new cr.EventTarget(), | 110 events: new cr.EventTarget(), |
| 110 | 111 getAllNodes: getAllNodes, |
| 112 getAllNodesCallback: getAllNodesCallback, |
| 111 registerForEvents: registerForEvents, | 113 registerForEvents: registerForEvents, |
| 112 requestUpdatedAboutInfo: requestUpdatedAboutInfo, | 114 requestUpdatedAboutInfo: requestUpdatedAboutInfo, |
| 113 requestListOfTypes: requestListOfTypes, | 115 requestListOfTypes: requestListOfTypes, |
| 114 }; | 116 }; |
| 115 }); | 117 }); |
| OLD | NEW |