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_
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"
22 class FileSystemContext
;
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
> {
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
);
52 // The origin for which the information is relevant.
54 // FileSystemType to usage (in bytes) map.
55 std::map
<storage::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 storage::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;
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
{
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 // 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
,
108 // Clear this helper's list of canned filesystems.
111 // True if no filesystems are currently stored.
114 // Returns the number of currently stored filesystems.
115 size_t GetFileSystemCount() const;
117 // Returns the current list of filesystems.
118 const std::list
<FileSystemInfo
>& GetFileSystemInfo() {
119 return file_system_info_
;
122 // BrowsingDataFileSystemHelper implementation.
124 const base::Callback
<void(const std::list
<FileSystemInfo
>&)>& callback
)
127 // Note that this doesn't actually have an implementation for this canned
128 // class. It hasn't been necessary for anything that uses the canned
129 // implementation, as the canned class is only used in tests, or in read-only
130 // contexts (like the non-modal cookie dialog).
131 void DeleteFileSystemOrigin(const GURL
& origin
) override
{}
134 ~CannedBrowsingDataFileSystemHelper() override
;
136 // Holds the current list of filesystems returned to the client.
137 std::list
<FileSystemInfo
> file_system_info_
;
139 // The callback passed in at the beginning of the StartFetching workflow so
140 // that it can be triggered via NotifyOnUIThread.
141 base::Callback
<void(const std::list
<FileSystemInfo
>&)> completion_callback_
;
143 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataFileSystemHelper
);
146 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_