blob: 55f1c3030b5552484cd24c920550396eedf23d84 [file] [log] [blame]
[email protected]d7c7c98a2012-07-12 21:27:441// 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
tfarinabccc34c72015-02-27 21:32:155#ifndef CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_
6#define CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_
[email protected]d7c7c98a2012-07-12 21:27:447
8#include <map>
avi6f9a1d412016-08-16 16:07:319#include <memory>
[email protected]d7c7c98a2012-07-12 21:27:4410#include <string>
11
12#include "base/callback_forward.h"
[email protected]14c1c232013-06-11 17:52:4413#include "base/containers/hash_tables.h"
[email protected]b471cf42012-11-13 09:11:3014#include "base/gtest_prod_util.h"
avi6f9a1d412016-08-16 16:07:3115#include "base/macros.h"
[email protected]d7c7c98a2012-07-12 21:27:4416#include "base/supports_user_data.h"
[email protected]1bc28312012-11-08 08:31:5317#include "content/browser/storage_partition_impl.h"
[email protected]4c3a23582012-08-18 08:54:3418#include "content/public/browser/browser_context.h"
[email protected]d7c7c98a2012-07-12 21:27:4419
[email protected]399583b2012-12-11 09:33:4220namespace base {
[email protected]a3ef4832013-02-02 05:12:3321class FilePath;
[email protected]399583b2012-12-11 09:33:4222class SequencedTaskRunner;
23} // namespace base
24
[email protected]d7c7c98a2012-07-12 21:27:4425namespace content {
26
27class BrowserContext;
[email protected]d7c7c98a2012-07-12 21:27:4428
29// A std::string to StoragePartition map for use with SupportsUserData APIs.
[email protected]995d05862014-06-07 09:03:0030class CONTENT_EXPORT StoragePartitionImplMap
31 : public base::SupportsUserData::Data {
[email protected]d7c7c98a2012-07-12 21:27:4432 public:
[email protected]4c3a23582012-08-18 08:54:3433 explicit StoragePartitionImplMap(BrowserContext* browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4434
dchengc2282aa2014-10-21 12:07:5835 ~StoragePartitionImplMap() override;
[email protected]d7c7c98a2012-07-12 21:27:4436
37 // This map retains ownership of the returned StoragePartition objects.
[email protected]1bc28312012-11-08 08:31:5338 StoragePartitionImpl* Get(const std::string& partition_domain,
39 const std::string& partition_name,
40 bool in_memory);
[email protected]d7c7c98a2012-07-12 21:27:4441
[email protected]14acc642012-11-17 12:20:1042 // 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]399583b2012-12-11 09:33:4244 //
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.
dcheng59716272016-04-09 05:19:0856 void GarbageCollect(
57 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
58 const base::Closure& done);
[email protected]14acc642012-11-17 12:20:1059
[email protected]4c3a23582012-08-18 08:54:3460 void ForEach(const BrowserContext::StoragePartitionCallback& callback);
[email protected]d7c7c98a2012-07-12 21:27:4461
62 private:
[email protected]b471cf42012-11-13 09:11:3063 FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest, OperatorLess);
[email protected]995d05862014-06-07 09:03:0064 FRIEND_TEST_ALL_PREFIXES(StoragePartitionImplMapTest, GarbageCollect);
[email protected]b471cf42012-11-13 09:11:3065
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,
avi6f9a1d412016-08-16 16:07:31106 std::unique_ptr<StoragePartitionImpl>,
[email protected]b471cf42012-11-13 09:11:30107 StoragePartitionConfigLess>
[email protected]1bc28312012-11-08 08:31:53108 PartitionMap;
109
[email protected]b471cf42012-11-13 09:11:30110 // 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]a3ef4832013-02-02 05:12:33113 static base::FilePath GetStoragePartitionPath(
114 const std::string& partition_domain,
115 const std::string& partition_name);
[email protected]b471cf42012-11-13 09:11:30116
[email protected]d7c7c98a2012-07-12 21:27:44117 // 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]14acc642012-11-17 12:20:10123 void PostCreateInitialization(StoragePartitionImpl* partition,
124 bool in_memory);
[email protected]d7c7c98a2012-07-12 21:27:44125
126 BrowserContext* browser_context_; // Not Owned.
[email protected]399583b2012-12-11 09:33:42127 scoped_refptr<base::SequencedTaskRunner> file_access_runner_;
[email protected]1bc28312012-11-08 08:31:53128 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_;
avi6f9a1d412016-08-16 16:07:31133
134 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplMap);
[email protected]d7c7c98a2012-07-12 21:27:44135};
136
137} // namespace content
138
tfarinabccc34c72015-02-27 21:32:15139#endif // CONTENT_BROWSER_STORAGE_PARTITION_IMPL_MAP_H_