summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/util/extensions_activity_monitor.h
blob: f19090d9a1b8ee37ee29b96970600d38809f0e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_
#define CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_
#pragma once

#include <map>

#include "base/message_loop.h"
#include "base/synchronization/lock.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"

namespace browser_sync {

// A class to monitor usage of extensions APIs to send to sync servers, with
// the ability to purge data once sync servers have acknowledged it (successful
// commit response).
//
// This can be used from any thread (it is a 'monitor' in the synchronization
// sense as well), HOWEVER
//
// *** IT MUST BE DELETED FROM THE UI LOOP ***
//
// Consider using MessageLoop::DeleteSoon. (Yes, this means if you allocate
// an ExtensionsActivityMonitor on a thread other than UI, you must 'new' it).
class ExtensionsActivityMonitor : public NotificationObserver {
 public:
  // A data record of activity performed by extension |extension_id|.
  struct Record {
    Record() : bookmark_write_count(0U) {}

    // The human-readable ID identifying the extension responsible
    // for the activity reported in this Record.
    std::string extension_id;

    // How many times the extension successfully invoked a write
    // operation through the bookmarks API since the last CommitMessage.
    uint32 bookmark_write_count;
  };

  typedef std::map<std::string, Record> Records;

  // Creates an ExtensionsActivityMonitor to monitor extensions activities on
  // BrowserThread::UI.
  ExtensionsActivityMonitor();
  ~ExtensionsActivityMonitor();

  // Fills |buffer| with snapshot of current records in constant time by
  // swapping.  This is done mutually exclusively w.r.t methods of this class.
  void GetAndClearRecords(Records* buffer);

  // Add |records| piece-wise (by extension id) to the set of active records.
  // This is done mutually exclusively w.r.t the methods of this class.
  void PutRecords(const Records& records);

  // NotificationObserver implementation.  Called on |ui_loop_|.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);
 private:
  Records records_;
  mutable base::Lock records_lock_;

  // Used only from UI loop.
  NotificationRegistrar registrar_;
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_