[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 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 | |
tfarina | bccc34c7 | 2015-02-27 21:32:15 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_ |
| 6 | #define CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_ |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 7 | |
| 8 | #include <map> |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 9 | #include <memory> |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include "base/callback_forward.h" |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 13 | #include "base/containers/hash_tables.h" |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 16 | #include "base/supports_user_data.h" |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 17 | #include "content/browser/storage_partition_impl.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 18 | #include "content/public/browser/browser_context.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 19 | |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 20 | namespace base { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 21 | class FilePath; |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 22 | class SequencedTaskRunner; |
| 23 | } // namespace base |
| 24 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 25 | namespace content { |
| 26 | |
| 27 | class BrowserContext; |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 28 | |
| 29 | // A std::string to StoragePartition map for use with SupportsUserData APIs. |
[email protected] | 995d0586 | 2014-06-07 09:03:00 | [diff] [blame] | 30 | class CONTENT_EXPORT StoragePartitionImplMap |
| 31 | : public base::SupportsUserData::Data { |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 32 | public: |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 33 | explicit StoragePartitionImplMap(BrowserContext* browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 34 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 35 | ~StoragePartitionImplMap() override; |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 36 | |
| 37 | // This map retains ownership of the returned StoragePartition objects. |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 38 | StoragePartitionImpl* Get(const std::string& partition_domain, |
| 39 | const std::string& partition_name, |
| 40 | bool in_memory); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 41 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 42 | // Starts an asynchronous best-effort attempt to delete all on-disk storage |
| 43 | // related to |site|, avoiding any directories that are known to be in use. |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 44 | // |
| 45 | // |on_gc_required| is called if the AsyncObliterate() call was unable to |
| 46 | // fully clean the on-disk storage requiring a call to GarbageCollect() on |
| 47 | // the next browser start. |
| 48 | void AsyncObliterate(const GURL& site, const base::Closure& on_gc_required); |
| 49 | |
| 50 | // Examines the on-disk storage and removes any entires that are not listed |
| 51 | // in the |active_paths|, or in use by current entries in the storage |
| 52 | // partition. |
| 53 | // |
| 54 | // The |done| closure is executed on the calling thread when garbage |
| 55 | // collection is complete. |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 56 | void GarbageCollect( |
| 57 | std::unique_ptr<base::hash_set<base::FilePath>> active_paths, |
| 58 | const base::Closure& done); |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 59 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 60 | void ForEach(const BrowserContext::StoragePartitionCallback& callback); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 61 | |
| 62 | private: |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 63 | FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest, OperatorLess); |
[email protected] | 995d0586 | 2014-06-07 09:03:00 | [diff] [blame] | 64 | FRIEND_TEST_ALL_PREFIXES(StoragePartitionImplMapTest, GarbageCollect); |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 65 | |
| 66 | // Each StoragePartition is uniquely identified by which partition domain |
| 67 | // it belongs to (such as an app or the browser itself), the user supplied |
| 68 | // partition name and the bit indicating whether it should be persisted on |
| 69 | // disk or not. This structure contains those elements and is used as |
| 70 | // uniqueness key to lookup StoragePartition objects in the global map. |
| 71 | // |
| 72 | // TODO(nasko): It is equivalent, though not identical to the same structure |
| 73 | // that lives in chrome profiles. The difference is that this one has |
| 74 | // partition_domain and partition_name separate, while the latter one has |
| 75 | // the path produced by combining the two pieces together. |
| 76 | // The fix for http://crbug.com/159193 will remove the chrome version. |
| 77 | struct StoragePartitionConfig { |
| 78 | const std::string partition_domain; |
| 79 | const std::string partition_name; |
| 80 | const bool in_memory; |
| 81 | |
| 82 | StoragePartitionConfig(const std::string& domain, |
| 83 | const std::string& partition, |
| 84 | const bool& in_memory_only) |
| 85 | : partition_domain(domain), |
| 86 | partition_name(partition), |
| 87 | in_memory(in_memory_only) {} |
| 88 | }; |
| 89 | |
| 90 | // Functor for operator <. |
| 91 | struct StoragePartitionConfigLess { |
| 92 | bool operator()(const StoragePartitionConfig& lhs, |
| 93 | const StoragePartitionConfig& rhs) const { |
| 94 | if (lhs.partition_domain != rhs.partition_domain) |
| 95 | return lhs.partition_domain < rhs.partition_domain; |
| 96 | else if (lhs.partition_name != rhs.partition_name) |
| 97 | return lhs.partition_name < rhs.partition_name; |
| 98 | else if (lhs.in_memory != rhs.in_memory) |
| 99 | return lhs.in_memory < rhs.in_memory; |
| 100 | else |
| 101 | return false; |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | typedef std::map<StoragePartitionConfig, |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 106 | std::unique_ptr<StoragePartitionImpl>, |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 107 | StoragePartitionConfigLess> |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 108 | PartitionMap; |
| 109 | |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 110 | // Returns the relative path from the profile's base directory, to the |
| 111 | // directory that holds all the state for storage contexts in the given |
| 112 | // |partition_domain| and |partition_name|. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 113 | static base::FilePath GetStoragePartitionPath( |
| 114 | const std::string& partition_domain, |
| 115 | const std::string& partition_name); |
[email protected] | b471cf4 | 2012-11-13 09:11:30 | [diff] [blame] | 116 | |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 117 | // This must always be called *after* |partition| has been added to the |
| 118 | // partitions_. |
| 119 | // |
| 120 | // TODO(ajwong): Is there a way to make it so that Get()'s implementation |
| 121 | // doesn't need to be aware of this ordering? Revisit when refactoring |
| 122 | // ResourceContext and AppCache to respect storage partitions. |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 123 | void PostCreateInitialization(StoragePartitionImpl* partition, |
| 124 | bool in_memory); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 125 | |
| 126 | BrowserContext* browser_context_; // Not Owned. |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 127 | scoped_refptr<base::SequencedTaskRunner> file_access_runner_; |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 128 | PartitionMap partitions_; |
| 129 | |
| 130 | // Set to true when the ResourceContext for the associated |browser_context_| |
| 131 | // is initialized. Can never return to false. |
| 132 | bool resource_context_initialized_; |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplMap); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace content |
| 138 | |
tfarina | bccc34c7 | 2015-02-27 21:32:15 | [diff] [blame] | 139 | #endif // CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_ |