Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / browsing_data_indexed_db_helper.h
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_INDEXED_DB_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_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/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"
19 #include "url/gurl.h"
20
21 class Profile;
22
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
27 // some later point.
28 class BrowsingDataIndexedDBHelper
29     : public base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper> {
30  public:
31   // Create a BrowsingDataIndexedDBHelper instance for the indexed databases
32   // stored in |context|'s associated profile's user data directory.
33   explicit BrowsingDataIndexedDBHelper(content::IndexedDBContext* context);
34
35   // Starts the fetching process, which will notify its completion via
36   // |callback|. This must be called only on the UI thread.
37   virtual void StartFetching(
38       const base::Callback<void(const std::list<content::IndexedDBInfo>&)>&
39           callback);
40   // Requests a single indexed database to be deleted in the IndexedDB thread.
41   virtual void DeleteIndexedDB(const GURL& origin);
42
43  protected:
44   virtual ~BrowsingDataIndexedDBHelper();
45
46   scoped_refptr<content::IndexedDBContext> indexed_db_context_;
47
48   // Access to |indexed_db_info_| is triggered indirectly via the UI thread and
49   // guarded by |is_fetching_|. This means |indexed_db_info_| is only accessed
50   // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on
51   // the UI thread.
52   // In the context of this class |indexed_db_info_| is only accessed on the
53   // context's IndexedDB thread.
54   std::list<content::IndexedDBInfo> indexed_db_info_;
55
56   // This member is only mutated on the UI thread.
57   base::Callback<void(const std::list<content::IndexedDBInfo>&)>
58       completion_callback_;
59
60   // Indicates whether or not we're currently fetching information:
61   // it's true when StartFetching() is called in the UI thread, and it's reset
62   // after we notified the callback in the UI thread.
63   // This member is only mutated on the UI thread.
64   bool is_fetching_;
65
66  private:
67   friend class base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper>;
68
69   // Enumerates all indexed database files in the IndexedDB thread.
70   void FetchIndexedDBInfoInIndexedDBThread();
71   // Notifies the completion callback in the UI thread.
72   void NotifyInUIThread();
73   // Delete a single indexed database in the IndexedDB thread.
74   void DeleteIndexedDBInIndexedDBThread(const GURL& origin);
75
76   DISALLOW_COPY_AND_ASSIGN(BrowsingDataIndexedDBHelper);
77 };
78
79 // This class is an implementation of BrowsingDataIndexedDBHelper that does
80 // not fetch its information from the indexed database tracker, but gets them
81 // passed as a parameter.
82 class CannedBrowsingDataIndexedDBHelper
83     : public BrowsingDataIndexedDBHelper {
84  public:
85   // Contains information about an indexed database.
86   struct PendingIndexedDBInfo {
87     PendingIndexedDBInfo(const GURL& origin, const base::string16& name);
88     ~PendingIndexedDBInfo();
89
90     bool operator<(const PendingIndexedDBInfo& other) const;
91
92     GURL origin;
93     base::string16 name;
94   };
95
96   explicit CannedBrowsingDataIndexedDBHelper(
97       content::IndexedDBContext* context);
98
99   // Add a indexed database to the set of canned indexed databases that is
100   // returned by this helper.
101   void AddIndexedDB(const GURL& origin,
102                     const base::string16& name);
103
104   // Clear the list of canned indexed databases.
105   void Reset();
106
107   // True if no indexed databases are currently stored.
108   bool empty() const;
109
110   // Returns the number of currently stored indexed databases.
111   size_t GetIndexedDBCount() const;
112
113   // Returns the current list of indexed data bases.
114   const std::set<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo>&
115       GetIndexedDBInfo() const;
116
117   // BrowsingDataIndexedDBHelper methods.
118   virtual void StartFetching(
119       const base::Callback<void(const std::list<content::IndexedDBInfo>&)>&
120           callback) OVERRIDE;
121   virtual void DeleteIndexedDB(const GURL& origin) OVERRIDE;
122
123  private:
124   virtual ~CannedBrowsingDataIndexedDBHelper();
125
126   std::set<PendingIndexedDBInfo> pending_indexed_db_info_;
127
128   DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataIndexedDBHelper);
129 };
130
131 #endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_INDEXED_DB_HELPER_H_