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

Side by Side Diff: webkit/dom_storage/dom_storage_database.cc

Issue 9467003: Delete empty dom storage databases on destruction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "webkit/dom_storage/dom_storage_database.h" 5 #include "webkit/dom_storage/dom_storage_database.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "sql/diagnostic_error_delegate.h" 9 #include "sql/diagnostic_error_delegate.h"
10 #include "sql/statement.h" 10 #include "sql/statement.h"
(...skipping 12 matching lines...) Expand all
23 } 23 }
24 24
25 } // anon namespace 25 } // anon namespace
26 26
27 namespace dom_storage { 27 namespace dom_storage {
28 28
29 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) 29 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path)
30 : file_path_(file_path), 30 : file_path_(file_path),
31 db_(NULL), 31 db_(NULL),
32 failed_to_open_(false), 32 failed_to_open_(false),
33 tried_to_recreate_(false) { 33 tried_to_recreate_(false),
34 values_stored_(0) {
34 // Note: in normal use we should never get an empty backing path here. 35 // Note: in normal use we should never get an empty backing path here.
35 // However, the unit test for this class defines another constructor 36 // However, the unit test for this class defines another constructor
36 // that will bypass this check to allow an empty path that signifies 37 // that will bypass this check to allow an empty path that signifies
37 // we should operate on an in-memory database for performance/reliability 38 // we should operate on an in-memory database for performance/reliability
38 // reasons. 39 // reasons.
39 DCHECK(!file_path_.empty()); 40 DCHECK(!file_path_.empty());
40 } 41 }
41 42
42 DomStorageDatabase::~DomStorageDatabase() { 43 DomStorageDatabase::~DomStorageDatabase() {
44 if (IsOpen() && values_stored_ == 0) {
45 // Delete the db from disk, it's empty.
46 Close();
47 file_util::Delete(file_path_, false);
michaeln 2012/02/25 02:55:55 maybe skip this if file_path.empty()? if (known_t
benm (inactive) 2012/02/27 12:06:25 Done.
48 }
43 } 49 }
44 50
45 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { 51 void DomStorageDatabase::ReadAllValues(ValuesMap* result) {
46 if (!LazyOpen(false)) 52 if (!LazyOpen(false))
47 return; 53 return;
48 54
49 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, 55 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE,
50 "SELECT * from ItemTable")); 56 "SELECT * from ItemTable"));
51 DCHECK(statement.is_valid()); 57 DCHECK(statement.is_valid());
52 58
53 while (statement.Step()) { 59 while (statement.Step()) {
54 string16 key = statement.ColumnString16(0); 60 string16 key = statement.ColumnString16(0);
55 string16 value; 61 string16 value;
56 statement.ColumnBlobAsString16(1, &value); 62 statement.ColumnBlobAsString16(1, &value);
57 (*result)[key] = NullableString16(value, false); 63 (*result)[key] = NullableString16(value, false);
58 } 64 }
michaeln 2012/02/25 02:55:55 known_to_be_empty_ = result->empty();
benm (inactive) 2012/02/27 12:06:25 Do we need to do this if we init known_to_be_empty
59 } 65 }
60 66
61 bool DomStorageDatabase::CommitChanges(bool clear_all_first, 67 bool DomStorageDatabase::CommitChanges(bool clear_all_first,
62 const ValuesMap& changes) { 68 const ValuesMap& changes) {
63 if (!LazyOpen(!changes.empty())) 69 if (!LazyOpen(!changes.empty()))
64 return false; 70 return false;
65 71
66 sql::Transaction transaction(db_.get()); 72 sql::Transaction transaction(db_.get());
67 if (!transaction.Begin()) 73 if (!transaction.Begin())
68 return false; 74 return false;
69 75
70 if (clear_all_first) { 76 if (clear_all_first) {
71 if (!db_->Execute("DELETE FROM ItemTable")) 77 if (!db_->Execute("DELETE FROM ItemTable"))
72 return false; 78 return false;
79 values_stored_ = 0;
73 } 80 }
74 81
75 ValuesMap::const_iterator it = changes.begin(); 82 ValuesMap::const_iterator it = changes.begin();
76 for(; it != changes.end(); ++it) { 83 for(; it != changes.end(); ++it) {
77 sql::Statement statement; 84 sql::Statement statement;
78 string16 key = it->first; 85 string16 key = it->first;
79 NullableString16 value = it->second; 86 NullableString16 value = it->second;
80 if (value.is_null()) { 87 if (value.is_null()) {
81 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, 88 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE,
82 "DELETE FROM ItemTable WHERE key=?")); 89 "DELETE FROM ItemTable WHERE key=?"));
83 statement.BindString16(0, key); 90 statement.BindString16(0, key);
91 values_stored_--;
84 } else { 92 } else {
85 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, 93 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE,
86 "INSERT INTO ItemTable VALUES (?,?)")); 94 "INSERT INTO ItemTable VALUES (?,?)"));
87 statement.BindString16(0, key); 95 statement.BindString16(0, key);
88 statement.BindBlob(1, value.string().data(), 96 statement.BindBlob(1, value.string().data(),
89 value.string().length() * sizeof(char16)); 97 value.string().length() * sizeof(char16));
98 values_stored_++;
michaeln 2012/02/25 02:55:55 This count will not be accurate, consider this is
benm (inactive) 2012/02/27 12:06:25 Good catch, i made the test a bit more robust.
90 } 99 }
91 DCHECK(statement.is_valid()); 100 DCHECK(statement.is_valid());
92 statement.Run(); 101 statement.Run();
93 } 102 }
94 return transaction.Commit(); 103 return transaction.Commit();
95 } 104 }
96 105
97 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { 106 bool DomStorageDatabase::LazyOpen(bool create_if_needed) {
98 if (failed_to_open_) { 107 if (failed_to_open_) {
99 // Don't try to open a database that we know has failed 108 // Don't try to open a database that we know has failed
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 151
143 if (!database_exists) { 152 if (!database_exists) {
144 // This is a new database, create the table and we're done! 153 // This is a new database, create the table and we're done!
145 if (CreateTableV2()) 154 if (CreateTableV2())
146 return true; 155 return true;
147 } else { 156 } else {
148 // The database exists already - check if we need to upgrade 157 // The database exists already - check if we need to upgrade
149 // and whether it's usable (i.e. not corrupted). 158 // and whether it's usable (i.e. not corrupted).
150 SchemaVersion current_version = DetectSchemaVersion(); 159 SchemaVersion current_version = DetectSchemaVersion();
151 160
161 bool valid_database = false;
152 if (current_version == V2) { 162 if (current_version == V2) {
153 return true; 163 valid_database = true;
154 } else if (current_version == V1) { 164 } else if (current_version == V1) {
155 if (UpgradeVersion1To2()) 165 if (UpgradeVersion1To2())
156 return true; 166 valid_database = true;
157 } 167 }
168
169 if (!valid_database) {
170 // This is the exceptional case - to try and recover we'll attempt
171 // to delete the file and start again.
172 Close();
173 return DeleteFileAndRecreate();
174 }
175
176 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE,
177 "SELECT count(key) from ItemTable"));
178 if (statement.Step())
179 values_stored_ = statement.ColumnInt(0);
158 } 180 }
159 181
160 // This is the exceptional case - to try and recover we'll attempt 182 return true;
161 // to delete the file and start again.
162 Close();
163 return DeleteFileAndRecreate();
164 } 183 }
165 184
166 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() { 185 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() {
167 DCHECK(IsOpen()); 186 DCHECK(IsOpen());
168 187
169 // Connection::Open() may succeed even if the file we try and open is not a 188 // Connection::Open() may succeed even if the file we try and open is not a
170 // database, however in the case that the database is corrupted to the point 189 // database, however in the case that the database is corrupted to the point
171 // that SQLite doesn't actually think it's a database, 190 // that SQLite doesn't actually think it's a database,
172 // sql::Connection::GetCachedStatement will DCHECK when we later try and 191 // sql::Connection::GetCachedStatement will DCHECK when we later try and
173 // run statements. So we run a query here that will not DCHECK but fail 192 // run statements. So we run a query here that will not DCHECK but fail
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 CreateTableV2() && 270 CreateTableV2() &&
252 CommitChanges(false, values) && 271 CommitChanges(false, values) &&
253 migration.Commit(); 272 migration.Commit();
254 } 273 }
255 274
256 void DomStorageDatabase::Close() { 275 void DomStorageDatabase::Close() {
257 db_.reset(NULL); 276 db_.reset(NULL);
258 } 277 }
259 278
260 } // namespace dom_storage 279 } // namespace dom_storage
OLDNEW
« no previous file with comments | « webkit/dom_storage/dom_storage_database.h ('k') | webkit/dom_storage/dom_storage_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698