15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/sandbox_file_system_backend.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/task_runner_util.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/file_stream_reader.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/async_file_util_adapter.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/copy_or_move_file_validator.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_stream_writer.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_context.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation_context.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_options.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_usage_cache.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/obfuscated_file_util.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/sandbox_quota_observer.h"
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/quota/quota_manager.h"
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_types.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_util.h"
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using storage::QuotaManagerProxy;
3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using storage::SpecialStoragePolicy;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
347dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochSandboxFileSystemBackend::SandboxFileSystemBackend(
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    SandboxFileSystemBackendDelegate* delegate)
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    : delegate_(delegate),
37a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      enable_temporary_file_system_in_incognito_(false) {
38bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
39bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
40bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben MurdochSandboxFileSystemBackend::~SandboxFileSystemBackend() {
41bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
42bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
43bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochbool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const {
44bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  return type == kFileSystemTypeTemporary ||
45a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)         type == kFileSystemTypePersistent;
46bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
47bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
48bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochvoid SandboxFileSystemBackend::Initialize(FileSystemContext* context) {
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set quota observers.
5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  delegate_->RegisterQuotaUpdateObserver(storage::kFileSystemTypeTemporary);
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  delegate_->AddFileAccessObserver(
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      storage::kFileSystemTypeTemporary, delegate_->quota_observer(), NULL);
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  delegate_->RegisterQuotaUpdateObserver(storage::kFileSystemTypePersistent);
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  delegate_->AddFileAccessObserver(
5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      storage::kFileSystemTypePersistent, delegate_->quota_observer(), NULL);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void SandboxFileSystemBackend::ResolveURL(
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const FileSystemURL& url,
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    OpenFileSystemMode mode,
64bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    const OpenFileSystemCallback& callback) {
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(CanHandleType(url.type()));
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (delegate_->file_system_options().is_incognito() &&
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !(url.type() == kFileSystemTypeTemporary &&
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        enable_temporary_file_system_in_incognito_)) {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(kinuko): return an isolated temporary directory.
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    callback.Run(GURL(), std::string(), base::File::FILE_ERROR_SECURITY);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  delegate_->OpenFileSystem(url.origin(),
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            url.type(),
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            mode,
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            callback,
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            GetFileSystemRootURI(url.origin(), url.type()));
80a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
827dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochAsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil(
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FileSystemType type) {
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return delegate_->file_util();
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciWatcherManager* SandboxFileSystemBackend::GetWatcherManager(
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    FileSystemType type) {
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return NULL;
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)CopyOrMoveFileValidatorFactory*
947dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochSandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FileSystemType type,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Error* error_code) {
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(error_code);
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *error_code = base::File::FILE_OK;
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return NULL;
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1027dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochFileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation(
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileSystemURL& url,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FileSystemContext* context,
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Error* error_code) const {
106a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(CanHandleType(url.type()));
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(error_code);
108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  scoped_ptr<FileSystemOperationContext> operation_context =
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      delegate_->CreateFileSystemOperationContext(url, context, error_code);
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!operation_context)
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return NULL;
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  SpecialStoragePolicy* policy = delegate_->special_storage_policy();
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (policy && policy->IsStorageUnlimited(url.origin()))
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    operation_context->set_quota_limit_type(storage::kQuotaLimitTypeUnlimited);
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  else
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    operation_context->set_quota_limit_type(storage::kQuotaLimitTypeLimited);
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return FileSystemOperation::Create(url, context, operation_context.Pass());
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool SandboxFileSystemBackend::SupportsStreaming(
12503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemURL& url) const {
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)bool SandboxFileSystemBackend::HasInplaceCopyImplementation(
13003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemType type) const {
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
13303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
13403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)scoped_ptr<storage::FileStreamReader>
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochSandboxFileSystemBackend::CreateFileStreamReader(
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileSystemURL& url,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int64 offset,
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int64 max_bytes_to_read,
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Time& expected_modification_time,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FileSystemContext* context) const {
141a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(CanHandleType(url.type()));
1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return delegate_->CreateFileStreamReader(
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      url, offset, expected_modification_time, context);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)scoped_ptr<storage::FileStreamWriter>
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochSandboxFileSystemBackend::CreateFileStreamWriter(
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileSystemURL& url,
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int64 offset,
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FileSystemContext* context) const {
152a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(CanHandleType(url.type()));
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return delegate_->CreateFileStreamWriter(url, offset, context, url.type());
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1577dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochFileSystemQuotaUtil* SandboxFileSystemBackend::GetQuotaUtil() {
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return delegate_;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst UpdateObserverList* SandboxFileSystemBackend::GetUpdateObservers(
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    FileSystemType type) const {
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return delegate_->GetUpdateObservers(type);
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst ChangeObserverList* SandboxFileSystemBackend::GetChangeObservers(
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    FileSystemType type) const {
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return delegate_->GetChangeObservers(type);
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst AccessObserverList* SandboxFileSystemBackend::GetAccessObservers(
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    FileSystemType type) const {
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return delegate_->GetAccessObservers(type);
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)SandboxFileSystemBackendDelegate::OriginEnumerator*
1777dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochSandboxFileSystemBackend::CreateOriginEnumerator() {
1783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(delegate_);
1793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return delegate_->CreateOriginEnumerator();
1807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
18203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
183