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 need to be | 32 // All of the methods except constructor and SetErrorCallback 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 |
40 // Opens the DB and creates tables as necessary. | 44 // Opens the DB and creates tables as necessary. |
41 void Init(const base::FilePath& db_name); | 45 void Init(const base::FilePath& db_name); |
42 | 46 |
43 // The ActivityLog should call this to kill the ActivityDatabase. | 47 // The ActivityLog should call this to kill the ActivityDatabase. |
44 void Close(); | 48 void Close(); |
45 | 49 |
46 void LogInitFailure(); | 50 void LogInitFailure(); |
47 | 51 |
48 // Record a DOMction in the database. | 52 // Record a DOMction in the database. |
49 void RecordDOMAction(scoped_refptr<DOMAction> action); | 53 void RecordDOMAction(scoped_refptr<DOMAction> action); |
50 | 54 |
51 // Record a APIAction in the database. | 55 // Record a APIAction in the database. |
52 void RecordAPIAction(scoped_refptr<APIAction> action); | 56 void RecordAPIAction(scoped_refptr<APIAction> action); |
53 | 57 |
54 // Record a BlockedAction in the database. | 58 // Record a BlockedAction in the database. |
55 void RecordBlockedAction(scoped_refptr<BlockedAction> action); | 59 void RecordBlockedAction(scoped_refptr<BlockedAction> action); |
56 | 60 |
57 // Record an Action in the database. | 61 // Record an Action in the database. |
58 void RecordAction(scoped_refptr<Action> action); | 62 void RecordAction(scoped_refptr<Action> action); |
59 | 63 |
60 // Gets all actions for a given extension for the specified day. 0 = today, | 64 // Gets all actions for a given extension for the specified day. 0 = today, |
61 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from | 65 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from |
62 // newest to oldest. | 66 // newest to oldest. |
63 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( | 67 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( |
64 const std::string& extension_id, const int days_ago); | 68 const std::string& extension_id, const int days_ago); |
65 | 69 |
66 // Handle errors in database writes. | 70 // Break any outstanding transactions, raze the database, and close |
67 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 71 // it. Future calls on the current database handle will fail, when |
| 72 // next opened the database will be empty. This is the ugly version of Close. |
| 73 void KillDatabase(); |
68 | 74 |
69 bool is_db_valid() const { return valid_db_; } | 75 bool initialized() const { return initialized_; } |
| 76 |
| 77 // Standard db operation wrappers. |
| 78 void BeginTransaction(); |
| 79 void CommitTransaction(); |
| 80 void RollbackTransaction(); |
| 81 bool Raze(); |
70 | 82 |
71 // For unit testing only. | 83 // For unit testing only. |
72 void SetBatchModeForTesting(bool batch_mode); | 84 void SetBatchModeForTesting(bool batch_mode); |
73 void SetClockForTesting(base::Clock* clock); | 85 void SetClockForTesting(base::Clock* clock); |
74 void SetTimerForTesting(int milliseconds); | 86 void SetTimerForTesting(int milliseconds); |
75 | 87 |
76 private: | 88 private: |
77 // This should never be invoked by another class. Use Close() to order a | 89 // This should never be invoked by another class. Use Close() to order a |
78 // suicide. | 90 // suicide. |
79 virtual ~ActivityDatabase(); | 91 virtual ~ActivityDatabase(); |
80 | 92 |
81 sql::InitStatus InitializeTable(const char* table_name, | 93 sql::InitStatus InitializeTable(const char* table_name, |
82 const char* table_structure); | 94 const char* table_structure); |
83 | 95 |
84 // When we're in batched mode (which is on by default), we write to the db | 96 // When we're in batched mode (which is on by default), we write to the db |
85 // every X minutes instead of on every API call. This prevents the annoyance | 97 // every X minutes instead of on every API call. This prevents the annoyance |
86 // of writing to disk multiple times a second. | 98 // of writing to disk multiple times a second. |
87 void StartTimer(); | 99 void StartTimer(); |
88 void RecordBatchedActions(); | 100 void RecordBatchedActions(); |
89 | 101 |
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 | |
100 // For unit testing only. | 102 // For unit testing only. |
101 void RecordBatchedActionsWhileTesting(); | 103 void RecordBatchedActionsWhileTesting(); |
102 | 104 |
103 base::Clock* testing_clock_; | 105 base::Clock* testing_clock_; |
104 sql::Connection db_; | 106 sql::Connection db_; |
105 bool valid_db_; | 107 bool initialized_; |
106 bool batch_mode_; | 108 bool batch_mode_; |
107 std::vector<scoped_refptr<Action> > batched_actions_; | 109 std::vector<scoped_refptr<Action> > batched_actions_; |
108 base::RepeatingTimer<ActivityDatabase> timer_; | 110 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 |