Skip to content
Snippets Groups Projects
Unverified Commit b70b76e9 authored by trop[bot]'s avatar trop[bot] Committed by GitHub
Browse files

fix: ElectronBrowserContext::PartitionKey comparisons (#41083)

* 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: https://github.com/electron/electron/pull/36728/commits/3f1aea9af91e17b2605eb8a5837bbb81888d76da#r1099745359

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



Co-authored-by: default avatarCharles Kerr <charles@charleskerr.com>

* fixup! fix: ElectronBrowserContext::PartitionKey comparisons

Co-authored-by: default avatarCharles Kerr <charles@charleskerr.com>

---------

Co-authored-by: default avatartrop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: default avatarCharles Kerr <charles@charleskerr.com>
parent 42728caa
No related merge requests found
......@@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include "base/memory/raw_ptr.h"
......@@ -78,41 +79,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& partition, bool in_memory)
: location(partition),
in_memory(in_memory),
partition_type(KeyType::Partition) {}
PartitionKey(const std::string_view partition, bool in_memory)
: type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
explicit PartitionKey(const base::FilePath& file_path)
: location(file_path.AsUTF8Unsafe()),
in_memory(false),
partition_type(KeyType::FilePath) {}
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;
}
}
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;
}
}
: type_{Type::Path},
location_{file_path.AsUTF8Unsafe()},
in_memory_{false} {}
friend auto operator<=>(const PartitionKey&, const PartitionKey&) = default;
private:
enum class Type { Partition, Path };
Type type_;
std::string location_;
bool in_memory_;
};
using BrowserContextMap =
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment