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

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

Issue 9159020: Create a class to represent a DOM Storage Database. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address comments and add handling of a corrupt database file in LazyOpen 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_
6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_
7
8 #include <map>
9
10 #include "base/file_path.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/nullable_string16.h"
14 #include "base/string16.h"
15 #include "sql/connection.h"
16
17 namespace dom_storage {
18
19 // Represents a SQLite based backing for DOM storage data. This
20 // class is designed to be used on a single thread.
21 class DomStorageDatabase {
22 public:
23 explicit DomStorageDatabase(const FilePath& file_path);
24 ~DomStorageDatabase();
25
26 typedef std::map<string16, NullableString16> ValuesMap;
27
28 // Reads all the key, value pairs stored in the database and returns
29 // them. |result| is assumed to be empty and any duplicate keys will
30 // be overwritten. If the database exists on disk then it will be
31 // opened. If it does not exist then it will not be created and
32 // |result| will be unmodified.
33 void ReadAllValues(ValuesMap* result);
34
35 // Updates the backing database. Will remove all keys before updating
36 // the database if |clear_all_first| is set. Then all entries in
37 // |changes| will be examined - keys mapped to a null NullableString16
38 // will be removed and all others will be inserted/updated as appropriate.
39 bool CommitChanges(bool clear_all_first, const ValuesMap& changes);
40
41 private:
42 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose);
43 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy);
44 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest,
45 TestLazyOpenUpgradesV1TableToV2);
46 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestFailedUpgrade);
47 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestIsOpen);
48 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleRead);
49 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWrite);
50 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear);
51 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, UpgradeFromV1ToV2NoData);
52 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest,
53 UpgradeFromV1ToV2WithData);
54 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest,
55 TestOpenCloseDataPreserved);
56 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue);
57 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest,
58 TestCanOpenAndReadWebCoreDatabase);
59 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest,
60 TestCanOpenFileThatIsNotADatabase);
61
62 // Open the database at file_path_ if it exists already and creates it if
63 // |create_if_needed| is true.
64 // Ensures we are at the correct database version and creates or updates
65 // tables as necessary. Returns false on failure.
66 bool LazyOpen(bool create_if_needed);
67
68 // Creates the ItemTable at the current version of the database. Used
69 // when creating the table from scratch.
70 bool CreateTable();
71
72 // Creates the database table at V2. Used when upgrading the database
73 // table to V2.
74 bool CreateTableV2();
75
76 // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT
77 // to a BLOB. Exisitng data is preserved on success. Returns false if the
78 // upgrade failed. If true is returned, the database is guaranteed to be at
79 // version 2 (either it already was, or the upgrade from version 1 was
80 // successful).
81 bool UpgradeVersion1To2IfNeeded();
82
83 void Close();
84 bool IsOpen() const { return db_.get() ? db_->is_open() : false; }
85
86 // Path to the database on disk.
87 FilePath file_path_;
88 scoped_ptr<sql::Connection> db_;
89 };
90
91 } // namespace dom_storage
92
93 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698