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