Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / browser / indexed_db / indexed_db_context_impl.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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
22
23 class GURL;
24
25 namespace base {
26 class ListValue;
27 class FilePath;
28 class SequencedTaskRunner;
29 }
30
31 namespace storage {
32 class QuotaManagerProxy;
33 class SpecialStoragePolicy;
34 }
35
36 namespace content {
37
38 class IndexedDBConnection;
39
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_REASON_MAX
49   };
50
51   // The indexed db directory.
52   static const base::FilePath::CharType kIndexedDBDirectory[];
53
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);
59
60   IndexedDBFactory* GetIDBFactory();
61
62   // Disables the exit-time deletion of session-only data.
63   void SetForceKeepSessionState() { force_keep_session_state_ = true; }
64
65   // IndexedDBContext implementation:
66   base::SequencedTaskRunner* TaskRunner() const override;
67   std::vector<IndexedDBInfo> GetAllOriginsInfo() override;
68   int64 GetOriginDiskUsage(const GURL& origin_url) override;
69   void DeleteForOrigin(const GURL& origin_url) override;
70   base::FilePath GetFilePathForTesting(
71       const std::string& origin_id) const override;
72   void SetTaskRunnerForTesting(base::SequencedTaskRunner* task_runner) override;
73
74   // Methods called by IndexedDBDispatcherHost for quota support.
75   void ConnectionOpened(const GURL& origin_url, IndexedDBConnection* db);
76   void ConnectionClosed(const GURL& origin_url, IndexedDBConnection* db);
77   void TransactionComplete(const GURL& origin_url);
78   void DatabaseDeleted(const GURL& origin_url);
79   bool WouldBeOverQuota(const GURL& origin_url, int64 additional_bytes);
80   bool IsOverQuota(const GURL& origin_url);
81
82   storage::QuotaManagerProxy* quota_manager_proxy();
83
84   std::vector<GURL> GetAllOrigins();
85   base::Time GetOriginLastModified(const GURL& origin_url);
86   base::ListValue* GetAllOriginsDetails();
87
88   // ForceClose takes a value rather than a reference since it may release the
89   // owning object.
90   void ForceClose(const GURL origin_url, ForceCloseReason reason);
91
92   base::FilePath GetFilePath(const GURL& origin_url) const;
93   base::FilePath data_path() const { return data_path_; }
94   bool IsInOriginSet(const GURL& origin_url) {
95     std::set<GURL>* set = GetOriginSet();
96     return set->find(origin_url) != set->end();
97   }
98   size_t GetConnectionCount(const GURL& origin_url);
99
100   // For unit tests allow to override the |data_path_|.
101   void set_data_path_for_testing(const base::FilePath& data_path) {
102     data_path_ = data_path;
103   }
104
105   bool is_incognito() const { return data_path_.empty(); }
106
107  protected:
108   ~IndexedDBContextImpl() override;
109
110  private:
111   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearLocalState);
112   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearSessionOnlyDatabases);
113   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, SetForceKeepSessionState);
114   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ForceCloseOpenDatabasesOnDelete);
115   friend class IndexedDBQuotaClientTest;
116
117   typedef std::map<GURL, int64> OriginToSizeMap;
118   class IndexedDBGetUsageAndQuotaCallback;
119
120   base::FilePath GetIndexedDBFilePath(const std::string& origin_id) const;
121   int64 ReadUsageFromDisk(const GURL& origin_url) const;
122   void EnsureDiskUsageCacheInitialized(const GURL& origin_url);
123   void QueryDiskAndUpdateQuotaUsage(const GURL& origin_url);
124   void GotUsageAndQuota(const GURL& origin_url,
125                         storage::QuotaStatusCode,
126                         int64 usage,
127                         int64 quota);
128   void GotUpdatedQuota(const GURL& origin_url, int64 usage, int64 quota);
129   void QueryAvailableQuota(const GURL& origin_url);
130
131   std::set<GURL>* GetOriginSet();
132   bool AddToOriginSet(const GURL& origin_url) {
133     return GetOriginSet()->insert(origin_url).second;
134   }
135   void RemoveFromOriginSet(const GURL& origin_url) {
136     GetOriginSet()->erase(origin_url);
137   }
138
139   // Only for testing.
140   void ResetCaches();
141
142   scoped_refptr<IndexedDBFactory> factory_;
143   base::FilePath data_path_;
144   // If true, nothing (not even session-only data) should be deleted on exit.
145   bool force_keep_session_state_;
146   scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
147   scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
148   scoped_refptr<base::SequencedTaskRunner> task_runner_;
149   scoped_ptr<std::set<GURL> > origin_set_;
150   OriginToSizeMap origin_size_map_;
151   OriginToSizeMap space_available_map_;
152
153   DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl);
154 };
155
156 }  // namespace content
157
158 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_