1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/native/aw_quota_manager_bridge_impl.h"
#include <set>
#include "android_webview/browser/aw_browser_context.h"
#include "android_webview/browser/aw_content_browser_client.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_client.h"
#include "googleurl/src/gurl.h"
#include "jni/AwQuotaManagerBridge_jni.h"
#include "webkit/quota/quota_manager.h"
#include "webkit/quota/quota_types.h"
using base::android::AttachCurrentThread;
using content::BrowserThread;
using content::StoragePartition;
using quota::QuotaClient;
using quota::QuotaManager;
namespace android_webview {
namespace {
// This object lives on UI and IO threads. Care need to be taken to make sure
// there are no concurrent accesses to instance variables. Also this object
// is refcounted in the various callbacks, and is destroyed when all callbacks
// are destroyed at the end of DoneOnUIThread.
class GetOriginsTask : public base::RefCountedThreadSafe<GetOriginsTask> {
public:
GetOriginsTask(
const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback,
QuotaManager* quota_manager);
void Run();
private:
friend class base::RefCountedThreadSafe<GetOriginsTask>;
~GetOriginsTask();
void OnOriginsObtained(const std::set<GURL>& origins,
quota::StorageType type);
void OnUsageAndQuotaObtained(const GURL& origin,
quota::QuotaStatusCode status_code,
int64 usage,
int64 quota);
void CheckDone();
void DoneOnUIThread();
AwQuotaManagerBridgeImpl::GetOriginsCallback ui_callback_;
scoped_refptr<QuotaManager> quota_manager_;
std::vector<std::string> origin_;
std::vector<int64> usage_;
std::vector<int64> quota_;
size_t num_callbacks_to_wait_;
size_t num_callbacks_received_;
DISALLOW_COPY_AND_ASSIGN(GetOriginsTask);
};
GetOriginsTask::GetOriginsTask(
const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback,
QuotaManager* quota_manager)
: ui_callback_(callback),
quota_manager_(quota_manager) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
GetOriginsTask::~GetOriginsTask() {}
void GetOriginsTask::Run() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&QuotaManager::GetOriginsModifiedSince,
quota_manager_,
quota::kStorageTypeTemporary,
base::Time() /* Since beginning of time. */,
base::Bind(&GetOriginsTask::OnOriginsObtained, this)));
}
void GetOriginsTask::OnOriginsObtained(
const std::set<GURL>& origins, quota::StorageType type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
num_callbacks_to_wait_ = origins.size();
num_callbacks_received_ = 0u;
for (std::set<GURL>::const_iterator origin = origins.begin();
origin != origins.end();
++origin) {
quota_manager_->GetUsageAndQuota(
*origin,
type,
base::Bind(&GetOriginsTask::OnUsageAndQuotaObtained, this, *origin));
}
CheckDone();
}
void GetOriginsTask::OnUsageAndQuotaObtained(const GURL& origin,
quota::QuotaStatusCode status_code,
int64 usage,
int64 quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (status_code == quota::kQuotaStatusOk) {
origin_.push_back(origin.spec());
usage_.push_back(usage);
quota_.push_back(quota);
}
++num_callbacks_received_;
CheckDone();
}
void GetOriginsTask::CheckDone() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (num_callbacks_received_ == num_callbacks_to_wait_) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&GetOriginsTask::DoneOnUIThread, this));
} else if (num_callbacks_received_ > num_callbacks_to_wait_) {
NOTREACHED();
}
}
// This method is to avoid copying the 3 vector arguments into a bound callback.
void GetOriginsTask::DoneOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ui_callback_.Run(origin_, usage_, quota_);
}
} // namespace
// static
jint GetDefaultNativeAwQuotaManagerBridge(JNIEnv* env, jclass clazz) {
AwBrowserContext* browser_context =
AwContentBrowserClient::GetAwBrowserContext();
AwQuotaManagerBridgeImpl* bridge = static_cast<AwQuotaManagerBridgeImpl*>(
browser_context->GetQuotaManagerBridge());
DCHECK(bridge);
return reinterpret_cast<jint>(bridge);
}
AwQuotaManagerBridgeImpl::AwQuotaManagerBridgeImpl(
AwBrowserContext* browser_context)
: weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
browser_context_(browser_context) {
}
AwQuotaManagerBridgeImpl::~AwQuotaManagerBridgeImpl() {}
void AwQuotaManagerBridgeImpl::Init(JNIEnv* env, jobject object) {
java_ref_ = JavaObjectWeakGlobalRef(env, object);
}
StoragePartition* AwQuotaManagerBridgeImpl::GetStoragePartition() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// AndroidWebview does not use per-site storage partitions.
StoragePartition* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context_);
DCHECK(storage_partition);
return storage_partition;
}
QuotaManager* AwQuotaManagerBridgeImpl::GetQuotaManager() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
QuotaManager* quota_manager = GetStoragePartition()->GetQuotaManager();
DCHECK(quota_manager);
return quota_manager;
}
// Cannot directly call StoragePartition clear data methods because cookies are
// controlled separately.
void AwQuotaManagerBridgeImpl::DeleteAllData(JNIEnv* env, jobject object) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
GetStoragePartition()->AsyncClearData(
StoragePartition::kQuotaManagedTemporaryStorage |
StoragePartition::kLocalDomStorage |
StoragePartition::kSessionDomStorage);
}
void AwQuotaManagerBridgeImpl::DeleteOrigin(
JNIEnv* env, jobject object, jstring origin) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
StoragePartition* storage_partition = GetStoragePartition();
storage_partition->AsyncClearDataForOrigin(
StoragePartition::kQuotaManagedTemporaryStorage,
GURL(base::android::ConvertJavaStringToUTF16(env, origin)),
storage_partition->GetURLRequestContext());
}
void AwQuotaManagerBridgeImpl::GetOrigins(
JNIEnv* env, jobject object, jint callback_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const GetOriginsCallback ui_callback = base::Bind(
&AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl,
weak_factory_.GetWeakPtr(),
callback_id);
(new GetOriginsTask(ui_callback, GetQuotaManager()))->Run();
}
void AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl(
int jcallback_id,
const std::vector<std::string>& origin,
const std::vector<int64>& usage,
const std::vector<int64>& quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
Java_AwQuotaManagerBridge_onGetOriginsCallback(
env,
obj.obj(),
jcallback_id,
base::android::ToJavaArrayOfStrings(env, origin).obj(),
base::android::ToJavaLongArray(env, usage).obj(),
base::android::ToJavaLongArray(env, quota).obj());
}
namespace {
void OnUsageAndQuotaObtained(
const AwQuotaManagerBridgeImpl::QuotaUsageCallback& ui_callback,
quota::QuotaStatusCode status_code,
int64 usage,
int64 quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (status_code != quota::kQuotaStatusOk) {
usage = 0;
quota = 0;
}
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(ui_callback, usage, quota));
}
} // namespace
void AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOrigin(
JNIEnv* env, jobject object,
jstring origin,
jint callback_id,
bool is_quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const QuotaUsageCallback ui_callback = base::Bind(
&AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl,
weak_factory_.GetWeakPtr(),
callback_id,
is_quota);
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&QuotaManager::GetUsageAndQuota,
GetQuotaManager(),
GURL(base::android::ConvertJavaStringToUTF16(env, origin)),
quota::kStorageTypeTemporary,
base::Bind(&OnUsageAndQuotaObtained, ui_callback)));
}
void AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl(
int jcallback_id, bool is_quota, int64 usage, int64 quota) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
Java_AwQuotaManagerBridge_onGetUsageAndQuotaForOriginCallback(
env, obj.obj(), jcallback_id, is_quota, usage, quota);
}
bool RegisterAwQuotaManagerBridge(JNIEnv* env) {
return RegisterNativesImpl(env) >= 0;
}
} // namespace android_webview
|