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 const string16& data_directory) { |
| 58 IDB_TRACE("IndexedDBFactoryImpl::get_database_names"); |
| 59 scoped_refptr<IndexedDBBackingStore> backing_store = |
| 60 OpenBackingStore(database_identifier, data_directory); |
| 61 if (!backing_store) { |
| 62 callbacks->OnError(IndexedDBDatabaseError::Create( |
| 63 WebKit::WebIDBDatabaseExceptionUnknownError, |
| 64 ASCIIToUTF16("Internal error opening backing store for " |
| 65 "indexed_db.webkit_get_database_names."))); |
| 66 return; |
| 67 } |
| 68 |
| 69 callbacks->OnSuccess(backing_store->GetDatabaseNames()); |
| 70 } |
| 71 |
| 72 void IndexedDBFactoryImpl::DeleteDatabase( |
| 73 const string16& name, |
| 74 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
| 75 const string16& database_identifier, |
| 76 const string16& data_directory) { |
| 77 IDB_TRACE("IndexedDBFactoryImpl::delete_database"); |
| 78 const string16 unique_identifier = |
| 79 ComputeUniqueIdentifier(name, database_identifier); |
| 80 |
| 81 IndexedDBDatabaseMap::iterator it = |
| 82 database_backend_map_.find(unique_identifier); |
| 83 if (it != database_backend_map_.end()) { |
| 84 // If there are any connections to the database, directly delete the |
| 85 // database. |
| 86 it->second->DeleteDatabase(callbacks); |
| 87 return; |
| 88 } |
| 89 |
| 90 // TODO(jsbell): Everything from now on should be done on another thread. |
| 91 scoped_refptr<IndexedDBBackingStore> backing_store = |
| 92 OpenBackingStore(database_identifier, data_directory); |
| 93 if (!backing_store) { |
| 94 callbacks->OnError(IndexedDBDatabaseError::Create( |
| 95 WebKit::WebIDBDatabaseExceptionUnknownError, |
| 96 ASCIIToUTF16("Internal error opening backing store " |
| 97 "for indexed_db.delete_database."))); |
| 98 return; |
| 99 } |
| 100 |
| 101 scoped_refptr<IndexedDBDatabaseImpl> database_backend = |
| 102 IndexedDBDatabaseImpl::Create( |
| 103 name, backing_store.get(), this, unique_identifier); |
| 104 if (!database_backend) { |
| 105 callbacks->OnError(IndexedDBDatabaseError::Create( |
| 106 WebKit::WebIDBDatabaseExceptionUnknownError, |
| 107 ASCIIToUTF16("Internal error creating database backend for " |
| 108 "indexed_db.delete_database."))); |
| 109 return; |
| 110 } |
| 111 |
| 112 database_backend_map_[unique_identifier] = database_backend.get(); |
| 113 database_backend->DeleteDatabase(callbacks); |
| 114 database_backend_map_.erase(unique_identifier); |
| 115 } |
| 116 |
| 117 scoped_refptr<IndexedDBBackingStore> IndexedDBFactoryImpl::OpenBackingStore( |
| 118 const string16& database_identifier, |
| 119 const string16& data_directory) { |
| 120 const string16 file_identifier = ComputeFileIdentifier(database_identifier); |
| 121 const bool open_in_memory = data_directory.empty(); |
| 122 |
| 123 IndexedDBBackingStoreMap::iterator it2 = |
| 124 backing_store_map_.find(file_identifier); |
| 125 if (it2 != backing_store_map_.end() && it2->second.get()) |
| 126 return it2->second.get(); |
| 127 |
| 128 scoped_refptr<IndexedDBBackingStore> backing_store; |
| 129 if (open_in_memory) { |
| 130 backing_store = IndexedDBBackingStore::OpenInMemory(file_identifier); |
| 131 } else { |
| 132 backing_store = IndexedDBBackingStore::Open( |
| 133 database_identifier, data_directory, file_identifier); |
| 134 } |
| 135 |
| 136 if (backing_store) { |
| 137 CleanWeakMap(backing_store_map_); |
| 138 backing_store_map_[file_identifier] = backing_store->GetWeakPtr(); |
| 139 // If an in-memory database, bind lifetime to this factory instance. |
| 140 if (open_in_memory) |
| 141 session_only_backing_stores_.insert(backing_store); |
| 142 |
| 143 // All backing stores associated with this factory should be of the same |
| 144 // type. |
| 145 DCHECK(session_only_backing_stores_.empty() || open_in_memory); |
| 146 |
| 147 return backing_store; |
| 148 } |
| 149 |
| 150 return 0; |
| 151 } |
| 152 |
| 153 void IndexedDBFactoryImpl::Open( |
| 154 const string16& name, |
| 155 int64_t version, |
| 156 int64_t transaction_id, |
| 157 scoped_refptr<IndexedDBCallbacksWrapper> callbacks, |
| 158 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> database_callbacks, |
| 159 const string16& database_identifier, |
| 160 const string16& data_directory) { |
| 161 IDB_TRACE("IndexedDBFactoryImpl::open"); |
| 162 const string16 unique_identifier = |
| 163 ComputeUniqueIdentifier(name, database_identifier); |
| 164 |
| 165 scoped_refptr<IndexedDBDatabaseImpl> database_backend; |
| 166 IndexedDBDatabaseMap::iterator it = |
| 167 database_backend_map_.find(unique_identifier); |
| 168 if (it == database_backend_map_.end()) { |
| 169 scoped_refptr<IndexedDBBackingStore> backing_store = |
| 170 OpenBackingStore(database_identifier, data_directory); |
| 171 if (!backing_store) { |
| 172 callbacks->OnError(IndexedDBDatabaseError::Create( |
| 173 WebKit::WebIDBDatabaseExceptionUnknownError, |
| 174 ASCIIToUTF16( |
| 175 "Internal error opening backing store for indexedDB.open."))); |
| 176 return; |
| 177 } |
| 178 |
| 179 database_backend = IndexedDBDatabaseImpl::Create( |
| 180 name, backing_store.get(), this, unique_identifier); |
| 181 if (!database_backend) { |
| 182 callbacks->OnError(IndexedDBDatabaseError::Create( |
| 183 WebKit::WebIDBDatabaseExceptionUnknownError, |
| 184 ASCIIToUTF16( |
| 185 "Internal error creating database backend for indexedDB.open."))); |
| 186 return; |
| 187 } |
| 188 |
| 189 database_backend_map_[unique_identifier] = database_backend.get(); |
| 190 } else { |
| 191 database_backend = it->second; |
| 192 } |
| 193 |
| 194 database_backend->OpenConnection( |
| 195 callbacks, database_callbacks, transaction_id, version); |
| 196 } |
| 197 |
| 198 } // namespace content |
OLD | NEW |