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.
5 #ifndef CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_
6 #define CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_
11 #include "base/callback_forward.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/supports_user_data.h"
15 #include "content/browser/storage_partition_impl.h"
16 #include "content/public/browser/browser_context.h"
20 class SequencedTaskRunner
;
27 // A std::string to StoragePartition map for use with SupportsUserData APIs.
28 class StoragePartitionImplMap
: public base::SupportsUserData::Data
{
30 explicit StoragePartitionImplMap(BrowserContext
* browser_context
);
32 virtual ~StoragePartitionImplMap();
34 // This map retains ownership of the returned StoragePartition objects.
35 StoragePartitionImpl
* Get(const std::string
& partition_domain
,
36 const std::string
& partition_name
,
39 // Starts an asynchronous best-effort attempt to delete all on-disk storage
40 // related to |site|, avoiding any directories that are known to be in use.
42 // |on_gc_required| is called if the AsyncObliterate() call was unable to
43 // fully clean the on-disk storage requiring a call to GarbageCollect() on
44 // the next browser start.
45 void AsyncObliterate(const GURL
& site
, const base::Closure
& on_gc_required
);
47 // Examines the on-disk storage and removes any entires that are not listed
48 // in the |active_paths|, or in use by current entries in the storage
51 // The |done| closure is executed on the calling thread when garbage
52 // collection is complete.
53 void GarbageCollect(scoped_ptr
<base::hash_set
<base::FilePath
> > active_paths
,
54 const base::Closure
& done
);
56 void ForEach(const BrowserContext::StoragePartitionCallback
& callback
);
59 FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest
, OperatorLess
);
61 // Each StoragePartition is uniquely identified by which partition domain
62 // it belongs to (such as an app or the browser itself), the user supplied
63 // partition name and the bit indicating whether it should be persisted on
64 // disk or not. This structure contains those elements and is used as
65 // uniqueness key to lookup StoragePartition objects in the global map.
67 // TODO(nasko): It is equivalent, though not identical to the same structure
68 // that lives in chrome profiles. The difference is that this one has
69 // partition_domain and partition_name separate, while the latter one has
70 // the path produced by combining the two pieces together.
71 // The fix for http://crbug.com/159193 will remove the chrome version.
72 struct StoragePartitionConfig
{
73 const std::string partition_domain
;
74 const std::string partition_name
;
77 StoragePartitionConfig(const std::string
& domain
,
78 const std::string
& partition
,
79 const bool& in_memory_only
)
80 : partition_domain(domain
),
81 partition_name(partition
),
82 in_memory(in_memory_only
) {}
85 // Functor for operator <.
86 struct StoragePartitionConfigLess
{
87 bool operator()(const StoragePartitionConfig
& lhs
,
88 const StoragePartitionConfig
& rhs
) const {
89 if (lhs
.partition_domain
!= rhs
.partition_domain
)
90 return lhs
.partition_domain
< rhs
.partition_domain
;
91 else if (lhs
.partition_name
!= rhs
.partition_name
)
92 return lhs
.partition_name
< rhs
.partition_name
;
93 else if (lhs
.in_memory
!= rhs
.in_memory
)
94 return lhs
.in_memory
< rhs
.in_memory
;
100 typedef std::map
<StoragePartitionConfig
,
101 StoragePartitionImpl
*,
102 StoragePartitionConfigLess
>
105 // Returns the relative path from the profile's base directory, to the
106 // directory that holds all the state for storage contexts in the given
107 // |partition_domain| and |partition_name|.
108 static base::FilePath
GetStoragePartitionPath(
109 const std::string
& partition_domain
,
110 const std::string
& partition_name
);
112 // This must always be called *after* |partition| has been added to the
115 // TODO(ajwong): Is there a way to make it so that Get()'s implementation
116 // doesn't need to be aware of this ordering? Revisit when refactoring
117 // ResourceContext and AppCache to respect storage partitions.
118 void PostCreateInitialization(StoragePartitionImpl
* partition
,
121 BrowserContext
* browser_context_
; // Not Owned.
122 scoped_refptr
<base::SequencedTaskRunner
> file_access_runner_
;
123 PartitionMap partitions_
;
125 // Set to true when the ResourceContext for the associated |browser_context_|
126 // is initialized. Can never return to false.
127 bool resource_context_initialized_
;
130 } // namespace content
132 #endif // CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_