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