1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 // require: cr/event_target.js
9 * @fileoverview This creates a log object which listens to and
10 * records all sync events.
13 cr
.define('chrome.sync', function() {
16 var eventsByCategory
= {
18 'onIncomingNotification',
19 'onNotificationStateChange',
26 'onClearServerDataFailed',
27 'onClearServerDataSucceeded',
28 'onConnectionStatusChange',
29 'onEncryptedTypesChanged',
30 'onEncryptionComplete',
31 'onInitializationComplete',
32 'onPassphraseAccepted',
33 'onPassphraseRequired',
34 'onStopSyncingPermanently',
35 'onSyncCycleCompleted',
48 * Creates a new log object which then immediately starts recording
49 * sync events. Recorded entries are available in the 'entries'
50 * property and there is an 'append' event which can be listened to.
52 * @extends {cr.EventTarget}
54 var Log = function() {
58 * Creates a callback function to be invoked when an event arrives.
60 var makeCallback = function(categoryName
, eventName
) {
62 self
.log_(categoryName
, eventName
, e
.details
);
66 for (var categoryName
in eventsByCategory
) {
67 for (var i
= 0; i
< eventsByCategory
[categoryName
].length
; ++i
) {
68 var eventName
= eventsByCategory
[categoryName
][i
];
69 chrome
.sync
.events
.addEventListener(
71 makeCallback(categoryName
, eventName
));
77 __proto__
: cr
.EventTarget
.prototype,
80 * The recorded log entries.
86 * Records a single event with the given parameters and fires the
87 * 'append' event with the newly-created event as the 'detail'
88 * field of a custom event.
89 * @param {string} submodule The sync submodule for the event.
90 * @param {string} event The name of the event.
91 * @param {dictionary} details A dictionary of event-specific details.
93 log_: function(submodule
, event
, details
) {
101 entry
.textDetails
= JSON
.stringify(entry
.details
, null, 2);
102 this.entries
.push(entry
);
103 // Fire append event.
104 var e
= cr
.doc
.createEvent('CustomEvent');
105 e
.initCustomEvent('append', false, false, entry
);
106 this.dispatchEvent(e
);