Home | History | Annotate | Download | only in fileapi
      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 #ifndef WEBKIT_BROWSER_FILEAPI_SANDBOX_CONTEXT_H_
      6 #define WEBKIT_BROWSER_FILEAPI_SANDBOX_CONTEXT_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <utility>
     11 
     12 #include "base/files/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/time/time.h"
     17 #include "webkit/browser/fileapi/file_system_backend.h"
     18 #include "webkit/browser/fileapi/file_system_options.h"
     19 #include "webkit/browser/fileapi/file_system_quota_util.h"
     20 #include "webkit/browser/webkit_storage_browser_export.h"
     21 
     22 namespace base {
     23 class SequencedTaskRunner;
     24 }
     25 
     26 namespace quota {
     27 class QuotaManagerProxy;
     28 class SpecialStoragePolicy;
     29 }
     30 
     31 namespace fileapi {
     32 
     33 class AsyncFileUtilAdapter;
     34 class FileSystemURL;
     35 class FileSystemUsageCache;
     36 class ObfuscatedFileUtil;
     37 class SandboxFileSystemBackend;
     38 class SandboxFileSystemTestHelper;
     39 class SandboxQuotaObserver;
     40 
     41 // This class keeps and provides a sandbox file system context.
     42 // An instance of this class is created and owned by FileSystemContext.
     43 class WEBKIT_STORAGE_BROWSER_EXPORT SandboxContext {
     44  public:
     45   typedef FileSystemBackend::OpenFileSystemCallback OpenFileSystemCallback;
     46 
     47   // The FileSystem directory name.
     48   static const base::FilePath::CharType kFileSystemDirectory[];
     49 
     50   // Origin enumerator interface.
     51   // An instance of this interface is assumed to be called on the file thread.
     52   class OriginEnumerator {
     53    public:
     54     virtual ~OriginEnumerator() {}
     55 
     56     // Returns the next origin.  Returns empty if there are no more origins.
     57     virtual GURL Next() = 0;
     58 
     59     // Returns the current origin's information.
     60     virtual bool HasFileSystemType(FileSystemType type) const = 0;
     61   };
     62 
     63   SandboxContext(
     64       quota::QuotaManagerProxy* quota_manager_proxy,
     65       base::SequencedTaskRunner* file_task_runner,
     66       const base::FilePath& profile_path,
     67       quota::SpecialStoragePolicy* special_storage_policy,
     68       const FileSystemOptions& file_system_options);
     69 
     70   ~SandboxContext();
     71 
     72   // Performs API-specific validity checks on the given path |url|.
     73   // Returns true if access to |url| is valid in this filesystem.
     74   bool IsAccessValid(const FileSystemURL& url) const;
     75 
     76   // Returns true if the given |url|'s scheme is allowed to access
     77   // filesystem.
     78   bool IsAllowedScheme(const GURL& url) const;
     79 
     80   // Returns an origin enumerator of sandbox filesystem.
     81   // This method can only be called on the file thread.
     82   OriginEnumerator* CreateOriginEnumerator();
     83 
     84   // Gets a base directory path of the sandboxed filesystem that is
     85   // specified by |origin_url| and |type|.
     86   // (The path is similar to the origin's root path but doesn't contain
     87   // the 'unique' part.)
     88   // Returns an empty path if the given type is invalid.
     89   // This method can only be called on the file thread.
     90   base::FilePath GetBaseDirectoryForOriginAndType(
     91       const GURL& origin_url,
     92       FileSystemType type,
     93       bool create);
     94 
     95   // FileSystemBackend helpers.
     96   void OpenFileSystem(
     97       const GURL& origin_url,
     98       FileSystemType type,
     99       OpenFileSystemMode mode,
    100       const OpenFileSystemCallback& callback,
    101       const GURL& root_url);
    102 
    103   // FileSystemQuotaUtil helpers.
    104   base::PlatformFileError DeleteOriginDataOnFileThread(
    105       FileSystemContext* context,
    106       quota::QuotaManagerProxy* proxy,
    107       const GURL& origin_url,
    108       FileSystemType type);
    109   void GetOriginsForTypeOnFileThread(
    110       FileSystemType type,
    111       std::set<GURL>* origins);
    112   void GetOriginsForHostOnFileThread(
    113       FileSystemType type,
    114       const std::string& host,
    115       std::set<GURL>* origins);
    116   int64 GetOriginUsageOnFileThread(
    117       FileSystemContext* context,
    118       const GURL& origin_url,
    119       FileSystemType type);
    120   void InvalidateUsageCache(
    121       const GURL& origin_url,
    122       FileSystemType type);
    123   void StickyInvalidateUsageCache(
    124       const GURL& origin_url,
    125       FileSystemType type);
    126 
    127   void CollectOpenFileSystemMetrics(base::PlatformFileError error_code);
    128 
    129   base::SequencedTaskRunner* file_task_runner() {
    130     return file_task_runner_.get();
    131   }
    132 
    133   AsyncFileUtilAdapter* file_util() { return sandbox_file_util_.get(); }
    134   FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
    135   SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); };
    136 
    137   quota::SpecialStoragePolicy* special_storage_policy() {
    138     return special_storage_policy_.get();
    139   }
    140 
    141   FileSystemOptions file_system_options() { return file_system_options_; }
    142 
    143   ObfuscatedFileUtil* sync_file_util();
    144 
    145  private:
    146   friend class SandboxQuotaObserver;
    147   friend class SandboxFileSystemTestHelper;
    148 
    149   // Returns a path to the usage cache file.
    150   base::FilePath GetUsageCachePathForOriginAndType(
    151       const GURL& origin_url,
    152       FileSystemType type);
    153 
    154   // Returns a path to the usage cache file (static version).
    155   static base::FilePath GetUsageCachePathForOriginAndType(
    156       ObfuscatedFileUtil* sandbox_file_util,
    157       const GURL& origin_url,
    158       FileSystemType type,
    159       base::PlatformFileError* error_out);
    160 
    161   int64 RecalculateUsage(FileSystemContext* context,
    162                          const GURL& origin,
    163                          FileSystemType type);
    164 
    165   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
    166 
    167   scoped_ptr<AsyncFileUtilAdapter> sandbox_file_util_;
    168   scoped_ptr<FileSystemUsageCache> file_system_usage_cache_;
    169   scoped_ptr<SandboxQuotaObserver> quota_observer_;
    170 
    171   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
    172 
    173   FileSystemOptions file_system_options_;
    174 
    175   // Acccessed only on the file thread.
    176   std::set<GURL> visited_origins_;
    177 
    178   std::set<std::pair<GURL, FileSystemType> > sticky_dirty_origins_;
    179 
    180   base::Time next_release_time_for_open_filesystem_stat_;
    181 
    182   base::WeakPtrFactory<SandboxContext> weak_factory_;
    183 
    184   DISALLOW_COPY_AND_ASSIGN(SandboxContext);
    185 };
    186 
    187 }  // namespace fileapi
    188 
    189 #endif  // WEBKIT_BROWSER_FILEAPI_SANDBOX_CONTEXT_H_
    190