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" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 // ActivityLogPolicy object. | 54 // ActivityLogPolicy object. |
| 55 // 5. ActivityDatabase::Close() finishes running by deleting the | 55 // 5. ActivityDatabase::Close() finishes running by deleting the |
| 56 // ActivityDatabase object. | 56 // ActivityDatabase object. |
| 57 // | 57 // |
| 58 // (This assumes the common case that the ActivityLogPolicy uses an | 58 // (This assumes the common case that the ActivityLogPolicy uses an |
| 59 // ActivityDatabase and implements the ActivityDatabase::Delegate interface. | 59 // ActivityDatabase and implements the ActivityDatabase::Delegate interface. |
| 60 // It is also possible for an ActivityLogPolicy to not use a database at all, | 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.) | 61 // in which case ActivityLogPolicy::Close() should directly delete itself.) |
| 62 class ActivityDatabase { | 62 class ActivityDatabase { |
| 63 public: | 63 public: |
| 64 // All the fields that need to be cleaned up when URLs are cleaned. | |
| 65 static const char* kURLFields[]; | |
|
felt
2013/07/17 01:55:40
This constant should probably be moved into the Ac
mvrable
2013/07/17 16:41:23
If the history clearing code is in the ActivityDat
| |
| 66 | |
| 64 // Interface defining calls that the ActivityDatabase can make into a | 67 // Interface defining calls that the ActivityDatabase can make into a |
| 65 // ActivityLogPolicy instance to implement policy-specific behavior. Methods | 68 // ActivityLogPolicy instance to implement policy-specific behavior. Methods |
| 66 // are always invoked on the database thread. Classes other than | 69 // are always invoked on the database thread. Classes other than |
| 67 // ActivityDatabase should not call these methods. | 70 // ActivityDatabase should not call these methods. |
| 68 class Delegate { | 71 class Delegate { |
| 69 protected: | 72 protected: |
| 70 friend class ActivityDatabase; | 73 friend class ActivityDatabase; |
| 71 | 74 |
| 72 // A Delegate is never directly deleted; it should instead delete itself | 75 // A Delegate is never directly deleted; it should instead delete itself |
| 73 // after any final cleanup when OnDatabaseClose() is invoked. | 76 // after any final cleanup when OnDatabaseClose() is invoked. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from | 117 // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from |
| 115 // newest to oldest. | 118 // newest to oldest. |
| 116 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( | 119 scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions( |
| 117 const std::string& extension_id, const int days_ago); | 120 const std::string& extension_id, const int days_ago); |
| 118 | 121 |
| 119 // Handle errors in database writes. | 122 // Handle errors in database writes. |
| 120 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 123 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
| 121 | 124 |
| 122 bool is_db_valid() const { return valid_db_; } | 125 bool is_db_valid() const { return valid_db_; } |
| 123 | 126 |
| 127 // Cleans all instances of particular URLs from the database. Used when user | |
| 128 // cleans particular URLs from history. If the vector is empty then all URLS | |
| 129 // are cleaned from the db. | |
| 130 void RemoveURLs(const std::vector<GURL>& gurls); | |
| 131 | |
| 132 // Removes a single URL from the db. If the top level domain of the gurl is | |
| 133 // empty then it does not attempt to remove anything from the db. | |
| 134 void RemoveURL(const GURL& gurl); | |
| 135 | |
| 136 // Cleans all URL columns from the database. | |
| 137 void RemoveAllURLs(); | |
| 138 | |
| 139 // Constructs an SQL query to clear urls from the database. If url is the | |
| 140 // empty string then constructs a query to remove all urls. | |
| 141 static void ConstructRemoveURLQuery(const std::string& url, | |
| 142 std::string* query); | |
| 143 | |
| 124 // For unit testing only. | 144 // For unit testing only. |
| 125 void SetBatchModeForTesting(bool batch_mode); | 145 void SetBatchModeForTesting(bool batch_mode); |
| 126 void SetClockForTesting(base::Clock* clock); | 146 void SetClockForTesting(base::Clock* clock); |
| 127 void SetTimerForTesting(int milliseconds); | 147 void SetTimerForTesting(int milliseconds); |
| 128 | 148 |
| 129 private: | 149 private: |
| 130 // This should never be invoked by another class. Use Close() to order a | 150 // This should never be invoked by another class. Use Close() to order a |
| 131 // suicide. | 151 // suicide. |
| 132 virtual ~ActivityDatabase(); | 152 virtual ~ActivityDatabase(); |
| 133 | 153 |
| 134 sql::InitStatus InitializeTable(const char* table_name, | 154 sql::InitStatus InitializeTable(const char* table_name, |
| 135 const char* table_structure); | 155 const char* table_structure); |
| 136 | 156 |
| 137 // When we're in batched mode (which is on by default), we write to the db | 157 // When we're in batched mode (which is on by default), we write to the db |
| 138 // every X minutes instead of on every API call. This prevents the annoyance | 158 // every X minutes instead of on every API call. This prevents the annoyance |
| 139 // of writing to disk multiple times a second. | 159 // of writing to disk multiple times a second. |
| 140 void StartTimer(); | 160 void StartTimer(); |
| 141 void RecordBatchedActions(); | 161 void RecordBatchedActions(); |
| 142 | 162 |
| 143 // If an error is unrecoverable or occurred while we were trying to close | 163 // If an error is unrecoverable or occurred while we were trying to close |
| 144 // the database properly, we take "emergency" actions: break any outstanding | 164 // the database properly, we take "emergency" actions: break any outstanding |
| 145 // transactions, raze the database, and close. When next opened, the | 165 // transactions, raze the database, and close. When next opened, the |
| 146 // database will be empty. | 166 // database will be empty. |
| 147 void HardFailureClose(); | 167 void HardFailureClose(); |
| 148 | 168 |
| 149 // Doesn't actually close the DB, but changes bools to prevent further writes | 169 // Doesn't actually close the DB, but changes bools to prevent further writes |
| 150 // or changes to it. | 170 // or changes to it. |
| 151 void SoftFailureClose(); | 171 void SoftFailureClose(); |
| 152 | 172 |
| 173 // Gets the top level domain for the URL and removes a '/' if one is present | |
| 174 // at the end of the URL. | |
| 175 // TODO(karenlees): this is the same code used in the DOMAction::Record | |
| 176 // method. Is there anywhere to put this method so it can be used by parts of | |
| 177 // the code so they are kept in sync? | |
| 178 static std::string ExtractTLD(const GURL& gurl); | |
|
felt
2013/07/17 01:55:40
Maybe it could be a static method on the activity_
mvrable
2013/07/17 16:41:23
That would be a good location for it.
| |
| 179 | |
| 153 // For unit testing only. | 180 // For unit testing only. |
| 154 void RecordBatchedActionsWhileTesting(); | 181 void RecordBatchedActionsWhileTesting(); |
| 155 | 182 |
| 156 // A reference a Delegate for policy-specific database behavior. See the | 183 // A reference a Delegate for policy-specific database behavior. See the |
| 157 // top-level comment for ActivityDatabase for comments on cleanup. | 184 // top-level comment for ActivityDatabase for comments on cleanup. |
| 158 Delegate* delegate_; | 185 Delegate* delegate_; |
| 159 | 186 |
| 160 base::Clock* testing_clock_; | 187 base::Clock* testing_clock_; |
| 161 sql::Connection db_; | 188 sql::Connection db_; |
| 162 bool valid_db_; | 189 bool valid_db_; |
| 163 bool batch_mode_; | 190 bool batch_mode_; |
| 164 std::vector<scoped_refptr<Action> > batched_actions_; | 191 std::vector<scoped_refptr<Action> > batched_actions_; |
| 165 base::RepeatingTimer<ActivityDatabase> timer_; | 192 base::RepeatingTimer<ActivityDatabase> timer_; |
| 166 bool already_closed_; | 193 bool already_closed_; |
| 167 bool did_init_; | 194 bool did_init_; |
| 168 | 195 |
| 169 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); | 196 DISALLOW_COPY_AND_ASSIGN(ActivityDatabase); |
| 170 }; | 197 }; |
| 171 | 198 |
| 172 } // namespace extensions | 199 } // namespace extensions |
| 173 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ | 200 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_ |
| OLD | NEW |