Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_file_system_helper.h
blob9e811576bf4e4c4b0582f75d219eb6a4f7e8cd12
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.
5 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_
8 #include <list>
9 #include <map>
10 #include <string>
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 "url/gurl.h"
19 #include "webkit/common/fileapi/file_system_types.h"
21 namespace fileapi {
22 class FileSystemContext;
25 class Profile;
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.
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.
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.
43 class 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();
52 // The origin for which the information is relevant.
53 GURL origin;
54 // FileSystemType to usage (in bytes) map.
55 std::map<fileapi::FileSystemType, int64> usage_map;
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.
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 fileapi::FileSystemContext* file_system_context);
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.
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;
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;
83 protected:
84 friend class base::RefCountedThreadSafe<BrowsingDataFileSystemHelper>;
86 BrowsingDataFileSystemHelper() {}
87 virtual ~BrowsingDataFileSystemHelper() {}
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.
93 class 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);
100 // Creates a copy of the file system helper. StartFetching can only respond
101 // to one client at a time; we need to be able to act on multiple parallel
102 // requests in certain situations (see CookiesTreeModel and its clients). For
103 // these cases, simply clone the object and fire off another fetching process.
105 // Clone() is safe to call while StartFetching() is running. Clients of the
106 // newly created object must themselves execute StartFetching(), however: the
107 // copy will not have a pending fetch.
108 CannedBrowsingDataFileSystemHelper* Clone();
110 // Manually adds a filesystem to the set of canned file systems that this
111 // helper returns via StartFetching. If an origin contains both a temporary
112 // and a persistent filesystem, AddFileSystem must be called twice (once for
113 // each file system type).
114 void AddFileSystem(const GURL& origin,
115 fileapi::FileSystemType type,
116 int64 size);
118 // Clear this helper's list of canned filesystems.
119 void Reset();
121 // True if no filesystems are currently stored.
122 bool empty() const;
124 // Returns the number of currently stored filesystems.
125 size_t GetFileSystemCount() const;
127 // Returns the current list of filesystems.
128 const std::list<FileSystemInfo>& GetFileSystemInfo() {
129 return file_system_info_;
132 // BrowsingDataFileSystemHelper implementation.
133 virtual void StartFetching(const base::Callback<
134 void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE;
136 // Note that this doesn't actually have an implementation for this canned
137 // class. It hasn't been necessary for anything that uses the canned
138 // implementation, as the canned class is only used in tests, or in read-only
139 // contexts (like the non-modal cookie dialog).
140 virtual void DeleteFileSystemOrigin(const GURL& origin) OVERRIDE {}
142 private:
143 // Used by Clone() to create an object without a Profile
144 CannedBrowsingDataFileSystemHelper();
145 virtual ~CannedBrowsingDataFileSystemHelper();
147 // Holds the current list of filesystems returned to the client. Access to
148 // |file_system_info_| is triggered indirectly via the UI thread and guarded
149 // by |is_fetching_|. This means |file_system_info_| is only accessed while
150 // |is_fetching_| is true. The flag |is_fetching_| is only accessed on the UI
151 // thread.
152 std::list<FileSystemInfo> file_system_info_;
154 // The callback passed in at the beginning of the StartFetching workflow so
155 // that it can be triggered via NotifyOnUIThread.
156 base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_;
158 // Indicates whether or not we're currently fetching information: set to true
159 // when StartFetching is called on the UI thread, and reset to false when
160 // NotifyOnUIThread triggers the success callback.
161 // This property only mutates on the UI thread.
162 bool is_fetching_;
164 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataFileSystemHelper);
167 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_