• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "url/gurl.h"
21 #include "webkit/common/quota/quota_types.h"
22 
23 class GURL;
24 
25 namespace base {
26 class ListValue;
27 class FilePath;
28 class SequencedTaskRunner;
29 }
30 
31 namespace quota {
32 class QuotaManagerProxy;
33 class SpecialStoragePolicy;
34 }
35 
36 namespace content {
37 
38 class IndexedDBConnection;
39 
40 class CONTENT_EXPORT IndexedDBContextImpl
NON_EXPORTED_BASE(public IndexedDBContext)41     : NON_EXPORTED_BASE(public IndexedDBContext) {
42  public:
43   // If |data_path| is empty, nothing will be saved to disk.
44   IndexedDBContextImpl(const base::FilePath& data_path,
45                        quota::SpecialStoragePolicy* special_storage_policy,
46                        quota::QuotaManagerProxy* quota_manager_proxy,
47                        base::SequencedTaskRunner* task_runner);
48 
49   IndexedDBFactory* GetIDBFactory();
50 
51   // The indexed db directory.
52   static const base::FilePath::CharType kIndexedDBDirectory[];
53 
54   // Disables the exit-time deletion of session-only data.
55   void SetForceKeepSessionState() { force_keep_session_state_ = true; }
56 
57   // IndexedDBContext implementation:
58   virtual base::TaskRunner* TaskRunner() const OVERRIDE;
59   virtual std::vector<IndexedDBInfo> GetAllOriginsInfo() OVERRIDE;
60   virtual int64 GetOriginDiskUsage(const GURL& origin_url) OVERRIDE;
61   virtual void DeleteForOrigin(const GURL& origin_url) OVERRIDE;
62   virtual base::FilePath GetFilePathForTesting(
63       const std::string& origin_id) const OVERRIDE;
64   virtual void SetTaskRunnerForTesting(base::SequencedTaskRunner* task_runner)
65       OVERRIDE;
66 
67   // Methods called by IndexedDBDispatcherHost for quota support.
68   void ConnectionOpened(const GURL& origin_url, IndexedDBConnection* db);
69   void ConnectionClosed(const GURL& origin_url, IndexedDBConnection* db);
70   void TransactionComplete(const GURL& origin_url);
71   void DatabaseDeleted(const GURL& origin_url);
72   bool WouldBeOverQuota(const GURL& origin_url, int64 additional_bytes);
73   bool IsOverQuota(const GURL& origin_url);
74 
75   quota::QuotaManagerProxy* quota_manager_proxy();
76 
77   std::vector<GURL> GetAllOrigins();
78   base::Time GetOriginLastModified(const GURL& origin_url);
79   base::ListValue* GetAllOriginsDetails();
80 
81   // Recorded in histograms, so append only.
82   enum ForceCloseReason {
83     FORCE_CLOSE_DELETE_ORIGIN = 0,
84     FORCE_CLOSE_BACKING_STORE_FAILURE,
85     FORCE_CLOSE_INTERNALS_PAGE,
86     FORCE_CLOSE_REASON_MAX
87   };
88 
89   // ForceClose takes a value rather than a reference since it may release the
90   // owning object.
91   void ForceClose(const GURL origin_url, ForceCloseReason reason);
92 
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();
98   }
99   size_t GetConnectionCount(const GURL& origin_url);
100 
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;
104   }
105 
106  protected:
107   virtual ~IndexedDBContextImpl();
108 
109  private:
110   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearLocalState);
111   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearSessionOnlyDatabases);
112   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, SetForceKeepSessionState);
113   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ForceCloseOpenDatabasesOnDelete);
114   friend class IndexedDBQuotaClientTest;
115 
116   typedef std::map<GURL, int64> OriginToSizeMap;
117   class IndexedDBGetUsageAndQuotaCallback;
118 
119   base::FilePath GetIndexedDBFilePath(const std::string& origin_id) const;
120   int64 ReadUsageFromDisk(const GURL& origin_url) const;
121   void EnsureDiskUsageCacheInitialized(const GURL& origin_url);
122   void QueryDiskAndUpdateQuotaUsage(const GURL& origin_url);
123   void GotUsageAndQuota(const GURL& origin_url,
124                         quota::QuotaStatusCode,
125                         int64 usage,
126                         int64 quota);
127   void GotUpdatedQuota(const GURL& origin_url, int64 usage, int64 quota);
128   void QueryAvailableQuota(const GURL& origin_url);
129 
130   std::set<GURL>* GetOriginSet();
131   bool AddToOriginSet(const GURL& origin_url) {
132     return GetOriginSet()->insert(origin_url).second;
133   }
134   void RemoveFromOriginSet(const GURL& origin_url) {
135     GetOriginSet()->erase(origin_url);
136   }
137 
138   // Only for testing.
139   void ResetCaches();
140 
141   scoped_refptr<IndexedDBFactory> factory_;
142   base::FilePath data_path_;
143   // If true, nothing (not even session-only data) should be deleted on exit.
144   bool force_keep_session_state_;
145   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
146   scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
147   base::SequencedTaskRunner* task_runner_;
148   scoped_ptr<std::set<GURL> > origin_set_;
149   OriginToSizeMap origin_size_map_;
150   OriginToSizeMap space_available_map_;
151 
152   DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl);
153 };
154 
155 }  // namespace content
156 
157 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
158