Home | History | Annotate | Download | only in indexed_db
      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 <vector>
     11 
     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"
     19 #include "url/gurl.h"
     20 #include "webkit/common/quota/quota_types.h"
     21 
     22 class GURL;
     23 
     24 namespace base {
     25 class ListValue;
     26 class FilePath;
     27 class SequencedTaskRunner;
     28 }
     29 
     30 namespace quota {
     31 class QuotaManagerProxy;
     32 class SpecialStoragePolicy;
     33 }
     34 
     35 namespace content {
     36 
     37 class IndexedDBConnection;
     38 
     39 class CONTENT_EXPORT IndexedDBContextImpl
     40     : NON_EXPORTED_BASE(public IndexedDBContext) {
     41  public:
     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);
     47 
     48   IndexedDBFactory* GetIDBFactory();
     49 
     50   // The indexed db directory.
     51   static const base::FilePath::CharType kIndexedDBDirectory[];
     52 
     53   // Disables the exit-time deletion of session-only data.
     54   void SetForceKeepSessionState() { force_keep_session_state_ = true; }
     55 
     56   // IndexedDBContext implementation:
     57   virtual base::TaskRunner* TaskRunner() const OVERRIDE;
     58   virtual std::vector<GURL> GetAllOrigins() OVERRIDE;
     59   virtual std::vector<IndexedDBInfo> GetAllOriginsInfo() OVERRIDE;
     60   virtual int64 GetOriginDiskUsage(const GURL& origin_url) OVERRIDE;
     61   virtual base::Time GetOriginLastModified(const GURL& origin_url) OVERRIDE;
     62   virtual void DeleteForOrigin(const GURL& origin_url) OVERRIDE;
     63   virtual base::FilePath GetFilePathForTesting(
     64       const std::string& origin_id) const OVERRIDE;
     65   virtual void SetTaskRunnerForTesting(base::SequencedTaskRunner* task_runner)
     66       OVERRIDE;
     67 
     68   // Methods called by IndexedDBDispatcherHost for quota support.
     69   void ConnectionOpened(const GURL& origin_url, IndexedDBConnection* db);
     70   void ConnectionClosed(const GURL& origin_url, IndexedDBConnection* db);
     71   void TransactionComplete(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   base::ListValue* GetAllOriginsDetails();
     78   void ForceClose(const GURL& origin_url);
     79   base::FilePath GetFilePath(const GURL& origin_url);
     80   base::FilePath data_path() const { return data_path_; }
     81   bool IsInOriginSet(const GURL& origin_url) {
     82     std::set<GURL>* set = GetOriginSet();
     83     return set->find(origin_url) != set->end();
     84   }
     85   size_t GetConnectionCount(const GURL& origin_url);
     86 
     87   // For unit tests allow to override the |data_path_|.
     88   void set_data_path_for_testing(const base::FilePath& data_path) {
     89     data_path_ = data_path;
     90   }
     91 
     92  protected:
     93   virtual ~IndexedDBContextImpl();
     94 
     95  private:
     96   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearLocalState);
     97   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ClearSessionOnlyDatabases);
     98   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, SetForceKeepSessionState);
     99   FRIEND_TEST_ALL_PREFIXES(IndexedDBTest, ForceCloseOpenDatabasesOnDelete);
    100   friend class IndexedDBQuotaClientTest;
    101 
    102   typedef std::map<GURL, int64> OriginToSizeMap;
    103   class IndexedDBGetUsageAndQuotaCallback;
    104 
    105   base::FilePath GetIndexedDBFilePath(const std::string& origin_id) const;
    106   int64 ReadUsageFromDisk(const GURL& origin_url) const;
    107   void EnsureDiskUsageCacheInitialized(const GURL& origin_url);
    108   void QueryDiskAndUpdateQuotaUsage(const GURL& origin_url);
    109   void GotUsageAndQuota(const GURL& origin_url,
    110                         quota::QuotaStatusCode,
    111                         int64 usage,
    112                         int64 quota);
    113   void GotUpdatedQuota(const GURL& origin_url, int64 usage, int64 quota);
    114   void QueryAvailableQuota(const GURL& origin_url);
    115 
    116   std::set<GURL>* GetOriginSet();
    117   bool AddToOriginSet(const GURL& origin_url) {
    118     return GetOriginSet()->insert(origin_url).second;
    119   }
    120   void RemoveFromOriginSet(const GURL& origin_url) {
    121     GetOriginSet()->erase(origin_url);
    122   }
    123 
    124   // Only for testing.
    125   void ResetCaches();
    126 
    127   scoped_refptr<IndexedDBFactory> factory_;
    128   base::FilePath data_path_;
    129   // If true, nothing (not even session-only data) should be deleted on exit.
    130   bool force_keep_session_state_;
    131   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
    132   scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
    133   base::SequencedTaskRunner* task_runner_;
    134   scoped_ptr<std::set<GURL> > origin_set_;
    135   OriginToSizeMap origin_size_map_;
    136   OriginToSizeMap space_available_map_;
    137   typedef std::set<IndexedDBConnection*> ConnectionSet;
    138   std::map<GURL, ConnectionSet> connections_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(IndexedDBContextImpl);
    141 };
    142 
    143 }  // namespace content
    144 
    145 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_CONTEXT_IMPL_H_
    146