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_DATABASE_HELPER_H_
6#define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_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/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"
20
21class Profile;
22
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.
28class 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();
39
40    storage::DatabaseIdentifier identifier;
41    std::string database_name;
42    std::string description;
43    int64 size;
44    base::Time last_modified;
45  };
46
47  explicit BrowsingDataDatabaseHelper(Profile* profile);
48
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);
54
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);
59
60 protected:
61  friend class base::RefCountedThreadSafe<BrowsingDataDatabaseHelper>;
62  virtual ~BrowsingDataDatabaseHelper();
63
64  // Notifies the completion callback. This must be called in the UI thread.
65  void NotifyInUIThread();
66
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_;
74
75  // This member is only mutated on the UI thread.
76  base::Callback<void(const std::list<DatabaseInfo>&)> completion_callback_;
77
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_;
83
84 private:
85  // Enumerates all databases. This must be called in the FILE thread.
86  void FetchDatabaseInfoOnFileThread();
87
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);
91
92  scoped_refptr<storage::DatabaseTracker> tracker_;
93
94  DISALLOW_COPY_AND_ASSIGN(BrowsingDataDatabaseHelper);
95};
96
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.
100class 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();
107
108    // The operator is needed to store |PendingDatabaseInfo| objects in a set.
109    bool operator<(const PendingDatabaseInfo& other) const;
110
111    GURL origin;
112    std::string name;
113    std::string description;
114  };
115
116  explicit CannedBrowsingDataDatabaseHelper(Profile* profile);
117
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);
123
124  // Clear the list of canned databases.
125  void Reset();
126
127  // True if no databases are currently stored.
128  bool empty() const;
129
130  // Returns the number of currently stored databases.
131  size_t GetDatabaseCount() const;
132
133  // Returns the current list of web databases.
134  const std::set<PendingDatabaseInfo>& GetPendingDatabaseInfo();
135
136  // BrowsingDataDatabaseHelper implementation.
137  virtual void StartFetching(
138      const base::Callback<void(const std::list<DatabaseInfo>&)>& callback)
139          OVERRIDE;
140  virtual void DeleteDatabase(const std::string& origin_identifier,
141                              const std::string& name) OVERRIDE;
142
143 private:
144  virtual ~CannedBrowsingDataDatabaseHelper();
145
146  std::set<PendingDatabaseInfo> pending_database_info_;
147
148  DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper);
149};
150
151#endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_
152