1// Copyright (c) 2012 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 CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
6#define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
7
8#include <list>
9#include <map>
10#include <string>
11
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/files/file_path.h"
15#include "base/memory/ref_counted.h"
16#include "base/synchronization/lock.h"
17#include "chrome/common/url_constants.h"
18#include "storage/common/fileapi/file_system_types.h"
19#include "url/gurl.h"
20
21namespace storage {
22class FileSystemContext;
23}
24
25class Profile;
26
27// Defines an interface for classes that deal with aggregating and deleting
28// browsing data stored in an origin's file systems.
29// BrowsingDataFileSystemHelper instances for a specific profile should be
30// created via the static Create method. Each instance will lazily fetch file
31// system data when a client calls StartFetching from the UI thread, and will
32// notify the client via a supplied callback when the data is available.
33// Only one StartFetching task can run at a time: executing StartFetching while
34// another StartFetching task is running will DCHECK.
35//
36// The client's callback is passed a list of FileSystemInfo objects containing
37// usage information for each origin's temporary and persistent file systems.
38//
39// Clients may remove an origin's file systems at any time (even before fetching
40// data) by calling DeleteFileSystemOrigin() on the UI thread. Calling
41// DeleteFileSystemOrigin() for an origin that doesn't have any is safe; it's
42// just an expensive NOOP.
43class BrowsingDataFileSystemHelper
44    : public base::RefCountedThreadSafe<BrowsingDataFileSystemHelper> {
45 public:
46  // Detailed information about a file system, including it's origin GURL,
47  // the amount of data (in bytes) for each sandboxed filesystem type.
48  struct FileSystemInfo {
49    explicit FileSystemInfo(const GURL& origin);
50    ~FileSystemInfo();
51
52    // The origin for which the information is relevant.
53    GURL origin;
54    // FileSystemType to usage (in bytes) map.
55    std::map<storage::FileSystemType, int64> usage_map;
56  };
57
58  // Creates a BrowsingDataFileSystemHelper instance for the file systems
59  // stored in |profile|'s user data directory. The BrowsingDataFileSystemHelper
60  // object will hold a reference to the Profile that's passed in, but is not
61  // responsible for destroying it.
62  //
63  // The BrowsingDataFileSystemHelper will not change the profile itself, but
64  // can modify data it contains (by removing file systems).
65  static BrowsingDataFileSystemHelper* Create(
66      storage::FileSystemContext* file_system_context);
67
68  // Starts the process of fetching file system data, which will call |callback|
69  // upon completion, passing it a constant list of FileSystemInfo objects.
70  // StartFetching must be called only in the UI thread; the provided Callback1
71  // will likewise be executed asynchronously on the UI thread.
72  //
73  // BrowsingDataFileSystemHelper takes ownership of the Callback1, and is
74  // responsible for deleting it once it's no longer needed.
75  virtual void StartFetching(const base::Callback<
76      void(const std::list<FileSystemInfo>&)>& callback) = 0;
77
78  // Deletes any temporary or persistent file systems associated with |origin|
79  // from the disk. Deletion will occur asynchronously on the FILE thread, but
80  // this function must be called only on the UI thread.
81  virtual void DeleteFileSystemOrigin(const GURL& origin) = 0;
82
83 protected:
84  friend class base::RefCountedThreadSafe<BrowsingDataFileSystemHelper>;
85
86  BrowsingDataFileSystemHelper() {}
87  virtual ~BrowsingDataFileSystemHelper() {}
88};
89
90// An implementation of the BrowsingDataFileSystemHelper interface that can
91// be manually populated with data, rather than fetching data from the file
92// systems created in a particular Profile.
93class CannedBrowsingDataFileSystemHelper
94    : public BrowsingDataFileSystemHelper {
95 public:
96  // |profile| is unused in this canned implementation, but it's the interface
97  // we're writing to, so we'll accept it, but not store it.
98  explicit CannedBrowsingDataFileSystemHelper(Profile* profile);
99
100  // Manually adds a filesystem to the set of canned file systems that this
101  // helper returns via StartFetching. If an origin contains both a temporary
102  // and a persistent filesystem, AddFileSystem must be called twice (once for
103  // each file system type).
104  void AddFileSystem(const GURL& origin,
105                     storage::FileSystemType type,
106                     int64 size);
107
108  // Clear this helper's list of canned filesystems.
109  void Reset();
110
111  // True if no filesystems are currently stored.
112  bool empty() const;
113
114  // Returns the number of currently stored filesystems.
115  size_t GetFileSystemCount() const;
116
117  // Returns the current list of filesystems.
118  const std::list<FileSystemInfo>& GetFileSystemInfo() {
119    return file_system_info_;
120  }
121
122  // BrowsingDataFileSystemHelper implementation.
123  virtual void StartFetching(const base::Callback<
124      void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE;
125
126  // Note that this doesn't actually have an implementation for this canned
127  // class. It hasn't been necessary for anything that uses the canned
128  // implementation, as the canned class is only used in tests, or in read-only
129  // contexts (like the non-modal cookie dialog).
130  virtual void DeleteFileSystemOrigin(const GURL& origin) OVERRIDE {}
131
132 private:
133  virtual ~CannedBrowsingDataFileSystemHelper();
134
135  // Holds the current list of filesystems returned to the client.
136  std::list<FileSystemInfo> file_system_info_;
137
138  // The callback passed in at the beginning of the StartFetching workflow so
139  // that it can be triggered via NotifyOnUIThread.
140  base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_;
141
142  DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataFileSystemHelper);
143};
144
145#endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
146