Skip to content

Commit

Permalink
Restore the extension info of a TemplateURL associated with an omnibo…
Browse files Browse the repository at this point in the history
…x extension after reload

This change is to fix a bug where the search provider for an omnibox extension is registered multiple times after reloading the web data.

Add a new method RestoreExtensionInfoIfNeeded() to TemplateURLServiceClient.
Remove DEPS under chrome/browser/search_engines which were used to prevent new dependencies from being added during the componentization effort.

BUG=392421
TEST=unit_tests

Review URL: https://codereview.chromium.org/401603002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285223 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hashimoto@chromium.org committed Jul 24, 2014
1 parent dd131d3 commit e7f47c6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions athena/main/url_search_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class AthenaTemplateURLServiceClient : public TemplateURLServiceClient {
TemplateURLID id,
const base::string16& term) OVERRIDE {}
virtual void AddKeywordGeneratedVisit(const GURL& url) OVERRIDE {}
virtual void RestoreExtensionInfoIfNecessary(
TemplateURL* template_url) OVERRIDE {}

DISALLOW_COPY_AND_ASSIGN(AthenaTemplateURLServiceClient);
};
Expand Down
4 changes: 0 additions & 4 deletions chrome/browser/search_engines/DEPS

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "extensions/common/constants.h"

ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(Profile* profile)
: profile_(profile),
Expand Down Expand Up @@ -64,6 +65,18 @@ void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) {
history::SOURCE_BROWSED, false);
}

void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
TemplateURL* template_url) {
const TemplateURLData& data = template_url->data();
GURL url(data.url());
if (url.SchemeIs(extensions::kExtensionScheme)) {
const std::string& extension_id = url.host();
template_url->set_extension_info(make_scoped_ptr(
new TemplateURL::AssociatedExtensionInfo(
TemplateURL::OMNIBOX_API_EXTENSION, extension_id)));
}
}

void ChromeTemplateURLServiceClient::Observe(
int type,
const content::NotificationSource& source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ChromeTemplateURLServiceClient : public TemplateURLServiceClient,
TemplateURLID id,
const base::string16& term) OVERRIDE;
virtual void AddKeywordGeneratedVisit(const GURL& url) OVERRIDE;
virtual void RestoreExtensionInfoIfNecessary(
TemplateURL* template_url) OVERRIDE;

// content::NotificationObserver:
virtual void Observe(int type,
Expand Down
19 changes: 19 additions & 0 deletions chrome/browser/search_engines/template_url_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,25 @@ TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) {
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_")));
}

TEST_F(TemplateURLServiceTest, RestoreOmniboxExtensionKeyword) {
test_util_->VerifyLoad();

// Register an omnibox keyword.
model()->RegisterOmniboxKeyword("test", "extension", "keyword",
"chrome-extension://test");
ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));

// Reload the data.
test_util_->ResetModel(true);

// Ensure the omnibox keyword is restored correctly.
TemplateURL* t_url =
model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
ASSERT_TRUE(t_url);
ASSERT_EQ(TemplateURL::OMNIBOX_API_EXTENSION, t_url->GetType());
EXPECT_EQ("test", t_url->GetExtensionId());
}

TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) {
Time now = Time::Now();
TimeDelta one_day = TimeDelta::FromDays(1);
Expand Down
6 changes: 6 additions & 0 deletions components/search_engines/template_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ class TemplateURL {
return contextual_search_url_ref_;
}

// This setter shouldn't be used except by TemplateURLService and
// TemplateURLServiceClient implementations.
void set_extension_info(scoped_ptr<AssociatedExtensionInfo> extension_info) {
extension_info_ = extension_info.Pass();
}

// Returns true if |url| supports replacement.
bool SupportsReplacement(const SearchTermsData& search_terms_data) const;

Expand Down
8 changes: 8 additions & 0 deletions components/search_engines/template_url_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,14 @@ void TemplateURLService::OnWebDataServiceRequestDone(
&new_resource_keyword_version,
&pre_sync_deletes_);

if (client_) {
// Restore extension info of loaded TemplateURLs.
for (size_t i = 0; i < template_urls.size(); ++i) {
DCHECK(!template_urls[i]->extension_info_);
client_->RestoreExtensionInfoIfNecessary(template_urls[i]);
}
}

KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());

PatchMissingSyncGUIDs(&template_urls);
Expand Down
4 changes: 4 additions & 0 deletions components/search_engines/template_url_service_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "components/search_engines/template_url_id.h"

class GURL;
class TemplateURL;
class TemplateURLService;

// This interface provides history related functionality required by
Expand All @@ -31,6 +32,9 @@ class TemplateURLServiceClient {

// Adds the given URL to history as a keyword generated visit.
virtual void AddKeywordGeneratedVisit(const GURL& url) = 0;

// Restores the extension info of a TemplateURL loaded from the database.
virtual void RestoreExtensionInfoIfNecessary(TemplateURL* template_url) = 0;
};

#endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_CLIENT_H_

0 comments on commit e7f47c6

Please sign in to comment.