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_DATABASE_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h"
16 #include "chrome/common/url_constants.h"
18 #include "webkit/browser/database/database_tracker.h"
19 #include "webkit/common/database/database_identifier.h"
23 // This class fetches database information in the FILE thread, and notifies
24 // the UI thread upon completion.
25 // A 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
27 // thread at some later point.
28 class BrowsingDataDatabaseHelper
29 : public base::RefCountedThreadSafe
<BrowsingDataDatabaseHelper
> {
31 // Contains detailed information about a web database.
33 DatabaseInfo(const webkit_database::DatabaseIdentifier
& identifier
,
34 const std::string
& database_name
,
35 const std::string
& description
,
37 base::Time last_modified
);
40 webkit_database::DatabaseIdentifier identifier
;
41 std::string database_name
;
42 std::string description
;
44 base::Time last_modified
;
47 explicit BrowsingDataDatabaseHelper(Profile
* profile
);
49 // Starts the fetching process, which will notify its completion via
51 // This must be called only in the UI thread.
52 virtual void StartFetching(
53 const base::Callback
<void(const std::list
<DatabaseInfo
>&)>& callback
);
55 // Requests a single database to be deleted in the FILE thread. This must be
56 // called in the UI thread.
57 virtual void DeleteDatabase(const std::string
& origin_identifier
,
58 const std::string
& name
);
61 friend class base::RefCountedThreadSafe
<BrowsingDataDatabaseHelper
>;
62 virtual ~BrowsingDataDatabaseHelper();
64 // Notifies the completion callback. This must be called in the UI thread.
65 void NotifyInUIThread();
67 // Access to |database_info_| is triggered indirectly via the UI thread and
68 // guarded by |is_fetching_|. This means |database_info_| is only accessed
69 // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on
71 // In the context of this class |database_info_| is only accessed on the FILE
73 std::list
<DatabaseInfo
> database_info_
;
75 // This only mutates on the UI thread.
76 base::Callback
<void(const std::list
<DatabaseInfo
>&)> completion_callback_
;
78 // Indicates whether or not we're currently fetching information:
79 // it's true when StartFetching() is called in the UI thread, and it's reset
80 // after we notify the callback in the UI thread.
81 // This only mutates on the UI thread.
85 // Enumerates all databases. This must be called in the FILE thread.
86 void FetchDatabaseInfoOnFileThread();
88 // Delete a single database file. This must be called in the FILE thread.
89 void DeleteDatabaseOnFileThread(const std::string
& origin
,
90 const std::string
& name
);
92 scoped_refptr
<webkit_database::DatabaseTracker
> tracker_
;
94 DISALLOW_COPY_AND_ASSIGN(BrowsingDataDatabaseHelper
);
97 // This class is a thin wrapper around BrowsingDataDatabaseHelper that does not
98 // fetch its information from the database tracker, but gets them passed as
99 // a parameter during construction.
100 class CannedBrowsingDataDatabaseHelper
: public BrowsingDataDatabaseHelper
{
102 struct PendingDatabaseInfo
{
103 PendingDatabaseInfo(const GURL
& origin
,
104 const std::string
& name
,
105 const std::string
& description
);
106 ~PendingDatabaseInfo();
108 // The operator is needed to store |PendingDatabaseInfo| objects in a set.
109 bool operator<(const PendingDatabaseInfo
& other
) const;
113 std::string description
;
116 explicit CannedBrowsingDataDatabaseHelper(Profile
* profile
);
118 // Return a copy of the database helper. Only one consumer can use the
119 // StartFetching method at a time, so we need to create a copy of the helper
120 // everytime we instantiate a cookies tree model for it.
121 CannedBrowsingDataDatabaseHelper
* Clone();
123 // Add a database to the set of canned databases that is returned by this
125 void AddDatabase(const GURL
& origin
,
126 const std::string
& name
,
127 const std::string
& description
);
129 // Clear the list of canned databases.
132 // True if no databases are currently stored.
135 // Returns the number of currently stored databases.
136 size_t GetDatabaseCount() const;
138 // Returns the current list of web databases.
139 const std::set
<PendingDatabaseInfo
>& GetPendingDatabaseInfo();
141 // BrowsingDataDatabaseHelper implementation.
142 virtual void StartFetching(
143 const base::Callback
<void(const std::list
<DatabaseInfo
>&)>& callback
)
145 virtual void DeleteDatabase(const std::string
& origin_identifier
,
146 const std::string
& name
) OVERRIDE
;
149 virtual ~CannedBrowsingDataDatabaseHelper();
151 std::set
<PendingDatabaseInfo
> pending_database_info_
;
155 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper
);
158 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_