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_INDEXED_DB_HELPER_H_ 6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_HELPER_H_ 7 8 #include <list> 9 #include <set> 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 "base/time/time.h" 18 #include "content/public/browser/indexed_db_context.h" 19 #include "url/gurl.h" 20 21 class Profile; 22 23 // BrowsingDataIndexedDBHelper is an interface for classes dealing with 24 // aggregating and deleting browsing data stored in indexed databases. A 25 // client of this class need to call StartFetching from the UI thread to 26 // initiate the flow, and it'll be notified by the callback in its UI thread at 27 // some later point. 28 class BrowsingDataIndexedDBHelper 29 : public base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper> { 30 public: 31 // Create a BrowsingDataIndexedDBHelper instance for the indexed databases 32 // stored in |profile|'s user data directory. 33 explicit BrowsingDataIndexedDBHelper(content::IndexedDBContext* content); 34 35 // Starts the fetching process, which will notify its completion via 36 // callback. 37 // This must be called only in the UI thread. 38 virtual void StartFetching( 39 const base::Callback<void(const std::list<content::IndexedDBInfo>&)>& 40 callback); 41 // Requests a single indexed database to be deleted in the IndexedDB thread. 42 virtual void DeleteIndexedDB(const GURL& origin); 43 44 protected: 45 virtual ~BrowsingDataIndexedDBHelper(); 46 47 scoped_refptr<content::IndexedDBContext> indexed_db_context_; 48 49 // Access to |indexed_db_info_| is triggered indirectly via the UI thread and 50 // guarded by |is_fetching_|. This means |indexed_db_info_| is only accessed 51 // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on 52 // the UI thread. 53 // In the context of this class |indexed_db_info_| is only accessed on the 54 // context's IndexedDB thread. 55 std::list<content::IndexedDBInfo> indexed_db_info_; 56 57 // This only mutates on the UI thread. 58 base::Callback<void(const std::list<content::IndexedDBInfo>&)> 59 completion_callback_; 60 61 // Indicates whether or not we're currently fetching information: 62 // it's true when StartFetching() is called in the UI thread, and it's reset 63 // after we notified the callback in the UI thread. 64 // This only mutates on the UI thread. 65 bool is_fetching_; 66 67 private: 68 friend class base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper>; 69 70 // Enumerates all indexed database files in the IndexedDB thread. 71 void FetchIndexedDBInfoInIndexedDBThread(); 72 // Notifies the completion callback in the UI thread. 73 void NotifyInUIThread(); 74 // Delete a single indexed database in the IndexedDB thread. 75 void DeleteIndexedDBInIndexedDBThread(const GURL& origin); 76 77 DISALLOW_COPY_AND_ASSIGN(BrowsingDataIndexedDBHelper); 78 }; 79 80 // This class is an implementation of BrowsingDataIndexedDBHelper that does 81 // not fetch its information from the indexed database tracker, but gets them 82 // passed as a parameter. 83 class CannedBrowsingDataIndexedDBHelper 84 : public BrowsingDataIndexedDBHelper { 85 public: 86 // Contains information about an indexed database. 87 struct PendingIndexedDBInfo { 88 PendingIndexedDBInfo(const GURL& origin, const base::string16& name); 89 ~PendingIndexedDBInfo(); 90 91 bool operator<(const PendingIndexedDBInfo& other) const; 92 93 GURL origin; 94 base::string16 name; 95 }; 96 97 explicit CannedBrowsingDataIndexedDBHelper( 98 content::IndexedDBContext* context); 99 100 // Return a copy of the IndexedDB helper. Only one consumer can use the 101 // StartFetching method at a time, so we need to create a copy of the helper 102 // every time we instantiate a cookies tree model for it. 103 CannedBrowsingDataIndexedDBHelper* Clone(); 104 105 // Add a indexed database to the set of canned indexed databases that is 106 // returned by this helper. 107 void AddIndexedDB(const GURL& origin, 108 const base::string16& name); 109 110 // Clear the list of canned indexed databases. 111 void Reset(); 112 113 // True if no indexed databases are currently stored. 114 bool empty() const; 115 116 // Returns the number of currently stored indexed databases. 117 size_t GetIndexedDBCount() const; 118 119 // Returns the current list of indexed data bases. 120 const std::set<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo>& 121 GetIndexedDBInfo() const; 122 123 // BrowsingDataIndexedDBHelper methods. 124 virtual void StartFetching( 125 const base::Callback<void(const std::list<content::IndexedDBInfo>&)>& 126 callback) OVERRIDE; 127 virtual void DeleteIndexedDB(const GURL& origin) OVERRIDE; 128 129 private: 130 virtual ~CannedBrowsingDataIndexedDBHelper(); 131 132 std::set<PendingIndexedDBInfo> pending_indexed_db_info_; 133 134 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataIndexedDBHelper); 135 }; 136 137 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_HELPER_H_ 138