[email protected] | 6e205989 | 2012-03-08 02:22:24 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 257f2c38 | 2011-03-03 04:06:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 5 | // require cr.js |
| 6 | // require cr/event_target.js |
[email protected] | 316cc66 | 2014-02-18 21:50:52 | [diff] [blame] | 7 | // require cr/util.js |
[email protected] | 76dea1e | 2012-04-13 20:41:12 | [diff] [blame] | 8 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 9 | cr.define('chrome.sync', function() { |
| 10 | 'use strict'; |
[email protected] | 76dea1e | 2012-04-13 20:41:12 | [diff] [blame] | 11 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 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(); |
[email protected] | be8ed8c1 | 2011-02-18 01:16:28 | [diff] [blame] | 24 | } |
[email protected] | be8ed8c1 | 2011-02-18 01:16:28 | [diff] [blame] | 25 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 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; |
[email protected] | 1b21a1d | 2011-05-06 00:01:24 | [diff] [blame] | 31 | }; |
| 32 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 33 | /** @return {!Timer} An object which measures elapsed time. */ |
| 34 | var makeTimer = function() { |
| 35 | return new Timer; |
[email protected] | 1b21a1d | 2011-05-06 00:01:24 | [diff] [blame] | 36 | }; |
| 37 | |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 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 | }; |
[email protected] | 62a0dcb | 2011-05-24 01:42:07 | [diff] [blame] | 47 | |
[email protected] | 316cc66 | 2014-02-18 21:50:52 | [diff] [blame] | 48 | /** |
[email protected] | 8644dfa1 | 2014-04-01 01:20:14 | [diff] [blame] | 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 | /** |
[email protected] | b0a15ef0 | 2014-06-02 22:16:07 | [diff] [blame] | 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 | /** |
[email protected] | 316cc66 | 2014-02-18 21:50:52 | [diff] [blame] | 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 | |
[email protected] | c774357 | 2014-04-07 20:30:34 | [diff] [blame] | 80 | /** |
skym | 6c14662 | 2017-05-25 21:39:04 | [diff] [blame] | 81 | * Asks the browser if we should show the User Events tab or not. |
| 82 | */ |
| 83 | var requestUserEventsVisibility = function() { |
| 84 | chrome.send('requestUserEventsVisibility'); |
| 85 | }; |
| 86 | |
| 87 | /** |
skym | 0115ea97 | 2017-06-19 19:30:32 | [diff] [blame] | 88 | * Updates the logic sending events to the protocol logic if they should |
| 89 | * include specifics or not when converting to a human readable format. |
| 90 | * |
| 91 | * @param {bool} includeSpecifics Whether protocol events include specifics. |
| 92 | */ |
| 93 | var setIncludeSpecifics = function(includeSpecifics) { |
| 94 | chrome.send('setIncludeSpecifics', [includeSpecifics]); |
| 95 | }; |
| 96 | |
| 97 | /** |
skym | 6c14662 | 2017-05-25 21:39:04 | [diff] [blame] | 98 | * Sends data to construct a user event that should be committed. |
| 99 | * |
| 100 | * @param {string} eventTimeUsec Timestamp for the new event. |
| 101 | * @param {string} navigationId Timestamp of linked sessions navigation. |
| 102 | */ |
| 103 | var writeUserEvent = function(eventTimeUsec, navigationId) { |
| 104 | chrome.send('writeUserEvent', [eventTimeUsec, navigationId]); |
| 105 | }; |
| 106 | |
| 107 | /** |
[email protected] | c774357 | 2014-04-07 20:30:34 | [diff] [blame] | 108 | * Counter to uniquely identify requests while they're in progress. |
| 109 | * Used in the implementation of GetAllNodes. |
| 110 | */ |
| 111 | var requestId = 0; |
| 112 | |
| 113 | /** |
| 114 | * A map from counter values to asynchronous request callbacks. |
| 115 | * Used in the implementation of GetAllNodes. |
| 116 | * @type {{number: !Function}} |
| 117 | */ |
| 118 | var requestCallbacks = {}; |
| 119 | |
| 120 | /** |
| 121 | * Asks the browser to send us a copy of all existing sync nodes. |
| 122 | * Will eventually invoke the given callback with the results. |
| 123 | * |
| 124 | * @param {function(!Object)} callback The function to call with the response. |
| 125 | */ |
| 126 | var getAllNodes = function(callback) { |
| 127 | requestId++; |
| 128 | requestCallbacks[requestId] = callback; |
| 129 | chrome.send('getAllNodes', [requestId]); |
| 130 | }; |
| 131 | |
| 132 | /** |
| 133 | * Called from C++ with the response to a getAllNodes request. |
skym | 6c14662 | 2017-05-25 21:39:04 | [diff] [blame] | 134 | * |
[email protected] | c774357 | 2014-04-07 20:30:34 | [diff] [blame] | 135 | * @param {number} id The requestId passed in with the request. |
| 136 | * @param {Object} response The response to the request. |
| 137 | */ |
| 138 | var getAllNodesCallback = function(id, response) { |
| 139 | requestCallbacks[id](response); |
| 140 | requestCallbacks[id] = undefined; |
| 141 | }; |
| 142 | |
[email protected] | 62a0dcb | 2011-05-24 01:42:07 | [diff] [blame] | 143 | return { |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 144 | makeTimer: makeTimer, |
| 145 | dispatchEvent: dispatchEvent, |
| 146 | events: new cr.EventTarget(), |
[email protected] | c774357 | 2014-04-07 20:30:34 | [diff] [blame] | 147 | getAllNodes: getAllNodes, |
| 148 | getAllNodesCallback: getAllNodesCallback, |
[email protected] | 8644dfa1 | 2014-04-01 01:20:14 | [diff] [blame] | 149 | registerForEvents: registerForEvents, |
[email protected] | b0a15ef0 | 2014-06-02 22:16:07 | [diff] [blame] | 150 | registerForPerTypeCounters: registerForPerTypeCounters, |
[email protected] | 316cc66 | 2014-02-18 21:50:52 | [diff] [blame] | 151 | requestUpdatedAboutInfo: requestUpdatedAboutInfo, |
| 152 | requestListOfTypes: requestListOfTypes, |
skym | 6c14662 | 2017-05-25 21:39:04 | [diff] [blame] | 153 | requestUserEventsVisibility: requestUserEventsVisibility, |
skym | 0115ea97 | 2017-06-19 19:30:32 | [diff] [blame] | 154 | setIncludeSpecifics: setIncludeSpecifics, |
skym | 6c14662 | 2017-05-25 21:39:04 | [diff] [blame] | 155 | writeUserEvent: writeUserEvent, |
[email protected] | 62a0dcb | 2011-05-24 01:42:07 | [diff] [blame] | 156 | }; |
[email protected] | b40b282 | 2014-02-12 23:24:50 | [diff] [blame] | 157 | }); |