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 "base/bind.h" | |
6 #include "base/location.h" | |
7 #include "base/task_runner.h" | |
8 #include "content/browser/indexed_db/indexed_db_active_blob_registry.h" | |
9 #include "content/browser/indexed_db/indexed_db_backing_store.h" | |
10 #include "content/browser/indexed_db/indexed_db_factory.h" | |
11 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" | |
12 | |
13 namespace content { | |
14 | |
15 IndexedDBActiveBlobRegistry::IndexedDBActiveBlobRegistry( | |
16 IndexedDBBackingStore* backing_store) | |
17 : backing_store_(backing_store), | |
18 weak_factory_(this) { | |
19 } | |
20 | |
21 IndexedDBActiveBlobRegistry::~IndexedDBActiveBlobRegistry() { | |
22 DCHECK(use_tracker_.empty()); | |
23 } | |
24 | |
25 void IndexedDBActiveBlobRegistry::AddBlobRef( | |
26 int64 database_id, int64 blob_key) { | |
27 DCHECK(backing_store_->task_runner()->RunsTasksOnCurrentThread()); | |
28 DCHECK(KeyPrefix::IsValidDatabaseId(database_id)); | |
29 DCHECK(DatabaseMetaDataKey::IsValidBlobKey(blob_key)); | |
30 DCHECK(deleted_dbs_.end() == deleted_dbs_.find(database_id)); | |
31 bool need_ref = use_tracker_.empty(); | |
32 SingleDBMap& single_db_map = use_tracker_[database_id]; | |
33 SingleDBMap::iterator iter = single_db_map.find(blob_key); | |
34 if (iter == single_db_map.end()) { | |
35 single_db_map[blob_key] = false; | |
36 if (need_ref) | |
jsbell
2013/12/18 23:04:40
Nit: braces around multi-line clauses (even if sin
ericu
2013/12/19 05:19:11
Done.
| |
37 backing_store_->factory()->ReportOutstandingBlobs( | |
38 backing_store_->origin_url(), true); | |
39 } else { | |
40 DCHECK(!need_ref); | |
41 DCHECK(!iter->second); // You can't add a reference once it's been deleted. | |
42 } | |
43 } | |
44 | |
45 void IndexedDBActiveBlobRegistry::ReleaseBlobRef( | |
46 int64 database_id, int64 blob_key) { | |
47 DCHECK(backing_store_->task_runner()->RunsTasksOnCurrentThread()); | |
48 DCHECK(KeyPrefix::IsValidDatabaseId(database_id)); | |
49 DCHECK(DatabaseMetaDataKey::IsValidBlobKey(blob_key)); | |
50 AllDBsMap::iterator db_pair = use_tracker_.find(database_id); | |
51 if (db_pair == use_tracker_.end()) { | |
52 NOTREACHED(); | |
53 return; | |
54 } | |
55 SingleDBMap& single_db = db_pair->second; | |
56 SingleDBMap::iterator blob_pair = single_db.find(blob_key); | |
57 if (blob_pair == single_db.end()) { | |
58 NOTREACHED(); | |
59 return; | |
60 } | |
61 bool delete_in_backend = false; | |
62 DeletedDBSet::iterator db_to_delete = deleted_dbs_.find(database_id); | |
63 bool db_marked_for_deletion = db_to_delete != deleted_dbs_.end(); | |
64 // Don't bother deleting the file if we're going to delete its whole | |
65 // database directory soon. | |
66 delete_in_backend = blob_pair->second && !db_marked_for_deletion; | |
67 single_db.erase(blob_pair); | |
68 if (single_db.empty()) { | |
69 use_tracker_.erase(db_pair); | |
70 if (db_marked_for_deletion) { | |
71 delete_in_backend = true; | |
72 blob_key = DatabaseMetaDataKey::kAllBlobsKey; | |
73 deleted_dbs_.erase(db_to_delete); | |
74 } | |
75 } | |
76 if (delete_in_backend) | |
77 backing_store_->ReportBlobUnused(database_id, blob_key); | |
78 if (use_tracker_.empty()) | |
jsbell
2013/12/18 23:04:40
Ditto, re: braces around multi-line clause
ericu
2013/12/19 05:19:11
Done.
| |
79 backing_store_->factory()->ReportOutstandingBlobs( | |
80 backing_store_->origin_url(), false); | |
81 } | |
82 | |
83 bool IndexedDBActiveBlobRegistry::MarkDeletedCheckIfUsed( | |
84 int64 database_id, int64 blob_key) { | |
85 DCHECK(backing_store_->task_runner()->RunsTasksOnCurrentThread()); | |
86 DCHECK(KeyPrefix::IsValidDatabaseId(database_id)); | |
87 AllDBsMap::iterator db_pair = use_tracker_.find(database_id); | |
88 if (db_pair == use_tracker_.end()) | |
89 return false; | |
90 | |
91 if (blob_key == DatabaseMetaDataKey::kAllBlobsKey) { | |
92 deleted_dbs_.insert(database_id); | |
93 return true; | |
94 } | |
95 | |
96 SingleDBMap& single_db = db_pair->second; | |
97 SingleDBMap::iterator iter = single_db.find(blob_key); | |
98 if (iter == single_db.end()) | |
99 return false; | |
100 | |
101 iter->second = true; | |
102 return true; | |
103 } | |
104 | |
105 void IndexedDBActiveBlobRegistry::ReleaseBlobRefThreadSafe( | |
106 scoped_refptr<base::TaskRunner> task_runner, | |
107 base::WeakPtr<IndexedDBActiveBlobRegistry> weak_ptr, int64 database_id, | |
108 int64 blob_key, const base::FilePath& unused) { | |
109 task_runner->PostTask( | |
110 FROM_HERE, | |
111 base::Bind( | |
112 &IndexedDBActiveBlobRegistry::ReleaseBlobRef, weak_ptr, | |
113 database_id, blob_key)); | |
114 } | |
115 | |
116 webkit_blob::ShareableFileReference::FinalReleaseCallback | |
117 IndexedDBActiveBlobRegistry::GetFinalReleaseCallback( | |
118 int64 database_id, int64 blob_key) { | |
119 return base::Bind( | |
120 &IndexedDBActiveBlobRegistry::ReleaseBlobRefThreadSafe, | |
121 scoped_refptr<base::TaskRunner>(backing_store_->task_runner()), | |
122 weak_factory_.GetWeakPtr(), database_id, blob_key); | |
123 } | |
124 | |
125 base::Closure IndexedDBActiveBlobRegistry::GetAddBlobRefCallback( | |
126 int64 database_id, int64 blob_key) { | |
127 return base::Bind( | |
128 &IndexedDBActiveBlobRegistry::AddBlobRef, | |
129 weak_factory_.GetWeakPtr(), database_id, blob_key); | |
130 } | |
131 | |
132 void IndexedDBActiveBlobRegistry::ForceShutdown() { | |
133 weak_factory_.InvalidateWeakPtrs(); | |
134 if (!use_tracker_.empty()) { | |
135 use_tracker_.clear(); | |
136 } | |
137 } | |
138 | |
139 } // namespace content | |
OLD | NEW |