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_
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/browser_main_loop.h"
17 #include "content/browser/indexed_db/indexed_db_factory.h"
18 #include "content/public/browser/indexed_db_context.h"
20 #include "webkit/common/quota/quota_types.h"
27 class SequencedTaskRunner
;
31 class QuotaManagerProxy
;
32 class SpecialStoragePolicy
;
37 class IndexedDBConnection
;
39 class CONTENT_EXPORT IndexedDBContextImpl
40 : NON_EXPORTED_BASE(public IndexedDBContext
) {
42 // If |data_path| is empty, nothing will be saved to disk.
43 IndexedDBContextImpl(const base::FilePath
& data_path
,
44 quota::SpecialStoragePolicy
* special_storage_policy
,
45 quota::QuotaManagerProxy
* quota_manager_proxy
,
46 base::SequencedTaskRunner
* task_runner
);
48 IndexedDBFactory
* GetIDBFactory();
50 // The indexed db directory.
51 static const base::FilePath::CharType kIndexedDBDirectory
[];
53 // Disables the exit-time deletion of session-only data.
54 void SetForceKeepSessionState() { force_keep_session_state_
= true; }
56 // IndexedDBContext implementation:
57 virtual base::TaskRunner
* TaskRunner() const OVERRIDE
;
58 virtual std::vector
<IndexedDBInfo
> GetAllOriginsInfo() OVERRIDE
;
59 virtual int64
GetOriginDiskUsage(const GURL
& origin_url
) OVERRIDE
;
60 virtual void DeleteForOrigin(const GURL
& origin_url
) OVERRIDE
;
61 virtual base::FilePath
GetFilePathForTesting(
62 const std::string
& origin_id
) const OVERRIDE
;
63 virtual void SetTaskRunnerForTesting(base::SequencedTaskRunner
* task_runner
)
66 // Methods called by IndexedDBDispatcherHost for quota support.
67 void ConnectionOpened(const GURL
& origin_url
, IndexedDBConnection
* db
);
68 void ConnectionClosed(const GURL
& origin_url
, IndexedDBConnection
* db
);
69 void TransactionComplete(const GURL
& origin_url
);
70 void DatabaseDeleted(const GURL
& origin_url
);
71 bool WouldBeOverQuota(const GURL
& origin_url
, int64 additional_bytes
);
72 bool IsOverQuota(const GURL
& origin_url
);
74 quota::QuotaManagerProxy
* quota_manager_proxy();
76 std::vector
<GURL
> GetAllOrigins();
77 base::Time
GetOriginLastModified(const GURL
& origin_url
);
78 base::ListValue
* GetAllOriginsDetails();
79 // ForceClose takes a value rather than a reference since it may release the
81 void ForceClose(const GURL origin_url
);
82 base::FilePath
GetFilePath(const GURL
& origin_url
) const;
83 base::FilePath
data_path() const { return data_path_
; }
84 bool IsInOriginSet(const GURL
& origin_url
) {
85 std::set
<GURL
>* set
= GetOriginSet();
86 return set
->find(origin_url
) != set
->end();
88 size_t GetConnectionCount(const GURL
& origin_url
);
90 // For unit tests allow to override the |data_path_|.
91 void set_data_path_for_testing(const base::FilePath
& data_path
) {
92 data_path_
= data_path
;
96 virtual ~IndexedDBContextImpl();
99 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ClearLocalState
);
100 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ClearSessionOnlyDatabases
);
101 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, SetForceKeepSessionState
);
102 FRIEND_TEST_ALL_PREFIXES(IndexedDBTest
, ForceCloseOpenDatabasesOnDelete
);
103 friend class IndexedDBQuotaClientTest
;
105 typedef std::map
<GURL
, int64
> OriginToSizeMap
;
106 class IndexedDBGetUsageAndQuotaCallback
;
108 base::FilePath
GetIndexedDBFilePath(const std::string
& origin_id
) const;
109 int64
ReadUsageFromDisk(const GURL
& origin_url
) const;
110 void EnsureDiskUsageCacheInitialized(const GURL
& origin_url
);
111 void QueryDiskAndUpdateQuotaUsage(const GURL
& origin_url
);
112 void GotUsageAndQuota(const GURL
& origin_url
,
113 quota::QuotaStatusCode
,
116 void GotUpdatedQuota(const GURL
& origin_url
, int64 usage
, int64 quota
);
117 void QueryAvailableQuota(const GURL
& origin_url
);
119 std::set
<GURL
>* GetOriginSet();
120 bool AddToOriginSet(const GURL
& origin_url
) {
121 return GetOriginSet()->insert(origin_url
).second
;
123 void RemoveFromOriginSet(const GURL
& origin_url
) {
124 GetOriginSet()->erase(origin_url
);
130 scoped_refptr
<IndexedDBFactory
> factory_
;
131 base::FilePath data_path_
;
132 // If true, nothing (not even session-only data) should be deleted on exit.
133 bool force_keep_session_state_
;
134 scoped_refptr
<quota::SpecialStoragePolicy
> special_storage_policy_
;
135 scoped_refptr
<quota::QuotaManagerProxy
> quota_manager_proxy_
;
136 base::SequencedTaskRunner
* task_runner_
;
137 scoped_ptr
<std::set
<GURL
> > origin_set_
;
138 OriginToSizeMap origin_size_map_
;
139 OriginToSizeMap space_available_map_
;
141 DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl
);
144 } // namespace content
146 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_