0

[Code Health Rotation] Spanification of VisitedHashLink

This CL converts the const char* canonical_url and its size into a
std::string_view throughout the stack, from the blink/ layer all the
way up to chrome/ and components/.

This change should be a no-op, since it just changes the way data
is sent through the stack instead of making any logic changes.

More context on Spanification: https://docs.google.com/document/d/1rV6zYT5l5oUeCcF149eTjIG3E4ukecvoUQpiMtB3wk0/edit?resourcekey=0-sHZDaXR51dCRy-3XiTbiGg&tab=t.0

Bug: 1490484
Change-Id: Iaed5ae06bf4bb95202c015af5af14a4f397967ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5169058
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Nate Fischer <ntfschr@chromium.org>
Commit-Queue: Dibyajyoti Pal <dibyapal@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1244847}
This commit is contained in:
Dibyajyoti Pal
2024-01-09 20:36:34 +00:00
committed by Chromium LUCI CQ
parent 38780ad3c5
commit 0ddc941b4c
17 changed files with 64 additions and 53 deletions

@@ -5,6 +5,7 @@
#include "android_webview/renderer/aw_content_renderer_client.h" #include "android_webview/renderer/aw_content_renderer_client.h"
#include <memory> #include <memory>
#include <string_view>
#include <vector> #include <vector>
#include "android_webview/common/aw_switches.h" #include "android_webview/common/aw_switches.h"
@@ -208,9 +209,9 @@ void AwContentRendererClient::PrepareErrorPage(
android_system_error_page::PopulateErrorPageHtml(error, error_html); android_system_error_page::PopulateErrorPageHtml(error, error_html);
} }
uint64_t AwContentRendererClient::VisitedLinkHash(const char* canonical_url, uint64_t AwContentRendererClient::VisitedLinkHash(
size_t length) { std::string_view canonical_url) {
return visited_link_reader_->ComputeURLFingerprint(canonical_url, length); return visited_link_reader_->ComputeURLFingerprint(canonical_url);
} }
bool AwContentRendererClient::IsLinkVisited(uint64_t link_hash) { bool AwContentRendererClient::IsLinkVisited(uint64_t link_hash) {

@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include "android_webview/common/mojom/render_message_filter.mojom.h" #include "android_webview/common/mojom/render_message_filter.mojom.h"
#include "android_webview/renderer/aw_render_thread_observer.h" #include "android_webview/renderer/aw_render_thread_observer.h"
@@ -51,7 +52,7 @@ class AwContentRendererClient : public content::ContentRendererClient,
content::mojom::AlternativeErrorPageOverrideInfoPtr content::mojom::AlternativeErrorPageOverrideInfoPtr
alternative_error_page_info, alternative_error_page_info,
std::string* error_html) override; std::string* error_html) override;
uint64_t VisitedLinkHash(const char* canonical_url, size_t length) override; uint64_t VisitedLinkHash(std::string_view canonical_url) override;
bool IsLinkVisited(uint64_t link_hash) override; bool IsLinkVisited(uint64_t link_hash) override;
void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override; void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;
void GetSupportedKeySystems(media::GetSupportedKeySystemsCB cb) override; void GetSupportedKeySystems(media::GetSupportedKeySystemsCB cb) override;

@@ -6,6 +6,7 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string_view>
#include <utility> #include <utility>
#include "base/check_op.h" #include "base/check_op.h"
@@ -1476,10 +1477,10 @@ bool ChromeContentRendererClient::IsPrefetchOnly(
return prerender::NoStatePrefetchHelper::IsPrefetching(render_frame); return prerender::NoStatePrefetchHelper::IsPrefetching(render_frame);
} }
uint64_t ChromeContentRendererClient::VisitedLinkHash(const char* canonical_url, uint64_t ChromeContentRendererClient::VisitedLinkHash(
size_t length) { std::string_view canonical_url) {
return chrome_observer_->visited_link_reader()->ComputeURLFingerprint( return chrome_observer_->visited_link_reader()->ComputeURLFingerprint(
canonical_url, length); canonical_url);
} }
bool ChromeContentRendererClient::IsLinkVisited(uint64_t link_hash) { bool ChromeContentRendererClient::IsLinkVisited(uint64_t link_hash) {

@@ -11,6 +11,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
@@ -150,7 +151,7 @@ class ChromeContentRendererClient
const url::Origin* initiator_origin, const url::Origin* initiator_origin,
GURL* new_url) override; GURL* new_url) override;
bool IsPrefetchOnly(content::RenderFrame* render_frame) override; bool IsPrefetchOnly(content::RenderFrame* render_frame) override;
uint64_t VisitedLinkHash(const char* canonical_url, size_t length) override; uint64_t VisitedLinkHash(std::string_view canonical_url) override;
bool IsLinkVisited(uint64_t link_hash) override; bool IsLinkVisited(uint64_t link_hash) override;
std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking( std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking(
content::RenderFrame* render_frame) override; content::RenderFrame* render_frame) override;

