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