| 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 "base/synchronization/waitable_event.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/storage_partition.h" | |
| 16 #include "content/public/common/content_client.h" | |
| 17 #include "jni/AwQuotaManagerBridge_jni.h" | |
| 18 #include "storage/browser/quota/quota_manager.h" | |
| 19 #include "storage/common/quota/quota_types.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 using base::android::AttachCurrentThread; | |
| 23 using base::android::JavaParamRef; | |
| 24 using base::android::ScopedJavaLocalRef; | |
| 25 using content::BrowserThread; | |
| 26 using content::StoragePartition; | |
| 27 using storage::QuotaClient; | |
| 28 using storage::QuotaManager; | |
| 29 | |
| 30 namespace android_webview { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // This object lives on UI and IO threads. Care need to be taken to make sure | |
| 35 // there are no concurrent accesses to instance variables. Also this object | |
| 36 // is refcounted in the various callbacks, and is destroyed when all callbacks | |
| 37 // are destroyed at the end of DoneOnUIThread. | |
| 38 class GetOriginsTask : public base::RefCountedThreadSafe<GetOriginsTask> { | |
| 39 public: | |
| 40 GetOriginsTask( | |
| 41 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, | |
| 42 QuotaManager* quota_manager); | |
| 43 | |
| 44 void Run(); | |
| 45 | |
| 46 private: | |
| 47 friend class base::RefCountedThreadSafe<GetOriginsTask>; | |
| 48 ~GetOriginsTask(); | |
| 49 | |
| 50 void OnOriginsObtained(const std::set<GURL>& origins, | |
| 51 storage::StorageType type); | |
| 52 | |
| 53 void OnUsageAndQuotaObtained(const GURL& origin, | |
| 54 storage::QuotaStatusCode status_code, | |
| 55 int64_t usage, | |
| 56 int64_t quota); | |
| 57 | |
| 58 void CheckDone(); | |
| 59 void DoneOnUIThread(); | |
| 60 | |
| 61 AwQuotaManagerBridgeImpl::GetOriginsCallback ui_callback_; | |
| 62 scoped_refptr<QuotaManager> quota_manager_; | |
| 63 | |
| 64 std::vector<std::string> origin_; | |
| 65 std::vector<int64_t> usage_; | |
| 66 std::vector<int64_t> quota_; | |
| 67 | |
| 68 size_t num_callbacks_to_wait_; | |
| 69 size_t num_callbacks_received_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(GetOriginsTask); | |
| 72 }; | |
| 73 | |
| 74 GetOriginsTask::GetOriginsTask( | |
| 75 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, | |
| 76 QuotaManager* quota_manager) | |
| 77 : ui_callback_(callback), | |
| 78 quota_manager_(quota_manager) { | |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 80 } | |
| 81 | |
| 82 GetOriginsTask::~GetOriginsTask() {} | |
| 83 | |
| 84 void GetOriginsTask::Run() { | |
| 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 86 BrowserThread::PostTask( | |
| 87 BrowserThread::IO, | |
| 88 FROM_HERE, | |
| 89 base::Bind(&QuotaManager::GetOriginsModifiedSince, | |
| 90 quota_manager_, | |
| 91 storage::kStorageTypeTemporary, | |
| 92 base::Time() /* Since beginning of time. */, | |
| 93 base::Bind(&GetOriginsTask::OnOriginsObtained, this))); | |
| 94 } | |
| 95 | |
| 96 void GetOriginsTask::OnOriginsObtained(const std::set<GURL>& origins, | |
| 97 storage::StorageType type) { | |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 99 num_callbacks_to_wait_ = origins.size(); | |
| 100 num_callbacks_received_ = 0u; | |
| 101 | |
| 102 for (std::set<GURL>::const_iterator origin = origins.begin(); | |
| 103 origin != origins.end(); | |
| 104 ++origin) { | |
| 105 quota_manager_->GetUsageAndQuota( | |
| 106 *origin, | |
| 107 type, | |
| 108 base::Bind(&GetOriginsTask::OnUsageAndQuotaObtained, this, *origin)); | |
| 109 } | |
| 110 | |
| 111 CheckDone(); | |
| 112 } | |
| 113 | |
| 114 void GetOriginsTask::OnUsageAndQuotaObtained( | |
| 115 const GURL& origin, | |
| 116 storage::QuotaStatusCode status_code, | |
| 117 int64_t usage, | |
| 118 int64_t quota) { | |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 120 if (status_code == storage::kQuotaStatusOk) { | |
| 121 origin_.push_back(origin.spec()); | |
| 122 usage_.push_back(usage); | |
| 123 quota_.push_back(quota); | |
| 124 } | |
| 125 | |
| 126 ++num_callbacks_received_; | |
| 127 CheckDone(); | |
| 128 } | |
| 129 | |
| 130 void GetOriginsTask::CheckDone() { | |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 132 if (num_callbacks_received_ == num_callbacks_to_wait_) { | |
| 133 BrowserThread::PostTask( | |
| 134 BrowserThread::UI, | |
| 135 FROM_HERE, | |
| 136 base::Bind(&GetOriginsTask::DoneOnUIThread, this)); | |
| 137 } else if (num_callbacks_received_ > num_callbacks_to_wait_) { | |
| 138 NOTREACHED(); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 // This method is to avoid copying the 3 vector arguments into a bound callback. | |
| 143 void GetOriginsTask::DoneOnUIThread() { | |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 145 ui_callback_.Run(origin_, usage_, quota_); | |
| 146 } | |
| 147 | |
| 148 void RunOnUIThread(const base::Closure& task) { | |
| 149 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 150 task.Run(); | |
| 151 } else { | |
| 152 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 } // namespace | |
| 157 | |
| 158 | |
| 159 // static | |
| 160 jlong GetDefaultNativeAwQuotaManagerBridge(JNIEnv* env, | |
| 161 const JavaParamRef<jclass>& clazz) { | |
| 162 AwBrowserContext* browser_context = | |
| 163 AwContentBrowserClient::GetAwBrowserContext(); | |
| 164 | |
| 165 AwQuotaManagerBridgeImpl* bridge = static_cast<AwQuotaManagerBridgeImpl*>( | |
| 166 browser_context->GetQuotaManagerBridge()); | |
| 167 DCHECK(bridge); | |
| 168 return reinterpret_cast<intptr_t>(bridge); | |
| 169 } | |
| 170 | |
| 171 // static | |
| 172 scoped_refptr<AwQuotaManagerBridge> AwQuotaManagerBridgeImpl::Create( | |
| 173 AwBrowserContext* browser_context) { | |
| 174 return new AwQuotaManagerBridgeImpl(browser_context); | |
| 175 } | |
| 176 | |
| 177 AwQuotaManagerBridgeImpl::AwQuotaManagerBridgeImpl( | |
| 178 AwBrowserContext* browser_context) | |
| 179 : browser_context_(browser_context), | |
| 180 weak_factory_(this) { | |
| 181 } | |
| 182 | |
| 183 AwQuotaManagerBridgeImpl::~AwQuotaManagerBridgeImpl() {} | |
| 184 | |
| 185 void AwQuotaManagerBridgeImpl::Init(JNIEnv* env, | |
| 186 const JavaParamRef<jobject>& object) { | |
| 187 java_ref_ = JavaObjectWeakGlobalRef(env, object); | |
| 188 } | |
| 189 | |
| 190 StoragePartition* AwQuotaManagerBridgeImpl::GetStoragePartition() const { | |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 192 | |
| 193 // AndroidWebview does not use per-site storage partitions. | |
| 194 StoragePartition* storage_partition = | |
| 195 content::BrowserContext::GetDefaultStoragePartition(browser_context_); | |
| 196 DCHECK(storage_partition); | |
| 197 return storage_partition; | |
| 198 } | |
| 199 | |
| 200 QuotaManager* AwQuotaManagerBridgeImpl::GetQuotaManager() const { | |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 202 | |
| 203 QuotaManager* quota_manager = GetStoragePartition()->GetQuotaManager(); | |
| 204 DCHECK(quota_manager); | |
| 205 return quota_manager; | |
| 206 } | |
| 207 | |
| 208 void AwQuotaManagerBridgeImpl::DeleteAllData( | |
| 209 JNIEnv* env, | |
| 210 const JavaParamRef<jobject>& object) { | |
| 211 RunOnUIThread(base::Bind(&AwQuotaManagerBridgeImpl::DeleteAllDataOnUiThread, | |
| 212 this)); | |
| 213 } | |
| 214 | |
| 215 void AwQuotaManagerBridgeImpl::DeleteAllDataOnUiThread() { | |
| 216 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 217 GetStoragePartition()->ClearData( | |
| 218 // Clear all web storage data except cookies. | |
| 219 StoragePartition::REMOVE_DATA_MASK_APPCACHE | | |
| 220 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS | | |
| 221 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB | | |
| 222 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE | | |
| 223 StoragePartition::REMOVE_DATA_MASK_WEBSQL, | |
| 224 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY, | |
| 225 GURL(), StoragePartition::OriginMatcherFunction(), | |
| 226 base::Time(), base::Time::Max(), base::Bind(&base::DoNothing)); | |
| 227 } | |
| 228 | |
| 229 void AwQuotaManagerBridgeImpl::DeleteOrigin( | |
| 230 JNIEnv* env, | |
| 231 const JavaParamRef<jobject>& object, | |
| 232 const JavaParamRef<jstring>& origin) { | |
| 233 base::string16 origin_string( | |
| 234 base::android::ConvertJavaStringToUTF16(env, origin)); | |
| 235 RunOnUIThread(base::Bind(&AwQuotaManagerBridgeImpl::DeleteOriginOnUiThread, | |
| 236 this, | |
| 237 origin_string)); | |
| 238 } | |
| 239 | |
| 240 void AwQuotaManagerBridgeImpl::DeleteOriginOnUiThread( | |
| 241 const base::string16& origin) { | |
| 242 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 243 StoragePartition* storage_partition = GetStoragePartition(); | |
| 244 storage_partition->ClearDataForOrigin( | |
| 245 // All (temporary) QuotaClient types. | |
| 246 StoragePartition::REMOVE_DATA_MASK_APPCACHE | | |
| 247 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS | | |
| 248 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB | | |
| 249 StoragePartition::REMOVE_DATA_MASK_WEBSQL, | |
| 250 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY, | |
| 251 GURL(origin), | |
| 252 storage_partition->GetURLRequestContext(), | |
| 253 base::Bind(&base::DoNothing)); | |
| 254 } | |
| 255 | |
| 256 void AwQuotaManagerBridgeImpl::GetOrigins(JNIEnv* env, | |
| 257 const JavaParamRef<jobject>& object, | |
| 258 jint callback_id) { | |
| 259 RunOnUIThread(base::Bind(&AwQuotaManagerBridgeImpl::GetOriginsOnUiThread, | |
| 260 this, | |
| 261 callback_id)); | |
| 262 } | |
| 263 | |
| 264 void AwQuotaManagerBridgeImpl::GetOriginsOnUiThread(jint callback_id) { | |
| 265 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 266 | |
| 267 const GetOriginsCallback ui_callback = base::Bind( | |
| 268 &AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl, | |
| 269 weak_factory_.GetWeakPtr(), | |
| 270 callback_id); | |
| 271 | |
| 272 (new GetOriginsTask(ui_callback, GetQuotaManager()))->Run(); | |
| 273 } | |
| 274 | |
| 275 void AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl( | |
| 276 int jcallback_id, | |
| 277 const std::vector<std::string>& origin, | |
| 278 const std::vector<int64_t>& usage, | |
| 279 const std::vector<int64_t>& quota) { | |
| 280 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 281 JNIEnv* env = AttachCurrentThread(); | |
| 282 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 283 if (obj.is_null()) | |
| 284 return; | |
| 285 | |
| 286 Java_AwQuotaManagerBridge_onGetOriginsCallback( | |
| 287 env, obj, jcallback_id, base::android::ToJavaArrayOfStrings(env, origin), | |
| 288 base::android::ToJavaLongArray(env, usage), | |
| 289 base::android::ToJavaLongArray(env, quota)); | |
| 290 } | |
| 291 | |
| 292 namespace { | |
| 293 | |
| 294 void OnUsageAndQuotaObtained( | |
| 295 const AwQuotaManagerBridgeImpl::QuotaUsageCallback& ui_callback, | |
| 296 storage::QuotaStatusCode status_code, | |
| 297 int64_t usage, | |
| 298 int64_t quota) { | |
| 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 300 if (status_code != storage::kQuotaStatusOk) { | |
| 301 usage = 0; | |
| 302 quota = 0; | |
| 303 } | |
| 304 BrowserThread::PostTask( | |
| 305 BrowserThread::UI, | |
| 306 FROM_HERE, | |
| 307 base::Bind(ui_callback, usage, quota)); | |
| 308 } | |
| 309 | |
| 310 } // namespace | |
| 311 | |
| 312 void AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOrigin( | |
| 313 JNIEnv* env, | |
| 314 const JavaParamRef<jobject>& object, | |
| 315 const JavaParamRef<jstring>& origin, | |
| 316 jint callback_id, | |
| 317 bool is_quota) { | |
| 318 base::string16 origin_string( | |
| 319 base::android::ConvertJavaStringToUTF16(env, origin)); | |
| 320 RunOnUIThread(base::Bind( | |
| 321 &AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOriginOnUiThread, | |
| 322 this, | |
| 323 origin_string, | |
| 324 callback_id, | |
| 325 is_quota)); | |
| 326 } | |
| 327 | |
| 328 void AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOriginOnUiThread( | |
| 329 const base::string16& origin, | |
| 330 jint callback_id, | |
| 331 bool is_quota) { | |
| 332 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 333 const QuotaUsageCallback ui_callback = base::Bind( | |
| 334 &AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl, | |
| 335 weak_factory_.GetWeakPtr(), | |
| 336 callback_id, | |
| 337 is_quota); | |
| 338 | |
| 339 BrowserThread::PostTask( | |
| 340 BrowserThread::IO, | |
| 341 FROM_HERE, | |
| 342 base::Bind(&QuotaManager::GetUsageAndQuota, | |
| 343 GetQuotaManager(), | |
| 344 GURL(origin), | |
| 345 storage::kStorageTypeTemporary, | |
| 346 base::Bind(&OnUsageAndQuotaObtained, ui_callback))); | |
| 347 } | |
| 348 | |
| 349 void AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl(int jcallback_id, | |
| 350 bool is_quota, | |
| 351 int64_t usage, | |
| 352 int64_t quota) { | |
| 353 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 354 JNIEnv* env = AttachCurrentThread(); | |
| 355 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 356 if (obj.is_null()) | |
| 357 return; | |
| 358 | |
| 359 Java_AwQuotaManagerBridge_onGetUsageAndQuotaForOriginCallback( | |
| 360 env, obj, jcallback_id, is_quota, usage, quota); | |
| 361 } | |
| 362 | |
| 363 bool RegisterAwQuotaManagerBridge(JNIEnv* env) { | |
| 364 return RegisterNativesImpl(env); | |
| 365 } | |
| 366 | |
| 367 } // namespace android_webview | |
| OLD | NEW |