| 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 // requier 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) { | 12 function makeSyncFunction(name) { |
| 13 var callbacks = []; | 13 var callbacks = []; |
| 14 | 14 |
| 15 // Calls the function, assuming the last argument is a callback to be | 15 // Calls the function, assuming the last argument is a callback to be |
| 16 // called with the return value. | 16 // called with the return value. |
| 17 var fn = function() { | 17 var fn = function() { |
| 18 var args = Array.prototype.slice.call(arguments); | 18 var args = Array.prototype.slice.call(arguments); |
| 19 callbacks.push(args.pop()); | 19 callbacks.push(args.pop()); |
| 20 chrome.send(name, args); | 20 chrome.send(name, args); |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 // Handle a reply, assuming that messages are processed in FIFO order. | 23 // Handle a reply, assuming that messages are processed in FIFO order. |
| 24 // Called by SyncInternalsUI::HandleJsReply(). | 24 // Called by SyncInternalsUI::HandleJsReply(). |
| 25 fn.handleReply = function() { | 25 fn.handleReply = function() { |
| 26 var args = Array.prototype.slice.call(arguments); | 26 var args = Array.prototype.slice.call(arguments); |
| 27 // Remove the callback before we call it since the callback may | 27 // Remove the callback before we call it since the callback may |
| 28 // throw. | 28 // throw. |
| 29 var callback = callbacks.shift(); | 29 var callback = callbacks.shift(); |
| 30 callback.apply(null, args); | 30 callback.apply(null, args); |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 return fn; | 33 return fn; |
| 34 } | 34 } |
| 35 | 35 |
| 36 var syncFunctions = [ | 36 var syncFunctions = [ |
| 37 // Sync service functions. | |
| 38 'getAboutInfo', | |
| 39 | |
| 40 // Notification functions. See chrome/browser/sync/engine/syncapi.h | 37 // Notification functions. See chrome/browser/sync/engine/syncapi.h |
| 41 // for docs. | 38 // for docs. |
| 42 'getNotificationState', | 39 'getNotificationState', |
| 43 'getNotificationInfo', | 40 'getNotificationInfo', |
| 44 | 41 |
| 45 // Get a static list of available data types. | |
| 46 'getListOfTypes', | |
| 47 | |
| 48 // Client server communication logging functions. | 42 // Client server communication logging functions. |
| 49 'getClientServerTraffic', | 43 'getClientServerTraffic', |
| 50 | 44 |
| 51 // Get an array containing a JSON representations of all known sync nodes. | 45 // Get an array containing a JSON representations of all known sync nodes. |
| 52 'getAllNodes', | 46 'getAllNodes', |
| 53 ]; | 47 ]; |
| 54 | 48 |
| 55 for (var i = 0; i < syncFunctions.length; ++i) { | 49 for (var i = 0; i < syncFunctions.length; ++i) { |
| 56 var syncFunction = syncFunctions[i]; | 50 var syncFunction = syncFunctions[i]; |
| 57 chrome.sync[syncFunction] = makeSyncFunction(syncFunction); | 51 chrome.sync[syncFunction] = makeSyncFunction(syncFunction); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 86 /** | 80 /** |
| 87 * @param {string} name The name of the event type. | 81 * @param {string} name The name of the event type. |
| 88 * @param {!Object} details A collection of event-specific details. | 82 * @param {!Object} details A collection of event-specific details. |
| 89 */ | 83 */ |
| 90 var dispatchEvent = function(name, details) { | 84 var dispatchEvent = function(name, details) { |
| 91 var e = new Event(name); | 85 var e = new Event(name); |
| 92 e.details = details; | 86 e.details = details; |
| 93 chrome.sync.events.dispatchEvent(e); | 87 chrome.sync.events.dispatchEvent(e); |
| 94 }; | 88 }; |
| 95 | 89 |
| 90 /** |
| 91 * Asks the browser to refresh our snapshot of sync state. Should result |
| 92 * in an onAboutInfoUpdated event being emitted. |
| 93 */ |
| 94 var requestUpdatedAboutInfo = function() { |
| 95 chrome.send('requestUpdatedAboutInfo'); |
| 96 }; |
| 97 |
| 98 /** |
| 99 * Asks the browser to send us the list of registered types. Should result |
| 100 * in an onReceivedListOfTypes event being emitted. |
| 101 */ |
| 102 var requestListOfTypes = function() { |
| 103 chrome.send('requestListOfTypes'); |
| 104 }; |
| 105 |
| 96 return { | 106 return { |
| 97 makeTimer: makeTimer, | 107 makeTimer: makeTimer, |
| 98 dispatchEvent: dispatchEvent, | 108 dispatchEvent: dispatchEvent, |
| 99 events: new cr.EventTarget(), | 109 events: new cr.EventTarget(), |
| 110 |
| 111 requestUpdatedAboutInfo: requestUpdatedAboutInfo, |
| 112 requestListOfTypes: requestListOfTypes, |
| 100 }; | 113 }; |
| 101 }); | 114 }); |
| OLD | NEW |