Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_database_helper.h
blob1b2ac04951a3a5c5bfb9eae31ba2647be7858d3f
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_
8 #include <list>
9 #include <set>
10 #include <string>
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"
17 #include "storage/browser/database/database_tracker.h"
18 #include "storage/common/database/database_identifier.h"
19 #include "url/gurl.h"
21 class Profile;
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> {
30 public:
31 // Contains detailed information about a web database.
32 struct DatabaseInfo {
33 DatabaseInfo(const storage::DatabaseIdentifier& identifier,
34 const std::string& database_name,
35 const std::string& description,
36 int64 size,
37 base::Time last_modified);
38 ~DatabaseInfo();
40 storage::DatabaseIdentifier identifier;
41 std::string database_name;
42 std::string description;
43 int64 size;
44 base::Time last_modified;
47 explicit BrowsingDataDatabaseHelper(Profile* profile);
49 // Starts the fetching process, which will notify its completion via
50 // callback.
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);
60 protected:
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
70 // the UI thread.
71 // In the context of this class |database_info_| is only accessed on the FILE
72 // thread.
73 std::list<DatabaseInfo> database_info_;
75 // This member is only mutated 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 member is only mutated on the UI thread.
82 bool is_fetching_ = false;
84 private:
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<storage::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 {
101 public:
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;
111 GURL origin;
112 std::string name;
113 std::string description;
116 explicit CannedBrowsingDataDatabaseHelper(Profile* profile);
118 // Add a database to the set of canned databases that is returned by this
119 // helper.
120 void AddDatabase(const GURL& origin,
121 const std::string& name,
122 const std::string& description);
124 // Clear the list of canned databases.
125 void Reset();
127 // True if no databases are currently stored.
128 bool empty() const;
130 // Returns the number of currently stored databases.
131 size_t GetDatabaseCount() const;
133 // Returns the current list of web databases.
134 const std::set<PendingDatabaseInfo>& GetPendingDatabaseInfo();
136 // BrowsingDataDatabaseHelper implementation.
137 void StartFetching(const base::Callback<void(const std::list<DatabaseInfo>&)>&
138 callback) override;
139 void DeleteDatabase(const std::string& origin_identifier,
140 const std::string& name) override;
142 private:
143 ~CannedBrowsingDataDatabaseHelper() override;
145 std::set<PendingDatabaseInfo> pending_database_info_;
147 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper);
150 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_