Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_context_impl.h
blob2955a3a2cf1c64519a37aea602e94e63f06dbcc2
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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "content/browser/browser_main_loop.h"
18 #include "content/browser/indexed_db/indexed_db_factory.h"
19 #include "content/public/browser/indexed_db_context.h"
20 #include "storage/common/quota/quota_types.h"
21 #include "url/gurl.h"
23 class GURL;
25 namespace base {
26 class ListValue;
27 class FilePath;
28 class SequencedTaskRunner;
31 namespace storage {
32 class QuotaManagerProxy;
33 class SpecialStoragePolicy;
36 namespace content {
38 class IndexedDBConnection;
40 class CONTENT_EXPORT IndexedDBContextImpl
41 : NON_EXPORTED_BASE(public IndexedDBContext) {
42 public:
43 // Recorded in histograms, so append only.
44 enum ForceCloseReason {
45 FORCE_CLOSE_DELETE_ORIGIN = 0,
46 FORCE_CLOSE_BACKING_STORE_FAILURE,
47 FORCE_CLOSE_INTERNALS_PAGE,
48 FORCE_CLOSE_COPY_ORIGIN,
49 FORCE_CLOSE_REASON_MAX
52 // The indexed db directory.
53 static const base::FilePath::CharType kIndexedDBDirectory[];
55 // If |data_path| is empty, nothing will be saved to disk.
56 IndexedDBContextImpl(const base::FilePath& data_path,
57 storage::SpecialStoragePolicy* special_storage_policy,
58 storage::QuotaManagerProxy* quota_manager_proxy,
59 base::SequencedTaskRunner* task_runner);
61 IndexedDBFactory* GetIDBFactory();
63 // Disables the exit-time deletion of session-only data.
64 void SetForceKeepSessionState() { force_keep_session_state_ = true; }
66 // IndexedDBContext implementation:
67 base::SequencedTaskRunner* TaskRunner() const override;
68 std::vector<IndexedDBInfo> GetAllOriginsInfo() override;
69 int64 GetOriginDiskUsage(const GURL& origin_url) override;
70 void DeleteForOrigin(const GURL& origin_url) override;
71 void CopyOriginData(const GURL& origin_url,
72 IndexedDBContext* dest_context) override;
73 base::FilePath GetFilePathForTesting(
74 const std::string& origin_id) const override;
75 void SetTaskRunnerForTesting(base::SequencedTaskRunner* task_runner) override;
77 // Methods called by IndexedDBDispatcherHost for quota support.
78 void ConnectionOpened(const GURL& origin_url, IndexedDBConnection* db);
79 void ConnectionClosed(const GURL& origin_url, IndexedDBConnection* db);
80 void TransactionComplete(const GURL& origin_url);
81 void DatabaseDeleted(const GURL& origin_url);
82 bool WouldBeOverQuota(const GURL& origin_url, int64 additional_bytes);
83 bool IsOverQuota(const GURL& origin_url);
85 storage::QuotaManagerProxy* quota_manager_proxy();
87 std::vector<GURL> GetAllOrigins();
88 base::Time GetOriginLastModified(const GURL& origin_url);
89 base::ListValue* GetAllOriginsDetails();
91 // ForceClose takes a value rather than a reference since it may release the
92 // owning object.
93 void ForceClose(const GURL origin_url, ForceCloseReason reason);
94 // GetStoragePaths returns all paths owned by this database, in arbitrary
95 // order.
96 std::vector<base::FilePath> GetStoragePaths(const GURL& origin_url) const;
98 base::FilePath data_path() const { return data_path_; }
99 bool IsInOriginSet(const GURL& origin_url) {
100 std::set<GURL>* set = GetOriginSet();
101 return set->find(origin_url) != set->end();
103 size_t GetConnectionCount(const GURL& origin_url);
104 int GetOriginBlobFileCount(const GURL& origin_url);
106 // For unit tests allow to override the |data_path_|.
107 void set_data_path_for_testing(const base::FilePath& data_path) {
108 data_path_ = data_path;
111 bool is_incognito() const { return data_path_.empty(); }
113 protected:
114 ~IndexedDBContextImpl() override;
116 private:
117 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearLocalState);
118 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearSessionOnlyDatabases);
119 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, SetForceKeepSessionState);
120 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ForceCloseOpenDatabasesOnDelete);
121 friend class IndexedDBQuotaClientTest;
123 typedef std::map<GURL, int64> OriginToSizeMap;
124 class IndexedDBGetUsageAndQuotaCallback;
126 base::FilePath GetBlobPath(const std::string& origin_id) const;
127 base::FilePath GetLevelDBPath(const GURL& origin_url) const;
128 base::FilePath GetLevelDBPath(const std::string& origin_id) const;
129 int64 ReadUsageFromDisk(const GURL& origin_url) const;
130 void EnsureDiskUsageCacheInitialized(const GURL& origin_url);
131 void QueryDiskAndUpdateQuotaUsage(const GURL& origin_url);
132 void GotUsageAndQuota(const GURL& origin_url,
133 storage::QuotaStatusCode,
134 int64 usage,
135 int64 quota);
136 void GotUpdatedQuota(const GURL& origin_url, int64 usage, int64 quota);
137 void QueryAvailableQuota(const GURL& origin_url);
139 std::set<GURL>* GetOriginSet();
140 bool AddToOriginSet(const GURL& origin_url) {
141 return GetOriginSet()->insert(origin_url).second;
143 void RemoveFromOriginSet(const GURL& origin_url) {
144 GetOriginSet()->erase(origin_url);
147 // Only for testing.
148 void ResetCaches();
150 scoped_refptr<IndexedDBFactory> factory_;
151 base::FilePath data_path_;
152 // If true, nothing (not even session-only data) should be deleted on exit.
153 bool force_keep_session_state_;
154 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
155 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
156 scoped_refptr<base::SequencedTaskRunner> task_runner_;
157 scoped_ptr<std::set<GURL> > origin_set_;
158 OriginToSizeMap origin_size_map_;
159 OriginToSizeMap space_available_map_;
161 DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl);
164 } // namespace content
166 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_