OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_quota_client.h" | 5 #include "content/browser/indexed_db/indexed_db_quota_client.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 10 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
14 #include "webkit/browser/database/database_util.h" | 13 #include "webkit/browser/database/database_util.h" |
15 | 14 |
16 using quota::QuotaClient; | 15 using quota::QuotaClient; |
17 using webkit_database::DatabaseUtil; | 16 using webkit_database::DatabaseUtil; |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 namespace { | 19 namespace { |
21 | 20 |
22 quota::QuotaStatusCode DeleteOriginDataOnWebKitThread( | 21 quota::QuotaStatusCode DeleteOriginDataOnIndexedDBThread( |
23 IndexedDBContextImpl* context, | 22 IndexedDBContextImpl* context, |
24 const GURL& origin) { | 23 const GURL& origin) { |
25 context->DeleteForOrigin(origin); | 24 context->DeleteForOrigin(origin); |
26 return quota::kQuotaStatusOk; | 25 return quota::kQuotaStatusOk; |
27 } | 26 } |
28 | 27 |
29 int64 GetOriginUsageOnWebKitThread(IndexedDBContextImpl* context, | 28 int64 GetOriginUsageOnIndexedDBThread(IndexedDBContextImpl* context, |
30 const GURL& origin) { | 29 const GURL& origin) { |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 30 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); |
32 return context->GetOriginDiskUsage(origin); | 31 return context->GetOriginDiskUsage(origin); |
33 } | 32 } |
34 | 33 |
35 void GetAllOriginsOnWebKitThread(IndexedDBContextImpl* context, | 34 void GetAllOriginsOnIndexedDBThread(IndexedDBContextImpl* context, |
36 std::set<GURL>* origins_to_return) { | 35 std::set<GURL>* origins_to_return) { |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 36 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); |
38 std::vector<GURL> all_origins = context->GetAllOrigins(); | 37 std::vector<GURL> all_origins = context->GetAllOrigins(); |
39 origins_to_return->insert(all_origins.begin(), all_origins.end()); | 38 origins_to_return->insert(all_origins.begin(), all_origins.end()); |
40 } | 39 } |
41 | 40 |
42 void DidGetOrigins(const IndexedDBQuotaClient::GetOriginsCallback& callback, | 41 void DidGetOrigins(const IndexedDBQuotaClient::GetOriginsCallback& callback, |
43 const std::set<GURL>* origins) { | 42 const std::set<GURL>* origins) { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
45 callback.Run(*origins); | 44 callback.Run(*origins); |
46 } | 45 } |
47 | 46 |
48 void GetOriginsForHostOnWebKitThread(IndexedDBContextImpl* context, | 47 void GetOriginsForHostOnIndexedDBThread(IndexedDBContextImpl* context, |
49 const std::string& host, | 48 const std::string& host, |
50 std::set<GURL>* origins_to_return) { | 49 std::set<GURL>* origins_to_return) { |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); | 50 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); |
52 std::vector<GURL> all_origins = context->GetAllOrigins(); | 51 std::vector<GURL> all_origins = context->GetAllOrigins(); |
53 for (std::vector<GURL>::const_iterator iter = all_origins.begin(); | 52 for (std::vector<GURL>::const_iterator iter = all_origins.begin(); |
54 iter != all_origins.end(); | 53 iter != all_origins.end(); |
55 ++iter) { | 54 ++iter) { |
56 if (host == net::GetHostOrSpecFromURL(*iter)) | 55 if (host == net::GetHostOrSpecFromURL(*iter)) |
57 origins_to_return->insert(*iter); | 56 origins_to_return->insert(*iter); |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
61 } // namespace | 60 } // namespace |
62 | 61 |
63 // IndexedDBQuotaClient -------------------------------------------------------- | 62 // IndexedDBQuotaClient -------------------------------------------------------- |
64 | 63 |
65 IndexedDBQuotaClient::IndexedDBQuotaClient( | 64 IndexedDBQuotaClient::IndexedDBQuotaClient( |
66 base::MessageLoopProxy* webkit_thread_message_loop, | |
67 IndexedDBContextImpl* indexed_db_context) | 65 IndexedDBContextImpl* indexed_db_context) |
68 : webkit_thread_message_loop_(webkit_thread_message_loop), | 66 : indexed_db_context_(indexed_db_context) {} |
69 indexed_db_context_(indexed_db_context) {} | |
70 | 67 |
71 IndexedDBQuotaClient::~IndexedDBQuotaClient() {} | 68 IndexedDBQuotaClient::~IndexedDBQuotaClient() {} |
72 | 69 |
73 QuotaClient::ID IndexedDBQuotaClient::id() const { return kIndexedDatabase; } | 70 QuotaClient::ID IndexedDBQuotaClient::id() const { return kIndexedDatabase; } |
74 | 71 |
75 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { delete this; } | 72 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { delete this; } |
76 | 73 |
77 void IndexedDBQuotaClient::GetOriginUsage(const GURL& origin_url, | 74 void IndexedDBQuotaClient::GetOriginUsage(const GURL& origin_url, |
78 quota::StorageType type, | 75 quota::StorageType type, |
79 const GetUsageCallback& callback) { | 76 const GetUsageCallback& callback) { |
80 DCHECK(!callback.is_null()); | 77 DCHECK(!callback.is_null()); |
81 DCHECK(indexed_db_context_.get()); | 78 DCHECK(indexed_db_context_.get()); |
82 | 79 |
83 // IndexedDB is in the temp namespace for now. | 80 // IndexedDB is in the temp namespace for now. |
84 if (type != quota::kStorageTypeTemporary) { | 81 if (type != quota::kStorageTypeTemporary) { |
85 callback.Run(0); | 82 callback.Run(0); |
86 return; | 83 return; |
87 } | 84 } |
88 | 85 |
89 base::PostTaskAndReplyWithResult( | 86 // No task runner means unit test; no cleanup necessary. |
jam
2013/06/21 19:41:59
hmm, just saw all these unit test lines in this fi
jsbell
2013/06/21 20:37:14
Not all tests. For example, all of the tests liste
| |
90 webkit_thread_message_loop_.get(), | 87 if (!indexed_db_context_->TaskRunner()) { |
88 callback.Run(0); | |
89 return; | |
90 } | |
91 | |
92 base::PostTaskAndReplyWithResult( | |
93 indexed_db_context_->TaskRunner(), | |
91 FROM_HERE, | 94 FROM_HERE, |
92 base::Bind( | 95 base::Bind( |
93 &GetOriginUsageOnWebKitThread, indexed_db_context_, origin_url), | 96 &GetOriginUsageOnIndexedDBThread, indexed_db_context_, origin_url), |
94 callback); | 97 callback); |
95 } | 98 } |
96 | 99 |
97 void IndexedDBQuotaClient::GetOriginsForType( | 100 void IndexedDBQuotaClient::GetOriginsForType( |
98 quota::StorageType type, | 101 quota::StorageType type, |
99 const GetOriginsCallback& callback) { | 102 const GetOriginsCallback& callback) { |
100 DCHECK(!callback.is_null()); | 103 DCHECK(!callback.is_null()); |
101 DCHECK(indexed_db_context_.get()); | 104 DCHECK(indexed_db_context_.get()); |
102 | 105 |
103 // All databases are in the temp namespace for now. | 106 // All databases are in the temp namespace for now. |
104 if (type != quota::kStorageTypeTemporary) { | 107 if (type != quota::kStorageTypeTemporary) { |
105 callback.Run(std::set<GURL>()); | 108 callback.Run(std::set<GURL>()); |
106 return; | 109 return; |
107 } | 110 } |
108 | 111 |
112 // No task runner means unit test; no cleanup necessary. | |
113 if (!indexed_db_context_->TaskRunner()) { | |
114 callback.Run(std::set<GURL>()); | |
115 return; | |
116 } | |
117 | |
109 std::set<GURL>* origins_to_return = new std::set<GURL>(); | 118 std::set<GURL>* origins_to_return = new std::set<GURL>(); |
110 webkit_thread_message_loop_->PostTaskAndReply( | 119 indexed_db_context_->TaskRunner()->PostTaskAndReply( |
111 FROM_HERE, | 120 FROM_HERE, |
112 base::Bind(&GetAllOriginsOnWebKitThread, | 121 base::Bind(&GetAllOriginsOnIndexedDBThread, |
113 indexed_db_context_, | 122 indexed_db_context_, |
114 base::Unretained(origins_to_return)), | 123 base::Unretained(origins_to_return)), |
115 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); | 124 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); |
116 } | 125 } |
117 | 126 |
118 void IndexedDBQuotaClient::GetOriginsForHost( | 127 void IndexedDBQuotaClient::GetOriginsForHost( |
119 quota::StorageType type, | 128 quota::StorageType type, |
120 const std::string& host, | 129 const std::string& host, |
121 const GetOriginsCallback& callback) { | 130 const GetOriginsCallback& callback) { |
122 DCHECK(!callback.is_null()); | 131 DCHECK(!callback.is_null()); |
123 DCHECK(indexed_db_context_.get()); | 132 DCHECK(indexed_db_context_.get()); |
124 | 133 |
125 // All databases are in the temp namespace for now. | 134 // All databases are in the temp namespace for now. |
126 if (type != quota::kStorageTypeTemporary) { | 135 if (type != quota::kStorageTypeTemporary) { |
127 callback.Run(std::set<GURL>()); | 136 callback.Run(std::set<GURL>()); |
128 return; | 137 return; |
129 } | 138 } |
130 | 139 |
140 // No task runner means unit test; no cleanup necessary. | |
141 if (!indexed_db_context_->TaskRunner()) { | |
142 callback.Run(std::set<GURL>()); | |
143 return; | |
144 } | |
145 | |
131 std::set<GURL>* origins_to_return = new std::set<GURL>(); | 146 std::set<GURL>* origins_to_return = new std::set<GURL>(); |
132 webkit_thread_message_loop_->PostTaskAndReply( | 147 indexed_db_context_->TaskRunner()->PostTaskAndReply( |
133 FROM_HERE, | 148 FROM_HERE, |
134 base::Bind(&GetOriginsForHostOnWebKitThread, | 149 base::Bind(&GetOriginsForHostOnIndexedDBThread, |
135 indexed_db_context_, | 150 indexed_db_context_, |
136 host, | 151 host, |
137 base::Unretained(origins_to_return)), | 152 base::Unretained(origins_to_return)), |
138 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); | 153 base::Bind(&DidGetOrigins, callback, base::Owned(origins_to_return))); |
139 } | 154 } |
140 | 155 |
141 void IndexedDBQuotaClient::DeleteOriginData(const GURL& origin, | 156 void IndexedDBQuotaClient::DeleteOriginData(const GURL& origin, |
142 quota::StorageType type, | 157 quota::StorageType type, |
143 const DeletionCallback& callback) { | 158 const DeletionCallback& callback) { |
144 if (type != quota::kStorageTypeTemporary) { | 159 if (type != quota::kStorageTypeTemporary) { |
145 callback.Run(quota::kQuotaErrorNotSupported); | 160 callback.Run(quota::kQuotaErrorNotSupported); |
146 return; | 161 return; |
147 } | 162 } |
148 | 163 |
164 // No task runner means unit test; no cleanup necessary. | |
165 if (!indexed_db_context_->TaskRunner()) { | |
166 callback.Run(quota::kQuotaStatusOk); | |
167 return; | |
168 } | |
169 | |
149 base::PostTaskAndReplyWithResult( | 170 base::PostTaskAndReplyWithResult( |
150 webkit_thread_message_loop_.get(), | 171 indexed_db_context_->TaskRunner(), |
151 FROM_HERE, | 172 FROM_HERE, |
152 base::Bind(&DeleteOriginDataOnWebKitThread, indexed_db_context_, origin), | 173 base::Bind( |
174 &DeleteOriginDataOnIndexedDBThread, indexed_db_context_, origin), | |
153 callback); | 175 callback); |
154 } | 176 } |
155 | 177 |
156 } // namespace content | 178 } // namespace content |
OLD | NEW |