/content/public/browser/storage_partition_config.h

https://github.com/chromium/chromium · C Header · 111 lines · 61 code · 17 blank · 33 comment · 2 complexity · 72e4fe61b40fb3fbc81fb043475993ea MD5 · raw file

  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. #ifndef CONTENT_PUBLIC_BROWSER_STORAGE_PARTITION_CONFIG_H_
  5. #define CONTENT_PUBLIC_BROWSER_STORAGE_PARTITION_CONFIG_H_
  6. #include <string>
  7. #include "base/check.h"
  8. #include "base/gtest_prod_util.h"
  9. #include "content/common/content_export.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace content {
  12. class BrowserContext;
  13. // Each StoragePartition is uniquely identified by which partition domain
  14. // it belongs to (such as an app or the browser itself), the user supplied
  15. // partition name and the bit indicating whether it should be persisted on
  16. // disk or not. This structure contains those elements and is used as
  17. // uniqueness key to lookup StoragePartition objects in the global map.
  18. class CONTENT_EXPORT StoragePartitionConfig {
  19. public:
  20. StoragePartitionConfig(); // Needed by the SiteInfo default constructor
  21. StoragePartitionConfig(const StoragePartitionConfig&);
  22. StoragePartitionConfig& operator=(const StoragePartitionConfig&);
  23. // Creates a default config for |browser_context|. If |browser_context| is an
  24. // off-the-record profile, then the config will have |in_memory_| set to true.
  25. static StoragePartitionConfig CreateDefault(BrowserContext* browser_context);
  26. // Creates a config tied to a specific domain.
  27. // The |partition_domain| is [a-z]* UTF-8 string, specifying the domain in
  28. // which partitions live (similar to namespace). |partition_domain| must NOT
  29. // be an empty string. Within a domain, partitions can be uniquely identified
  30. // by the combination of |partition_name| and |in_memory| values. When a
  31. // partition is not to be persisted, the |in_memory| value must be set to
  32. // true. If |browser_context| is an off-the-record profile, then the config
  33. // will have |in_memory_| set to true independent of what is specified in
  34. // the |in_memory| parameter. This is because these profiles are not allowed
  35. // to persist information on disk.
  36. static StoragePartitionConfig Create(BrowserContext* browser_context,
  37. const std::string& partition_domain,
  38. const std::string& partition_name,
  39. bool in_memory);
  40. std::string partition_domain() const { return partition_domain_; }
  41. std::string partition_name() const { return partition_name_; }
  42. bool in_memory() const { return in_memory_; }
  43. // Returns true if this config was created by CreateDefault() or is
  44. // a copy of a config created with that method.
  45. bool is_default() const { return partition_domain_.empty(); }
  46. // In some cases we want a "child" storage partition to resolve blob URLs that
  47. // were created by their "parent", while not allowing the reverse. To enable
  48. // this, set this flag to a value other than kNone, which will result in the
  49. // storage partition with the same partition_domain but empty partition_name
  50. // being used as fallback for the purpose of resolving blob URLs.
  51. enum class FallbackMode {
  52. kNone,
  53. kFallbackPartitionOnDisk,
  54. kFallbackPartitionInMemory,
  55. };
  56. void set_fallback_to_partition_domain_for_blob_urls(FallbackMode fallback) {
  57. if (fallback != FallbackMode::kNone) {
  58. DCHECK(!is_default());
  59. DCHECK(!partition_domain_.empty());
  60. // TODO(https://crbug.com/1279537): Ideally we shouldn't have storage
  61. // partition configs that differ only in their fallback mode, but
  62. // unfortunately that isn't true. When that is fixed this can be made more
  63. // robust by disallowing fallback from storage partitions with an empty
  64. // partition name.
  65. // DCHECK(!partition_name_.empty());
  66. }
  67. fallback_to_partition_domain_for_blob_urls_ = fallback;
  68. }
  69. FallbackMode fallback_to_partition_domain_for_blob_urls() const {
  70. return fallback_to_partition_domain_for_blob_urls_;
  71. }
  72. absl::optional<StoragePartitionConfig> GetFallbackForBlobUrls() const;
  73. bool operator<(const StoragePartitionConfig& rhs) const;
  74. bool operator==(const StoragePartitionConfig& rhs) const;
  75. bool operator!=(const StoragePartitionConfig& rhs) const;
  76. private:
  77. friend StoragePartitionConfig CreateStoragePartitionConfigForTesting(
  78. bool,
  79. const std::string&,
  80. const std::string&);
  81. FRIEND_TEST_ALL_PREFIXES(StoragePartitionConfigTest, OperatorLess);
  82. StoragePartitionConfig(const std::string& partition_domain,
  83. const std::string& partition_name,
  84. bool in_memory);
  85. std::string partition_domain_;
  86. std::string partition_name_;
  87. bool in_memory_ = false;
  88. FallbackMode fallback_to_partition_domain_for_blob_urls_ =
  89. FallbackMode::kNone;
  90. };
  91. CONTENT_EXPORT std::ostream& operator<<(std::ostream& out,
  92. const StoragePartitionConfig& config);
  93. } // namespace content
  94. #endif // CONTENT_PUBLIC_BROWSER_STORAGE_PARTITION_CONFIG_H_