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 "url/gurl.h" 19 #include "webkit/common/fileapi/file_system_types.h" 20 21 namespace fileapi { 22 class FileSystemContext; 23 } 24 25 class 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. 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 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<fileapi::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 fileapi::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. 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); 99 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. 104 // 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(); 109 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); 117 118 // Clear this helper's list of canned filesystems. 119 void Reset(); 120 121 // True if no filesystems are currently stored. 122 bool empty() const; 123 124 // Returns the number of currently stored filesystems. 125 size_t GetFileSystemCount() const; 126 127 // Returns the current list of filesystems. 128 const std::list<FileSystemInfo>& GetFileSystemInfo() { 129 return file_system_info_; 130 } 131 132 // BrowsingDataFileSystemHelper implementation. 133 virtual void StartFetching(const base::Callback< 134 void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE; 135 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 {} 141 142 private: 143 // Used by Clone() to create an object without a Profile 144 CannedBrowsingDataFileSystemHelper(); 145 virtual ~CannedBrowsingDataFileSystemHelper(); 146 147 // Triggers the success callback as the end of a StartFetching workflow. This 148 // must be called on the UI thread. 149 void NotifyOnUIThread(); 150 151 // Holds the current list of filesystems returned to the client. Access to 152 // |file_system_info_| is triggered indirectly via the UI thread and guarded 153 // by |is_fetching_|. This means |file_system_info_| is only accessed while 154 // |is_fetching_| is true. The flag |is_fetching_| is only accessed on the UI 155 // thread. 156 std::list<FileSystemInfo> file_system_info_; 157 158 // The callback passed in at the beginning of the StartFetching workflow so 159 // that it can be triggered via NotifyOnUIThread. 160 base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_; 161 162 // Indicates whether or not we're currently fetching information: set to true 163 // when StartFetching is called on the UI thread, and reset to false when 164 // NotifyOnUIThread triggers the success callback. 165 // This property only mutates on the UI thread. 166 bool is_fetching_; 167 168 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataFileSystemHelper); 169 }; 170 171 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_FILE_SYSTEM_HELPER_H_ 172