@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <utility> #include <utility>
#include "base/functional/bind.h" #include "base/functional/bind.h"
@@ -44,7 +45,7 @@ class TestChromeContentRendererClient : public ChromeContentRendererClient {
~TestChromeContentRendererClient() override {} ~TestChromeContentRendererClient() override {}
// Since visited_link_reader_ in ChromeContentRenderClient never get // Since visited_link_reader_ in ChromeContentRenderClient never get
// initiated, overrides VisitedLinkedHash() function to prevent crashing. // initiated, overrides VisitedLinkedHash() function to prevent crashing.
uint64_t VisitedLinkHash(const char* canonical_url, size_t length) override { uint64_t VisitedLinkHash(std::string_view canonical_url) override {
return 0; return 0;
} }
}; };

@@ -5,6 +5,7 @@
#include "components/safe_browsing/content/renderer/phishing_classifier/phishing_dom_feature_extractor.h" #include "components/safe_browsing/content/renderer/phishing_classifier/phishing_dom_feature_extractor.h"
#include <memory> #include <memory>
#include <string_view>
#include <unordered_map> #include <unordered_map>
#include "base/functional/bind.h" #include "base/functional/bind.h"
@@ -132,7 +133,7 @@ class TestChromeContentRendererClient : public ChromeContentRendererClient {
~TestChromeContentRendererClient() override {} ~TestChromeContentRendererClient() override {}
// Since visited_link_reader_ in ChromeContentRenderClient never get // Since visited_link_reader_ in ChromeContentRenderClient never get
// initiated, overrides VisitedLinkedHash() function to prevent crashing. // initiated, overrides VisitedLinkedHash() function to prevent crashing.
uint64_t VisitedLinkHash(const char* canonical_url, size_t length) override { uint64_t VisitedLinkHash(std::string_view canonical_url) override {
return 0; return 0;
} }
}; };

