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_tracing.h" | 10 #include "content/browser/indexed_db/indexed_db_tracing.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 void IndexedDBFactory::RemoveIDBDatabaseBackend( | 37 void IndexedDBFactory::RemoveIDBDatabaseBackend( |
38 const IndexedDBDatabase::Identifier& unique_identifier) { | 38 const IndexedDBDatabase::Identifier& unique_identifier) { |
39 DCHECK(database_map_.find(unique_identifier) != database_map_.end()); | 39 DCHECK(database_map_.find(unique_identifier) != database_map_.end()); |
40 database_map_.erase(unique_identifier); | 40 database_map_.erase(unique_identifier); |
41 } | 41 } |
42 | 42 |
43 void IndexedDBFactory::GetDatabaseNames( | 43 void IndexedDBFactory::GetDatabaseNames( |
44 scoped_refptr<IndexedDBCallbacks> callbacks, | 44 scoped_refptr<IndexedDBCallbacks> callbacks, |
45 const std::string& origin_identifier, | 45 const std::string& origin_identifier, |
46 const base::FilePath& data_directory) { | 46 const base::FilePath& data_directory, |
| 47 base::TaskRunner* task_runner) { |
47 IDB_TRACE("IndexedDBFactory::GetDatabaseNames"); | 48 IDB_TRACE("IndexedDBFactory::GetDatabaseNames"); |
48 // TODO(dgrogan): Plumb data_loss back to script eventually? | 49 // TODO(dgrogan): Plumb data_loss back to script eventually? |
49 WebKit::WebIDBCallbacks::DataLoss data_loss; | 50 WebKit::WebIDBCallbacks::DataLoss data_loss; |
50 scoped_refptr<IndexedDBBackingStore> backing_store = | 51 scoped_refptr<IndexedDBBackingStore> backing_store = |
51 OpenBackingStore(origin_identifier, data_directory, &data_loss); | 52 OpenBackingStore(origin_identifier, data_directory, NULL, &data_loss, |
| 53 task_runner); |
52 if (!backing_store) { | 54 if (!backing_store) { |
53 callbacks->OnError( | 55 callbacks->OnError( |
54 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError, | 56 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError, |
55 "Internal error opening backing store for " | 57 "Internal error opening backing store for " |
56 "indexedDB.webkitGetDatabaseNames.")); | 58 "indexedDB.webkitGetDatabaseNames.")); |
57 return; | 59 return; |
58 } | 60 } |
59 | 61 |
60 callbacks->OnSuccess(backing_store->GetDatabaseNames()); | 62 callbacks->OnSuccess(backing_store->GetDatabaseNames()); |
61 } | 63 } |
62 | 64 |
63 void IndexedDBFactory::DeleteDatabase( | 65 void IndexedDBFactory::DeleteDatabase( |
64 const string16& name, | 66 const string16& name, |
| 67 net::URLRequestContext* request_context, |
65 scoped_refptr<IndexedDBCallbacks> callbacks, | 68 scoped_refptr<IndexedDBCallbacks> callbacks, |
66 const std::string& origin_identifier, | 69 const std::string& origin_identifier, |
67 const base::FilePath& data_directory) { | 70 const base::FilePath& data_directory, |
| 71 base::TaskRunner* task_runner) { |
68 IDB_TRACE("IndexedDBFactory::DeleteDatabase"); | 72 IDB_TRACE("IndexedDBFactory::DeleteDatabase"); |
69 IndexedDBDatabase::Identifier unique_identifier(origin_identifier, name); | 73 IndexedDBDatabase::Identifier unique_identifier(origin_identifier, name); |
70 IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier); | 74 IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier); |
71 if (it != database_map_.end()) { | 75 if (it != database_map_.end()) { |
72 // If there are any connections to the database, directly delete the | 76 // If there are any connections to the database, directly delete the |
73 // database. | 77 // database. |
74 it->second->DeleteDatabase(callbacks); | 78 it->second->DeleteDatabase(callbacks); |
75 return; | 79 return; |
76 } | 80 } |
77 | 81 |
78 // TODO(dgrogan): Plumb data_loss back to script eventually? | 82 // TODO(dgrogan): Plumb data_loss back to script eventually? |
79 WebKit::WebIDBCallbacks::DataLoss data_loss; | 83 WebKit::WebIDBCallbacks::DataLoss data_loss; |
80 scoped_refptr<IndexedDBBackingStore> backing_store = | 84 scoped_refptr<IndexedDBBackingStore> backing_store = |
81 OpenBackingStore(origin_identifier, data_directory, &data_loss); | 85 OpenBackingStore(origin_identifier, data_directory, request_context, |
| 86 &data_loss, task_runner); |
82 if (!backing_store) { | 87 if (!backing_store) { |
83 callbacks->OnError( | 88 callbacks->OnError( |
84 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError, | 89 IndexedDBDatabaseError(WebKit::WebIDBDatabaseExceptionUnknownError, |
85 ASCIIToUTF16( | 90 ASCIIToUTF16( |
86 "Internal error opening backing store " | 91 "Internal error opening backing store " |
87 "for indexedDB.deleteDatabase."))); | 92 "for indexedDB.deleteDatabase."))); |
88 return; | 93 return; |
89 } | 94 } |
90 | 95 |
91 scoped_refptr<IndexedDBDatabase> database = | 96 scoped_refptr<IndexedDBDatabase> database = |
92 IndexedDBDatabase::Create(name, backing_store, this, unique_identifier); | 97 IndexedDBDatabase::Create(name, backing_store, this, unique_identifier); |
93 if (!database) { | 98 if (!database) { |
94 callbacks->OnError(IndexedDBDatabaseError( | 99 callbacks->OnError(IndexedDBDatabaseError( |
95 WebKit::WebIDBDatabaseExceptionUnknownError, | 100 WebKit::WebIDBDatabaseExceptionUnknownError, |
96 ASCIIToUTF16( | 101 ASCIIToUTF16( |
97 "Internal error creating database backend for " | 102 "Internal error creating database backend for " |
98 "indexedDB.deleteDatabase."))); | 103 "indexedDB.deleteDatabase."))); |
99 return; | 104 return; |
100 } | 105 } |
101 | 106 |
102 database_map_[unique_identifier] = database; | 107 database_map_[unique_identifier] = database; |
103 database->DeleteDatabase(callbacks); | 108 database->DeleteDatabase(callbacks); |
104 database_map_.erase(unique_identifier); | 109 database_map_.erase(unique_identifier); |
105 } | 110 } |
106 | 111 |
107 scoped_refptr<IndexedDBBackingStore> IndexedDBFactory::OpenBackingStore( | 112 scoped_refptr<IndexedDBBackingStore> IndexedDBFactory::OpenBackingStore( |
108 const std::string& origin_identifier, | 113 const std::string& origin_identifier, |
109 const base::FilePath& data_directory, | 114 const base::FilePath& data_directory, |
110 WebKit::WebIDBCallbacks::DataLoss* data_loss) { | 115 net::URLRequestContext* request_context, |
| 116 WebKit::WebIDBCallbacks::DataLoss* data_loss, |
| 117 base::TaskRunner* task_runner) { |
111 const std::string file_identifier = ComputeFileIdentifier(origin_identifier); | 118 const std::string file_identifier = ComputeFileIdentifier(origin_identifier); |
112 const bool open_in_memory = data_directory.empty(); | 119 const bool open_in_memory = data_directory.empty(); |
113 | 120 |
114 IndexedDBBackingStoreMap::iterator it2 = | 121 IndexedDBBackingStoreMap::iterator it2 = |
115 backing_store_map_.find(file_identifier); | 122 backing_store_map_.find(file_identifier); |
116 if (it2 != backing_store_map_.end() && it2->second.get()) | 123 if (it2 != backing_store_map_.end() && it2->second.get()) |
117 return it2->second.get(); | 124 return it2->second.get(); |
118 | 125 |
119 scoped_refptr<IndexedDBBackingStore> backing_store; | 126 scoped_refptr<IndexedDBBackingStore> backing_store; |
| 127 bool first_time = false; |
120 if (open_in_memory) { | 128 if (open_in_memory) { |
| 129 // TODO(ericu): Support blobs in in-memory backends. |
121 backing_store = IndexedDBBackingStore::OpenInMemory(file_identifier); | 130 backing_store = IndexedDBBackingStore::OpenInMemory(file_identifier); |
122 } else { | 131 } else { |
| 132 first_time = !backends_opened_since_boot_.count(file_identifier); |
123 backing_store = IndexedDBBackingStore::Open( | 133 backing_store = IndexedDBBackingStore::Open( |
124 origin_identifier, data_directory, file_identifier, data_loss); | 134 origin_identifier, data_directory, file_identifier, request_context, |
| 135 data_loss, task_runner, first_time); |
125 } | 136 } |
126 | 137 |
127 if (backing_store.get()) { | 138 if (backing_store.get()) { |
| 139 if (first_time) |
| 140 backends_opened_since_boot_.insert(file_identifier); |
128 CleanWeakMap(&backing_store_map_); | 141 CleanWeakMap(&backing_store_map_); |
129 backing_store_map_[file_identifier] = backing_store->GetWeakPtr(); | 142 backing_store_map_[file_identifier] = backing_store->GetWeakPtr(); |
130 // If an in-memory database, bind lifetime to this factory instance. | 143 // If an in-memory database, bind lifetime to this factory instance. |
131 if (open_in_memory) | 144 if (open_in_memory) |
132 session_only_backing_stores_.insert(backing_store); | 145 session_only_backing_stores_.insert(backing_store); |
133 | 146 |
134 // All backing stores associated with this factory should be of the same | 147 // All backing stores associated with this factory should be of the same |
135 // type. | 148 // type. |
136 DCHECK(session_only_backing_stores_.empty() || open_in_memory); | 149 DCHECK(session_only_backing_stores_.empty() || open_in_memory); |
137 | 150 |
138 return backing_store; | 151 return backing_store; |
139 } | 152 } |
140 | 153 |
141 return 0; | 154 return 0; |
142 } | 155 } |
143 | 156 |
144 void IndexedDBFactory::Open( | 157 void IndexedDBFactory::Open( |
145 const string16& name, | 158 const string16& name, |
146 int64 version, | 159 int64 version, |
| 160 net::URLRequestContext* request_context, |
147 int64 transaction_id, | 161 int64 transaction_id, |
148 scoped_refptr<IndexedDBCallbacks> callbacks, | 162 scoped_refptr<IndexedDBCallbacks> callbacks, |
149 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, | 163 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, |
150 const std::string& origin_identifier, | 164 const std::string& origin_identifier, |
151 const base::FilePath& data_directory) { | 165 const base::FilePath& data_directory, |
| 166 int child_process_id, |
| 167 base::TaskRunner* task_runner) { |
152 IDB_TRACE("IndexedDBFactory::Open"); | 168 IDB_TRACE("IndexedDBFactory::Open"); |
153 scoped_refptr<IndexedDBDatabase> database; | 169 scoped_refptr<IndexedDBDatabase> database; |
154 IndexedDBDatabase::Identifier unique_identifier(origin_identifier, name); | 170 IndexedDBDatabase::Identifier unique_identifier(origin_identifier, name); |
155 IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier); | 171 IndexedDBDatabaseMap::iterator it = database_map_.find(unique_identifier); |
156 WebKit::WebIDBCallbacks::DataLoss data_loss = | 172 WebKit::WebIDBCallbacks::DataLoss data_loss = |
157 WebKit::WebIDBCallbacks::DataLossNone; | 173 WebKit::WebIDBCallbacks::DataLossNone; |
158 if (it == database_map_.end()) { | 174 if (it == database_map_.end()) { |
159 scoped_refptr<IndexedDBBackingStore> backing_store = | 175 scoped_refptr<IndexedDBBackingStore> backing_store = |
160 OpenBackingStore(origin_identifier, data_directory, &data_loss); | 176 OpenBackingStore(origin_identifier, data_directory, request_context, |
| 177 &data_loss, task_runner); |
161 if (!backing_store) { | 178 if (!backing_store) { |
162 callbacks->OnError(IndexedDBDatabaseError( | 179 callbacks->OnError(IndexedDBDatabaseError( |
163 WebKit::WebIDBDatabaseExceptionUnknownError, | 180 WebKit::WebIDBDatabaseExceptionUnknownError, |
164 ASCIIToUTF16( | 181 ASCIIToUTF16( |
165 "Internal error opening backing store for indexedDB.open."))); | 182 "Internal error opening backing store for indexedDB.open."))); |
166 return; | 183 return; |
167 } | 184 } |
168 | 185 |
169 database = | 186 database = |
170 IndexedDBDatabase::Create(name, backing_store, this, unique_identifier); | 187 IndexedDBDatabase::Create(name, backing_store, this, unique_identifier); |
171 if (!database) { | 188 if (!database) { |
172 callbacks->OnError(IndexedDBDatabaseError( | 189 callbacks->OnError(IndexedDBDatabaseError( |
173 WebKit::WebIDBDatabaseExceptionUnknownError, | 190 WebKit::WebIDBDatabaseExceptionUnknownError, |
174 ASCIIToUTF16( | 191 ASCIIToUTF16( |
175 "Internal error creating database backend for indexedDB.open."))); | 192 "Internal error creating database backend for indexedDB.open."))); |
176 return; | 193 return; |
177 } | 194 } |
178 | 195 |
179 database_map_[unique_identifier] = database; | 196 database_map_[unique_identifier] = database; |
180 } else { | 197 } else { |
181 database = it->second; | 198 database = it->second; |
182 } | 199 } |
183 | 200 |
184 database->OpenConnection( | 201 database->OpenConnection( |
185 callbacks, database_callbacks, transaction_id, version, data_loss); | 202 callbacks, database_callbacks, child_process_id, transaction_id, version, |
| 203 data_loss); |
186 } | 204 } |
187 | 205 |
188 std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin( | 206 std::vector<IndexedDBDatabase*> IndexedDBFactory::GetOpenDatabasesForOrigin( |
189 const std::string& origin_identifier) const { | 207 const std::string& origin_identifier) const { |
190 std::vector<IndexedDBDatabase*> result; | 208 std::vector<IndexedDBDatabase*> result; |
191 for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin(); | 209 for (IndexedDBDatabaseMap::const_iterator it = database_map_.begin(); |
192 it != database_map_.end(); | 210 it != database_map_.end(); |
193 ++it) { | 211 ++it) { |
194 if (it->first.first == origin_identifier) | 212 if (it->first.first == origin_identifier) |
195 result.push_back(it->second.get()); | 213 result.push_back(it->second.get()); |
196 } | 214 } |
197 return result; | 215 return result; |
198 } | 216 } |
199 | 217 |
200 } // namespace content | 218 } // namespace content |
OLD | NEW |