text: // Copyright 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 "content/browser/dom_storage/dom_storage_context_wrapper.h"
  
  #include "base/bind.h"
  #include "base/bind_helpers.h"
  #include "base/files/file_path.h"
  #include "base/message_loop/message_loop_proxy.h"
  #include "content/browser/dom_storage/dom_storage_area.h"
  #include "content/browser/dom_storage/dom_storage_context_impl.h"
  #include "content/browser/dom_storage/dom_storage_task_runner.h"
  #include "content/browser/dom_storage/session_storage_namespace_impl.h"
  #include "content/public/browser/browser_thread.h"
  #include "content/public/browser/local_storage_usage_info.h"
  #include "content/public/browser/session_storage_usage_info.h"
  
  namespace content {
  namespace {
  
  const char kLocalStorageDirectory[] = "Local Storage";
  const char kSessionStorageDirectory[] = "Session Storage";
  
  void InvokeLocalStorageUsageCallbackHelper(
        const DOMStorageContext::GetLocalStorageUsageCallback& callback,
        const std::vector<LocalStorageUsageInfo>* infos) {
    callback.Run(*infos);
  }
  
  void GetLocalStorageUsageHelper(
      base::MessageLoopProxy* reply_loop,
      DOMStorageContextImpl* context,
      const DOMStorageContext::GetLocalStorageUsageCallback& callback) {
    std::vector<LocalStorageUsageInfo>* infos =
        new std::vector<LocalStorageUsageInfo>;
    context->GetLocalStorageUsage(infos, true);
    reply_loop->PostTask(
        FROM_HERE,
        base::Bind(&InvokeLocalStorageUsageCallbackHelper,
                   callback, base::Owned(infos)));
  }
  
  void InvokeSessionStorageUsageCallbackHelper(
        const DOMStorageContext::GetSessionStorageUsageCallback& callback,
        const std::vector<SessionStorageUsageInfo>* infos) {
    callback.Run(*infos);
  }
  
  void GetSessionStorageUsageHelper(
      base::MessageLoopProxy* reply_loop,
      DOMStorageContextImpl* context,
      const DOMStorageContext::GetSessionStorageUsageCallback& callback) {
    std::vector<SessionStorageUsageInfo>* infos =
        new std::vector<SessionStorageUsageInfo>;
    context->GetSessionStorageUsage(infos);
    reply_loop->PostTask(
        FROM_HERE,
        base::Bind(&InvokeSessionStorageUsageCallbackHelper,
                   callback, base::Owned(infos)));
  }
  
  }  // namespace
  
  DOMStorageContextWrapper::DOMStorageContextWrapper(
      const base::FilePath& data_path,
      storage::SpecialStoragePolicy* special_storage_policy) {
    base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool();
    context_ = new DOMStorageContextImpl(
        data_path.empty() ? data_path
                          : data_path.AppendASCII(kLocalStorageDirectory),
        data_path.empty() ? data_path
                          : data_path.AppendASCII(kSessionStorageDirectory),
        special_storage_policy,
        new DOMStorageWorkerPoolTaskRunner(
            worker_pool,
            worker_pool->GetNamedSequenceToken("dom_storage_primary"),
            worker_pool->GetNamedSequenceToken("dom_storage_commit"),
            BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
                .get()));
  }
  
  DOMStorageContextWrapper::~DOMStorageContextWrapper() {
  }
  
  void DOMStorageContextWrapper::GetLocalStorageUsage(
      const GetLocalStorageUsageCallback& callback) {
    DCHECK(context_.get());
    context_->task_runner()
        ->PostShutdownBlockingTask(FROM_HERE,
                                   DOMStorageTaskRunner::PRIMARY_SEQUENCE,
                                   base::Bind(&GetLocalStorageUsageHelper,
                                              base::MessageLoopProxy::current(),
                                              context_,
                                              callback));
  }
  
  void DOMStorageContextWrapper::GetSessionStorageUsage(
      const GetSessionStorageUsageCallback& callback) {
    DCHECK(context_.get());
    context_->task_runner()
        ->PostShutdownBlockingTask(FROM_HERE,
                                   DOMStorageTaskRunner::PRIMARY_SEQUENCE,
                                   base::Bind(&GetSessionStorageUsageHelper,
                                              base::MessageLoopProxy::current(),
                                              context_,
                                              callback));
  }
  
  void DOMStorageContextWrapper::DeleteLocalStorage(const GURL& origin) {
    DCHECK(context_.get());
    context_->task_runner()->PostShutdownBlockingTask(
        FROM_HERE,
        DOMStorageTaskRunner::PRIMARY_SEQUENCE,
        base::Bind(&DOMStorageContextImpl::DeleteLocalStorage, context_, origin));
  }
  
  void DOMStorageContextWrapper::DeleteSessionStorage(
      const SessionStorageUsageInfo& usage_info) {
    DCHECK(context_.get());
    context_->task_runner()->PostShutdownBlockingTask(
        FROM_HERE,
        DOMStorageTaskRunner::PRIMARY_SEQUENCE,
        base::Bind(&DOMStorageContextImpl::DeleteSessionStorage,
                   context_, usage_info));
  }
  
  void DOMStorageContextWrapper::SetSaveSessionStorageOnDisk() {
    DCHECK(context_.get());
    context_->SetSaveSessionStorageOnDisk();
  }
  
  scoped_refptr<SessionStorageNamespace>
  DOMStorageContextWrapper::RecreateSessionStorage(
      const std::string& persistent_id) {
    return scoped_refptr<SessionStorageNamespace>(
        new SessionStorageNamespaceImpl(this, persistent_id));
  }
  
  void DOMStorageContextWrapper::StartScavengingUnusedSessionStorage() {
    DCHECK(context_.get());
    context_->task_runner()->PostShutdownBlockingTask(
        FROM_HERE,
        DOMStorageTaskRunner::PRIMARY_SEQUENCE,
        base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage,
                   context_));
  }
  
  void DOMStorageContextWrapper::SetForceKeepSessionState() {
    DCHECK(context_.get());
    context_->task_runner()->PostShutdownBlockingTask(
        FROM_HERE,
        DOMStorageTaskRunner::PRIMARY_SEQUENCE,
        base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_));
  }
  
  void DOMStorageContextWrapper::Shutdown() {
    DCHECK(context_.get());
    context_->task_runner()->PostShutdownBlockingTask(
        FROM_HERE,
        DOMStorageTaskRunner::PRIMARY_SEQUENCE,
        base::Bind(&DOMStorageContextImpl::Shutdown, context_));
  }
  
  }  // namespace content
  