@@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <string_view>
#include <utility> #include <utility>
#include "base/files/file_util.h" #include "base/files/file_util.h"
@@ -328,8 +329,7 @@ VisitedLinkWriter::Hash VisitedLinkWriter::TryToAddURL(const GURL& url) {
if (!url.is_valid()) if (!url.is_valid())
return null_hash_; // Don't add invalid URLs. return null_hash_; // Don't add invalid URLs.
Fingerprint fingerprint = Fingerprint fingerprint = ComputeURLFingerprint(url.spec(), salt_);
ComputeURLFingerprint(url.spec().data(), url.spec().size(), salt_);
// If the table isn't loaded the table will be rebuilt and after // If the table isn't loaded the table will be rebuilt and after
// that accumulated fingerprints will be applied to the table. // that accumulated fingerprints will be applied to the table.
if (table_builder_.get() || table_is_loading_from_file_) { if (table_builder_.get() || table_is_loading_from_file_) {
@@ -424,8 +424,7 @@ void VisitedLinkWriter::DeleteURLs(URLIterator* urls) {
if (!url.is_valid()) if (!url.is_valid())
continue; continue;
Fingerprint fingerprint = Fingerprint fingerprint = ComputeURLFingerprint(url.spec(), salt_);
ComputeURLFingerprint(url.spec().data(), url.spec().size(), salt_);
deleted_since_rebuild_.insert(fingerprint); deleted_since_rebuild_.insert(fingerprint);
// If the URL was just added and now we're deleting it, it may be in the // If the URL was just added and now we're deleting it, it may be in the
@@ -450,8 +449,7 @@ void VisitedLinkWriter::DeleteURLs(URLIterator* urls) {
const GURL& url(urls->NextURL()); const GURL& url(urls->NextURL());
if (!url.is_valid()) if (!url.is_valid())
continue; continue;
deleted_fingerprints.insert( deleted_fingerprints.insert(ComputeURLFingerprint(url.spec(), salt_));
ComputeURLFingerprint(url.spec().data(), url.spec().size(), salt_));
} }
DeleteFingerprintsFromCurrentTable(deleted_fingerprints); DeleteFingerprintsFromCurrentTable(deleted_fingerprints);
} }
@@ -760,16 +758,14 @@ void VisitedLinkWriter::OnTableLoadComplete(
// Also add anything that was added while we were asynchronously // Also add anything that was added while we were asynchronously
// loading the table. // loading the table.
for (const GURL& url : added_since_load_) { for (const GURL& url : added_since_load_) {
Fingerprint fingerprint = Fingerprint fingerprint = ComputeURLFingerprint(url.spec(), salt_);
ComputeURLFingerprint(url.spec().data(), url.spec().size(), salt_);
AddFingerprint(fingerprint, false); AddFingerprint(fingerprint, false);
} }
added_since_load_.clear(); added_since_load_.clear();
// Now handle deletions. // Now handle deletions.
for (const GURL& url : deleted_since_load_) { for (const GURL& url : deleted_since_load_) {
Fingerprint fingerprint = Fingerprint fingerprint = ComputeURLFingerprint(url.spec(), salt_);
ComputeURLFingerprint(url.spec().data(), url.spec().size(), salt_);
DeleteFingerprint(fingerprint, false); DeleteFingerprint(fingerprint, false);
} }
deleted_since_load_.clear(); deleted_since_load_.clear();
@@ -1160,8 +1156,8 @@ void VisitedLinkWriter::TableBuilder::DisownWriter() {
void VisitedLinkWriter::TableBuilder::OnURL(const GURL& url) { void VisitedLinkWriter::TableBuilder::OnURL(const GURL& url) {
if (!url.is_empty()) { if (!url.is_empty()) {
fingerprints_.push_back(VisitedLinkWriter::ComputeURLFingerprint( fingerprints_.push_back(
url.spec().data(), url.spec().length(), salt_)); VisitedLinkWriter::ComputeURLFingerprint(url.spec(), salt_));
} }
} }

@@ -7,6 +7,7 @@
#include <string.h> // for memset() #include <string.h> // for memset()
#include <ostream> #include <ostream>
#include <string_view>
#include "base/bit_cast.h" #include "base/bit_cast.h"
#include "base/check.h" #include "base/check.h"
@@ -27,17 +28,18 @@ VisitedLinkCommon::~VisitedLinkCommon() = default;
// FIXME: this uses linear probing, it should be replaced with quadratic // FIXME: this uses linear probing, it should be replaced with quadratic
// probing or something better. See VisitedLinkWriter::AddFingerprint // probing or something better. See VisitedLinkWriter::AddFingerprint
bool VisitedLinkCommon::IsVisited(const char* canonical_url, bool VisitedLinkCommon::IsVisited(std::string_view canonical_url) const {
size_t url_len) const { if (canonical_url.size() == 0) {
if (url_len == 0)
return false; return false;
if (!hash_table_ || table_length_ == 0) }
if (!hash_table_ || table_length_ == 0) {
return false; return false;
return IsVisited(ComputeURLFingerprint(canonical_url, url_len)); }
return IsVisited(ComputeURLFingerprint(canonical_url));
} }
bool VisitedLinkCommon::IsVisited(const GURL& url) const { bool VisitedLinkCommon::IsVisited(const GURL& url) const {
return IsVisited(url.spec().data(), url.spec().size()); return IsVisited(url.spec());
} }
bool VisitedLinkCommon::IsVisited(Fingerprint fingerprint) const { bool VisitedLinkCommon::IsVisited(Fingerprint fingerprint) const {
@@ -77,16 +79,15 @@ bool VisitedLinkCommon::IsVisited(Fingerprint fingerprint) const {
// static // static
VisitedLinkCommon::Fingerprint VisitedLinkCommon::ComputeURLFingerprint( VisitedLinkCommon::Fingerprint VisitedLinkCommon::ComputeURLFingerprint(
const char* canonical_url, std::string_view canonical_url,
size_t url_len,
const uint8_t salt[LINK_SALT_LENGTH]) { const uint8_t salt[LINK_SALT_LENGTH]) {
DCHECK(url_len > 0) << "Canonical URLs should not be empty"; DCHECK(canonical_url.size() > 0) << "Canonical URLs should not be empty";
base::MD5Context ctx; base::MD5Context ctx;
base::MD5Init(&ctx); base::MD5Init(&ctx);
base::MD5Update(&ctx, base::StringPiece(reinterpret_cast<const char*>(salt), base::MD5Update(&ctx, base::StringPiece(reinterpret_cast<const char*>(salt),
LINK_SALT_LENGTH)); LINK_SALT_LENGTH));
base::MD5Update(&ctx, base::StringPiece(canonical_url, url_len)); base::MD5Update(&ctx, canonical_url);
base::MD5Digest digest; base::MD5Digest digest;
base::MD5Final(&digest, &ctx); base::MD5Final(&digest, &ctx);

@@ -8,6 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string_view>
#include <vector> #include <vector>
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
@@ -66,15 +67,14 @@ class VisitedLinkCommon {
virtual ~VisitedLinkCommon(); virtual ~VisitedLinkCommon();
// Returns the fingerprint for the given URL. // Returns the fingerprint for the given URL.
Fingerprint ComputeURLFingerprint(const char* canonical_url, Fingerprint ComputeURLFingerprint(std::string_view canonical_url) const {
size_t url_len) const { return ComputeURLFingerprint(canonical_url, salt_);
return ComputeURLFingerprint(canonical_url, url_len, salt_);
} }
// Looks up the given key in the table. The fingerprint for the URL is // Looks up the given key in the table. The fingerprint for the URL is
// computed if you call one with the string argument. Returns true if found. // computed if you call one with the string argument. Returns true if found.
// Does not modify the hastable. // Does not modify the hastable.
bool IsVisited(const char* canonical_url, size_t url_len) const; bool IsVisited(const std::string_view canonical_url) const;
bool IsVisited(const GURL& url) const; bool IsVisited(const GURL& url) const;
bool IsVisited(Fingerprint fingerprint) const; bool IsVisited(Fingerprint fingerprint) const;
@@ -119,8 +119,7 @@ class VisitedLinkCommon {
// pass the salt as a parameter. See the non-static version above if you // pass the salt as a parameter. See the non-static version above if you
// want to use the current class' salt. // want to use the current class' salt.
static Fingerprint ComputeURLFingerprint( static Fingerprint ComputeURLFingerprint(
const char* canonical_url, std::string_view canonical_url,
size_t url_len,
const uint8_t salt[LINK_SALT_LENGTH]); const uint8_t salt[LINK_SALT_LENGTH]);
// Computes the hash value of the given fingerprint, this is used as a lookup // Computes the hash value of the given fingerprint, this is used as a lookup

@@ -9,6 +9,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include "base/notreached.h" #include "base/notreached.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
@@ -61,8 +62,8 @@ blink::WebSandboxSupport* PpapiBlinkPlatformImpl::GetSandboxSupport() {
#endif #endif
} }
uint64_t PpapiBlinkPlatformImpl::VisitedLinkHash(const char* canonical_url, uint64_t PpapiBlinkPlatformImpl::VisitedLinkHash(
size_t length) { std::string_view canonical_url) {
NOTREACHED(); NOTREACHED();
return 0; return 0;
} }

@@ -8,6 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <memory> #include <memory>
#include <string_view>
#include "build/build_config.h" #include "build/build_config.h"
#include "content/child/blink_platform_impl.h" #include "content/child/blink_platform_impl.h"
@@ -28,7 +29,7 @@ class PpapiBlinkPlatformImpl : public BlinkPlatformImpl {
// BlinkPlatformImpl methods: // BlinkPlatformImpl methods:
blink::WebSandboxSupport* GetSandboxSupport() override; blink::WebSandboxSupport* GetSandboxSupport() override;
uint64_t VisitedLinkHash(const char* canonical_url, size_t length) override; uint64_t VisitedLinkHash(std::string_view canonical_url) override;
bool IsLinkVisited(uint64_t link_hash) override; bool IsLinkVisited(uint64_t link_hash) override;
blink::WebString DefaultLocale() override; blink::WebString DefaultLocale() override;

@@ -4,6 +4,8 @@
#include "content/public/renderer/content_renderer_client.h" #include "content/public/renderer/content_renderer_client.h"
#include <string_view>
#include "base/command_line.h" #include "base/command_line.h"
#include "base/task/sequenced_task_runner.h" #include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h" #include "base/task/single_thread_task_runner.h"
@@ -135,8 +137,8 @@ bool ContentRendererClient::IsPrefetchOnly(RenderFrame* render_frame) {
return false; return false;
} }
uint64_t ContentRendererClient::VisitedLinkHash(const char* canonical_url, uint64_t ContentRendererClient::VisitedLinkHash(
size_t length) { std::string_view canonical_url) {
return 0; return 0;
} }

@@ -10,6 +10,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "base/files/file_path.h" #include "base/files/file_path.h"
@@ -260,7 +261,7 @@ class CONTENT_EXPORT ContentRendererClient {
virtual bool IsPrefetchOnly(RenderFrame* render_frame); virtual bool IsPrefetchOnly(RenderFrame* render_frame);
// See blink::Platform. // See blink::Platform.
virtual uint64_t VisitedLinkHash(const char* canonical_url, size_t length); virtual uint64_t VisitedLinkHash(std::string_view canonical_url);
virtual bool IsLinkVisited(uint64_t link_hash); virtual bool IsLinkVisited(uint64_t link_hash);
// Creates a WebPrescientNetworking instance for |render_frame|. The returned // Creates a WebPrescientNetworking instance for |render_frame|. The returned

@@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <string_view>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -279,9 +280,9 @@ bool RendererBlinkPlatformImpl::sandboxEnabled() {
switches::kSingleProcess); switches::kSingleProcess);
} }
uint64_t RendererBlinkPlatformImpl::VisitedLinkHash(const char* canonical_url, uint64_t RendererBlinkPlatformImpl::VisitedLinkHash(
size_t length) { std::string_view canonical_url) {
return GetContentClient()->renderer()->VisitedLinkHash(canonical_url, length); return GetContentClient()->renderer()->VisitedLinkHash(canonical_url);
} }
bool RendererBlinkPlatformImpl::IsLinkVisited(uint64_t link_hash) { bool RendererBlinkPlatformImpl::IsLinkVisited(uint64_t link_hash) {

@@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include "base/containers/id_map.h" #include "base/containers/id_map.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
@@ -78,7 +79,7 @@ class CONTENT_EXPORT RendererBlinkPlatformImpl : public BlinkPlatformImpl {
// blink::Platform implementation. // blink::Platform implementation.
blink::WebSandboxSupport* GetSandboxSupport() override; blink::WebSandboxSupport* GetSandboxSupport() override;
virtual bool sandboxEnabled(); virtual bool sandboxEnabled();
uint64_t VisitedLinkHash(const char* canonicalURL, size_t length) override; uint64_t VisitedLinkHash(std::string_view canonical_url) override;
bool IsLinkVisited(uint64_t linkHash) override; bool IsLinkVisited(uint64_t linkHash) override;
blink::WebString UserAgent() override; blink::WebString UserAgent() override;
blink::UserAgentMetadata UserAgentMetadata() override; blink::UserAgentMetadata UserAgentMetadata() override;

@@ -33,6 +33,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
@@ -223,9 +224,7 @@ class BLINK_PLATFORM_EXPORT Platform {
// Returns the hash for the given canonicalized URL for use in visited // Returns the hash for the given canonicalized URL for use in visited
// link coloring. // link coloring.
virtual uint64_t VisitedLinkHash(const char* canonical_url, size_t length) { virtual uint64_t VisitedLinkHash(std::string_view canonical_url) { return 0; }
return 0;
}
// Returns whether the given link hash is in the user's history. The // Returns whether the given link hash is in the user's history. The
// hash must have been generated by calling VisitedLinkHash(). // hash must have been generated by calling VisitedLinkHash().

@@ -30,10 +30,11 @@
#include "third_party/blink/renderer/platform/link_hash.h" #include "third_party/blink/renderer/platform/link_hash.h"
#include <string_view>
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h" #include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h" #include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "url/url_util.h" #include "url/url_util.h"
namespace blink { namespace blink {
@@ -62,7 +63,9 @@ LinkHash VisitedLinkHash(const KURL& base, const AtomicString& relative) {
url::RawCanonOutput<2048> buffer; url::RawCanonOutput<2048> buffer;
if (!ResolveRelative(base, relative.GetString(), &buffer)) if (!ResolveRelative(base, relative.GetString(), &buffer))
return 0; return 0;
return Platform::Current()->VisitedLinkHash(buffer.data(), buffer.length());
return Platform::Current()->VisitedLinkHash(
std::string_view(buffer.data(), buffer.length()));
} }
} // namespace blink } // namespace blink