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), weak_factory_(this) {} | |
18 | |
19 IndexedDBActiveBlobRegistry::~IndexedDBActiveBlobRegistry() { | |
20 DCHECK(use_tracker_.empty()); | |
21 } | |
22 | |
23 void IndexedDBActiveBlobRegistry::AddBlobRef(int64 database_id, | |
24 int64 blob_key) { | |
25 DCHECK(backing_store_->task_runner()->RunsTasksOnCurrentThread()); | |
26 DCHECK(KeyPrefix::IsValidDatabaseId(database_id)); | |
27 DCHECK(DatabaseMetaDataKey::IsValidBlobKey(blob_key)); | |
28 DCHECK(deleted_dbs_.end() == deleted_dbs_.find(database_id)); | |
jsbell
2014/03/20 18:04:33
Could use stl_util's ContainsKey()
ericu
2014/03/25 01:22:14
Done.
| |
29 bool need_ref = use_tracker_.empty(); | |
30 SingleDBMap& single_db_map = use_tracker_[database_id]; | |
31 SingleDBMap::iterator iter = single_db_map.find(blob_key); | |
32 if (iter == single_db_map.end()) { | |
33 single_db_map[blob_key] = false; | |
34 if (need_ref) { | |
35 backing_store_->factory()->ReportOutstandingBlobs( | |
36 backing_store_->origin_url(), true); | |
37 } | |
38 } else { | |
39 DCHECK(!need_ref); | |
40 DCHECK(!iter->second); // You can't add a reference once it's been deleted. | |
41 } | |
42 } | |
43 | |
44 void IndexedDBActiveBlobRegistry::ReleaseBlobRef(int64 database_id, | |
45 int64 blob_key) { | |
46 DCHECK(backing_store_->task_runner()->RunsTasksOnCurrentThread()); | |
47 DCHECK(KeyPrefix::IsValidDatabaseId(database_id)); | |
48 DCHECK(DatabaseMetaDataKey::IsValidBlobKey(blob_key)); | |
49 AllDBsMap::iterator db_pair = use_tracker_.find(database_id); | |
50 if (db_pair == use_tracker_.end()) { | |
51 NOTREACHED(); | |
52 return; | |
53 } | |
54 SingleDBMap& single_db = db_pair->second; | |
55 SingleDBMap::iterator blob_pair = single_db.find(blob_key); | |
56 if (blob_pair == single_db.end()) { | |
57 NOTREACHED(); | |
58 return; | |
59 } | |
60 bool delete_in_backend = false; | |
61 DeletedDBSet::iterator db_to_delete = deleted_dbs_.find(database_id); | |
62 bool db_marked_for_deletion = db_to_delete != deleted_dbs_.end(); | |
63 // Don't bother deleting the file if we're going to delete its whole | |
64 // database directory soon. | |
65 delete_in_backend = blob_pair->second && !db_marked_for_deletion; | |
66 single_db.erase(blob_pair); | |
67 if (single_db.empty()) { | |
68 use_tracker_.erase(db_pair); | |
69 if (db_marked_for_deletion) { | |
70 delete_in_backend = true; | |
71 blob_key = DatabaseMetaDataKey::kAllBlobsKey; | |
72 deleted_dbs_.erase(db_to_delete); | |
73 } | |
74 } | |
75 if (delete_in_backend) | |
76 backing_store_->ReportBlobUnused(database_id, blob_key); | |
77 if (use_tracker_.empty()) { | |
78 backing_store_->factory()->ReportOutstandingBlobs( | |
79 backing_store_->origin_url(), false); | |
80 } | |
81 } | |
82 | |
83 bool IndexedDBActiveBlobRegistry::MarkDeletedCheckIfUsed(int64 database_id, | |
84 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; | |
cmumford
2014/03/24 23:14:35
Should this be a failed DCHECK, or is it a legitim
ericu
2014/03/25 01:22:14
It's legit; the tracker only contains entries for
| |
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, | |
108 int64 database_id, | |
109 int64 blob_key, | |
110 const base::FilePath& unused) { | |
111 task_runner->PostTask(FROM_HERE, | |
112 base::Bind(&IndexedDBActiveBlobRegistry::ReleaseBlobRef, | |
113 weak_ptr, | |
114 database_id, | |
115 blob_key)); | |
116 } | |
117 | |
118 webkit_blob::ShareableFileReference::FinalReleaseCallback | |
119 IndexedDBActiveBlobRegistry::GetFinalReleaseCallback(int64 database_id, | |
120 int64 blob_key) { | |
121 return base::Bind( | |
122 &IndexedDBActiveBlobRegistry::ReleaseBlobRefThreadSafe, | |
123 scoped_refptr<base::TaskRunner>(backing_store_->task_runner()), | |
124 weak_factory_.GetWeakPtr(), | |
125 database_id, | |
126 blob_key); | |
127 } | |
128 | |
129 base::Closure IndexedDBActiveBlobRegistry::GetAddBlobRefCallback( | |
130 int64 database_id, | |
131 int64 blob_key) { | |
132 return base::Bind(&IndexedDBActiveBlobRegistry::AddBlobRef, | |
133 weak_factory_.GetWeakPtr(), | |
134 database_id, | |
135 blob_key); | |
136 } | |
137 | |
138 void IndexedDBActiveBlobRegistry::ForceShutdown() { | |
jsbell
2014/03/20 18:04:33
This doesn't actually "drop its reference to the b
ericu
2014/03/25 01:22:14
Comment: fixed; it dated back from an older way of
| |
139 weak_factory_.InvalidateWeakPtrs(); | |
140 use_tracker_.clear(); | |
141 } | |
142 | |
143 } // namespace content | |
OLD | NEW |