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_
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"
21 #include "webkit/common/quota/quota_types.h"
28 class SequencedTaskRunner
;
32 class QuotaManagerProxy
;
33 class SpecialStoragePolicy
;
38 class IndexedDBConnection
;
40 class CONTENT_EXPORT IndexedDBContextImpl
41 : NON_EXPORTED_BASE(public IndexedDBContext
) {
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_REASON_MAX
51 // The indexed db directory.
52 static const base::FilePath::CharType kIndexedDBDirectory
[];
54 // If |data_path| is empty, nothing will be saved to disk.
55 IndexedDBContextImpl(const base::FilePath
& data_path
,
56 storage::SpecialStoragePolicy
* special_storage_policy
,
57 storage::QuotaManagerProxy
* quota_manager_proxy
,
58 base::SequencedTaskRunner
* task_runner
);
60 IndexedDBFactory
* GetIDBFactory();
62 // Disables the exit-time deletion of session-only data.
63 void SetForceKeepSessionState() { force_keep_session_state_
= true; }
65 // IndexedDBContext implementation:
66 virtual base::SequencedTaskRunner
* TaskRunner() const OVERRIDE
;
67 virtual std::vector
<IndexedDBInfo
> GetAllOriginsInfo() OVERRIDE
;
68 virtual int64
GetOriginDiskUsage(const GURL
& origin_url
) OVERRIDE
;
69 virtual void DeleteForOrigin(const GURL
& origin_url
) OVERRIDE
;
70 virtual base::FilePath
GetFilePathForTesting(
71 const std::string
& origin_id
) const OVERRIDE
;
72 virtual void SetTaskRunnerForTesting(base::SequencedTaskRunner
* task_runner
)
75 // Methods called by IndexedDBDispatcherHost for quota support.
76 void ConnectionOpened(const GURL
& origin_url
, IndexedDBConnection
* db
);
77 void ConnectionClosed(const GURL
& origin_url
, IndexedDBConnection
* db
);
78 void TransactionComplete(const GURL
& origin_url
);
79 void DatabaseDeleted(const GURL
& origin_url
);
80 bool WouldBeOverQuota(const GURL
& origin_url
, int64 additional_bytes
);
81 bool IsOverQuota(const GURL
& origin_url
);
83 storage::QuotaManagerProxy
* quota_manager_proxy();
85 std::vector
<GURL
> GetAllOrigins();
86 base::Time
GetOriginLastModified(const GURL
& origin_url
);
87 base::ListValue
* GetAllOriginsDetails();
89 // ForceClose takes a value rather than a reference since it may release the
91 void ForceClose(const GURL origin_url
, ForceCloseReason reason
);
93 base::FilePath
GetFilePath(const GURL
& origin_url
) const;
94 base::FilePath
data_path() const { return data_path_
; }
95 bool IsInOriginSet(const GURL
& origin_url
) {
96 std::set
<GURL
>* set
= GetOriginSet();
97 return set
->find(origin_url
) != set
->end();
99 size_t GetConnectionCount(const GURL
& origin_url
);
101 // For unit tests allow to override the |data_path_|.
102 void set_data_path_for_testing(const base::FilePath
& data_path
) {
103 data_path_
= data_path
;
106 bool is_incognito() const { return data_path_
.empty(); }
109 virtual ~IndexedDBContextImpl();
112 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ClearLocalState
);
113 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ClearSessionOnlyDatabases
);
114 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, SetForceKeepSessionState
);
115 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ForceCloseOpenDatabasesOnDelete
);
116 friend class IndexedDBQuotaClientTest
;
118 typedef std::map
<GURL
, int64
> OriginToSizeMap
;
119 class IndexedDBGetUsageAndQuotaCallback
;
121 base::FilePath
GetIndexedDBFilePath(const std::string
& origin_id
) const;
122 int64
ReadUsageFromDisk(const GURL
& origin_url
) const;
123 void EnsureDiskUsageCacheInitialized(const GURL
& origin_url
);
124 void QueryDiskAndUpdateQuotaUsage(const GURL
& origin_url
);
125 void GotUsageAndQuota(const GURL
& origin_url
,
126 storage::QuotaStatusCode
,
129 void GotUpdatedQuota(const GURL
& origin_url
, int64 usage
, int64 quota
);
130 void QueryAvailableQuota(const GURL
& origin_url
);
132 std::set
<GURL
>* GetOriginSet();
133 bool AddToOriginSet(const GURL
& origin_url
) {
134 return GetOriginSet()->insert(origin_url
).second
;
136 void RemoveFromOriginSet(const GURL
& origin_url
) {
137 GetOriginSet()->erase(origin_url
);
143 scoped_refptr
<IndexedDBFactory
> factory_
;
144 base::FilePath data_path_
;
145 // If true, nothing (not even session-only data) should be deleted on exit.
146 bool force_keep_session_state_
;
147 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
148 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
149 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
150 scoped_ptr
<std::set
<GURL
> > origin_set_
;
151 OriginToSizeMap origin_size_map_
;
152 OriginToSizeMap space_available_map_
;
154 DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl
);
157 } // namespace content
159 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_