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