1 // Copyright (c) 2011 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_DATABASE_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_DATABASE_HELPER_H_
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "chrome/common/url_constants.h"
16 #include "googleurl/src/gurl.h"
17 #include "webkit/database/database_tracker.h"
21 // This class fetches database information in the FILE thread, and notifies
22 // the UI thread upon completion.
23 // A client of this class need to call StartFetching from the UI thread to
24 // initiate the flow, and it'll be notified by the callback in its UI
25 // thread at some later point.
26 // The client must call CancelNotification() if it's destroyed before the
27 // callback is notified.
28 class BrowsingDataDatabaseHelper
29 : public base::RefCountedThreadSafe
<BrowsingDataDatabaseHelper
> {
31 // Contains detailed information about a web database.
34 DatabaseInfo(const std::string
& host
,
35 const std::string
& database_name
,
36 const std::string
& origin_identifier
,
37 const std::string
& description
,
38 const std::string
& origin
,
40 base::Time last_modified
);
43 bool IsFileSchemeData();
46 std::string database_name
;
47 std::string origin_identifier
;
48 std::string description
;
51 base::Time last_modified
;
54 explicit BrowsingDataDatabaseHelper(Profile
* profile
);
56 // Starts the fetching process, which will notify its completion via
58 // This must be called only in the UI thread.
59 virtual void StartFetching(
60 Callback1
<const std::vector
<DatabaseInfo
>& >::Type
* callback
);
62 // Cancels the notification callback (i.e., the window that created it no
64 // This must be called only in the UI thread.
65 virtual void CancelNotification();
67 // Requests a single database to be deleted in the FILE thread. This must be
68 // called in the UI thread.
69 virtual void DeleteDatabase(const std::string
& origin
,
70 const std::string
& name
);
73 friend class base::RefCountedThreadSafe
<BrowsingDataDatabaseHelper
>;
74 virtual ~BrowsingDataDatabaseHelper();
76 // Notifies the completion callback. This must be called in the UI thread.
77 void NotifyInUIThread();
79 // This only mutates in the FILE thread.
80 std::vector
<DatabaseInfo
> database_info_
;
82 // This only mutates on the UI thread.
83 scoped_ptr
<Callback1
<const std::vector
<DatabaseInfo
>& >::Type
>
86 // Indicates whether or not we're currently fetching information:
87 // it's true when StartFetching() is called in the UI thread, and it's reset
88 // after we notify the callback in the UI thread.
89 // This only mutates on the UI thread.
93 // Enumerates all databases. This must be called in the FILE thread.
94 void FetchDatabaseInfoOnFileThread();
96 // Delete a single database file. This must be called in the FILE thread.
97 void DeleteDatabaseOnFileThread(const std::string
& origin
,
98 const std::string
& name
);
100 scoped_refptr
<webkit_database::DatabaseTracker
> tracker_
;
102 DISALLOW_COPY_AND_ASSIGN(BrowsingDataDatabaseHelper
);
105 // This class is a thin wrapper around BrowsingDataDatabaseHelper that does not
106 // fetch its information from the database tracker, but gets them passed as
107 // a parameter during construction.
108 class CannedBrowsingDataDatabaseHelper
: public BrowsingDataDatabaseHelper
{
110 explicit CannedBrowsingDataDatabaseHelper(Profile
* profile
);
112 // Return a copy of the database helper. Only one consumer can use the
113 // StartFetching method at a time, so we need to create a copy of the helper
114 // everytime we instantiate a cookies tree model for it.
115 CannedBrowsingDataDatabaseHelper
* Clone();
117 // Add a database to the set of canned databases that is returned by this
119 void AddDatabase(const GURL
& origin
,
120 const std::string
& name
,
121 const std::string
& description
);
123 // Clear the list of canned databases.
126 // True if no databases are currently stored.
129 // BrowsingDataDatabaseHelper methods.
130 virtual void StartFetching(
131 Callback1
<const std::vector
<DatabaseInfo
>& >::Type
* callback
);
132 virtual void CancelNotification() {}
135 struct PendingDatabaseInfo
{
136 PendingDatabaseInfo();
137 PendingDatabaseInfo(const GURL
& origin
,
138 const std::string
& name
,
139 const std::string
& description
);
140 ~PendingDatabaseInfo();
144 std::string description
;
147 virtual ~CannedBrowsingDataDatabaseHelper();
149 // Converts the pending database info structs to database info structs.
150 void ConvertInfoInWebKitThread();
152 // Used to protect access to pending_database_info_.
153 mutable base::Lock lock_
;
155 // This may mutate on WEBKIT and UI threads.
156 std::vector
<PendingDatabaseInfo
> pending_database_info_
;
160 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper
);
163 #endif // CHROME_BROWSER_BROWSING_DATA_DATABASE_HELPER_H_