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 { |
| 30 |
| 31 // This object lives on UI and IO threads. Care need to be taken to make sure |
| 32 // there are no concurrent accesses to instance variables. Also this object |
| 33 // is refcounted in the various callbacks, and is destroyed when all callbacks |
| 34 // are destroyed at the end of DoneOnUIThread. |
| 35 class GetOriginsTask : public base::RefCountedThreadSafe<GetOriginsTask> { |
| 36 public: |
| 37 GetOriginsTask( |
| 38 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, |
| 39 QuotaManager* quota_manager); |
| 40 |
| 41 void Run(); |
| 42 |
| 43 private: |
| 44 friend class base::RefCountedThreadSafe<GetOriginsTask>; |
| 45 ~GetOriginsTask(); |
| 46 |
| 47 void OnOriginsObtained(const std::set<GURL>& origins, |
| 48 quota::StorageType type); |
| 49 |
| 50 void OnUsageAndQuotaObtained(const GURL& origin, |
| 51 quota::QuotaStatusCode status_code, |
| 52 int64 usage, |
| 53 int64 quota); |
| 54 |
| 55 void CheckDone(); |
| 56 void DoneOnUIThread(); |
| 57 |
| 58 AwQuotaManagerBridgeImpl::GetOriginsCallback ui_callback_; |
| 59 scoped_refptr<QuotaManager> quota_manager_; |
| 60 |
| 61 std::vector<std::string> origin_; |
| 62 std::vector<int64> usage_; |
| 63 std::vector<int64> quota_; |
| 64 |
| 65 size_t num_callbacks_to_wait_; |
| 66 size_t num_callbacks_received_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(GetOriginsTask); |
| 69 }; |
| 70 |
| 71 GetOriginsTask::GetOriginsTask( |
| 72 const AwQuotaManagerBridgeImpl::GetOriginsCallback& callback, |
| 73 QuotaManager* quota_manager) |
| 74 : ui_callback_(callback), |
| 75 quota_manager_(quota_manager) { |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 77 } |
| 78 |
| 79 GetOriginsTask::~GetOriginsTask() {} |
| 80 |
| 81 void GetOriginsTask::Run() { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 BrowserThread::PostTask( |
| 84 BrowserThread::IO, |
| 85 FROM_HERE, |
| 86 base::Bind(&QuotaManager::GetOriginsModifiedSince, |
| 87 quota_manager_, |
| 88 quota::kStorageTypeTemporary, |
| 89 base::Time() /* Since beginning of time. */, |
| 90 base::Bind(&GetOriginsTask::OnOriginsObtained, this))); |
| 91 } |
| 92 |
| 93 void GetOriginsTask::OnOriginsObtained( |
| 94 const std::set<GURL>& origins, quota::StorageType type) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 96 num_callbacks_to_wait_ = origins.size(); |
| 97 num_callbacks_received_ = 0u; |
| 98 |
| 99 for (std::set<GURL>::const_iterator origin = origins.begin(); |
| 100 origin != origins.end(); |
| 101 ++origin) { |
| 102 quota_manager_->GetUsageAndQuota( |
| 103 *origin, |
| 104 type, |
| 105 base::Bind(&GetOriginsTask::OnUsageAndQuotaObtained, this, *origin)); |
| 106 } |
| 107 |
| 108 CheckDone(); |
| 109 } |
| 110 |
| 111 void GetOriginsTask::OnUsageAndQuotaObtained(const GURL& origin, |
| 112 quota::QuotaStatusCode status_code, |
| 113 int64 usage, |
| 114 int64 quota) { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 116 if (status_code == quota::kQuotaStatusOk) { |
| 117 origin_.push_back(origin.spec()); |
| 118 usage_.push_back(usage); |
| 119 quota_.push_back(quota); |
| 120 } |
| 121 |
| 122 ++num_callbacks_received_; |
| 123 CheckDone(); |
| 124 } |
| 125 |
| 126 void GetOriginsTask::CheckDone() { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 128 if (num_callbacks_received_ == num_callbacks_to_wait_) { |
| 129 BrowserThread::PostTask( |
| 130 BrowserThread::UI, |
| 131 FROM_HERE, |
| 132 base::Bind(&GetOriginsTask::DoneOnUIThread, this)); |
| 133 } else if (num_callbacks_received_ > num_callbacks_to_wait_) { |
| 134 NOTREACHED(); |
| 135 } |
| 136 } |
| 137 |
| 138 // This method is to avoid copying the 3 vector arguments into a bound callback. |
| 139 void GetOriginsTask::DoneOnUIThread() { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 141 ui_callback_.Run(origin_, usage_, quota_); |
| 142 } |
| 143 |
| 144 } // namespace |
| 145 |
| 146 |
| 147 // static |
| 148 jint GetDefaultNativeAwQuotaManagerBridge(JNIEnv* env, jclass clazz) { |
| 149 content::ContentBrowserClient* browser_client = |
| 150 content::GetContentClient()->browser(); |
| 151 DCHECK(browser_client); |
| 152 |
| 153 AwContentBrowserClient* aw_browser_client = |
| 154 AwContentBrowserClient::FromContentBrowserClient(browser_client); |
| 155 AwBrowserContext* browser_context = aw_browser_client->GetAwBrowserContext(); |
| 156 DCHECK(browser_context); |
| 157 |
| 158 AwQuotaManagerBridgeImpl* bridge = static_cast<AwQuotaManagerBridgeImpl*>( |
| 159 browser_context->GetQuotaManagerBridge()); |
| 160 DCHECK(bridge); |
| 161 return reinterpret_cast<jint>(bridge); |
| 162 } |
| 163 |
| 164 AwQuotaManagerBridgeImpl::AwQuotaManagerBridgeImpl( |
| 165 AwBrowserContext* browser_context) |
| 166 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 167 browser_context_(browser_context) { |
| 168 } |
| 169 |
| 170 AwQuotaManagerBridgeImpl::~AwQuotaManagerBridgeImpl() {} |
| 171 |
| 172 void AwQuotaManagerBridgeImpl::Init(JNIEnv* env, jobject object) { |
| 173 java_ref_ = JavaObjectWeakGlobalRef(env, object); |
| 174 } |
| 175 |
| 176 QuotaManager* AwQuotaManagerBridgeImpl::GetQuotaManager() const { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 178 |
| 179 // AndroidWebview does not use per-site storage partitions. |
| 180 StoragePartition* storage_partition = |
| 181 content::BrowserContext::GetStoragePartitionForSite( |
| 182 browser_context_, GURL()); |
| 183 DCHECK(storage_partition); |
| 184 |
| 185 QuotaManager* quota_manager = storage_partition->GetQuotaManager(); |
| 186 DCHECK(quota_manager); |
| 187 return quota_manager; |
| 188 } |
| 189 |
| 190 namespace { |
| 191 |
| 192 void IgnoreStatus(quota::QuotaStatusCode status_code) {} |
| 193 |
| 194 void DeleteAllOriginData( |
| 195 QuotaManager* quota_manager, |
| 196 const std::set<GURL>& origins, |
| 197 quota::StorageType type) { |
| 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 199 for (std::set<GURL>::const_iterator origin = origins.begin(); |
| 200 origin != origins.end(); |
| 201 ++origin) { |
| 202 quota_manager->DeleteOriginData(*origin, |
| 203 type, |
| 204 QuotaClient::kAllClientsMask, |
| 205 base::Bind(&IgnoreStatus)); |
| 206 } |
| 207 } |
| 208 |
| 209 } // namespace |
| 210 |
| 211 // Cannot directly call StoragePartition clear data methods because cookies are |
| 212 // controlled separately. |
| 213 // TODO(boliu): Should consider dedup delete code with StoragePartition. |
| 214 void AwQuotaManagerBridgeImpl::DeleteAllData(JNIEnv* env, jobject object) { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 216 scoped_refptr<QuotaManager> quota_manager = GetQuotaManager(); |
| 217 BrowserThread::PostTask( |
| 218 BrowserThread::IO, |
| 219 FROM_HERE, |
| 220 base::Bind(&QuotaManager::GetOriginsModifiedSince, |
| 221 quota_manager, |
| 222 quota::kStorageTypeTemporary, |
| 223 base::Time() /* Since beginning of time. */, |
| 224 base::Bind(&DeleteAllOriginData, quota_manager))); |
| 225 |
| 226 // TODO(boliu): This needs to clear WebStorage (ie localStorage and |
| 227 // sessionStorage). |
| 228 } |
| 229 |
| 230 void AwQuotaManagerBridgeImpl::DeleteOrigin( |
| 231 JNIEnv* env, jobject object, jstring origin) { |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 233 BrowserThread::PostTask( |
| 234 BrowserThread::IO, |
| 235 FROM_HERE, |
| 236 base::Bind(&QuotaManager::DeleteOriginData, |
| 237 GetQuotaManager(), |
| 238 GURL(base::android::ConvertJavaStringToUTF16(env, origin)), |
| 239 quota::kStorageTypeTemporary, |
| 240 QuotaClient::kAllClientsMask, |
| 241 base::Bind(&IgnoreStatus))); |
| 242 } |
| 243 |
| 244 void AwQuotaManagerBridgeImpl::GetOrigins( |
| 245 JNIEnv* env, jobject object, jint callback_id) { |
| 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 247 |
| 248 // TODO(boliu): Consider expanding QuotaManager::GetUsageInfo to include |
| 249 // quota so can be used here. |
| 250 |
| 251 const GetOriginsCallback ui_callback = base::Bind( |
| 252 &AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl, |
| 253 weak_factory_.GetWeakPtr(), |
| 254 callback_id); |
| 255 |
| 256 (new GetOriginsTask(ui_callback, GetQuotaManager()))->Run(); |
| 257 } |
| 258 |
| 259 void AwQuotaManagerBridgeImpl::GetOriginsCallbackImpl( |
| 260 int jcallback_id, |
| 261 const std::vector<std::string>& origin, |
| 262 const std::vector<int64>& usage, |
| 263 const std::vector<int64>& quota) { |
| 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 265 JNIEnv* env = AttachCurrentThread(); |
| 266 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 267 if (obj.is_null()) |
| 268 return; |
| 269 |
| 270 Java_AwQuotaManagerBridge_onGetOriginsCallback( |
| 271 env, |
| 272 obj.obj(), |
| 273 jcallback_id, |
| 274 base::android::ToJavaArrayOfStrings(env, origin).obj(), |
| 275 base::android::ToJavaLongArray(env, usage).obj(), |
| 276 base::android::ToJavaLongArray(env, quota).obj()); |
| 277 } |
| 278 |
| 279 namespace { |
| 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 usage = 0; |
| 289 quota = 0; |
| 290 } |
| 291 BrowserThread::PostTask( |
| 292 BrowserThread::UI, |
| 293 FROM_HERE, |
| 294 base::Bind(ui_callback, usage, quota)); |
| 295 } |
| 296 |
| 297 } // namespace |
| 298 |
| 299 void AwQuotaManagerBridgeImpl::GetUsageAndQuotaForOrigin( |
| 300 JNIEnv* env, jobject object, |
| 301 jstring origin, |
| 302 jint callback_id, |
| 303 bool is_quota) { |
| 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 305 const QuotaUsageCallback ui_callback = base::Bind( |
| 306 &AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl, |
| 307 weak_factory_.GetWeakPtr(), |
| 308 callback_id, |
| 309 is_quota); |
| 310 |
| 311 BrowserThread::PostTask( |
| 312 BrowserThread::IO, |
| 313 FROM_HERE, |
| 314 base::Bind(&QuotaManager::GetUsageAndQuota, |
| 315 GetQuotaManager(), |
| 316 GURL(base::android::ConvertJavaStringToUTF16(env, origin)), |
| 317 quota::kStorageTypeTemporary, |
| 318 base::Bind(&OnUsageAndQuotaObtained, ui_callback))); |
| 319 } |
| 320 |
| 321 void AwQuotaManagerBridgeImpl::QuotaUsageCallbackImpl( |
| 322 int jcallback_id, bool is_quota, int64 usage, int64 quota) { |
| 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 324 JNIEnv* env = AttachCurrentThread(); |
| 325 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 326 if (obj.is_null()) |
| 327 return; |
| 328 |
| 329 Java_AwQuotaManagerBridge_onGetUsageAndQuotaForOriginCallback( |
| 330 env, obj.obj(), jcallback_id, is_quota, usage, quota); |
| 331 } |
| 332 |
| 333 bool RegisterAwQuotaManagerBridge(JNIEnv* env) { |
| 334 return RegisterNativesImpl(env) >= 0; |
| 335 } |
| 336 |
| 337 } // namespace android_webview |
OLD | NEW |