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

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: Address comments. Created 8 years, 9 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 known_to_be_empty_(false) {
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 (known_to_be_empty_ && !file_path_.empty()) {
45 // Delete the db from disk, it's empty.
46 Close();
47 file_util::Delete(file_path_, false);
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 }
65
59 } 66 }
60 67
61 bool DomStorageDatabase::CommitChanges(bool clear_all_first, 68 bool DomStorageDatabase::CommitChanges(bool clear_all_first,
62 const ValuesMap& changes) { 69 const ValuesMap& changes) {
63 if (!LazyOpen(!changes.empty())) 70 if (!LazyOpen(!changes.empty()))
64 return false; 71 return false;
65 72
66 sql::Transaction transaction(db_.get()); 73 sql::Transaction transaction(db_.get());
67 if (!transaction.Begin()) 74 if (!transaction.Begin())
68 return false; 75 return false;
69 76
70 if (clear_all_first) { 77 if (clear_all_first) {
71 if (!db_->Execute("DELETE FROM ItemTable")) 78 if (!db_->Execute("DELETE FROM ItemTable"))
72 return false; 79 return false;
80 known_to_be_empty_ = true;
73 } 81 }
74 82
83 bool did_delete = false;
84 bool did_insert = false;
75 ValuesMap::const_iterator it = changes.begin(); 85 ValuesMap::const_iterator it = changes.begin();
76 for(; it != changes.end(); ++it) { 86 for(; it != changes.end(); ++it) {
77 sql::Statement statement; 87 sql::Statement statement;
78 string16 key = it->first; 88 string16 key = it->first;
79 NullableString16 value = it->second; 89 NullableString16 value = it->second;
80 if (value.is_null()) { 90 if (value.is_null()) {
81 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, 91 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE,
82 "DELETE FROM ItemTable WHERE key=?")); 92 "DELETE FROM ItemTable WHERE key=?"));
83 statement.BindString16(0, key); 93 statement.BindString16(0, key);
94 did_delete = true;
84 } else { 95 } else {
85 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, 96 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE,
86 "INSERT INTO ItemTable VALUES (?,?)")); 97 "INSERT INTO ItemTable VALUES (?,?)"));
87 statement.BindString16(0, key); 98 statement.BindString16(0, key);
88 statement.BindBlob(1, value.string().data(), 99 statement.BindBlob(1, value.string().data(),
89 value.string().length() * sizeof(char16)); 100 value.string().length() * sizeof(char16));
101 known_to_be_empty_ = false;
102 did_insert = true;
90 } 103 }
91 DCHECK(statement.is_valid()); 104 DCHECK(statement.is_valid());
92 statement.Run(); 105 statement.Run();
93 } 106 }
107
108 if (!known_to_be_empty_ && did_delete && !did_insert) {
109 // See if the database is empty.
110 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE,
111 "SELECT count(key) from ItemTable"));
112 if (statement.Step())
113 known_to_be_empty_ = statement.ColumnInt(0) == 0;
114 }
115
94 return transaction.Commit(); 116 return transaction.Commit();
benm (inactive) 2012/02/27 12:19:24 hm, if the transaction fails, then we need to swit
95 } 117 }
96 118
97 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { 119 bool DomStorageDatabase::LazyOpen(bool create_if_needed) {
98 if (failed_to_open_) { 120 if (failed_to_open_) {
99 // Don't try to open a database that we know has failed 121 // Don't try to open a database that we know has failed
100 // already. 122 // already.
101 return false; 123 return false;
102 } 124 }
103 125
104 if (IsOpen()) 126 if (IsOpen())
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 164
143 if (!database_exists) { 165 if (!database_exists) {
144 // This is a new database, create the table and we're done! 166 // This is a new database, create the table and we're done!
145 if (CreateTableV2()) 167 if (CreateTableV2())
146 return true; 168 return true;
147 } else { 169 } else {
148 // The database exists already - check if we need to upgrade 170 // The database exists already - check if we need to upgrade
149 // and whether it's usable (i.e. not corrupted). 171 // and whether it's usable (i.e. not corrupted).
150 SchemaVersion current_version = DetectSchemaVersion(); 172 SchemaVersion current_version = DetectSchemaVersion();
151 173
174 bool valid_database = false;
152 if (current_version == V2) { 175 if (current_version == V2) {
153 return true; 176 valid_database = true;
154 } else if (current_version == V1) { 177 } else if (current_version == V1) {
155 if (UpgradeVersion1To2()) 178 if (UpgradeVersion1To2())
156 return true; 179 valid_database = true;
180 }
181
182 if (!valid_database) {
183 // Try and recover by attempting to delete the file and start again.
184 Close();
185 return DeleteFileAndRecreate();
157 } 186 }
158 } 187 }
159 188
160 // This is the exceptional case - to try and recover we'll attempt 189 return true;
161 // to delete the file and start again.
162 Close();
163 return DeleteFileAndRecreate();
164 } 190 }
165 191
166 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() { 192 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() {
167 DCHECK(IsOpen()); 193 DCHECK(IsOpen());
168 194
169 // Connection::Open() may succeed even if the file we try and open is not a 195 // 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 196 // database, however in the case that the database is corrupted to the point
171 // that SQLite doesn't actually think it's a database, 197 // that SQLite doesn't actually think it's a database,
172 // sql::Connection::GetCachedStatement will DCHECK when we later try and 198 // 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 199 // 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() && 277 CreateTableV2() &&
252 CommitChanges(false, values) && 278 CommitChanges(false, values) &&
253 migration.Commit(); 279 migration.Commit();
254 } 280 }
255 281
256 void DomStorageDatabase::Close() { 282 void DomStorageDatabase::Close() {
257 db_.reset(NULL); 283 db_.reset(NULL);
258 } 284 }
259 285
260 } // namespace dom_storage 286 } // 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