OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/dom_storage/local_storage_context_mojo.h" | 5 #include "content/browser/dom_storage/local_storage_context_mojo.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "components/leveldb/public/cpp/util.h" | 11 #include "components/leveldb/public/cpp/util.h" |
12 #include "components/leveldb/public/interfaces/leveldb.mojom.h" | 12 #include "components/leveldb/public/interfaces/leveldb.mojom.h" |
13 #include "content/browser/dom_storage/dom_storage_area.h" | 13 #include "content/browser/dom_storage/dom_storage_area.h" |
14 #include "content/browser/dom_storage/dom_storage_database.h" | 14 #include "content/browser/dom_storage/dom_storage_database.h" |
15 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 15 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
16 #include "content/browser/dom_storage/local_storage_database.pb.h" | 16 #include "content/browser/dom_storage/local_storage_database.pb.h" |
17 #include "content/browser/leveldb_wrapper_impl.h" | 17 #include "content/browser/leveldb_wrapper_impl.h" |
18 #include "content/common/dom_storage/dom_storage_types.h" | 18 #include "content/common/dom_storage/dom_storage_types.h" |
19 #include "content/public/browser/local_storage_usage_info.h" | 19 #include "content/public/browser/local_storage_usage_info.h" |
20 #include "services/file/public/interfaces/constants.mojom.h" | 20 #include "services/file/public/interfaces/constants.mojom.h" |
21 #include "services/service_manager/public/cpp/connector.h" | 21 #include "services/service_manager/public/cpp/connector.h" |
22 #include "sql/connection.h" | 22 #include "sql/connection.h" |
| 23 #include "storage/browser/quota/special_storage_policy.h" |
23 #include "third_party/leveldatabase/env_chromium.h" | 24 #include "third_party/leveldatabase/env_chromium.h" |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 | 27 |
27 // LevelDB database schema | 28 // LevelDB database schema |
28 // ======================= | 29 // ======================= |
29 // | 30 // |
30 // Version 1 (in sorted order): | 31 // Version 1 (in sorted order): |
31 // key: "VERSION" | 32 // key: "VERSION" |
32 // value: "1" | 33 // value: "1" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // called and need access to the LevelDBWrapperImpl instance. The unique_ptr | 231 // called and need access to the LevelDBWrapperImpl instance. The unique_ptr |
231 // could already be null, but this field should still be valid. | 232 // could already be null, but this field should still be valid. |
232 LevelDBWrapperImpl* level_db_wrapper_ptr_; | 233 LevelDBWrapperImpl* level_db_wrapper_ptr_; |
233 bool deleted_old_data_ = false; | 234 bool deleted_old_data_ = false; |
234 }; | 235 }; |
235 | 236 |
236 LocalStorageContextMojo::LocalStorageContextMojo( | 237 LocalStorageContextMojo::LocalStorageContextMojo( |
237 service_manager::Connector* connector, | 238 service_manager::Connector* connector, |
238 scoped_refptr<DOMStorageTaskRunner> task_runner, | 239 scoped_refptr<DOMStorageTaskRunner> task_runner, |
239 const base::FilePath& old_localstorage_path, | 240 const base::FilePath& old_localstorage_path, |
240 const base::FilePath& subdirectory) | 241 const base::FilePath& subdirectory, |
241 : connector_(connector), | 242 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy) |
| 243 : connector_(connector ? connector->Clone() : nullptr), |
242 subdirectory_(subdirectory), | 244 subdirectory_(subdirectory), |
| 245 special_storage_policy_(std::move(special_storage_policy)), |
243 task_runner_(std::move(task_runner)), | 246 task_runner_(std::move(task_runner)), |
244 old_localstorage_path_(old_localstorage_path), | 247 old_localstorage_path_(old_localstorage_path), |
245 weak_ptr_factory_(this) {} | 248 weak_ptr_factory_(this) {} |
246 | 249 |
247 LocalStorageContextMojo::~LocalStorageContextMojo() {} | |
248 | |
249 void LocalStorageContextMojo::OpenLocalStorage( | 250 void LocalStorageContextMojo::OpenLocalStorage( |
250 const url::Origin& origin, | 251 const url::Origin& origin, |
251 mojom::LevelDBWrapperRequest request) { | 252 mojom::LevelDBWrapperRequest request) { |
252 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::BindLocalStorage, | 253 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::BindLocalStorage, |
253 weak_ptr_factory_.GetWeakPtr(), origin, | 254 weak_ptr_factory_.GetWeakPtr(), origin, |
254 std::move(request))); | 255 std::move(request))); |
255 } | 256 } |
256 | 257 |
257 void LocalStorageContextMojo::GetStorageUsage( | 258 void LocalStorageContextMojo::GetStorageUsage( |
258 GetStorageUsageCallback callback) { | 259 GetStorageUsageCallback callback) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 void LocalStorageContextMojo::Flush() { | 293 void LocalStorageContextMojo::Flush() { |
293 if (connection_state_ != CONNECTION_FINISHED) { | 294 if (connection_state_ != CONNECTION_FINISHED) { |
294 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::Flush, | 295 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::Flush, |
295 weak_ptr_factory_.GetWeakPtr())); | 296 weak_ptr_factory_.GetWeakPtr())); |
296 return; | 297 return; |
297 } | 298 } |
298 for (const auto& it : level_db_wrappers_) | 299 for (const auto& it : level_db_wrappers_) |
299 it.second->level_db_wrapper()->ScheduleImmediateCommit(); | 300 it.second->level_db_wrapper()->ScheduleImmediateCommit(); |
300 } | 301 } |
301 | 302 |
| 303 void LocalStorageContextMojo::ShutdownAndDelete() { |
| 304 DCHECK_NE(connection_state_, CONNECTION_SHUTDOWN); |
| 305 |
| 306 // Nothing to do if no connection to the database was ever finished. |
| 307 if (connection_state_ != CONNECTION_FINISHED) { |
| 308 connection_state_ = CONNECTION_SHUTDOWN; |
| 309 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 310 return; |
| 311 } |
| 312 |
| 313 connection_state_ = CONNECTION_SHUTDOWN; |
| 314 |
| 315 // Flush any uncommitted data. |
| 316 for (const auto& it : level_db_wrappers_) |
| 317 it.second->level_db_wrapper()->ScheduleImmediateCommit(); |
| 318 |
| 319 // Respect the content policy settings about what to |
| 320 // keep and what to discard. |
| 321 if (force_keep_session_state_) { |
| 322 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 323 return; // Keep everything. |
| 324 } |
| 325 |
| 326 bool has_session_only_origins = |
| 327 special_storage_policy_.get() && |
| 328 special_storage_policy_->HasSessionOnlyOrigins(); |
| 329 |
| 330 if (has_session_only_origins) { |
| 331 RetrieveStorageUsage( |
| 332 base::BindOnce(&LocalStorageContextMojo::OnGotStorageUsageForShutdown, |
| 333 base::Unretained(this))); |
| 334 } else { |
| 335 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 336 } |
| 337 } |
| 338 |
302 void LocalStorageContextMojo::PurgeMemory() { | 339 void LocalStorageContextMojo::PurgeMemory() { |
303 for (const auto& it : level_db_wrappers_) | 340 for (const auto& it : level_db_wrappers_) |
304 it.second->level_db_wrapper()->PurgeMemory(); | 341 it.second->level_db_wrapper()->PurgeMemory(); |
305 } | 342 } |
306 | 343 |
307 leveldb::mojom::LevelDBDatabaseAssociatedRequest | 344 leveldb::mojom::LevelDBDatabaseAssociatedRequest |
308 LocalStorageContextMojo::DatabaseRequestForTesting() { | 345 LocalStorageContextMojo::DatabaseRequestForTesting() { |
309 DCHECK_EQ(connection_state_, NO_CONNECTION); | 346 DCHECK_EQ(connection_state_, NO_CONNECTION); |
310 connection_state_ = CONNECTION_IN_PROGRESS; | 347 connection_state_ = CONNECTION_IN_PROGRESS; |
311 leveldb::mojom::LevelDBDatabaseAssociatedRequest request = | 348 leveldb::mojom::LevelDBDatabaseAssociatedRequest request = |
312 MakeIsolatedRequest(&database_); | 349 MakeIsolatedRequest(&database_); |
313 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK); | 350 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK); |
314 return request; | 351 return request; |
315 } | 352 } |
316 | 353 |
317 // static | 354 // static |
318 std::vector<uint8_t> LocalStorageContextMojo::MigrateString( | 355 std::vector<uint8_t> LocalStorageContextMojo::MigrateString( |
319 const base::string16& input) { | 356 const base::string16& input) { |
320 static const uint8_t kUTF16Format = 0; | 357 static const uint8_t kUTF16Format = 0; |
321 | 358 |
322 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); | 359 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); |
323 std::vector<uint8_t> result; | 360 std::vector<uint8_t> result; |
324 result.reserve(input.size() * sizeof(base::char16) + 1); | 361 result.reserve(input.size() * sizeof(base::char16) + 1); |
325 result.push_back(kUTF16Format); | 362 result.push_back(kUTF16Format); |
326 result.insert(result.end(), data, data + input.size() * sizeof(base::char16)); | 363 result.insert(result.end(), data, data + input.size() * sizeof(base::char16)); |
327 return result; | 364 return result; |
328 } | 365 } |
329 | 366 |
| 367 LocalStorageContextMojo::~LocalStorageContextMojo() { |
| 368 DCHECK_EQ(connection_state_, CONNECTION_SHUTDOWN); |
| 369 } |
| 370 |
330 void LocalStorageContextMojo::RunWhenConnected(base::OnceClosure callback) { | 371 void LocalStorageContextMojo::RunWhenConnected(base::OnceClosure callback) { |
| 372 DCHECK_NE(connection_state_, CONNECTION_SHUTDOWN); |
| 373 |
331 // If we don't have a filesystem_connection_, we'll need to establish one. | 374 // If we don't have a filesystem_connection_, we'll need to establish one. |
332 if (connection_state_ == NO_CONNECTION) { | 375 if (connection_state_ == NO_CONNECTION) { |
333 connection_state_ = CONNECTION_IN_PROGRESS; | 376 connection_state_ = CONNECTION_IN_PROGRESS; |
334 InitiateConnection(); | 377 InitiateConnection(); |
335 } | 378 } |
336 | 379 |
337 if (connection_state_ == CONNECTION_IN_PROGRESS) { | 380 if (connection_state_ == CONNECTION_IN_PROGRESS) { |
338 // Queue this OpenLocalStorage call for when we have a level db pointer. | 381 // Queue this OpenLocalStorage call for when we have a level db pointer. |
339 on_database_opened_callbacks_.push_back(std::move(callback)); | 382 on_database_opened_callbacks_.push_back(std::move(callback)); |
340 return; | 383 return; |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 std::vector<LocalStorageUsageInfo> usage) { | 645 std::vector<LocalStorageUsageInfo> usage) { |
603 for (const auto& info : usage) { | 646 for (const auto& info : usage) { |
604 url::Origin origin_candidate(info.origin); | 647 url::Origin origin_candidate(info.origin); |
605 if (!origin_candidate.IsSameOriginWith(origin) && | 648 if (!origin_candidate.IsSameOriginWith(origin) && |
606 origin_candidate.IsSamePhysicalOriginWith(origin)) | 649 origin_candidate.IsSamePhysicalOriginWith(origin)) |
607 DeleteStorage(origin_candidate); | 650 DeleteStorage(origin_candidate); |
608 } | 651 } |
609 DeleteStorage(origin); | 652 DeleteStorage(origin); |
610 } | 653 } |
611 | 654 |
| 655 void LocalStorageContextMojo::OnGotStorageUsageForShutdown( |
| 656 std::vector<LocalStorageUsageInfo> usage) { |
| 657 std::vector<leveldb::mojom::BatchedOperationPtr> operations; |
| 658 for (const auto& info : usage) { |
| 659 if (special_storage_policy_->IsStorageProtected(info.origin)) |
| 660 continue; |
| 661 if (!special_storage_policy_->IsStorageSessionOnly(info.origin)) |
| 662 continue; |
| 663 |
| 664 AddDeleteOriginOperations(&operations, url::Origin(info.origin)); |
| 665 } |
| 666 |
| 667 if (!operations.empty()) { |
| 668 database_->Write(std::move(operations), |
| 669 base::Bind(&LocalStorageContextMojo::OnShutdownComplete, |
| 670 base::Unretained(this))); |
| 671 } else { |
| 672 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 673 } |
| 674 } |
| 675 |
| 676 void LocalStorageContextMojo::OnShutdownComplete( |
| 677 leveldb::mojom::DatabaseError error) { |
| 678 delete this; |
| 679 } |
| 680 |
612 } // namespace content | 681 } // namespace content |
OLD | NEW |