feat: Allow usage of an absolute path for partitions used in a session (#37604)

* Allow an absolute path to be used for creating sessions

Allows an absolute path to be used for creating sessions
by adding the session.fromPath() API.

* Fixup! Clarify that an emptry string is not permitted as a parameter to fromPath()
This commit is contained in:
George Joseph 2023-03-20 14:34:49 +00:00 committed by GitHub
parent eb613ef3d4
commit e0c348a2f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 153 additions and 18 deletions

View file

@ -65,6 +65,9 @@ using DisplayMediaResponseCallbackJs =
using DisplayMediaRequestHandler =
base::RepeatingCallback<void(const content::MediaStreamRequest&,
DisplayMediaResponseCallbackJs)>;
using PartitionOrPath =
std::variant<std::reference_wrapper<const std::string>,
std::reference_wrapper<const base::FilePath>>;
class ElectronBrowserContext : public content::BrowserContext {
public:
@ -74,22 +77,43 @@ class ElectronBrowserContext : public content::BrowserContext {
// partition_id => browser_context
struct PartitionKey {
std::string partition;
enum class KeyType { Partition, FilePath };
std::string location;
bool in_memory;
KeyType partition_type;
PartitionKey(const std::string& partition, bool in_memory)
: partition(partition), in_memory(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) {}
bool operator<(const PartitionKey& other) const {
if (partition == other.partition)
return in_memory < other.in_memory;
return partition < other.partition;
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 {
return (partition == other.partition) && (in_memory == other.in_memory);
if (partition_type == KeyType::Partition) {
return (location == other.location) && (in_memory < other.in_memory);
} else {
if (location == other.location)
return true;
return false;
}
}
};
using BrowserContextMap =
std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>;
@ -100,6 +124,12 @@ class ElectronBrowserContext : public content::BrowserContext {
bool in_memory,
base::Value::Dict options = {});
// Get or create the BrowserContext using the |path|.
// The |options| will be passed to constructor when there is no
// existing BrowserContext.
static ElectronBrowserContext* FromPath(const base::FilePath& path,
base::Value::Dict options = {});
static BrowserContextMap& browser_context_map();
void SetUserAgent(const std::string& user_agent);
@ -190,10 +220,12 @@ class ElectronBrowserContext : public content::BrowserContext {
blink::PermissionType permissionType);
private:
ElectronBrowserContext(const std::string& partition,
ElectronBrowserContext(const PartitionOrPath partition_location,
bool in_memory,
base::Value::Dict options);
ElectronBrowserContext(base::FilePath partition, base::Value::Dict options);
static void DisplayMediaDeviceChosen(
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback,