browsing_data_indexed_db_helper.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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.
4
5#ifndef CHROME_BROWSER_BROWSING_DATA_INDEXED_DB_HELPER_H_
6#define CHROME_BROWSER_BROWSING_DATA_INDEXED_DB_HELPER_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/callback.h"
13#include "base/file_path.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/synchronization/lock.h"
17#include "base/time.h"
18#include "chrome/common/url_constants.h"
19#include "googleurl/src/gurl.h"
20
21class 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.  The client must call CancelNotification() if it's
28// destroyed before the callback is notified.
29class BrowsingDataIndexedDBHelper
30    : public base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper> {
31 public:
32  // Contains detailed information about an indexed database.
33  struct IndexedDBInfo {
34    IndexedDBInfo(
35        const std::string& protocol,
36        const std::string& host,
37        unsigned short port,
38        const std::string& database_identifier,
39        const std::string& origin,
40        const FilePath& file_path,
41        int64 size,
42        base::Time last_modified);
43    ~IndexedDBInfo();
44
45    bool IsFileSchemeData() {
46      return protocol == chrome::kFileScheme;
47    }
48
49    std::string protocol;
50    std::string host;
51    unsigned short port;
52    std::string database_identifier;
53    std::string origin;
54    FilePath file_path;
55    int64 size;
56    base::Time last_modified;
57  };
58
59  // Create a BrowsingDataIndexedDBHelper instance for the indexed databases
60  // stored in |profile|'s user data directory.
61  static BrowsingDataIndexedDBHelper* Create(Profile* profile);
62
63  // Starts the fetching process, which will notify its completion via
64  // callback.
65  // This must be called only in the UI thread.
66  virtual void StartFetching(
67      Callback1<const std::vector<IndexedDBInfo>& >::Type* callback) = 0;
68  // Cancels the notification callback (i.e., the window that created it no
69  // longer exists).
70  // This must be called only in the UI thread.
71  virtual void CancelNotification() = 0;
72  // Requests a single indexed database file to be deleted in the WEBKIT thread.
73  virtual void DeleteIndexedDBFile(const FilePath& file_path) = 0;
74
75 protected:
76  friend class base::RefCountedThreadSafe<BrowsingDataIndexedDBHelper>;
77  virtual ~BrowsingDataIndexedDBHelper() {}
78};
79
80// This class is an implementation of BrowsingDataIndexedDBHelper that does
81// not fetch its information from the indexed database tracker, but gets them
82// passed as a parameter.
83class CannedBrowsingDataIndexedDBHelper
84    : public BrowsingDataIndexedDBHelper {
85 public:
86  explicit CannedBrowsingDataIndexedDBHelper(Profile* profile);
87
88  // Return a copy of the IndexedDB helper. Only one consumer can use the
89  // StartFetching method at a time, so we need to create a copy of the helper
90  // everytime we instantiate a cookies tree model for it.
91  CannedBrowsingDataIndexedDBHelper* Clone();
92
93  // Add a indexed database to the set of canned indexed databases that is
94  // returned by this helper.
95  void AddIndexedDB(const GURL& origin,
96                    const string16& description);
97
98  // Clear the list of canned indexed databases.
99  void Reset();
100
101  // True if no indexed databases are currently stored.
102  bool empty() const;
103
104  // BrowsingDataIndexedDBHelper methods.
105  virtual void StartFetching(
106      Callback1<const std::vector<IndexedDBInfo>& >::Type* callback);
107  virtual void CancelNotification() {}
108  virtual void DeleteIndexedDBFile(const FilePath& file_path) {}
109
110 private:
111  struct PendingIndexedDBInfo {
112    PendingIndexedDBInfo();
113    PendingIndexedDBInfo(const GURL& origin, const string16& description);
114    ~PendingIndexedDBInfo();
115
116    GURL origin;
117    string16 description;
118  };
119
120  virtual ~CannedBrowsingDataIndexedDBHelper();
121
122  // Convert the pending indexed db info to indexed db info objects.
123  void ConvertPendingInfoInWebKitThread();
124
125  void NotifyInUIThread();
126
127  Profile* profile_;
128
129  // Lock to protect access to pending_indexed_db_info_;
130  mutable base::Lock lock_;
131
132  // This may mutate on WEBKIT and UI threads.
133  std::vector<PendingIndexedDBInfo> pending_indexed_db_info_;
134
135  // This only mutates on the WEBKIT thread.
136  std::vector<IndexedDBInfo> indexed_db_info_;
137
138  // This only mutates on the UI thread.
139  scoped_ptr<Callback1<const std::vector<IndexedDBInfo>& >::Type >
140      completion_callback_;
141
142  // Indicates whether or not we're currently fetching information:
143  // it's true when StartFetching() is called in the UI thread, and it's reset
144  // after we notified the callback in the UI thread.
145  // This only mutates on the UI thread.
146  bool is_fetching_;
147
148  DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataIndexedDBHelper);
149};
150
151#endif  // CHROME_BROWSER_BROWSING_DATA_INDEXED_DB_HELPER_H_
152