OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ | 5 #ifndef CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ |
6 #define CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ | 6 #define CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/gtest_prod_util.h" |
12 #include "base/supports_user_data.h" | 13 #include "base/supports_user_data.h" |
13 #include "content/browser/storage_partition_impl.h" | 14 #include "content/browser/storage_partition_impl.h" |
14 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
15 | 16 |
16 class FilePath; | 17 class FilePath; |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 class BrowserContext; | 21 class BrowserContext; |
21 | 22 |
22 // A std::string to StoragePartition map for use with SupportsUserData APIs. | 23 // A std::string to StoragePartition map for use with SupportsUserData APIs. |
23 class StoragePartitionImplMap : public base::SupportsUserData::Data { | 24 class StoragePartitionImplMap : public base::SupportsUserData::Data { |
24 public: | 25 public: |
25 explicit StoragePartitionImplMap(BrowserContext* browser_context); | 26 explicit StoragePartitionImplMap(BrowserContext* browser_context); |
26 | 27 |
27 virtual ~StoragePartitionImplMap(); | 28 virtual ~StoragePartitionImplMap(); |
28 | 29 |
29 // This map retains ownership of the returned StoragePartition objects. | 30 // This map retains ownership of the returned StoragePartition objects. |
30 StoragePartitionImpl* Get(const std::string& partition_domain, | 31 StoragePartitionImpl* Get(const std::string& partition_domain, |
31 const std::string& partition_name, | 32 const std::string& partition_name, |
32 bool in_memory); | 33 bool in_memory); |
33 | 34 |
34 void ForEach(const BrowserContext::StoragePartitionCallback& callback); | 35 void ForEach(const BrowserContext::StoragePartitionCallback& callback); |
35 | 36 |
36 private: | 37 private: |
37 typedef std::map<StoragePartitionImpl::StoragePartitionConfig, | 38 FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest, OperatorLess); |
| 39 |
| 40 // Each StoragePartition is uniquely identified by which partition domain |
| 41 // it belongs to (such as an app or the browser itself), the user supplied |
| 42 // partition name and the bit indicating whether it should be persisted on |
| 43 // disk or not. This structure contains those elements and is used as |
| 44 // uniqueness key to lookup StoragePartition objects in the global map. |
| 45 // |
| 46 // TODO(nasko): It is equivalent, though not identical to the same structure |
| 47 // that lives in chrome profiles. The difference is that this one has |
| 48 // partition_domain and partition_name separate, while the latter one has |
| 49 // the path produced by combining the two pieces together. |
| 50 // The fix for http://crbug.com/159193 will remove the chrome version. |
| 51 struct StoragePartitionConfig { |
| 52 const std::string partition_domain; |
| 53 const std::string partition_name; |
| 54 const bool in_memory; |
| 55 |
| 56 StoragePartitionConfig(const std::string& domain, |
| 57 const std::string& partition, |
| 58 const bool& in_memory_only) |
| 59 : partition_domain(domain), |
| 60 partition_name(partition), |
| 61 in_memory(in_memory_only) {} |
| 62 }; |
| 63 |
| 64 // Functor for operator <. |
| 65 struct StoragePartitionConfigLess { |
| 66 bool operator()(const StoragePartitionConfig& lhs, |
| 67 const StoragePartitionConfig& rhs) const { |
| 68 if (lhs.partition_domain != rhs.partition_domain) |
| 69 return lhs.partition_domain < rhs.partition_domain; |
| 70 else if (lhs.partition_name != rhs.partition_name) |
| 71 return lhs.partition_name < rhs.partition_name; |
| 72 else if (lhs.in_memory != rhs.in_memory) |
| 73 return lhs.in_memory < rhs.in_memory; |
| 74 else |
| 75 return false; |
| 76 } |
| 77 }; |
| 78 |
| 79 typedef std::map<StoragePartitionConfig, |
38 StoragePartitionImpl*, | 80 StoragePartitionImpl*, |
39 StoragePartitionImpl::StoragePartitionConfigLess> | 81 StoragePartitionConfigLess> |
40 PartitionMap; | 82 PartitionMap; |
41 | 83 |
| 84 // Returns the relative path from the profile's base directory, to the |
| 85 // directory that holds all the state for storage contexts in the given |
| 86 // |partition_domain| and |partition_name|. |
| 87 static FilePath GetStoragePartitionPath(const std::string& partition_domain, |
| 88 const std::string& partition_name); |
| 89 |
42 // This must always be called *after* |partition| has been added to the | 90 // This must always be called *after* |partition| has been added to the |
43 // partitions_. | 91 // partitions_. |
44 // | 92 // |
45 // TODO(ajwong): Is there a way to make it so that Get()'s implementation | 93 // TODO(ajwong): Is there a way to make it so that Get()'s implementation |
46 // doesn't need to be aware of this ordering? Revisit when refactoring | 94 // doesn't need to be aware of this ordering? Revisit when refactoring |
47 // ResourceContext and AppCache to respect storage partitions. | 95 // ResourceContext and AppCache to respect storage partitions. |
48 void PostCreateInitialization(StoragePartitionImpl* partition); | 96 void PostCreateInitialization(StoragePartitionImpl* partition); |
49 | 97 |
50 BrowserContext* browser_context_; // Not Owned. | 98 BrowserContext* browser_context_; // Not Owned. |
51 PartitionMap partitions_; | 99 PartitionMap partitions_; |
52 | 100 |
53 // Set to true when the ResourceContext for the associated |browser_context_| | 101 // Set to true when the ResourceContext for the associated |browser_context_| |
54 // is initialized. Can never return to false. | 102 // is initialized. Can never return to false. |
55 bool resource_context_initialized_; | 103 bool resource_context_initialized_; |
56 }; | 104 }; |
57 | 105 |
58 } // namespace content | 106 } // namespace content |
59 | 107 |
60 #endif // CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ | 108 #endif // CONTENT_BROWSER_STORAGE_PARTITION_MAP_H_ |
OLD | NEW |