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 @@ ...@@ -44,6 +44,7 @@
#include "content/public/browser/service_worker_context.h" #include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
...@@ -392,7 +393,6 @@ void ChromeAutocompleteProviderClient::StartServiceWorker( ...@@ -392,7 +393,6 @@ void ChromeAutocompleteProviderClient::StartServiceWorker(
base::DoNothing()); base::DoNothing());
} }
// TODO(crbug.com/46623): Maintain a map of URL->WebContents for fast look-up.
bool ChromeAutocompleteProviderClient::IsTabOpenWithURL( bool ChromeAutocompleteProviderClient::IsTabOpenWithURL(
const GURL& url, const GURL& url,
const AutocompleteInput* input) { const AutocompleteInput* input) {
...@@ -413,8 +413,7 @@ bool ChromeAutocompleteProviderClient::IsTabOpenWithURL( ...@@ -413,8 +413,7 @@ bool ChromeAutocompleteProviderClient::IsTabOpenWithURL(
content::WebContents* web_contents = content::WebContents* web_contents =
browser->tab_strip_model()->GetWebContentsAt(i); browser->tab_strip_model()->GetWebContentsAt(i);
if (web_contents != active_tab && if (web_contents != active_tab &&
IsURLEqualToStrippedURL(web_contents->GetLastCommittedURL(), IsStrippedURLEqualToWebContentsURL(stripped_url, web_contents))
stripped_url, *input))
return true; return true;
} }
} }
...@@ -445,12 +444,59 @@ bool ChromeAutocompleteProviderClient::StrippedURLsAreEqual( ...@@ -445,12 +444,59 @@ bool ChromeAutocompleteProviderClient::StrippedURLsAreEqual(
url2, *input, template_url_service, base::string16()); url2, *input, template_url_service, base::string16());
} }
bool ChromeAutocompleteProviderClient::IsURLEqualToStrippedURL( class AutocompleteClientWebContentsUserData
const GURL& url1, : public content::WebContentsUserData<
const GURL& stripped_url2, AutocompleteClientWebContentsUserData> {
const AutocompleteInput& input) const { public:
const TemplateURLService* template_url_service = GetTemplateURLService(); ~AutocompleteClientWebContentsUserData() override = default;
return AutocompleteMatch::GURLToStrippedGURL(
url1, input, template_url_service, base::string16()) == int GetLastCommittedEntryIndex() { return last_committed_entry_index_; }
stripped_url2; 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; ...@@ -13,6 +13,7 @@ class Profile;
namespace content { namespace content {
class StoragePartition; class StoragePartition;
class WebContents;
} }
namespace unified_consent { namespace unified_consent {
...@@ -83,14 +84,13 @@ class ChromeAutocompleteProviderClient : public AutocompleteProviderClient { ...@@ -83,14 +84,13 @@ class ChromeAutocompleteProviderClient : public AutocompleteProviderClient {
const GURL& url2, const GURL& url2,
const AutocompleteInput* input) const; const AutocompleteInput* input) const;
private: // Performs a comparison of |stripped_url| to the stripped last committed
// Like StrippedURLsAreEqual(), but second URL is already stripped. The // URL of |web_contents|, using the internal cache to avoid repeatedly
// input corresponds to this second URL. This is a small optimization when // re-stripping the URL.
// comparing lots of URLs to a single one. bool IsStrippedURLEqualToWebContentsURL(const GURL& stripped_url,
bool IsURLEqualToStrippedURL(const GURL& url1, content::WebContents* web_contents);
const GURL& stripped_url2,
const AutocompleteInput& input) const;
private:
Profile* profile_; Profile* profile_;
ChromeAutocompleteSchemeClassifier scheme_classifier_; ChromeAutocompleteSchemeClassifier scheme_classifier_;
std::unique_ptr<OmniboxPedalProvider> pedal_provider_; 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