fix: ElectronBrowserContext::PartitionKey comparisons (#41055)

* fix: ElectronBrowserContext::PartitionKey comparisons

Use c++20 default comparisons to simplify + fix PartitionKey sorting:

- The equality operator is broken. `PartitionKey{"foo", false}` is both
  equal, to and less than, `PartitionKey{"foo", true}`

- For some keys, the same session can be retrieved via both `fromPath()`
  and `fromPartition()`. This use case was discussed and removed from
  the original PR after code review said "always returning different
  sessions feels lower maintenance." The current behavior is a bug that
  comes from the comparison operators not checking the keys' types.

Xref: 3f1aea9af9 (r1099745359)

Xref: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md#Default-comparisons-allowed

* fixup! fix: ElectronBrowserContext::PartitionKey comparisons
This commit is contained in:
Charles Kerr 2024-01-23 09:41:44 -06:00 коммит произвёл GitHub
Родитель 1300e83884
Коммит 1af9612edf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 13 добавлений и 31 удалений

Просмотреть файл

@ -9,6 +9,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
@ -80,41 +81,22 @@ class ElectronBrowserContext : public content::BrowserContext {
// partition_id => browser_context
struct PartitionKey {
enum class KeyType { Partition, FilePath };
std::string location;
bool in_memory;
KeyType partition_type;
PartitionKey(const std::string_view partition, bool in_memory)
: type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
PartitionKey(const std::string& partition, bool in_memory)
: location(partition),
in_memory(in_memory),
partition_type(KeyType::Partition) {}
explicit PartitionKey(const base::FilePath& file_path)
: location(file_path.AsUTF8Unsafe()),
in_memory(false),
partition_type(KeyType::FilePath) {}
: type_{Type::Path},
location_{file_path.AsUTF8Unsafe()},
in_memory_{false} {}
bool operator<(const PartitionKey& other) const {
if (partition_type == KeyType::Partition) {
if (location == other.location)
return in_memory < other.in_memory;
return location < other.location;
} else {
if (location == other.location)
return false;
return location < other.location;
}
}
friend auto operator<=>(const PartitionKey&, const PartitionKey&) = default;
bool operator==(const PartitionKey& other) const {
if (partition_type == KeyType::Partition) {
return (location == other.location) && (in_memory < other.in_memory);
} else {
if (location == other.location)
return true;
return false;
}
}
private:
enum class Type { Partition, Path };
Type type_;
std::string location_;
bool in_memory_;
};
using BrowserContextMap =