Home | History | Annotate | Download | only in dom_storage
      1 // Copyright 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 "content/browser/dom_storage/dom_storage_context_wrapper.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/files/file_path.h"
     10 #include "base/message_loop/message_loop_proxy.h"
     11 #include "content/browser/dom_storage/dom_storage_area.h"
     12 #include "content/browser/dom_storage/dom_storage_context_impl.h"
     13 #include "content/browser/dom_storage/dom_storage_task_runner.h"
     14 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "content/public/browser/local_storage_usage_info.h"
     17 #include "content/public/browser/session_storage_usage_info.h"
     18 
     19 namespace content {
     20 namespace {
     21 
     22 const char kLocalStorageDirectory[] = "Local Storage";
     23 const char kSessionStorageDirectory[] = "Session Storage";
     24 
     25 void InvokeLocalStorageUsageCallbackHelper(
     26       const DOMStorageContext::GetLocalStorageUsageCallback& callback,
     27       const std::vector<LocalStorageUsageInfo>* infos) {
     28   callback.Run(*infos);
     29 }
     30 
     31 void GetLocalStorageUsageHelper(
     32     base::MessageLoopProxy* reply_loop,
     33     DOMStorageContextImpl* context,
     34     const DOMStorageContext::GetLocalStorageUsageCallback& callback) {
     35   std::vector<LocalStorageUsageInfo>* infos =
     36       new std::vector<LocalStorageUsageInfo>;
     37   context->GetLocalStorageUsage(infos, true);
     38   reply_loop->PostTask(
     39       FROM_HERE,
     40       base::Bind(&InvokeLocalStorageUsageCallbackHelper,
     41                  callback, base::Owned(infos)));
     42 }
     43 
     44 void InvokeSessionStorageUsageCallbackHelper(
     45       const DOMStorageContext::GetSessionStorageUsageCallback& callback,
     46       const std::vector<SessionStorageUsageInfo>* infos) {
     47   callback.Run(*infos);
     48 }
     49 
     50 void GetSessionStorageUsageHelper(
     51     base::MessageLoopProxy* reply_loop,
     52     DOMStorageContextImpl* context,
     53     const DOMStorageContext::GetSessionStorageUsageCallback& callback) {
     54   std::vector<SessionStorageUsageInfo>* infos =
     55       new std::vector<SessionStorageUsageInfo>;
     56   context->GetSessionStorageUsage(infos);
     57   reply_loop->PostTask(
     58       FROM_HERE,
     59       base::Bind(&InvokeSessionStorageUsageCallbackHelper,
     60                  callback, base::Owned(infos)));
     61 }
     62 
     63 }  // namespace
     64 
     65 DOMStorageContextWrapper::DOMStorageContextWrapper(
     66     const base::FilePath& data_path,
     67     quota::SpecialStoragePolicy* special_storage_policy) {
     68   base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool();
     69   context_ = new DOMStorageContextImpl(
     70       data_path.empty() ? data_path
     71                         : data_path.AppendASCII(kLocalStorageDirectory),
     72       data_path.empty() ? data_path
     73                         : data_path.AppendASCII(kSessionStorageDirectory),
     74       special_storage_policy,
     75       new DOMStorageWorkerPoolTaskRunner(
     76           worker_pool,
     77           worker_pool->GetNamedSequenceToken("dom_storage_primary"),
     78           worker_pool->GetNamedSequenceToken("dom_storage_commit"),
     79           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
     80               .get()));
     81 }
     82 
     83 DOMStorageContextWrapper::~DOMStorageContextWrapper() {
     84 }
     85 
     86 void DOMStorageContextWrapper::GetLocalStorageUsage(
     87     const GetLocalStorageUsageCallback& callback) {
     88   DCHECK(context_.get());
     89   context_->task_runner()
     90       ->PostShutdownBlockingTask(FROM_HERE,
     91                                  DOMStorageTaskRunner::PRIMARY_SEQUENCE,
     92                                  base::Bind(&GetLocalStorageUsageHelper,
     93                                             base::MessageLoopProxy::current(),
     94                                             context_,
     95                                             callback));
     96 }
     97 
     98 void DOMStorageContextWrapper::GetSessionStorageUsage(
     99     const GetSessionStorageUsageCallback& callback) {
    100   DCHECK(context_.get());
    101   context_->task_runner()
    102       ->PostShutdownBlockingTask(FROM_HERE,
    103                                  DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    104                                  base::Bind(&GetSessionStorageUsageHelper,
    105                                             base::MessageLoopProxy::current(),
    106                                             context_,
    107                                             callback));
    108 }
    109 
    110 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL& origin) {
    111   DCHECK(context_.get());
    112   context_->task_runner()->PostShutdownBlockingTask(
    113       FROM_HERE,
    114       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    115       base::Bind(&DOMStorageContextImpl::DeleteLocalStorage, context_, origin));
    116 }
    117 
    118 void DOMStorageContextWrapper::DeleteSessionStorage(
    119     const SessionStorageUsageInfo& usage_info) {
    120   DCHECK(context_.get());
    121   context_->task_runner()->PostShutdownBlockingTask(
    122       FROM_HERE,
    123       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    124       base::Bind(&DOMStorageContextImpl::DeleteSessionStorage,
    125                  context_, usage_info));
    126 }
    127 
    128 void DOMStorageContextWrapper::SetSaveSessionStorageOnDisk() {
    129   DCHECK(context_.get());
    130   context_->SetSaveSessionStorageOnDisk();
    131 }
    132 
    133 scoped_refptr<SessionStorageNamespace>
    134 DOMStorageContextWrapper::RecreateSessionStorage(
    135     const std::string& persistent_id) {
    136   return scoped_refptr<SessionStorageNamespace>(
    137       new SessionStorageNamespaceImpl(this, persistent_id));
    138 }
    139 
    140 void DOMStorageContextWrapper::StartScavengingUnusedSessionStorage() {
    141   DCHECK(context_.get());
    142   context_->task_runner()->PostShutdownBlockingTask(
    143       FROM_HERE,
    144       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    145       base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage,
    146                  context_));
    147 }
    148 
    149 void DOMStorageContextWrapper::PurgeMemory() {
    150   DCHECK(context_.get());
    151   context_->task_runner()->PostShutdownBlockingTask(
    152       FROM_HERE,
    153       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    154       base::Bind(&DOMStorageContextImpl::PurgeMemory, context_));
    155 }
    156 
    157 void DOMStorageContextWrapper::SetForceKeepSessionState() {
    158   DCHECK(context_.get());
    159   context_->task_runner()->PostShutdownBlockingTask(
    160       FROM_HERE,
    161       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    162       base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_));
    163 }
    164 
    165 void DOMStorageContextWrapper::Shutdown() {
    166   DCHECK(context_.get());
    167   context_->task_runner()->PostShutdownBlockingTask(
    168       FROM_HERE,
    169       DOMStorageTaskRunner::PRIMARY_SEQUENCE,
    170       base::Bind(&DOMStorageContextImpl::Shutdown, context_));
    171 }
    172 
    173 }  // namespace content
    174