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_INDEXED_DB_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_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 "base/time/time.h"
18 #include "content/public/browser/indexed_db_context.h"
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
28 class BrowsingDataIndexedDBHelper
29 : public base::RefCountedThreadSafe
<BrowsingDataIndexedDBHelper
> {
31 // Create a BrowsingDataIndexedDBHelper instance for the indexed databases
32 // stored in |profile|'s user data directory.
33 explicit BrowsingDataIndexedDBHelper(content::IndexedDBContext
* content
);
35 // Starts the fetching process, which will notify its completion via
37 // This must be called only in the UI thread.
38 virtual void StartFetching(
39 const base::Callback
<void(const std::list
<content::IndexedDBInfo
>&)>&
41 // Requests a single indexed database to be deleted in the IndexedDB thread.
42 virtual void DeleteIndexedDB(const GURL
& origin
);
45 virtual ~BrowsingDataIndexedDBHelper();
47 scoped_refptr
<content::IndexedDBContext
> indexed_db_context_
;
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
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_
;
57 // This only mutates on the UI thread.
58 base::Callback
<void(const std::list
<content::IndexedDBInfo
>&)>
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.
68 friend class base::RefCountedThreadSafe
<BrowsingDataIndexedDBHelper
>;
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
);
77 DISALLOW_COPY_AND_ASSIGN(BrowsingDataIndexedDBHelper
);
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
{
86 // Contains information about an indexed database.
87 struct PendingIndexedDBInfo
{
88 PendingIndexedDBInfo(const GURL
& origin
, const base::string16
& name
);
89 ~PendingIndexedDBInfo();
91 bool operator<(const PendingIndexedDBInfo
& other
) const;
97 explicit CannedBrowsingDataIndexedDBHelper(
98 content::IndexedDBContext
* context
);
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();
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
);
110 // Clear the list of canned indexed databases.
113 // True if no indexed databases are currently stored.
116 // Returns the number of currently stored indexed databases.
117 size_t GetIndexedDBCount() const;
119 // Returns the current list of indexed data bases.
120 const std::set
<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo
>&
121 GetIndexedDBInfo() const;
123 // BrowsingDataIndexedDBHelper methods.
124 virtual void StartFetching(
125 const base::Callback
<void(const std::list
<content::IndexedDBInfo
>&)>&
127 virtual void DeleteIndexedDB(const GURL
& origin
) OVERRIDE
;
130 virtual ~CannedBrowsingDataIndexedDBHelper();
132 std::set
<PendingIndexedDBInfo
> pending_indexed_db_info_
;
134 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataIndexedDBHelper
);
137 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_HELPER_H_