OLD | NEW |
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/browser/dom_storage/dom_storage_database.h" | 5 #include "webkit/browser/dom_storage/dom_storage_database.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 known_to_be_empty_ = result->empty(); | 82 known_to_be_empty_ = result->empty(); |
83 } | 83 } |
84 | 84 |
85 bool DomStorageDatabase::CommitChanges(bool clear_all_first, | 85 bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
86 const ValuesMap& changes) { | 86 const ValuesMap& changes) { |
87 if (!LazyOpen(!changes.empty())) { | 87 if (!LazyOpen(!changes.empty())) { |
88 // If we're being asked to commit changes that will result in an | 88 // If we're being asked to commit changes that will result in an |
89 // empty database, we return true if the database file doesn't exist. | 89 // empty database, we return true if the database file doesn't exist. |
90 return clear_all_first && changes.empty() && | 90 return clear_all_first && changes.empty() && |
91 !file_util::PathExists(file_path_); | 91 !base::PathExists(file_path_); |
92 } | 92 } |
93 | 93 |
94 bool old_known_to_be_empty = known_to_be_empty_; | 94 bool old_known_to_be_empty = known_to_be_empty_; |
95 sql::Transaction transaction(db_.get()); | 95 sql::Transaction transaction(db_.get()); |
96 if (!transaction.Begin()) | 96 if (!transaction.Begin()) |
97 return false; | 97 return false; |
98 | 98 |
99 if (clear_all_first) { | 99 if (clear_all_first) { |
100 if (!db_->Execute("DELETE FROM ItemTable")) | 100 if (!db_->Execute("DELETE FROM ItemTable")) |
101 return false; | 101 return false; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { | 143 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { |
144 if (failed_to_open_) { | 144 if (failed_to_open_) { |
145 // Don't try to open a database that we know has failed | 145 // Don't try to open a database that we know has failed |
146 // already. | 146 // already. |
147 return false; | 147 return false; |
148 } | 148 } |
149 | 149 |
150 if (IsOpen()) | 150 if (IsOpen()) |
151 return true; | 151 return true; |
152 | 152 |
153 bool database_exists = file_util::PathExists(file_path_); | 153 bool database_exists = base::PathExists(file_path_); |
154 | 154 |
155 if (!database_exists && !create_if_needed) { | 155 if (!database_exists && !create_if_needed) { |
156 // If the file doesn't exist already and we haven't been asked to create | 156 // If the file doesn't exist already and we haven't been asked to create |
157 // a file on disk, then we don't bother opening the database. This means | 157 // a file on disk, then we don't bother opening the database. This means |
158 // we wait until we absolutely need to put something onto disk before we | 158 // we wait until we absolutely need to put something onto disk before we |
159 // do so. | 159 // do so. |
160 return false; | 160 return false; |
161 } | 161 } |
162 | 162 |
163 db_.reset(new sql::Connection()); | 163 db_.reset(new sql::Connection()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 DCHECK(IsOpen()); | 250 DCHECK(IsOpen()); |
251 | 251 |
252 return db_->Execute( | 252 return db_->Execute( |
253 "CREATE TABLE ItemTable (" | 253 "CREATE TABLE ItemTable (" |
254 "key TEXT UNIQUE ON CONFLICT REPLACE, " | 254 "key TEXT UNIQUE ON CONFLICT REPLACE, " |
255 "value BLOB NOT NULL ON CONFLICT FAIL)"); | 255 "value BLOB NOT NULL ON CONFLICT FAIL)"); |
256 } | 256 } |
257 | 257 |
258 bool DomStorageDatabase::DeleteFileAndRecreate() { | 258 bool DomStorageDatabase::DeleteFileAndRecreate() { |
259 DCHECK(!IsOpen()); | 259 DCHECK(!IsOpen()); |
260 DCHECK(file_util::PathExists(file_path_)); | 260 DCHECK(base::PathExists(file_path_)); |
261 | 261 |
262 // We should only try and do this once. | 262 // We should only try and do this once. |
263 if (tried_to_recreate_) | 263 if (tried_to_recreate_) |
264 return false; | 264 return false; |
265 | 265 |
266 tried_to_recreate_ = true; | 266 tried_to_recreate_ = true; |
267 | 267 |
268 // If it's not a directory and we can delete the file, try and open it again. | 268 // If it's not a directory and we can delete the file, try and open it again. |
269 if (!file_util::DirectoryExists(file_path_) && | 269 if (!file_util::DirectoryExists(file_path_) && |
270 sql::Connection::Delete(file_path_)) { | 270 sql::Connection::Delete(file_path_)) { |
(...skipping 28 matching lines...) Expand all Loading... |
299 CreateTableV2() && | 299 CreateTableV2() && |
300 CommitChanges(false, values) && | 300 CommitChanges(false, values) && |
301 migration.Commit(); | 301 migration.Commit(); |
302 } | 302 } |
303 | 303 |
304 void DomStorageDatabase::Close() { | 304 void DomStorageDatabase::Close() { |
305 db_.reset(NULL); | 305 db_.reset(NULL); |
306 } | 306 } |
307 | 307 |
308 } // namespace dom_storage | 308 } // namespace dom_storage |
OLD | NEW |