Skip to content

Commit

Permalink
Demo dumping storage hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert committed Dec 16, 2024
1 parent 77da172 commit ace2a01
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
92 changes: 92 additions & 0 deletions content/shell/browser/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom-forward.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"

#include "content/public/browser/cookie_access_details.h"
#include "third_party/blink/public/mojom/frame/frame.mojom-shared.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/dom_storage_context.h"

namespace content {

namespace {
Expand Down Expand Up @@ -766,4 +771,91 @@ void Shell::TitleWasSet(NavigationEntry* entry) {
g_platform->SetTitle(this, entry->GetTitle());
}

void print_cookie_details(const CookieAccessDetails &details) {
LOG(WARNING) << "details.url: " << details.url.spec() <<
" details.first_party_url: " << details.first_party_url.spec() <<
" details.blocked_by_policy: " << details.blocked_by_policy;
}

void Shell::OnCookiesAccessed(RenderFrameHost* render_frame_host,
const CookieAccessDetails& details) {
LOG(WARNING) << "OnCookiesAccessed(RenderFrameHost): ";
print_cookie_details(details);
}
void Shell::OnCookiesAccessed(NavigationHandle* navigation_handle,
const CookieAccessDetails& details) {
LOG(WARNING) << "OnCookiesAccessed(NavigationHandle): ";
print_cookie_details(details);
}
void Shell::NotifyStorageAccessed(
RenderFrameHost* render_frame_host,
blink::mojom::StorageTypeAccessed storage_type,
bool blocked) {


switch(storage_type) {
case blink::mojom::StorageTypeAccessed::kLocalStorage:
LOG(WARNING) << "LocalStorage:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kSessionStorage:
LOG(WARNING) << "SessionStorage:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kIndexedDB:
LOG(WARNING) << "IndexedDB:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kFileSystem:
LOG(WARNING) << "FileSystem:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kWebLocks:
LOG(WARNING) << "WebLocks:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kDatabase:
LOG(WARNING) << "Database:" << " blocked: " << blocked;
break;
case blink::mojom::StorageTypeAccessed::kCacheStorage:
LOG(WARNING) << "CacheStorage:" << " blocked: " << blocked;
break;
}
}

void Shell::PrimaryMainDocumentElementAvailable() {
LOG(WARNING) << "Primary doc element created";

}

void Shell::DOMContentLoaded(RenderFrameHost* render_frame_host) {
LOG(WARNING) << "DOMContentLoaded";
}
void Shell::DidFinishLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url) {
LOG(WARNING) << "DidFinishLoad url:" << validated_url.spec();
}
void Shell::DidFailLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code) {
LOG(WARNING) << "DidFailLoad url:" << validated_url.spec() << " error_code:" << error_code;
}
void Shell::DocumentOnLoadCompletedInPrimaryMainFrame() {
LOG(WARNING) << "DocumentOnLoadCompletedInPrimaryMainFrame";
BrowserContext * browser_context = web_contents()->GetBrowserContext();

browser_context->ForEachLoadedStoragePartition(
[&](content::StoragePartition* partition) {
LOG(WARNING) << "partition: " << partition->GetPath().value();
[[maybe_unused]] DOMStorageContext * dom = partition->GetDOMStorageContext();
dom->GetLocalStorageUsage(base::BindOnce(&Shell::OnGotLocalStorageUsage, base::Unretained(this)));
});
}

void Shell::OnGotLocalStorageUsage(
const std::vector<content::StorageUsageInfo>& infos) {
LOG(WARNING) << "OnGotLocalStorageUsage";
// Note this isn't a list of key/value pairs of localStorage
// It's all storage "blobs" keyed by origin
for(auto info : infos) {
LOG(WARNING) << "key: " << info.storage_key.GetDebugString()
<< " total_size_bytes:" << info.total_size_bytes;
}
}

} // namespace content
24 changes: 24 additions & 0 deletions content/shell/browser/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/native_widget_types.h"

#include "content/public/browser/storage_usage_info.h"

class GURL;

