Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2509)

Unified Diff: chrome/browser/extensions/activity_log/activity_database.h

Issue 18660004: Extension activity log database refactoring (step 1) (Closed) Base URL: http://git.chromium.org/chromium/src.git@incognito-tests
Patch Set: Delegate renaming and comment cleanup Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/activity_log/activity_database.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/activity_log/activity_database.h
diff --git a/chrome/browser/extensions/activity_log/activity_database.h b/chrome/browser/extensions/activity_log/activity_database.h
index 8c3c42fae1bd993390c0f22c5accb03dbc11afb3..f7472e905a7fb2afc217e9156584be95d546a156 100644
--- a/chrome/browser/extensions/activity_log/activity_database.h
+++ b/chrome/browser/extensions/activity_log/activity_database.h
@@ -27,17 +27,70 @@ class FilePath;
namespace extensions {
-// Encapsulates the SQL connection for the activity log database.
-// This class holds the database connection and has methods for writing.
-// All of the methods except constructor need to be
-// called on the DB thread. For this reason, the ActivityLog calls Close from
-// its destructor instead of destructing its ActivityDatabase object.
+// Encapsulates the SQL connection for the activity log database. This class
+// holds the database connection and has methods for writing. All of the
+// methods except the constructor need to be called on the DB thread.
+//
+// Object ownership and lifetime is a bit complicated for ActivityLog,
+// ActivityLogPolicy, and ActivityDatabase:
+//
+// ActivityLog ----> ActivityLogPolicy ----> ActivityDatabase
+// ^ |
+// | |
+// \--(ActivityDatabase::Delegate)-/
+//
+// The ActivityLogPolicy object contains a pointer to the ActivityDatabase, and
+// the ActivityDatabase contains a pointer back to the ActivityLogPolicy object
+// (as an instance of ActivityDatabase::Delegate).
+//
+// Since some cleanup must happen on the database thread, deletion should occur
+// as follows:
+// 1. ActivityLog calls ActivityLogPolicy::Close()
+// 2. ActivityLogPolicy should call ActivityDatabase::Close() on the database
+// thread.
+// 3. ActivityDatabase::Close() shuts down the database, then calls
+// ActivityDatabase::Delegate::OnDatabaseClose().
+// 4. ActivityDatabase::Delegate::OnDatabaseClose() should delete the
+// ActivityLogPolicy object.
+// 5. ActivityDatabase::Close() finishes running by deleting the
+// ActivityDatabase object.
+//
+// (This assumes the common case that the ActivityLogPolicy uses an
+// ActivityDatabase and implements the ActivityDatabase::Delegate interface.
+// It is also possible for an ActivityLogPolicy to not use a database at all,
+// in which case ActivityLogPolicy::Close() should directly delete itself.)
class ActivityDatabase {
public:
- // Need to call Init to actually use the ActivityDatabase.
- ActivityDatabase();
-
- // Opens the DB and creates tables as necessary.
+ // Interface defining calls that the ActivityDatabase can make into a
+ // ActivityLogPolicy instance to implement policy-specific behavior. Methods
+ // are always invoked on the database thread. Classes other than
+ // ActivityDatabase should not call these methods.
+ class Delegate {
+ protected:
+ friend class ActivityDatabase;
+
+ // A Delegate is never directly deleted; it should instead delete itself
+ // after any final cleanup when OnDatabaseClose() is invoked.
+ virtual ~Delegate() {}
+
+ // Initializes the database schema; this gives a policy a chance to create
+ // or update database tables as needed. Should return true on success.
+ virtual bool OnDatabaseInit(sql::Connection* db) = 0;
+
+ // Called by ActivityDatabase just before the ActivityDatabase object is
+ // deleted. The database will make no further callbacks after invoking
+ // this method, so it is an appropriate time for the policy to delete
+ // itself.
+ virtual void OnDatabaseClose() = 0;
+ };
+
+ // Need to call Init to actually use the ActivityDatabase. The Delegate
+ // provides hooks for an ActivityLogPolicy to control the database schema and
+ // reads/writes.
+ explicit ActivityDatabase(Delegate* delegate);
+
+ // Opens the DB. This invokes OnDatabaseInit in the delegate to create or
+ // update the database schema if needed.
void Init(const base::FilePath& db_name);
// The ActivityLog should call this to kill the ActivityDatabase.
@@ -100,6 +153,10 @@ class ActivityDatabase {
// For unit testing only.
void RecordBatchedActionsWhileTesting();
+ // A reference a Delegate for policy-specific database behavior. See the
+ // top-level comment for ActivityDatabase for comments on cleanup.
+ Delegate* delegate_;
+
base::Clock* testing_clock_;
sql::Connection db_;
bool valid_db_;
« no previous file with comments | « no previous file | chrome/browser/extensions/activity_log/activity_database.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698