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

Side by Side Diff: content/browser/indexed_db/indexed_db_factory.cc

Issue 16870007: Switch database/file_identifier to std::string, remove createFromDatabaseIdentifier calls (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 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) 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 #include "content/browser/indexed_db/indexed_db_factory.h" 5 #include "content/browser/indexed_db/indexed_db_factory.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "content/browser/indexed_db/indexed_db_backing_store.h" 9 #include "content/browser/indexed_db/indexed_db_backing_store.h"
10 #include "content/browser/indexed_db/indexed_db_database.h" 10 #include "content/browser/indexed_db/indexed_db_database.h"
11 #include "content/browser/indexed_db/indexed_db_tracing.h" 11 #include "content/browser/indexed_db/indexed_db_tracing.h"
12 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" 12 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
13 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" 13 #include "third_party/WebKit/public/platform/WebIDBDatabaseException.h"
14 14
15 namespace content { 15 namespace content {
16 16
17 template <typename K, typename M> 17 template <typename K, typename M>
18 static void CleanWeakMap(std::map<K, base::WeakPtr<M> >* map) { 18 static void CleanWeakMap(std::map<K, base::WeakPtr<M> >* map) {
19 std::map<K, base::WeakPtr<M> > other; 19 std::map<K, base::WeakPtr<M> > other;
20 other.swap(*map); 20 other.swap(*map);
21 21
22 typename std::map<K, base::WeakPtr<M> >::const_iterator iter = other.begin(); 22 typename std::map<K, base::WeakPtr<M> >::const_iterator iter = other.begin();
23 while (iter != other.end()) { 23 while (iter != other.end()) {
24 if (iter->second.get()) 24 if (iter->second.get())
25 (*map)[iter->first] = iter->second; 25 (*map)[iter->first] = iter->second;
26 ++iter; 26 ++iter;
27 } 27 }
28 } 28 }
29 29
30 static string16 ComputeFileIdentifier(const string16& database_identifier) { 30 static std::string ComputeFileIdentifier(const std::string& origin_identifier) {
31 string16 suffix(ASCIIToUTF16("@1")); 31 return origin_identifier + "@1";
32 string16 result(database_identifier);
33 result.insert(result.end(), suffix.begin(), suffix.end());
34 return result;
35 } 32 }
36 33
37 static string16 ComputeUniqueIdentifier(const string16& name, 34 static string16 ComputeUniqueIdentifier(const string16& name,
jsbell 2013/06/20 16:38:40 There's actually no reason this needs to be a stri
jamesr 2013/06/20 16:54:32 Ah, I had assumed that 'name' was the same as the
38 const string16& database_identifier) { 35 const std::string& origin_identifier) {
39 return ComputeFileIdentifier(database_identifier) + name; 36 return ASCIIToUTF16(ComputeFileIdentifier(origin_identifier)) + name;
40 } 37 }
41 38
42 IndexedDBFactory::IndexedDBFactory() {} 39 IndexedDBFactory::IndexedDBFactory() {}
43 40
44 IndexedDBFactory::~IndexedDBFactory() {} 41 IndexedDBFactory::~IndexedDBFactory() {}
45 42
46 void IndexedDBFactory::RemoveIDBDatabaseBackend( 43 void IndexedDBFactory::RemoveIDBDatabaseBackend(
47 const string16& unique_identifier) { 44 const string16& unique_identifier) {
48 DCHECK(database_backend_map_.find(unique_identifier) != 45 DCHECK(database_backend_map_.find(unique_identifier) !=
49 database_backend_map_.end()); 46 database_backend_map_.end());
50 database_backend_map_.erase(unique_identifier); 47 database_backend_map_.erase(unique_identifier);
51 } 48 }
52 49
53 void IndexedDBFactory::GetDatabaseNames( 50 void IndexedDBFactory::GetDatabaseNames(
54 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, 51 scoped_refptr<IndexedDBCallbacksWrapper> callbacks,
55 const string16& database_identifier, 52 const std::string& origin_identifier,
56 const base::FilePath& data_directory) { 53 const base::FilePath& data_directory) {
57 IDB_TRACE("IndexedDBFactory::get_database_names"); 54 IDB_TRACE("IndexedDBFactory::get_database_names");
58 scoped_refptr<IndexedDBBackingStore> backing_store = 55 scoped_refptr<IndexedDBBackingStore> backing_store =
59 OpenBackingStore(database_identifier, data_directory); 56 OpenBackingStore(origin_identifier, data_directory);
60 if (!backing_store.get()) { 57 if (!backing_store.get()) {
61 callbacks->OnError( 58 callbacks->OnError(
62 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError, 59 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError,
63 "Internal error opening backing store for " 60 "Internal error opening backing store for "
64 "indexedDB.webkitGetDatabaseNames.")); 61 "indexedDB.webkitGetDatabaseNames."));
65 return; 62 return;
66 } 63 }
67 64
68 callbacks->OnSuccess(backing_store->GetDatabaseNames()); 65 callbacks->OnSuccess(backing_store->GetDatabaseNames());
69 } 66 }
70 67
71 void IndexedDBFactory::DeleteDatabase( 68 void IndexedDBFactory::DeleteDatabase(
72 const string16& name, 69 const string16& name,
73 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, 70 scoped_refptr<IndexedDBCallbacksWrapper> callbacks,
74 const string16& database_identifier, 71 const std::string& origin_identifier,
75 const base::FilePath& data_directory) { 72 const base::FilePath& data_directory) {
76 IDB_TRACE("IndexedDBFactory::delete_database"); 73 IDB_TRACE("IndexedDBFactory::delete_database");
77 const string16 unique_identifier = 74 const string16 unique_identifier =
78 ComputeUniqueIdentifier(name, database_identifier); 75 ComputeUniqueIdentifier(name, origin_identifier);
79 76
80 IndexedDBDatabaseMap::iterator it = 77 IndexedDBDatabaseMap::iterator it =
81 database_backend_map_.find(unique_identifier); 78 database_backend_map_.find(unique_identifier);
82 if (it != database_backend_map_.end()) { 79 if (it != database_backend_map_.end()) {
83 // If there are any connections to the database, directly delete the 80 // If there are any connections to the database, directly delete the
84 // database. 81 // database.
85 it->second->DeleteDatabase(callbacks); 82 it->second->DeleteDatabase(callbacks);
86 return; 83 return;
87 } 84 }
88 85
89 // TODO(jsbell): Everything from now on should be done on another thread. 86 // TODO(jsbell): Everything from now on should be done on another thread.
90 scoped_refptr<IndexedDBBackingStore> backing_store = 87 scoped_refptr<IndexedDBBackingStore> backing_store =
91 OpenBackingStore(database_identifier, data_directory); 88 OpenBackingStore(origin_identifier, data_directory);
92 if (!backing_store.get()) { 89 if (!backing_store.get()) {
93 callbacks->OnError(IndexedDBDatabaseError( 90 callbacks->OnError(IndexedDBDatabaseError(
94 WebKit::WebIDBDatabaseExceptionUnknownError, 91 WebKit::WebIDBDatabaseExceptionUnknownError,
95 ASCIIToUTF16("Internal error opening backing store " 92 ASCIIToUTF16("Internal error opening backing store "
96 "for indexed_db.delete_database."))); 93 "for indexed_db.delete_database.")));
97 return; 94 return;
98 } 95 }
99 96
100 scoped_refptr<IndexedDBDatabase> database_backend = IndexedDBDatabase::Create( 97 scoped_refptr<IndexedDBDatabase> database_backend = IndexedDBDatabase::Create(
101 name, backing_store.get(), this, unique_identifier); 98 name, backing_store.get(), this, unique_identifier);
102 if (!database_backend.get()) { 99 if (!database_backend.get()) {
103 callbacks->OnError(IndexedDBDatabaseError( 100 callbacks->OnError(IndexedDBDatabaseError(
104 WebKit::WebIDBDatabaseExceptionUnknownError, 101 WebKit::WebIDBDatabaseExceptionUnknownError,
105 ASCIIToUTF16("Internal error creating database backend for " 102 ASCIIToUTF16("Internal error creating database backend for "
106 "indexed_db.delete_database."))); 103 "indexed_db.delete_database.")));
107 return; 104 return;
108 } 105 }
109 106
110 database_backend_map_[unique_identifier] = database_backend.get(); 107 database_backend_map_[unique_identifier] = database_backend.get();
111 database_backend->DeleteDatabase(callbacks); 108 database_backend->DeleteDatabase(callbacks);
112 database_backend_map_.erase(unique_identifier); 109 database_backend_map_.erase(unique_identifier);
113 } 110 }
114 111
115 scoped_refptr<IndexedDBBackingStore> IndexedDBFactory::OpenBackingStore( 112 scoped_refptr<IndexedDBBackingStore> IndexedDBFactory::OpenBackingStore(
116 const string16& database_identifier, 113 const std::string& origin_identifier,
117 const base::FilePath& data_directory) { 114 const base::FilePath& data_directory) {
118 const string16 file_identifier = ComputeFileIdentifier(database_identifier); 115 const std::string file_identifier = ComputeFileIdentifier(origin_identifier);
119 const bool open_in_memory = data_directory.empty(); 116 const bool open_in_memory = data_directory.empty();
120 117
121 IndexedDBBackingStoreMap::iterator it2 = 118 IndexedDBBackingStoreMap::iterator it2 =
122 backing_store_map_.find(file_identifier); 119 backing_store_map_.find(file_identifier);
123 if (it2 != backing_store_map_.end() && it2->second.get()) 120 if (it2 != backing_store_map_.end() && it2->second.get())
124 return it2->second.get(); 121 return it2->second.get();
125 122
126 scoped_refptr<IndexedDBBackingStore> backing_store; 123 scoped_refptr<IndexedDBBackingStore> backing_store;
127 if (open_in_memory) { 124 if (open_in_memory) {
128 backing_store = IndexedDBBackingStore::OpenInMemory(file_identifier); 125 backing_store = IndexedDBBackingStore::OpenInMemory(file_identifier);
129 } else { 126 } else {
130 backing_store = IndexedDBBackingStore::Open( 127 backing_store = IndexedDBBackingStore::Open(
131 database_identifier, data_directory, file_identifier); 128 origin_identifier, data_directory, file_identifier);
132 } 129 }
133 130
134 if (backing_store.get()) { 131 if (backing_store.get()) {
135 CleanWeakMap(&backing_store_map_); 132 CleanWeakMap(&backing_store_map_);
136 backing_store_map_[file_identifier] = backing_store->GetWeakPtr(); 133 backing_store_map_[file_identifier] = backing_store->GetWeakPtr();
137 // If an in-memory database, bind lifetime to this factory instance. 134 // If an in-memory database, bind lifetime to this factory instance.
138 if (open_in_memory) 135 if (open_in_memory)
139 session_only_backing_stores_.insert(backing_store); 136 session_only_backing_stores_.insert(backing_store);
140 137
141 // All backing stores associated with this factory should be of the same 138 // All backing stores associated with this factory should be of the same
142 // type. 139 // type.
143 DCHECK(session_only_backing_stores_.empty() || open_in_memory); 140 DCHECK(session_only_backing_stores_.empty() || open_in_memory);
144 141
145 return backing_store; 142 return backing_store;
146 } 143 }
147 144
148 return 0; 145 return 0;
149 } 146 }
150 147
151 void IndexedDBFactory::Open( 148 void IndexedDBFactory::Open(
152 const string16& name, 149 const string16& name,
153 int64 version, 150 int64 version,
154 int64 transaction_id, 151 int64 transaction_id,
155 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, 152 scoped_refptr<IndexedDBCallbacksWrapper> callbacks,
156 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks, 153 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks,
157 const string16& database_identifier, 154 const std::string& origin_identifier,
158 const base::FilePath& data_directory) { 155 const base::FilePath& data_directory) {
159 IDB_TRACE("IndexedDBFactory::open"); 156 IDB_TRACE("IndexedDBFactory::open");
160 const string16 unique_identifier = 157 const string16 unique_identifier =
161 ComputeUniqueIdentifier(name, database_identifier); 158 ComputeUniqueIdentifier(name, origin_identifier);
162 159
163 scoped_refptr<IndexedDBDatabase> database_backend; 160 scoped_refptr<IndexedDBDatabase> database_backend;
164 IndexedDBDatabaseMap::iterator it = 161 IndexedDBDatabaseMap::iterator it =
165 database_backend_map_.find(unique_identifier); 162 database_backend_map_.find(unique_identifier);
166 if (it == database_backend_map_.end()) { 163 if (it == database_backend_map_.end()) {
167 scoped_refptr<IndexedDBBackingStore> backing_store = 164 scoped_refptr<IndexedDBBackingStore> backing_store =
168 OpenBackingStore(database_identifier, data_directory); 165 OpenBackingStore(origin_identifier, data_directory);
169 if (!backing_store.get()) { 166 if (!backing_store.get()) {
170 callbacks->OnError(IndexedDBDatabaseError( 167 callbacks->OnError(IndexedDBDatabaseError(
171 WebKit::WebIDBDatabaseExceptionUnknownError, 168 WebKit::WebIDBDatabaseExceptionUnknownError,
172 ASCIIToUTF16( 169 ASCIIToUTF16(
173 "Internal error opening backing store for indexedDB.open."))); 170 "Internal error opening backing store for indexedDB.open.")));
174 return; 171 return;
175 } 172 }
176 173
177 database_backend = IndexedDBDatabase::Create( 174 database_backend = IndexedDBDatabase::Create(
178 name, backing_store.get(), this, unique_identifier); 175 name, backing_store.get(), this, unique_identifier);
179 if (!database_backend.get()) { 176 if (!database_backend.get()) {
180 callbacks->OnError(IndexedDBDatabaseError( 177 callbacks->OnError(IndexedDBDatabaseError(
181 WebKit::WebIDBDatabaseExceptionUnknownError, 178 WebKit::WebIDBDatabaseExceptionUnknownError,
182 ASCIIToUTF16( 179 ASCIIToUTF16(
183 "Internal error creating database backend for indexedDB.open."))); 180 "Internal error creating database backend for indexedDB.open.")));
184 return; 181 return;
185 } 182 }
186 183
187 database_backend_map_[unique_identifier] = database_backend.get(); 184 database_backend_map_[unique_identifier] = database_backend.get();
188 } else { 185 } else {
189 database_backend = it->second; 186 database_backend = it->second;
190 } 187 }
191 188
192 database_backend->OpenConnection( 189 database_backend->OpenConnection(
193 callbacks, database_callbacks, transaction_id, version); 190 callbacks, database_callbacks, transaction_id, version);
194 } 191 }
195 192
196 } // namespace content 193 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698