| 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 // Encapsulates the SQL connection for the activity log database. | 30 // Encapsulates the SQL connection for the activity log database. This class |
| 31 // This class holds the database connection and has methods for writing. | 31 // holds the database connection and has methods for writing. All of the |
| 32 // All of the methods except constructor need to be | 32 // methods except the constructor need to be called on the DB thread. |
| 33 // called on the DB thread. For this reason, the ActivityLog calls Close from | 33 // |
| 34 // its destructor instead of destructing its ActivityDatabase object. | 34 // Object ownership and lifetime is a bit complicated for ActivityLog, |
| 35 // ActivityLogPolicy, and ActivityDatabase: |
| 36 // |
| 37 // ActivityLog ----> ActivityLogPolicy ----> ActivityDatabase |
| 38 // ^ | |
| 39 // | | |
| 40 // \--(ActivityDatabase::Delegate)-/ |
| 41 // |
| 42 // The ActivityLogPolicy object contains a pointer to the ActivityDatabase, and |
| 43 // the ActivityDatabase contains a pointer back to the ActivityLogPolicy object |
| 44 // (as an instance of ActivityDatabase::Delegate). |
| 45 // |
| 46 // Since some cleanup must happen on the database thread, deletion should occur |
| 47 // as follows: |
| 48 // 1. ActivityLog calls ActivityLogPolicy::Close() |
| 49 // 2. ActivityLogPolicy should call ActivityDatabase::Close() on the database |
| 50 // thread. |
| 51 // 3. ActivityDatabase::Close() shuts down the database, then calls |
| 52 // ActivityDatabase::Delegate::OnDatabaseClose(). |
| 53 // 4. ActivityDatabase::Delegate::OnDatabaseClose() should delete the |
| 54 // ActivityLogPolicy object. |
| 55 // 5. ActivityDatabase::Close() finishes running by deleting the |
| 56 // ActivityDatabase object. |
| 57 // |
| 58 // (This assumes the common case that the ActivityLogPolicy uses an |
| 59 // ActivityDatabase and implements the ActivityDatabase::Delegate interface. |
| 60 // It is also possible for an ActivityLogPolicy to not use a database at all, |
| 61 // in which case ActivityLogPolicy::Close() should directly delete itself.) |
| 35 class ActivityDatabase { | 62 class ActivityDatabase { |
| 36 public: | 63 public: |
| 37 // Need to call Init to actually use the ActivityDatabase. | 64 // Interface defining calls that the ActivityDatabase can make into a |
| 38 ActivityDatabase(); | 65 // ActivityLogPolicy instance to implement policy-specific behavior. Methods |
| 66 // are always invoked on the database thread. Classes other than |
| 67 // ActivityDatabase should not call these methods. |
| 68 class Delegate { |
| 69 protected: |
| 70 friend class ActivityDatabase; |
| 39 | 71 |
| 40 // Opens the DB and creates tables as necessary. | 72 // A Delegate is never directly deleted; it should instead delete itself |
| 73 // after any final cleanup when OnDatabaseClose() is invoked. |
| 74 virtual ~Delegate() {} |
| 75 |
| 76 // Initializes the database schema; this gives a policy a chance to create |
| 77 // or update database tables as needed. Should return true on success. |
| 78 virtual bool OnDatabaseInit(sql::Connection* db) = 0; |
| 79 |
| 80 // Called by ActivityDatabase just before the ActivityDatabase object is |
| 81 // deleted. The database will make no further callbacks after invoking |
| 82 // this method, so it is an appropriate time for the policy to delete |
| 83 // itself. |
| 84 virtual void OnDatabaseClose() = 0; |
| 85 }; |
| 86 |
| 87 // Need to call Init to actually use the ActivityDatabase. The Delegate |
| 88 // provides hooks for an ActivityLogPolicy to control the database schema and |
| 89 // reads/writes. |
| 90 explicit ActivityDatabase(Delegate* delegate); |
| 91 |
| 92 // Opens the DB. This invokes OnDatabaseInit in the delegate to create or |
| 93 // update the database schema if needed. |
| 41 void Init(const base::FilePath& db_name); | 94 void Init(const base::FilePath& db_name); |
| 42 | 95 |
| 43 // The ActivityLog should call this to kill the ActivityDatabase. | 96 // The ActivityLog should call this to kill the ActivityDatabase. |
| 44 void Close(); | 97 void Close(); |
| 45 | 98 |
| 46 void LogInitFailure(); | 99 void LogInitFailure(); |
| 47 | 100 |
| 48 // Record a DOMction in the database. | 101 // Record a DOMction in the database. |
| 49 void RecordDOMAction(scoped_refptr<DOMAction> action); | 102 void RecordDOMAction(scoped_refptr<DOMAction> action); |
| 50 | 103 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // database will be empty. | 146 // database will be empty. |
| 94 void HardFailureClose(); | 147 void HardFailureClose(); |
| 95 | 148 |
| 96 // Doesn't actually close the DB, but changes bools to prevent further writes | 149 // Doesn't actually close the DB, but changes bools to prevent further writes |
| 97 // or changes to it. | 150 // or changes to it. |
| 98 void SoftFailureClose(); | 151 void SoftFailureClose(); |
| 99 | 152 |
| 100 // For unit testing only. | 153 // For unit testing only. |
| 101 void RecordBatchedActionsWhileTesting(); | 154 void RecordBatchedActionsWhileTesting(); |
| 102 | 155 |
| 156 // A reference a Delegate for policy-specific database behavior. See the |
| 157 // top-level comment for ActivityDatabase for comments on cleanup. |
| 158 Delegate* delegate_; |
| 159 |
| 103 base::Clock* testing_clock_; | 160 base::Clock* testing_clock_; |
| 104 sql::Connection db_; | 161 sql::Connection db_; |
| 105 bool valid_db_; | 162 bool valid_db_; |
| 106 bool batch_mode_; | 163 bool batch_mode_; |
| 107 std::vector<scoped_refptr<Action> > batched_actions_; | 164 std::vector<scoped_refptr<Action> > batched_actions_; |
| 108 base::RepeatingTimer<ActivityDatabase> timer_; | 165 base::RepeatingTimer<ActivityDatabase> timer_; |
| 109 bool already_closed_; | 166 bool already_closed_; |
| 110 bool did_init_; | 167 bool did_init_; |
| 111 | 168 |
| 112 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); | 169 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); |
| 113 }; | 170 }; |
| 114 | 171 |
| 115 } // namespace extensions | 172 } // namespace extensions |
| 116 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 173 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
| OLD | NEW |