Commit 6099f61f authored by Kevin Bailey's avatar Kevin Bailey Committed by Commit Bot

[omnibox] Cache stripped URLs of tabs for quick comparison

We currently re-strip each tab's URL prior to comparison with the
URL to be navigated to, in order to offer a tab switch instead.
This has grown to be non-trivially expensive. This CL caches the
stripped version of each tab's URL so that we at least don't have
to re-strip it (malloc, etc.)

Bug: 780835
Change-Id: Ifa56f289e20b66ed1979ed56b7d2211726b4a3e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1896508
Commit-Queue: Kevin Bailey <krb@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713067}
parent f00bd88d
......@@ -44,6 +44,7 @@
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "extensions/buildflags/buildflags.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
......@@ -392,7 +393,6 @@ void ChromeAutocompleteProviderClient::StartServiceWorker(
base::DoNothing());
}
// TODO(crbug.com/46623): Maintain a map of URL->WebContents for fast look-up.
bool ChromeAutocompleteProviderClient::IsTabOpenWithURL(
const GURL& url,
const AutocompleteInput* input) {
......@@ -413,8 +413,7 @@ bool ChromeAutocompleteProviderClient::IsTabOpenWithURL(
content::WebContents* web_contents =
browser->tab_strip_model()->GetWebContentsAt(i);
if (web_contents != active_tab &&
IsURLEqualToStrippedURL(web_contents->GetLastCommittedURL(),
stripped_url, *input))
IsStrippedURLEqualToWebContentsURL(stripped_url, web_contents))
return true;
}
}
......@@ -445,12 +444,59 @@ bool ChromeAutocompleteProviderClient::StrippedURLsAreEqual(
url2, *input, template_url_service, base::string16());
}
bool ChromeAutocompleteProviderClient::IsURLEqualToStrippedURL(
const GURL& url1,
const GURL& stripped_url2,
const AutocompleteInput& input) const {
const TemplateURLService* template_url_service = GetTemplateURLService();
return AutocompleteMatch::GURLToStrippedGURL(
url1, input, template_url_service, base::string16()) ==
stripped_url2;
class AutocompleteClientWebContentsUserData
: public content::WebContentsUserData<
AutocompleteClientWebContentsUserData> {
public:
~AutocompleteClientWebContentsUserData() override = default;
int GetLastCommittedEntryIndex() { return last_committed_entry_index_; }
const GURL& GetLastCommittedStrippedURL() {
return last_committed_stripped_url_;
}
void UpdateLastCommittedStrippedURL(
int last_committed_index,
const GURL& last_committed_url,
TemplateURLService* template_url_service) {
if (last_committed_url.is_valid()) {
last_committed_entry_index_ = last_committed_index;
// Use blank input since we will re-use this stripped URL with other
// inputs.
last_committed_stripped_url_ = AutocompleteMatch::GURLToStrippedGURL(
last_committed_url, AutocompleteInput(), template_url_service,
base::string16());
}
}
private:
explicit AutocompleteClientWebContentsUserData(
content::WebContents* contents);
friend class content::WebContentsUserData<
AutocompleteClientWebContentsUserData>;
int last_committed_entry_index_ = -1;
GURL last_committed_stripped_url_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
AutocompleteClientWebContentsUserData::AutocompleteClientWebContentsUserData(
content::WebContents*)
: content::WebContentsUserData<AutocompleteClientWebContentsUserData>() {}
WEB_CONTENTS_USER_DATA_KEY_IMPL(AutocompleteClientWebContentsUserData)
bool ChromeAutocompleteProviderClient::IsStrippedURLEqualToWebContentsURL(
const GURL& stripped_url,
content::WebContents* web_contents) {
AutocompleteClientWebContentsUserData::CreateForWebContents(web_contents);
AutocompleteClientWebContentsUserData* user_data =
AutocompleteClientWebContentsUserData::FromWebContents(web_contents);
DCHECK(user_data);
if (user_data->GetLastCommittedEntryIndex() !=
web_contents->GetController().GetLastCommittedEntryIndex()) {
user_data->UpdateLastCommittedStrippedURL(
web_contents->GetController().GetLastCommittedEntryIndex(),
web_contents->GetLastCommittedURL(), GetTemplateURLService());
}
return stripped_url == user_data->GetLastCommittedStrippedURL();
}
......@@ -13,6 +13,7 @@ class Profile;
namespace content {
class StoragePartition;
class WebContents;
}
namespace unified_consent {
......@@ -83,14 +84,13 @@ class ChromeAutocompleteProviderClient : public AutocompleteProviderClient {
const GURL& url2,
const AutocompleteInput* input) const;
private:
// Like StrippedURLsAreEqual(), but second URL is already stripped. The
// input corresponds to this second URL. This is a small optimization when
// comparing lots of URLs to a single one.
bool IsURLEqualToStrippedURL(const GURL& url1,
const GURL& stripped_url2,
const AutocompleteInput& input) const;
// Performs a comparison of |stripped_url| to the stripped last committed
// URL of |web_contents|, using the internal cache to avoid repeatedly
// re-stripping the URL.
bool IsStrippedURLEqualToWebContentsURL(const GURL& stripped_url,
content::WebContents* web_contents);
private:
Profile* profile_;
ChromeAutocompleteSchemeClassifier scheme_classifier_;
std::unique_ptr<OmniboxPedalProvider> pedal_provider_;
......
Markdown is supported
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