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 13 cr.define('chrome.sync', function() { 14 /** 15 * Creates a new log object which then immediately starts recording 16 * sync events. Recorded entries are available in the 'entries' 17 * property and there is an 'append' event which can be listened to. 18 * @constructor 19 * @extends {cr.EventTarget} 20 */ 21 var Log = function() { 22 var self = this; 23 24 var makeListener = function(service, event) { 25 return function(details) { 26 self.log_(service, event, details); 27 }; 28 }; 29 30 for (var eventType in chrome.sync.events) { 31 var events = chrome.sync.events[eventType]; 32 for (var i = 0; i < events.length; ++i) { 33 var eventName = events[i]; 34 var event = chrome.sync[eventName]; 35 event.addListener(makeListener(eventType, eventName)); 36 } 37 } 38 }; 39 40 Log.prototype = { 41 __proto__: cr.EventTarget.prototype, 42 43 /** 44 * The recorded log entries. 45 * @type {array} 46 */ 47 entries: [], 48 49 /** 50 * Records a single event with the given parameters and fires the 51 * 'append' event with the newly-created event as the 'detail' 52 * field of a custom event. 53 * @param {string} submodule The sync submodule for the event. 54 * @param {string} event The name of the event. 55 * @param {dictionary} details A dictionary of event-specific details. 56 */ 57 log_: function(submodule, event, details) { 58 var entry = { 59 submodule: submodule, 60 event: event, 61 date: new Date(), 62 details: details, 63 textDetails: '' 64 }; 65 entry.textDetails = JSON.stringify(entry.details, null, 2); 66 this.entries.push(entry); 67 // Fire append event. 68 var e = cr.doc.createEvent('CustomEvent'); 69 e.initCustomEvent('append', false, false, entry); 70 this.dispatchEvent(e); 71 } 72 }; 73 74 return { 75 log: new Log() 76 }; 77 }); 78