Home | History | Annotate | Download | only in quota
      1 // Copyright 2014 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 "storage/browser/quota/quota_manager_proxy.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/callback.h"
     10 #include "base/sequenced_task_runner.h"
     11 #include "base/single_thread_task_runner.h"
     12 #include "base/strings/string_number_conversions.h"
     13 #include "base/task_runner_util.h"
     14 
     15 namespace storage {
     16 
     17 namespace {
     18 
     19 void DidGetUsageAndQuota(
     20     base::SequencedTaskRunner* original_task_runner,
     21     const QuotaManagerProxy::GetUsageAndQuotaCallback& callback,
     22     QuotaStatusCode status, int64 usage, int64 quota) {
     23   if (!original_task_runner->RunsTasksOnCurrentThread()) {
     24     original_task_runner->PostTask(
     25         FROM_HERE,
     26         base::Bind(&DidGetUsageAndQuota,
     27                    make_scoped_refptr(original_task_runner),
     28                    callback, status, usage, quota));
     29     return;
     30   }
     31   callback.Run(status, usage, quota);
     32 }
     33 
     34 }  // namespace
     35 
     36 void QuotaManagerProxy::RegisterClient(QuotaClient* client) {
     37   if (!io_thread_->BelongsToCurrentThread() &&
     38       io_thread_->PostTask(
     39           FROM_HERE,
     40           base::Bind(&QuotaManagerProxy::RegisterClient, this, client))) {
     41     return;
     42   }
     43 
     44   if (manager_)
     45     manager_->RegisterClient(client);
     46   else
     47     client->OnQuotaManagerDestroyed();
     48 }
     49 
     50 void QuotaManagerProxy::NotifyStorageAccessed(
     51     QuotaClient::ID client_id,
     52     const GURL& origin,
     53     StorageType type) {
     54   if (!io_thread_->BelongsToCurrentThread()) {
     55     io_thread_->PostTask(
     56         FROM_HERE,
     57         base::Bind(&QuotaManagerProxy::NotifyStorageAccessed, this, client_id,
     58                    origin, type));
     59     return;
     60   }
     61 
     62   if (manager_)
     63     manager_->NotifyStorageAccessed(client_id, origin, type);
     64 }
     65 
     66 void QuotaManagerProxy::NotifyStorageModified(
     67     QuotaClient::ID client_id,
     68     const GURL& origin,
     69     StorageType type,
     70     int64 delta) {
     71   if (!io_thread_->BelongsToCurrentThread()) {
     72     io_thread_->PostTask(
     73         FROM_HERE,
     74         base::Bind(&QuotaManagerProxy::NotifyStorageModified, this, client_id,
     75                    origin, type, delta));
     76     return;
     77   }
     78 
     79   if (manager_)
     80     manager_->NotifyStorageModified(client_id, origin, type, delta);
     81 }
     82 
     83 void QuotaManagerProxy::NotifyOriginInUse(
     84     const GURL& origin) {
     85   if (!io_thread_->BelongsToCurrentThread()) {
     86     io_thread_->PostTask(
     87         FROM_HERE,
     88         base::Bind(&QuotaManagerProxy::NotifyOriginInUse, this, origin));
     89     return;
     90   }
     91 
     92   if (manager_)
     93     manager_->NotifyOriginInUse(origin);
     94 }
     95 
     96 void QuotaManagerProxy::NotifyOriginNoLongerInUse(
     97     const GURL& origin) {
     98   if (!io_thread_->BelongsToCurrentThread()) {
     99     io_thread_->PostTask(
    100         FROM_HERE,
    101         base::Bind(&QuotaManagerProxy::NotifyOriginNoLongerInUse, this,
    102                    origin));
    103     return;
    104   }
    105   if (manager_)
    106     manager_->NotifyOriginNoLongerInUse(origin);
    107 }
    108 
    109 void QuotaManagerProxy::SetUsageCacheEnabled(QuotaClient::ID client_id,
    110                                              const GURL& origin,
    111                                              StorageType type,
    112                                              bool enabled) {
    113   if (!io_thread_->BelongsToCurrentThread()) {
    114     io_thread_->PostTask(
    115         FROM_HERE,
    116         base::Bind(&QuotaManagerProxy::SetUsageCacheEnabled, this,
    117                    client_id, origin, type, enabled));
    118     return;
    119   }
    120   if (manager_)
    121     manager_->SetUsageCacheEnabled(client_id, origin, type, enabled);
    122 }
    123 
    124 void QuotaManagerProxy::GetUsageAndQuota(
    125     base::SequencedTaskRunner* original_task_runner,
    126     const GURL& origin,
    127     StorageType type,
    128     const GetUsageAndQuotaCallback& callback) {
    129   if (!io_thread_->BelongsToCurrentThread()) {
    130     io_thread_->PostTask(
    131         FROM_HERE,
    132         base::Bind(&QuotaManagerProxy::GetUsageAndQuota, this,
    133                    make_scoped_refptr(original_task_runner),
    134                    origin, type, callback));
    135     return;
    136   }
    137   if (!manager_) {
    138     DidGetUsageAndQuota(original_task_runner, callback, kQuotaErrorAbort, 0, 0);
    139     return;
    140   }
    141   manager_->GetUsageAndQuota(
    142       origin, type,
    143       base::Bind(&DidGetUsageAndQuota,
    144                  make_scoped_refptr(original_task_runner), callback));
    145 }
    146 
    147 QuotaManager* QuotaManagerProxy::quota_manager() const {
    148   DCHECK(!io_thread_.get() || io_thread_->BelongsToCurrentThread());
    149   return manager_;
    150 }
    151 
    152 QuotaManagerProxy::QuotaManagerProxy(
    153     QuotaManager* manager,
    154     const scoped_refptr<base::SingleThreadTaskRunner>& io_thread)
    155     : manager_(manager), io_thread_(io_thread) {
    156 }
    157 
    158 QuotaManagerProxy::~QuotaManagerProxy() {
    159 }
    160 
    161 }  // namespace storage
    162