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 "android_webview/native/aw_quota_manager_bridge_impl.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "android_webview/browser/aw_browser_context.h" | |
10 #include "android_webview/browser/aw_content_browser_client.h" | |
11 #include "base/android/jni_array.h" | |
12 #include "base/android/jni_string.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/storage_partition.h" | |
15 #include "content/public/common/content_client.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "jni/AwQuotaManagerBridge_jni.h" | |
18 #include "webkit/quota/quota_manager.h" | |
19 #include "webkit/quota/quota_types.h" | |
20 | |
21 using base::android::AttachCurrentThread; | |
22 using content::BrowserThread; | |
23 using content::StoragePartition; | |
24 using quota::QuotaClient; | |
25 using quota::QuotaManager; | |
26 | |
27 namespace android_webview { | |
28 | |
29 namespace { | |
boliu
2013/02/20 00:53:51
The anonymous namespace is split into 3 pieces in
| |
30 | |
31 class GetOriginsTask : public base::RefCountedThreadSafe<GetOriginsTask> { | |
boliu
2013/02/20 00:53:51
This could be moved to a separate h/cc file, but s
| |
32 public: | |
33 GetOriginsTask( | |
34 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, | |
35 const scoped_refptr<QuotaManager>& quota_manager); | |
36 | |
37 void Run(); | |
38 | |
39 private: | |
40 friend class base::RefCountedThreadSafe<GetOriginsTask>; | |
41 ~GetOriginsTask(); | |
42 | |
43 void OnOriginsObtained(const std::set<GURL>& origins, | |
44 quota::StorageType type); | |
45 | |
46 void OnUsageAndQuotaObtained(const GURL& origin, | |
47 quota::QuotaStatusCode status_code, | |
48 int64 usage, | |
49 int64 quota); | |
50 | |
51 void CheckDone(); | |
52 void DoneOnUIThread(); | |
53 | |
54 AwQuotaManagerBridgeImpl::GetOriginsCallback ui_callback_; | |
55 scoped_refptr<QuotaManager> quota_manager_; | |
56 | |
57 std::vector<std::string> origin_; | |
58 std::vector<int64> usage_; | |
59 std::vector<int64> quota_; | |
60 | |
61 size_t num_callbacks_to_wait_; | |
62 size_t num_callbacks_received_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(GetOriginsTask); | |
65 }; | |
66 | |
67 GetOriginsTask::GetOriginsTask( | |
68 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, | |
69 const scoped_refptr<QuotaManager>& quota_manager) | |
70 : ui_callback_(callback), | |
71 quota_manager_(quota_manager) { | |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
73 } | |
74 | |
75 GetOriginsTask::~GetOriginsTask() {} | |
76 | |
77 void GetOriginsTask::Run() { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 BrowserThread::PostTask( | |
80 BrowserThread::IO, | |
81 FROM_HERE, | |
82 base::Bind(&QuotaManager::GetOriginsModifiedSince, | |
83 quota_manager_, | |
84 quota::kStorageTypeTemporary, | |
85 base::Time() /* Since beginning of time. */, | |
86 base::Bind(&GetOriginsTask::OnOriginsObtained, this))); | |
87 } | |
88 | |
89 void GetOriginsTask::OnOriginsObtained( | |
90 const std::set<GURL>& origins, quota::StorageType type) { | |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
92 num_callbacks_to_wait_ = origins.size(); | |
93 num_callbacks_received_ = 0u; | |
94 | |
95 for (std::set<GURL>::const_iterator origin = origins.begin(); | |
96 origin != origins.end(); | |
97 ++origin) { | |
98 quota_manager_->GetUsageAndQuota( | |
99 *origin, | |
100 type, | |
101 base::Bind(&GetOriginsTask::OnUsageAndQuotaObtained, this, *origin)); | |
102 } | |
103 | |
104 CheckDone(); | |
105 } | |
106 | |
107 void GetOriginsTask::OnUsageAndQuotaObtained(const GURL& origin, | |
108 quota::QuotaStatusCode status_code, | |
109 int64 usage, | |
110 int64 quota) { | |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
112 if (status_code == quota::kQuotaStatusOk) { | |
113 origin_.push_back(origin.spec()); | |
114 usage_.push_back(usage); | |
115 quota_.push_back(quota); | |
116 } | |
117 | |
118 ++num_callbacks_received_; | |
119 CheckDone(); | |
120 } | |
121 | |
122 void GetOriginsTask::CheckDone() { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
124 if (num_callbacks_received_ == num_callbacks_to_wait_) { | |
125 BrowserThread::PostTask( | |
126 BrowserThread::UI, | |
127 FROM_HERE, | |
128 base::Bind(&GetOriginsTask::DoneOnUIThread, this)); | |
129 } else if (num_callbacks_received_ > num_callbacks_to_wait_) { | |
130 NOTREACHED(); | |
131 } | |
132 } | |
133 | |
134 void GetOriginsTask::DoneOnUIThread() { | |
boliu
2013/02/20 00:53:51
This and RunGetUsageAndQuotaCallback below can be
| |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
136 ui_callback_.Run(origin_, usage_, quota_); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 | |
142 // static | |
143 jint GetDefaultNativeAwQuotaManagerBridge(JNIEnv* env, jclass clazz) { | |
144 content::ContentBrowserClient* browser_client = | |
145 content::GetContentClient()->browser(); | |
146 DCHECK(browser_client); | |
147 | |
148 AwContentBrowserClient* aw_browser_client = | |
149 AwContentBrowserClient::FromContentBrowserClient(browser_client); | |
150 AwBrowserContext* browser_context = aw_browser_client->GetAwBrowserContext(); | |
151 DCHECK(browser_context); | |
152 | |
153 AwQuotaManagerBridgeImpl* bridge = static_cast<AwQuotaManagerBridgeImpl*>( | |
154 browser_context->GetQuotaManagerBridge()); | |
155 DCHECK(bridge); | |
156 return reinterpret_cast<jint>(bridge); | |
157 } | |
158 | |
159 AwQuotaManagerBridgeImpl::AwQuotaManagerBridgeImpl( | |
160 AwBrowserContext* browser_context) | |
161 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
162 browser_context_(browser_context) { | |
163 } | |
164 | |
165 void AwQuotaManagerBridgeImpl::Init(JNIEnv* env, jobject object) { | |
166 java_ref_ = JavaObjectWeakGlobalRef(env, object); | |
167 } | |
168 | |
169 scoped_refptr<QuotaManager>AwQuotaManagerBridgeImpl::GetQuotaManager() const { | |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
171 | |
172 // AndroidWebview does not use per-site storage partitions. | |
173 // TODO(boliu): Get @ajwong to review this before submit. | |
174 StoragePartition* storage_partition = | |
175 content::BrowserContext::GetStoragePartitionForSite( | |
176 browser_context_, GURL()); | |
177 DCHECK(storage_partition); | |
178 | |
179 QuotaManager* quota_manager = storage_partition->GetQuotaManager(); | |
mkosiba (inactive)
2013/02/20 12:21:38
This bit me a couple of times so just double-check
boliu
2013/02/20 20:14:16
Good point.
First, BrowsingDataRemover::RemoveImp
| |
180 DCHECK(quota_manager); | |
181 return make_scoped_refptr(quota_manager); | |
182 } | |
183 | |
184 namespace { | |
185 | |
186 // TODO(boliu): Does this create a static initializer? | |
187 int ALL_CLIENT_MASK = QuotaClient::kFileSystem | | |
joth
2013/02/20 19:49:33
you should make it const.
also, I think the kFoo
boliu
2013/02/20 20:14:16
In the documentation for QuotaManager::DeleteOrigi
mnaganov (inactive)
2013/02/20 21:54:34
I think we are misinterpreting the comment, as loo
boliu
2013/02/20 22:26:26
You are right. It's a confusing comment. And I lea
| |
188 QuotaClient::kDatabase | | |
189 QuotaClient::kAppcache | | |
boliu
2013/02/20 00:53:51
Mikhail: You've implemented some app cache methods
mnaganov (inactive)
2013/02/20 09:29:03
I wasn't doing anything for clearing Appcache. It
joth
2013/02/20 19:49:33
That's a neat trick! Nice.
boliu
2013/02/20 20:14:16
The enum test failed with:
error: 'android_webview
mnaganov (inactive)
2013/02/20 21:54:34
Sorry, are you trying to write "enum { X = ALL_CLI
boliu
2013/02/20 22:26:26
And you are right again. I did it the first way. T
| |
190 QuotaClient::kIndexedDatabase; | |
191 | |
192 void IgnoreStatus(quota::QuotaStatusCode status_code) {} | |
193 | |
194 void DeleteOrigins( | |
mkosiba (inactive)
2013/02/20 12:21:38
nit2: it would also be nice if the name indicated
boliu
2013/02/20 20:14:16
Don't like "Worker" moniker for a function. Change
| |
195 const scoped_refptr<QuotaManager>& quota_manager, | |
196 const std::set<GURL>& origins, | |
197 quota::StorageType type) { | |
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
199 // TODO(boliu): Maybe not spam the db thread? | |
200 for (std::set<GURL>::const_iterator origin = origins.begin(); | |
201 origin != origins.end(); | |
202 ++origin) { | |
203 quota_manager->DeleteOriginData(*origin, | |
204 type, | |
205 ALL_CLIENT_MASK, | |
206 base::Bind(&IgnoreStatus)); | |
207 } | |
208 } | |
209 | |
210 } // namespace | |
211 | |
212 void AwQuotaManagerBridgeImpl::DeleteAllData(JNIEnv* env, jobject object) { | |
mkosiba (inactive)
2013/02/20 12:21:38
nit: still think this should match the quota manag
boliu
2013/02/20 20:14:16
So these names match the public java WebStorage ap
mkosiba (inactive)
2013/02/21 11:38:30
ah, OK.
| |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
214 scoped_refptr<quota::QuotaManager> quota_manager = GetQuotaManager(); | |
215 BrowserThread::PostTask( | |
216 BrowserThread::IO, | |
217 FROM_HERE, | |
218 base::Bind(&QuotaManager::GetOriginsModifiedSince, | |
219 quota_manager, | |
220 quota::kStorageTypeTemporary, | |
221 base::Time() /* Since beginning of time. */, | |
222 base::Bind(&DeleteOrigins, quota_manager))); | |
223 } | |
224 | |
225 void AwQuotaManagerBridgeImpl::DeleteOrigin( | |
mkosiba (inactive)
2013/02/20 12:21:38
same nit: DeleteOriginData
| |
226 JNIEnv* env, jobject object, jstring origin) { | |
227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
228 BrowserThread::PostTask( | |
229 BrowserThread::IO, | |
230 FROM_HERE, | |
231 base::Bind(&QuotaManager::DeleteOriginData, | |
232 GetQuotaManager(), | |
233 GURL(base::android::ConvertJavaStringToUTF16(env, origin)), | |
234 quota::kStorageTypeTemporary, | |
235 ALL_CLIENT_MASK, | |
236 base::Bind(&IgnoreStatus))); | |
237 } | |
238 | |
239 void AwQuotaManagerBridgeImpl::GetOrigins( | |
240 JNIEnv* env, jobject object, jint callback_id) { | |
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
242 | |
243 const GetOriginsCallback ui_callback = base::Bind( | |
244 &AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl, | |
245 weak_factory_.GetWeakPtr(), | |
246 callback_id); | |
247 | |
248 (new GetOriginsTask(ui_callback, GetQuotaManager()))->Run(); | |
249 } | |
250 | |
251 void AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl( | |
252 int jcallback_id, | |
253 const std::vector<std::string>& origin, | |
254 const std::vector<int64>& usage, | |
255 const std::vector<int64>& quota) { | |
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
257 JNIEnv* env = AttachCurrentThread(); | |
258 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
259 if (obj.is_null()) | |
260 return; | |
261 | |
262 Java_AwQuotaManagerBridge_onGetOriginsCallback( | |
263 env, | |
264 obj.obj(), | |
265 jcallback_id, | |
266 base::android::ToJavaArrayOfStrings(env, origin).obj(), | |
267 base::android::ToJavaLongArray(env, usage.begin(), usage.size()).obj(), | |
268 base::android::ToJavaLongArray(env, quota.begin(), quota.size()).obj()); | |
269 } | |
270 | |
271 namespace { | |
272 | |
273 void RunGetUsageAndQuotaCallback( | |
274 const AwQuotaManagerBridgeImpl::QuotaUsageCallback& ui_callback, | |
275 int64 usage, | |
276 int64 quota) { | |
277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
278 ui_callback.Run(usage, quota); | |
279 } | |
280 | |
281 void OnUsageAndQuotaObtained( | |
282 const AwQuotaManagerBridgeImpl::QuotaUsageCallback& ui_callback, | |
283 quota::QuotaStatusCode status_code, | |
284 int64 usage, | |
285 int64 quota) { | |
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
287 if (status_code != quota::kQuotaStatusOk) { | |
288 // Indicate error. TODO(boliu): Check this does not mean infinite. | |
289 usage = -1; | |
290 quota = -1; | |
291 } | |
292 BrowserThread::PostTask( | |
293 BrowserThread::UI, | |
294 FROM_HERE, | |
295 base::Bind(&RunGetUsageAndQuotaCallback, | |
296 ui_callback, | |
297 usage, | |
298 quota)); | |
299 } | |
300 | |
301 } // namespace | |
302 | |
303 void AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOrigin( | |
304 JNIEnv* env, jobject object, | |
305 jstring origin, | |
306 jint callback_id, | |
307 bool is_quota) { | |
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
309 const QuotaUsageCallback ui_callback = base::Bind( | |
310 &AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl, | |
311 weak_factory_.GetWeakPtr(), | |
312 callback_id, | |
313 is_quota); | |
314 | |
315 BrowserThread::PostTask( | |
316 BrowserThread::IO, | |
317 FROM_HERE, | |
318 base::Bind(&QuotaManager::GetUsageAndQuota, | |
319 GetQuotaManager(), | |
320 GURL(base::android::ConvertJavaStringToUTF16(env, origin)), | |
321 quota::kStorageTypeTemporary, | |
322 base::Bind(&OnUsageAndQuotaObtained, ui_callback))); | |
323 } | |
324 | |
325 void AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl( | |
326 int jcallback_id, bool is_quota, int64 usage, int64 quota) { | |
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
328 JNIEnv* env = AttachCurrentThread(); | |
329 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
330 if (obj.is_null()) | |
331 return; | |
332 | |
333 Java_AwQuotaManagerBridge_onGetUsageAndQuotaForOriginCallback( | |
334 env, obj.obj(), jcallback_id, is_quota, usage, quota); | |
335 } | |
336 | |
337 bool RegisterAwQuotaManagerBridge(JNIEnv* env) { | |
338 return RegisterNativesImpl(env) >= 0; | |
339 } | |
340 | |
341 } // namespace android_webview | |
OLD | NEW |