Home | History | Annotate | Download | only in sync_internals
      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     // Service
     25 
     26     chrome.sync.onSyncServiceStateChanged.addListener(function () {
     27       self.log_('service', 'onSyncServiceStateChanged', {});
     28     });
     29 
     30     // Notifier
     31 
     32     chrome.sync.onSyncNotificationStateChange.addListener(
     33       function (notificationsEnabled) {
     34         self.log_('notifier', 'onSyncNotificationStateChange', {
     35                     notificationsEnabled: notificationsEnabled
     36                   });
     37       });
     38 
     39     chrome.sync.onSyncIncomingNotification.addListener(function (changedTypes) {
     40       self.log_('notifier', 'onSyncIncomingNotification', {
     41                   changedTypes: changedTypes
     42                 });
     43     });
     44 
     45     // Manager
     46 
     47     chrome.sync.onChangesApplied.addListener(function (modelType, changes) {
     48       self.log_('manager', 'onChangesApplied', {
     49                   modelType: modelType,
     50                   changes: changes
     51                 });
     52     });
     53 
     54     chrome.sync.onChangesComplete.addListener(function (modelType) {
     55       self.log_('manager', 'onChangesComplete', {
     56                   modelType: modelType
     57                 });
     58     });
     59 
     60     chrome.sync.onSyncCycleCompleted.addListener(function (snapshot) {
     61       self.log_('manager', 'onSyncCycleCompleted', {
     62                   snapshot: snapshot
     63                 });
     64     });
     65 
     66     chrome.sync.onAuthError.addListener(function (authError) {
     67       self.log_('manager', 'onAuthError', {
     68                   authError: authError
     69                 });
     70     });
     71 
     72     chrome.sync.onUpdatedToken.addListener(function (token) {
     73       self.log_('manager', 'onUpdatedToken', {
     74                   token: token
     75                 });
     76     });
     77 
     78     chrome.sync.onPassphraseRequired.addListener(function (forDecryption) {
     79       self.log_('manager', 'onPassphraseRequired', {
     80                   forDecryption: forDecryption
     81                 });
     82     });
     83 
     84     chrome.sync.onPassphraseAccepted.addListener(function (bootstrapToken) {
     85       self.log_('manager', 'onPassphraseAccepted', {
     86                   bootstrapToken: bootstrapToken
     87                 });
     88     });
     89 
     90     chrome.sync.onEncryptionComplete.addListener(function (encrypted_types) {
     91       self.log_('manager', 'onEncryptionComplete', {
     92                   encrypted_types: encrypted_types
     93                 });
     94     });
     95 
     96     chrome.sync.onMigrationNeededForTypes.addListener(function (model_types) {
     97       self.log_('manager', 'onMigrationNeededForTypes', {
     98                   model_types: model_types
     99                 });
    100     });
    101 
    102     chrome.sync.onInitializationComplete.addListener(function () {
    103       self.log_('manager', 'onInitializationComplete', {});
    104     });
    105 
    106     chrome.sync.onPaused.addListener(function () {
    107       self.log_('manager', 'onPaused', {});
    108     });
    109 
    110     chrome.sync.onResumed.addListener(function () {
    111       self.log_('manager', 'onResumed', {});
    112     });
    113 
    114     chrome.sync.onStopSyncingPermanently.addListener(function () {
    115       self.log_('manager', 'onStopSyncingPermanently', {});
    116     });
    117 
    118     chrome.sync.onClearServerDataSucceeded.addListener(function () {
    119       self.log_('manager', 'onClearServerDataSucceeded', {});
    120     });
    121 
    122     chrome.sync.onClearServerDataFailed.addListener(function () {
    123       self.log_('manager', 'onClearServerDataFailed', {});
    124     });
    125   };
    126 
    127   Log.prototype = {
    128     __proto__: cr.EventTarget.prototype,
    129 
    130     /**
    131      * The recorded log entries.
    132      * @type {array}
    133      */
    134     entries: [],
    135 
    136     /**
    137      * Records a single event with the given parameters and fires the
    138      * 'append' event with the newly-created event as the 'detail'
    139      * field of a custom event.
    140      * @param {string} submodule The sync submodule for the event.
    141      * @param {string} event The name of the event.
    142      * @param {dictionary} details A dictionary of event-specific details.
    143      */
    144     log_: function(submodule, event, details) {
    145       var entry = {
    146         submodule: submodule,
    147         event: event,
    148         date: new Date(),
    149         details: details
    150       };
    151       this.entries.push(entry);
    152       // Fire append event.
    153       var e = cr.doc.createEvent('CustomEvent');
    154       e.initCustomEvent('append', false, false, entry);
    155       this.dispatchEvent(e);
    156     }
    157   };
    158 
    159   return {
    160     log: new Log()
    161   };
    162 });
    163