• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_
6 #define CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_
7 #pragma once
8 
9 #include <map>
10 
11 #include "base/message_loop.h"
12 #include "base/synchronization/lock.h"
13 #include "content/common/notification_observer.h"
14 #include "content/common/notification_registrar.h"
15 
16 namespace browser_sync {
17 
18 // A class to monitor usage of extensions APIs to send to sync servers, with
19 // the ability to purge data once sync servers have acknowledged it (successful
20 // commit response).
21 //
22 // This can be used from any thread (it is a 'monitor' in the synchronization
23 // sense as well), HOWEVER
24 //
25 // *** IT MUST BE DELETED FROM THE UI LOOP ***
26 //
27 // Consider using MessageLoop::DeleteSoon. (Yes, this means if you allocate
28 // an ExtensionsActivityMonitor on a thread other than UI, you must 'new' it).
29 class ExtensionsActivityMonitor : public NotificationObserver {
30  public:
31   // A data record of activity performed by extension |extension_id|.
32   struct Record {
RecordRecord33     Record() : bookmark_write_count(0U) {}
34 
35     // The human-readable ID identifying the extension responsible
36     // for the activity reported in this Record.
37     std::string extension_id;
38 
39     // How many times the extension successfully invoked a write
40     // operation through the bookmarks API since the last CommitMessage.
41     uint32 bookmark_write_count;
42   };
43 
44   typedef std::map<std::string, Record> Records;
45 
46   // Creates an ExtensionsActivityMonitor to monitor extensions activities on
47   // BrowserThread::UI.
48   ExtensionsActivityMonitor();
49   ~ExtensionsActivityMonitor();
50 
51   // Fills |buffer| with snapshot of current records in constant time by
52   // swapping.  This is done mutually exclusively w.r.t methods of this class.
53   void GetAndClearRecords(Records* buffer);
54 
55   // Add |records| piece-wise (by extension id) to the set of active records.
56   // This is done mutually exclusively w.r.t the methods of this class.
57   void PutRecords(const Records& records);
58 
59   // NotificationObserver implementation.  Called on |ui_loop_|.
60   virtual void Observe(NotificationType type,
61                        const NotificationSource& source,
62                        const NotificationDetails& details);
63  private:
64   Records records_;
65   mutable base::Lock records_lock_;
66 
67   // Used only from UI loop.
68   NotificationRegistrar registrar_;
69 };
70 
71 }  // namespace browser_sync
72 
73 #endif  // CHROME_BROWSER_SYNC_UTIL_EXTENSIONS_ACTIVITY_MONITOR_H_
74