Benjamin Lerman | d15e3f4 | 2015-09-17 11:26:18 +0200 | [diff] [blame] | 1 | // Copyright 2015 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 SERVICES_URL_RESPONSE_DISK_CACHE_URL_RESPONSE_DISK_CACHE_DB_H_ |
| 6 | #define SERVICES_URL_RESPONSE_DISK_CACHE_URL_RESPONSE_DISK_CACHE_DB_H_ |
| 7 | |
| 8 | #include "base/files/file_path.h" |
| 9 | #include "base/memory/linked_ptr.h" |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h" |
| 13 | |
| 14 | namespace leveldb { |
| 15 | class Comparator; |
| 16 | class DB; |
| 17 | class Iterator; |
| 18 | }; |
| 19 | |
| 20 | namespace mojo { |
| 21 | |
| 22 | // Specialized database for the cache content. |
| 23 | class URLResponseDiskCacheDB |
| 24 | : public base::RefCountedThreadSafe<URLResponseDiskCacheDB> { |
| 25 | public: |
| 26 | class Iterator { |
| 27 | public: |
| 28 | explicit Iterator(linked_ptr<leveldb::DB> db); |
| 29 | ~Iterator(); |
| 30 | |
| 31 | bool HasNext(); |
| 32 | void GetNext(CacheKeyPtr* key, CacheEntryPtr* entry); |
| 33 | |
| 34 | private: |
| 35 | linked_ptr<leveldb::DB> db_; |
| 36 | scoped_ptr<leveldb::Iterator> it_; |
| 37 | }; |
| 38 | |
| 39 | // Constructs the database. |db_path| is the path to the leveldb database. If |
| 40 | // the path exists, the database will be opened, otherwise it will be created. |
| 41 | explicit URLResponseDiskCacheDB(const base::FilePath& db_path); |
| 42 | |
| 43 | // Set and get the version of the database. |
| 44 | uint64_t GetVersion(); |
| 45 | void SetVersion(uint64_t version); |
| 46 | |
Benjamin Lerman | 8dbcae1 | 2015-09-24 10:05:46 +0200 | [diff] [blame] | 47 | // Set and get an entry for a given key. |
| 48 | void Put(CacheKeyPtr key, CacheEntryPtr entry); |
| 49 | CacheEntryPtr Get(CacheKeyPtr key); |
| 50 | |
Benjamin Lerman | d15e3f4 | 2015-09-17 11:26:18 +0200 | [diff] [blame] | 51 | // Put an entry for the given |request_origin| and |url|. Older entry for the |
| 52 | // same |request_origin| and |url| will not be removed, but will be shadowed |
| 53 | // by the new one. |
| 54 | void PutNew(const std::string& request_origin, |
| 55 | const std::string& url, |
| 56 | CacheEntryPtr entry); |
| 57 | |
| 58 | // Returns the newest entry for the given |request_origin| and |url|, or null |
Benjamin Lerman | 8dbcae1 | 2015-09-24 10:05:46 +0200 | [diff] [blame] | 59 | // if none exist. If |key| is not null and GetNewest returns an entry, |*key| |
| 60 | // will contain the actual key of the entry. |
Benjamin Lerman | d15e3f4 | 2015-09-17 11:26:18 +0200 | [diff] [blame] | 61 | CacheEntryPtr GetNewest(const std::string& request_origin, |
Benjamin Lerman | 8dbcae1 | 2015-09-24 10:05:46 +0200 | [diff] [blame] | 62 | const std::string& url, |
| 63 | CacheKeyPtr* key); |
Benjamin Lerman | d15e3f4 | 2015-09-17 11:26:18 +0200 | [diff] [blame] | 64 | |
| 65 | // Delete the entry for the given |key|. |
| 66 | void Delete(CacheKeyPtr key); |
| 67 | |
| 68 | // Returns an iterator over all the entries in the database. For a given |
| 69 | // |request_origin| and |url|, entries will be sorted from newest to oldest. |
| 70 | // An iterator will not be invalidated nor any of its values will be modified |
| 71 | // by further changes to the database. |
| 72 | scoped_ptr<Iterator> GetIterator(); |
| 73 | |
| 74 | private: |
| 75 | friend class base::RefCountedThreadSafe<URLResponseDiskCacheDB>; |
| 76 | ~URLResponseDiskCacheDB(); |
| 77 | |
| 78 | scoped_ptr<leveldb::Comparator> comparator_; |
| 79 | linked_ptr<leveldb::DB> db_; |
| 80 | }; |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | #endif // SERVICES_URL_RESPONSE_DISK_CACHE_URL_RESPONSE_DISK_CACHE_DB_H_ |