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