- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / storage_partition_impl_map.h
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
18 namespace base {
19 class FilePath;
20 class SequencedTaskRunner;
21 }  // namespace base
22
23 namespace content {
24
25 class BrowserContext;
26
27 // A std::string to StoragePartition map for use with SupportsUserData APIs.
28 class StoragePartitionImplMap : public base::SupportsUserData::Data {
29  public:
30   explicit StoragePartitionImplMap(BrowserContext* browser_context);
31
32   virtual ~StoragePartitionImplMap();
33
34   // This map retains ownership of the returned StoragePartition objects.
35   StoragePartitionImpl* Get(const std::string& partition_domain,
36                             const std::string& partition_name,
37                             bool in_memory);
38
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.
41   //
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);
46
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
49   // partition.
50   //
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);
55
56   void ForEach(const BrowserContext::StoragePartitionCallback& callback);
57
58  private:
59   FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest, OperatorLess);
60
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.
66   //
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;
75     const bool in_memory;
76
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) {}
83   };
84
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;
95       else
96         return false;
97     }
98   };
99
100   typedef std::map<StoragePartitionConfig,
101                    StoragePartitionImpl*,
102                    StoragePartitionConfigLess>
103       PartitionMap;
104
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);
111
112   // This must always be called *after* |partition| has been added to the
113   // partitions_.
114   //
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,
119                                 bool in_memory);
120
121   BrowserContext* browser_context_;  // Not Owned.
122   scoped_refptr<base::SequencedTaskRunner> file_access_runner_;
123   PartitionMap partitions_;
124
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_;
128 };
129
130 }  // namespace content
131
132 #endif  // CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_