namespace content {
Expand All @@ -48,6 +50,28 @@ class Shell : public WebContentsDelegate, public WebContentsObserver {
const std::string& data,
const GURL& base_url);

// Hackety sax
void OnCookiesAccessed(RenderFrameHost* render_frame_host,
const CookieAccessDetails& details) override;
void OnCookiesAccessed(NavigationHandle* navigation_handle,
const CookieAccessDetails& details) override;
void NotifyStorageAccessed(
RenderFrameHost* render_frame_host,
blink::mojom::StorageTypeAccessed storage_type,
bool blocked) override;
void PrimaryMainDocumentElementAvailable() override;

void DOMContentLoaded(RenderFrameHost* render_frame_host) override;
void DidFinishLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url) override;
void DidFailLoad(RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code) override;
void DocumentOnLoadCompletedInPrimaryMainFrame() override;
void OnGotLocalStorageUsage(
const std::vector<content::StorageUsageInfo>& infos);
// End hackety sax

#if BUILDFLAG(IS_ANDROID)
// Android-only path to allow loading long data strings.
void LoadDataAsStringWithBaseURL(const GURL& url,
Expand Down
51 changes: 51 additions & 0 deletions content/shell/browser/shell_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,55 @@ ShellContentBrowserClient::GetShellContentBrowserClientInstances() {
return GetShellContentBrowserClientInstancesImpl();
}

bool ShellContentBrowserClient::IsFullCookieAccessAllowed(
content::BrowserContext* browser_context,
content::WebContents* web_contents,
const GURL& url,
const blink::StorageKey& storage_key) {
LOG(WARNING) << "IsFullCookieAccessAllowed: " << url.spec() <<
" storagekey:" << storage_key.GetDebugString();
return true;
}
bool ShellContentBrowserClient::WillCreateRestrictedCookieManager(
network::mojom::RestrictedCookieManagerRole role,
BrowserContext* browser_context,
const url::Origin& origin,
const net::IsolationInfo& isolation_info,
bool is_service_worker,
int process_id,
int routing_id,
mojo::PendingReceiver<network::mojom::RestrictedCookieManager>* receiver) {
LOG(WARNING) << "Creating cookie manager for origin:" << origin.GetDebugString() <<
" is_service_worker:" << is_service_worker <<
" isolation:" << isolation_info.DebugString();
switch(role) {
case network::mojom::RestrictedCookieManagerRole::SCRIPT:
LOG(WARNING) << " Role: SCRIPT";
break;
case network::mojom::RestrictedCookieManagerRole::NETWORK:
LOG(WARNING) << " Role: NETWORK";
break;
}
return true;
}

StoragePartitionConfig ShellContentBrowserClient::GetStoragePartitionConfigForSite(
BrowserContext* browser_context,
const GURL& site) {
// Spammy, this gets called a lot
//LOG(WARNING) << "GetStoragePartitionConfigForSite: " << site.spec();

// Demo to mapp two sites to same storage partition
GURL::Replacements replacements;
replacements.ClearPort();
if (site.ReplaceComponents(replacements) == GURL("https://httpbin.org")) {
return StoragePartitionConfig::Create(browser_context,
/*partition_domain=*/"httpbin.org",
/*partition_name=*/"http_bin_test_partition",
/*in_memory=*/false);
}
return StoragePartitionConfig::CreateDefault(browser_context);
}


} // namespace content
20 changes: 20 additions & 0 deletions content/shell/browser/shell_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ class ShellContentBrowserClient : public ContentBrowserClient {
ShellContentBrowserClient();
~ShellContentBrowserClient() override;

// Hackety overrides
bool IsFullCookieAccessAllowed(
content::BrowserContext* browser_context,
content::WebContents* web_contents,
const GURL& url,
const blink::StorageKey& storage_key) override;
bool WillCreateRestrictedCookieManager(
network::mojom::RestrictedCookieManagerRole role,
BrowserContext* browser_context,
const url::Origin& origin,
const net::IsolationInfo& isolation_info,
bool is_service_worker,
int process_id,
int routing_id,
mojo::PendingReceiver<network::mojom::RestrictedCookieManager>* receiver) override;
StoragePartitionConfig GetStoragePartitionConfigForSite(
BrowserContext* browser_context,
const GURL& site) override;
// Hackety overrides

// The value supplied here is set when creating the NetworkContext.
// Specifically
// network::mojom::NetworkContext::allow_any_cors_exempt_header_for_browser.
Expand Down

0 comments on commit ace2a01

Please sign in to comment.