Chromium Code Reviews| 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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 15 #include "chrome/browser/extensions/activity_log/api_actions.h" | 15 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 16 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | 16 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
| 17 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 17 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "sql/connection.h" | 20 #include "sql/connection.h" |
| 21 #include "sql/init_status.h" | 21 #include "sql/init_status.h" |
| 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 // Interface defining callbacks that the ActivityDatabase can make into a | |
| 31 // higher-level ActivityLogPolicy instance. Callbacks are always invoked on | |
| 32 // the database thread. Classes other than ActivityDatabase should not call | |
| 33 // these methods. | |
| 34 class ActivityPolicyCallbacks { | |
| 35 protected: | |
|
mvrable
2013/07/09 21:41:02
Interfaces are normally public instead of protecte
felt
2013/07/09 22:14:51
This seems right to me.
| |
| 36 friend class ActivityDatabase; | |
| 37 | |
| 38 // Initializes the database schema; this gives a policy a change to create or | |
| 39 // update database tables as needed. Should return true on success. | |
| 40 virtual bool DatabaseInitCallback(sql::Connection* db) = 0; | |
| 41 | |
| 42 // Called by ActivityDatabase just before the ActivityDatabase object is | |
| 43 // deleted. The database will make no further callbacks after invoking this | |
| 44 // method, so it is an appropriate time for the policy to delete itself. | |
| 45 virtual void DatabaseCloseCallback() = 0; | |
|
mvrable
2013/07/09 21:41:02
Alternately, it could make sense to drop the use o
felt
2013/07/10 00:14:36
I personally like the current way better because y
| |
| 46 | |
| 47 // An ActivityDatabaseCallbacks instance should not be deleted directly; it | |
| 48 // is instead deleted by the ActivityDatabase which calls the Cleanup() | |
| 49 // method. | |
| 50 virtual ~ActivityPolicyCallbacks() {} | |
| 51 }; | |
| 52 | |
| 30 // Encapsulates the SQL connection for the activity log database. | 53 // Encapsulates the SQL connection for the activity log database. |
| 31 // This class holds the database connection and has methods for writing. | 54 // This class holds the database connection and has methods for writing. |
| 32 // All of the methods except constructor need to be | 55 // All of the methods except constructor need to be |
| 33 // called on the DB thread. For this reason, the ActivityLog calls Close from | 56 // called on the DB thread. For this reason, the ActivityLog calls Close from |
| 34 // its destructor instead of destructing its ActivityDatabase object. | 57 // its destructor instead of destructing its ActivityDatabase object. |
| 35 class ActivityDatabase { | 58 class ActivityDatabase { |
| 36 public: | 59 public: |
| 37 // Need to call Init to actually use the ActivityDatabase. | 60 // Need to call Init to actually use the ActivityDatabase. |
| 38 ActivityDatabase(); | 61 explicit ActivityDatabase(ActivityPolicyCallbacks* callbacks); |
| 39 | 62 |
| 40 // Opens the DB and creates tables as necessary. | 63 // Opens the DB. Calls DatabaseInitCallback in the policy callbacks, which |
| 64 // is responsible for creating or updating the database schema if needed. | |
| 41 void Init(const base::FilePath& db_name); | 65 void Init(const base::FilePath& db_name); |
| 42 | 66 |
| 43 // The ActivityLog should call this to kill the ActivityDatabase. | 67 // The ActivityLog should call this to kill the ActivityDatabase. |
| 44 void Close(); | 68 void Close(); |
| 45 | 69 |
| 46 void LogInitFailure(); | 70 void LogInitFailure(); |
| 47 | 71 |
| 48 // Record a DOMction in the database. | 72 // Record a DOMction in the database. |
| 49 void RecordDOMAction(scoped_refptr<DOMAction> action); | 73 void RecordDOMAction(scoped_refptr<DOMAction> action); |
| 50 | 74 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 // database will be empty. | 117 // database will be empty. |
| 94 void HardFailureClose(); | 118 void HardFailureClose(); |
| 95 | 119 |
| 96 // Doesn't actually close the DB, but changes bools to prevent further writes | 120 // Doesn't actually close the DB, but changes bools to prevent further writes |
| 97 // or changes to it. | 121 // or changes to it. |
| 98 void SoftFailureClose(); | 122 void SoftFailureClose(); |
| 99 | 123 |
| 100 // For unit testing only. | 124 // For unit testing only. |
| 101 void RecordBatchedActionsWhileTesting(); | 125 void RecordBatchedActionsWhileTesting(); |
| 102 | 126 |
| 127 ActivityPolicyCallbacks* policy_callbacks_; | |
| 103 base::Clock* testing_clock_; | 128 base::Clock* testing_clock_; |
| 104 sql::Connection db_; | 129 sql::Connection db_; |
| 105 bool valid_db_; | 130 bool valid_db_; |
| 106 bool batch_mode_; | 131 bool batch_mode_; |
| 107 std::vector<scoped_refptr<Action> > batched_actions_; | 132 std::vector<scoped_refptr<Action> > batched_actions_; |
| 108 base::RepeatingTimer<ActivityDatabase> timer_; | 133 base::RepeatingTimer<ActivityDatabase> timer_; |
| 109 bool already_closed_; | 134 bool already_closed_; |
| 110 bool did_init_; | 135 bool did_init_; |
| 111 | 136 |
| 112 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); | 137 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); |
| 113 }; | 138 }; |
| 114 | 139 |
| 115 } // namespace extensions | 140 } // namespace extensions |
| 116 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 141 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
| OLD | NEW |