Skip to content

Commit

Permalink
Migrate our storage::LocalStorageImpl override to using StorageKey
Browse files Browse the repository at this point in the history
Methods from the storage.mojom.LocalStorageControl mojo interface
have migrated from url.mojom.Origin to blink.mojom.StorageKey, so
we need to adapt our implementation of storage::LocalStorageImpl
to account for that change.

Chromium change:

https://source.chromium.org/chromium/chromium/src/+/fecbcc550638152eddbb67adf1ea3530e741a39f

commit fecbcc550638152eddbb67adf1ea3530e741a39f
Author: Ari Chivukula <arichiv@chromium.org>
Date:   Thu Jul 29 20:34:07 2021 +0000

    Migrate LocalStorageControl to StorageKey

    This is part of a larger effort to migrate dom_storage to use
    storage_key in preparation for storage partitioning.

    Bug: 1212808
  • Loading branch information
mariospr committed Aug 4, 2021
1 parent b706736 commit 2cf2e6b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,33 @@ void LocalStorageImpl::ShutDown(base::OnceClosure callback) {
}

void LocalStorageImpl::BindStorageArea(
const url::Origin& origin,
const blink::StorageKey& storage_key,
mojo::PendingReceiver<blink::mojom::StorageArea> receiver) {
if (origin.opaque()) {
in_memory_local_storage_->BindStorageArea(*GetNonOpaqueOrigin(origin, true),
std::move(receiver));
if (storage_key.origin().opaque()) {
in_memory_local_storage_->BindStorageArea(
*GetStorageKeyWithNonOpaqueOrigin(storage_key, true),
std::move(receiver));
} else {
local_storage_->BindStorageArea(origin, std::move(receiver));
local_storage_->BindStorageArea(storage_key, std::move(receiver));
}
}

void LocalStorageImpl::GetUsage(GetUsageCallback callback) {
local_storage_->GetUsage(std::move(callback));
}

void LocalStorageImpl::DeleteStorage(const url::Origin& origin,
void LocalStorageImpl::DeleteStorage(const blink::StorageKey& storage_key,
DeleteStorageCallback callback) {
if (origin.opaque()) {
if (const auto* non_opaque_origin = GetNonOpaqueOrigin(origin, false)) {
in_memory_local_storage_->DeleteStorage(*non_opaque_origin,
const url::Origin& storage_key_origin = storage_key.origin();
if (storage_key_origin.opaque()) {
if (const auto* non_opaque_origins_storage_key =
GetStorageKeyWithNonOpaqueOrigin(storage_key, false)) {
in_memory_local_storage_->DeleteStorage(*non_opaque_origins_storage_key,
std::move(callback));
non_opaque_origins_.erase(origin);
storage_keys_with_non_opaque_origin_.erase(storage_key_origin);
}
} else {
local_storage_->DeleteStorage(origin, std::move(callback));
local_storage_->DeleteStorage(storage_key, std::move(callback));
}
}

Expand All @@ -86,26 +89,28 @@ void LocalStorageImpl::ForceKeepSessionState() {
local_storage_->ForceKeepSessionState();
}

const url::Origin* LocalStorageImpl::GetNonOpaqueOrigin(
const url::Origin& origin,
const blink::StorageKey* LocalStorageImpl::GetStorageKeyWithNonOpaqueOrigin(
const blink::StorageKey& storage_key,
bool create) {
DCHECK(origin.opaque());
auto non_opaque_origin_it = non_opaque_origins_.find(origin);
if (non_opaque_origin_it != non_opaque_origins_.end()) {
return &non_opaque_origin_it->second;
const url::Origin& storage_key_origin = storage_key.origin();
DCHECK(storage_key_origin.opaque());
auto storage_keys_it =
storage_keys_with_non_opaque_origin_.find(storage_key_origin);
if (storage_keys_it != storage_keys_with_non_opaque_origin_.end()) {
return &storage_keys_it->second;
}
if (!create) {
return nullptr;
}

const auto& origin_scheme_host_port =
origin.GetTupleOrPrecursorTupleIfOpaque();
auto emplaced_pair = non_opaque_origins_.emplace(
origin,
url::Origin::CreateFromNormalizedTuple(
origin_scheme_host_port.scheme(),
base::ToLowerASCII(base::UnguessableToken::Create().ToString()),
origin_scheme_host_port.port()));
storage_key_origin.GetTupleOrPrecursorTupleIfOpaque();
url::Origin non_opaque_origin = url::Origin::CreateFromNormalizedTuple(
origin_scheme_host_port.scheme(),
base::ToLowerASCII(base::UnguessableToken::Create().ToString()),
origin_scheme_host_port.port());
auto emplaced_pair = storage_keys_with_non_opaque_origin_.emplace(
storage_key_origin, blink::StorageKey(non_opaque_origin));
return &emplaced_pair.first->second;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class LocalStorageImpl : public mojom::LocalStorageControl {

// mojom::LocalStorageControl implementation:
void BindStorageArea(
const url::Origin& origin,
const blink::StorageKey& storage_key,
mojo::PendingReceiver<blink::mojom::StorageArea> receiver) override;
void GetUsage(GetUsageCallback callback) override;
void DeleteStorage(const url::Origin& origin,
void DeleteStorage(const blink::StorageKey& storage_key,
DeleteStorageCallback callback) override;
void CleanUpStorage(CleanUpStorageCallback callback) override;
void Flush(FlushCallback callback) override;
Expand All @@ -38,16 +38,18 @@ class LocalStorageImpl : public mojom::LocalStorageControl {
void ForceKeepSessionState() override;

private:
const url::Origin* GetNonOpaqueOrigin(const url::Origin& origin, bool create);
const blink::StorageKey* GetStorageKeyWithNonOpaqueOrigin(
const blink::StorageKey& storage_key,
bool create);

void ShutDownInMemoryStorage(base::OnceClosure callback);

std::unique_ptr<LocalStorageImpl_ChromiumImpl> local_storage_;
std::unique_ptr<LocalStorageImpl_ChromiumImpl> in_memory_local_storage_;
mojo::Receiver<mojom::LocalStorageControl> control_receiver_{this};
// LocalStorageImpl works only with non-opaque origins, that's why we use an
// opaque->non-opaque origin map.
std::map<url::Origin, url::Origin> non_opaque_origins_;
// map of opaque origins to StorageKeys with non-opaque origins.
std::map<url::Origin, blink::StorageKey> storage_keys_with_non_opaque_origin_;

base::WeakPtrFactory<LocalStorageImpl> weak_ptr_factory_{this};
};
Expand Down

0 comments on commit 2cf2e6b

Please sign in to comment.