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. 4 5// require: cr.js 6// require: cr/event_target.js 7 8/** 9 * @fileoverview This creates a log object which listens to and 10 * records all sync events. 11 */ 12 13cr.define('chrome.sync', function() { 14 'use strict'; 15 16 var eventsByCategory = { 17 notifier: [ 18 'onIncomingNotification', 19 'onNotificationStateChange', 20 ], 21 22 manager: [ 23 'onActionableError', 24 'onChangesApplied', 25 'onChangesComplete', 26 'onClearServerDataFailed', 27 'onClearServerDataSucceeded', 28 'onConnectionStatusChange', 29 'onEncryptedTypesChanged', 30 'onEncryptionComplete', 31 'onInitializationComplete', 32 'onPassphraseAccepted', 33 'onPassphraseRequired', 34 'onStopSyncingPermanently', 35 'onSyncCycleCompleted', 36 ], 37 38 transaction: [ 39 'onTransactionWrite', 40 ], 41 42 protocol: [ 43 'onProtocolEvent', 44 ] 45 }; 46 47 /** 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. 51 * @constructor 52 * @extends {cr.EventTarget} 53 */ 54 var Log = function() { 55 var self = this; 56 57 /** 58 * Creates a callback function to be invoked when an event arrives. 59 */ 60 var makeCallback = function(categoryName, eventName) { 61 return function(e) { 62 self.log_(categoryName, eventName, e.details); 63 }; 64 }; 65 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( 70 eventName, 71 makeCallback(categoryName, eventName)); 72 } 73 } 74 } 75 76 Log.prototype = { 77 __proto__: cr.EventTarget.prototype, 78 79 /** 80 * The recorded log entries. 81 * @type {array} 82 */ 83 entries: [], 84 85 /** 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. 92 */ 93 log_: function(submodule, event, details) { 94 var entry = { 95 submodule: submodule, 96 event: event, 97 date: new Date(), 98 details: details, 99 textDetails: '' 100 }; 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); 107 } 108 }; 109 110 return { 111 log: new Log() 112 }; 113}); 114