blob: 481174ee057023cece03f8c74dc3782c323dd60c [file] [log] [blame]
[email protected]6e2059892012-03-08 02:22:241// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]257f2c382011-03-03 04:06:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b40b2822014-02-12 23:24:505// require cr.js
6// require cr/event_target.js
[email protected]316cc662014-02-18 21:50:527// require cr/util.js
[email protected]76dea1e2012-04-13 20:41:128
[email protected]b40b2822014-02-12 23:24:509cr.define('chrome.sync', function() {
10 'use strict';
[email protected]76dea1e2012-04-13 20:41:1211
[email protected]b40b2822014-02-12 23:24:5012 /**
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]be8ed8c12011-02-18 01:16:2824 }
[email protected]be8ed8c12011-02-18 01:16:2825
[email protected]b40b2822014-02-12 23:24:5026 /**
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]1b21a1d2011-05-06 00:01:2431 };
32
[email protected]b40b2822014-02-12 23:24:5033 /** @return {!Timer} An object which measures elapsed time. */
34 var makeTimer = function() {
35 return new Timer;
[email protected]1b21a1d2011-05-06 00:01:2436 };
37
[email protected]b40b2822014-02-12 23:24:5038 /**
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]62a0dcb2011-05-24 01:42:0747
[email protected]316cc662014-02-18 21:50:5248 /**
[email protected]8644dfa12014-04-01 01:20:1449 * 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]b0a15ef02014-06-02 22:16:0757 * 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]316cc662014-02-18 21:50:5265 * 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]c7743572014-04-07 20:30:3480 /**
skym6c146622017-05-25 21:39:0481 * 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 /**
skym0115ea972017-06-19 19:30:3288 * 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 /**
skym6c146622017-05-25 21:39:0498 * 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]c7743572014-04-07 20:30:34108 * 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.
skym6c146622017-05-25 21:39:04134 *
[email protected]c7743572014-04-07 20:30:34135 * @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]62a0dcb2011-05-24 01:42:07143 return {
[email protected]b40b2822014-02-12 23:24:50144 makeTimer: makeTimer,
145 dispatchEvent: dispatchEvent,
146 events: new cr.EventTarget(),
[email protected]c7743572014-04-07 20:30:34147 getAllNodes: getAllNodes,
148 getAllNodesCallback: getAllNodesCallback,
[email protected]8644dfa12014-04-01 01:20:14149 registerForEvents: registerForEvents,
[email protected]b0a15ef02014-06-02 22:16:07150 registerForPerTypeCounters: registerForPerTypeCounters,
[email protected]316cc662014-02-18 21:50:52151 requestUpdatedAboutInfo: requestUpdatedAboutInfo,
152 requestListOfTypes: requestListOfTypes,
skym6c146622017-05-25 21:39:04153 requestUserEventsVisibility: requestUserEventsVisibility,
skym0115ea972017-06-19 19:30:32154 setIncludeSpecifics: setIncludeSpecifics,
skym6c146622017-05-25 21:39:04155 writeUserEvent: writeUserEvent,
[email protected]62a0dcb2011-05-24 01:42:07156 };
[email protected]b40b2822014-02-12 23:24:50157});