OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 class Clock; | 24 class Clock; |
25 class FilePath; | 25 class FilePath; |
26 } | 26 } |
27 | 27 |
28 namespace extensions { | 28 namespace extensions { |
29 | 29 |
30 // Encapsulates the SQL connection for the activity log database. | 30 // Encapsulates the SQL connection for the activity log database. |
31 // This class holds the database connection and has methods for writing. | 31 // This class holds the database connection and has methods for writing. |
32 // All of the methods except constructor and SetErrorCallback need to be | 32 // All of the methods except constructor need to be |
33 // called on the DB thread. For this reason, the ActivityLog calls Close from | 33 // called on the DB thread. For this reason, the ActivityLog calls Close from |
34 // its destructor instead of destructing its ActivityDatabase object. | 34 // its destructor instead of destructing its ActivityDatabase object. |
35 class ActivityDatabase { | 35 class ActivityDatabase { |
36 public: | 36 public: |
37 // Need to call Init to actually use the ActivityDatabase. | 37 // Need to call Init to actually use the ActivityDatabase. |
38 ActivityDatabase(); | 38 ActivityDatabase(); |
39 | 39 |
40 // Sets up an optional error callback. | |
41 // Should be the only thing done before Init. | |
42 void SetErrorCallback(const sql::Connection::ErrorCallback& error_callback); | |
43 | |
44 // Opens the DB and creates tables as necessary. | 40 // Opens the DB and creates tables as necessary. |
45 void Init(const base::FilePath& db_name); | 41 void Init(const base::FilePath& db_name); |
46 | 42 |
47 // The ActivityLog should call this to kill the ActivityDatabase. | 43 // The ActivityLog should call this to kill the ActivityDatabase. |
48 void Close(); | 44 void Close(); |
49 | 45 |
50 void LogInitFailure(); | 46 void LogInitFailure(); |
51 | 47 |
52 // Record a DOMction in the database. | 48 // Record a DOMction in the database. |
53 void RecordDOMAction(scoped_refptr<DOMAction> action); | 49 void RecordDOMAction(scoped_refptr<DOMAction> action); |
54 | 50 |
55 // Record a APIAction in the database. | 51 // Record a APIAction in the database. |
56 void RecordAPIAction(scoped_refptr<APIAction> action); | 52 void RecordAPIAction(scoped_refptr<APIAction> action); |
57 | 53 |
58 // Record a BlockedAction in the database. | 54 // Record a BlockedAction in the database. |
59 void RecordBlockedAction(scoped_refptr<BlockedAction> action); | 55 void RecordBlockedAction(scoped_refptr<BlockedAction> action); |
60 | 56 |
61 // Record an Action in the database. | 57 // Record an Action in the database. |
62 void RecordAction(scoped_refptr<Action> action); | 58 void RecordAction(scoped_refptr<Action> action); |
63 | 59 |
64 // Gets all actions for a given extension for the specified day. 0 = today, | 60 // Gets all actions for a given extension for the specified day. 0 = today, |
65 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from | 61 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from |
66 // newest to oldest. | 62 // newest to oldest. |
67 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( | 63 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( |
68 const std::string& extension_id, const int days_ago); | 64 const std::string& extension_id, const int days_ago); |
69 | 65 |
70 // Break any outstanding transactions, raze the database, and close | 66 // Handle errors in database writes. |
71 // it. Future calls on the current database handle will fail, when | 67 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
72 // next opened the database will be empty. This is the ugly version of Close. | |
73 void KillDatabase(); | |
74 | 68 |
75 bool initialized() const { return initialized_; } | 69 bool is_db_valid() const { return valid_db_; } |
76 | |
77 // Standard db operation wrappers. | |
78 void BeginTransaction(); | |
79 void CommitTransaction(); | |
80 void RollbackTransaction(); | |
81 bool Raze(); | |
82 | 70 |
83 // For unit testing only. | 71 // For unit testing only. |
84 void SetBatchModeForTesting(bool batch_mode); | 72 void SetBatchModeForTesting(bool batch_mode); |
85 void SetClockForTesting(base::Clock* clock); | 73 void SetClockForTesting(base::Clock* clock); |
86 void SetTimerForTesting(int milliseconds); | 74 void SetTimerForTesting(int milliseconds); |
87 | 75 |
88 private: | 76 private: |
89 // This should never be invoked by another class. Use Close() to order a | 77 // This should never be invoked by another class. Use Close() to order a |
90 // suicide. | 78 // suicide. |
91 virtual ~ActivityDatabase(); | 79 virtual ~ActivityDatabase(); |
92 | 80 |
93 sql::InitStatus InitializeTable(const char* table_name, | 81 sql::InitStatus InitializeTable(const char* table_name, |
94 const char* table_structure); | 82 const char* table_structure); |
95 | 83 |
96 // When we're in batched mode (which is on by default), we write to the db | 84 // When we're in batched mode (which is on by default), we write to the db |
97 // every X minutes instead of on every API call. This prevents the annoyance | 85 // every X minutes instead of on every API call. This prevents the annoyance |
98 // of writing to disk multiple times a second. | 86 // of writing to disk multiple times a second. |
99 void StartTimer(); | 87 void StartTimer(); |
100 void RecordBatchedActions(); | 88 void RecordBatchedActions(); |
101 | 89 |
| 90 // If an error is unrecoverable or occurred while we were trying to close |
| 91 // the database properly, we take "emergency" actions: break any outstanding |
| 92 // transactions, raze the database, and close. When next opened, the |
| 93 // database will be empty. |
| 94 void HardFailureClose(); |
| 95 |
| 96 // Doesn't actually close the DB, but changes bools to prevent further writes |
| 97 // or changes to it. |
| 98 void SoftFailureClose(); |
| 99 |
102 // For unit testing only. | 100 // For unit testing only. |
103 void RecordBatchedActionsWhileTesting(); | 101 void RecordBatchedActionsWhileTesting(); |
104 | 102 |
105 base::Clock* testing_clock_; | 103 base::Clock* testing_clock_; |
106 sql::Connection db_; | 104 sql::Connection db_; |
107 bool initialized_; | 105 bool valid_db_; |
108 bool batch_mode_; | 106 bool batch_mode_; |
109 std::vector<scoped_refptr<Action> > batched_actions_; | 107 std::vector<scoped_refptr<Action> > batched_actions_; |
110 base::RepeatingTimer<ActivityDatabase> timer_; | 108 base::RepeatingTimer<ActivityDatabase> timer_; |
| 109 bool already_closed_; |
| 110 bool did_init_; |
111 | 111 |
112 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); | 112 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); |
113 }; | 113 }; |
114 | 114 |
115 } // namespace extensions | 115 } // namespace extensions |
116 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 116 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
OLD | NEW |