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 static BrowsingDataIndexedDBHelper* Create( 34 content::IndexedDBContext* context); 35 36 // Starts the fetching process, which will notify its completion via 37 // callback. 38 // This must be called only in the UI thread. 39 virtual void StartFetching( 40 const base::Callback<void(const std::list<content::IndexedDBInfo>&)>& 41 callback) = 0; 42 // Requests a single indexed database to be deleted in the IndexedDB thread. 43 virtual void DeleteIndexedDB(const GURL& origin) = 0; 44 45 protected: 46 friend class base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper>; 47 virtual ~BrowsingDataIndexedDBHelper() {} 48 }; 49 50 // This class is an implementation of BrowsingDataIndexedDBHelper that does 51 // not fetch its information from the indexed database tracker, but gets them 52 // passed as a parameter. 53 class CannedBrowsingDataIndexedDBHelper 54 : public BrowsingDataIndexedDBHelper { 55 public: 56 // Contains information about an indexed database. 57 struct PendingIndexedDBInfo { 58 PendingIndexedDBInfo(const GURL& origin, const string16& name); 59 ~PendingIndexedDBInfo(); 60 61 bool operator<(const PendingIndexedDBInfo& other) const; 62 63 GURL origin; 64 string16 name; 65 }; 66 67 CannedBrowsingDataIndexedDBHelper(); 68 69 // Return a copy of the IndexedDB helper. Only one consumer can use the 70 // StartFetching method at a time, so we need to create a copy of the helper 71 // every time we instantiate a cookies tree model for it. 72 CannedBrowsingDataIndexedDBHelper* Clone(); 73 74 // Add a indexed database to the set of canned indexed databases that is 75 // returned by this helper. 76 void AddIndexedDB(const GURL& origin, 77 const string16& name); 78 79 // Clear the list of canned indexed databases. 80 void Reset(); 81 82 // True if no indexed databases are currently stored. 83 bool empty() const; 84 85 // Returns the number of currently stored indexed databases. 86 size_t GetIndexedDBCount() const; 87 88 // Returns the current list of indexed data bases. 89 const std::set<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo>& 90 GetIndexedDBInfo() const; 91 92 // BrowsingDataIndexedDBHelper methods. 93 virtual void StartFetching( 94 const base::Callback<void(const std::list<content::IndexedDBInfo>&)>& 95 callback) OVERRIDE; 96 97 virtual void DeleteIndexedDB(const GURL& origin) OVERRIDE {} 98 99 private: 100 virtual ~CannedBrowsingDataIndexedDBHelper(); 101 102 // Convert the pending indexed db info to indexed db info objects. 103 void ConvertPendingInfo(); 104 105 std::set<PendingIndexedDBInfo> pending_indexed_db_info_; 106 107 // Access to |indexed_db_info_| is triggered indirectly via the UI thread and 108 // guarded by |is_fetching_|. This means |indexed_db_info_| is only accessed 109 // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on 110 // the UI thread. 111 // In the context of this class |indexed_db_info_| is only accessed on the UI 112 // thread. 113 std::list<content::IndexedDBInfo> indexed_db_info_; 114 115 // This only mutates on the UI thread. 116 base::Callback<void(const std::list<content::IndexedDBInfo>&)> 117 completion_callback_; 118 119 // Indicates whether or not we're currently fetching information: 120 // it's true when StartFetching() is called in the UI thread, and it's reset 121 // after we notified the callback in the UI thread. 122 // This only mutates on the UI thread. 123 bool is_fetching_; 124 125 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataIndexedDBHelper); 126 }; 127 128 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_HELPER_H_ 129