diff options
author | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-11 19:48:53 +0000 |
---|---|---|
committer | erikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-11 19:48:53 +0000 |
commit | c4b2af2d835cbe803b887023e55d41f44e01ee62 (patch) | |
tree | d788cff70f24f157d94169c3da824f2c9c799ec9 | |
parent | 28f1d82073ff16dab8d12d6714548fab07933b70 (diff) | |
download | chromium_src-c4b2af2d835cbe803b887023e55d41f44e01ee62.zip chromium_src-c4b2af2d835cbe803b887023e55d41f44e01ee62.tar.gz chromium_src-c4b2af2d835cbe803b887023e55d41f44e01ee62.tar.bz2 |
Handle TemplateURLService load failure better, and make some test correctness fixes that will be needed later.
This also does a variety of miscellaneous cleanups to the modified files.
BUG=364183
TEST=none
R=jered@chromium.org, pkasting@chromium.org
TBR=engedy
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=269329
Review URL: https://codereview.chromium.org/272573004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269716 0039d316-1c4b-4281-b951-d872f2087c98
18 files changed, 357 insertions, 339 deletions
diff --git a/chrome/browser/autocomplete/base_search_provider.cc b/chrome/browser/autocomplete/base_search_provider.cc index 4565b46..5a60ddc 100644 --- a/chrome/browser/autocomplete/base_search_provider.cc +++ b/chrome/browser/autocomplete/base_search_provider.cc @@ -34,6 +34,7 @@ #include "net/base/net_util.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "net/http/http_response_headers.h" +#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher_delegate.h" #include "url/gurl.h" @@ -97,10 +98,10 @@ SuggestionDeletionHandler::SuggestionDeletionHandler( this)); deletion_fetcher_->SetRequestContext(profile->GetRequestContext()); deletion_fetcher_->Start(); -}; +} SuggestionDeletionHandler::~SuggestionDeletionHandler() { -}; +} void SuggestionDeletionHandler::OnURLFetchComplete( const net::URLFetcher* source) { @@ -108,7 +109,7 @@ void SuggestionDeletionHandler::OnURLFetchComplete( callback_.Run( source->GetStatus().is_success() && (source->GetResponseCode() == 200), this); -}; +} // BaseSearchProvider --------------------------------------------------------- diff --git a/chrome/browser/profile_resetter/profile_resetter.cc b/chrome/browser/profile_resetter/profile_resetter.cc index 9bc7dc9..3cfc57c 100644 --- a/chrome/browser/profile_resetter/profile_resetter.cc +++ b/chrome/browser/profile_resetter/profile_resetter.cc @@ -170,7 +170,7 @@ void ProfileResetter::ResetDefaultSearchEngine() { const TemplateURL* default_search_provider = template_url_service_->GetDefaultSearchProvider(); if (default_search_provider && - default_search_provider->url_ref().HasGoogleBaseURLs()) + default_search_provider->HasGoogleBaseURLs()) GoogleURLTracker::RequestServerCheck(profile_, true); MarkAsDone(DEFAULT_SEARCH_ENGINE); diff --git a/chrome/browser/search/instant_service.cc b/chrome/browser/search/instant_service.cc index 8eb856b..4deb2cb 100644 --- a/chrome/browser/search/instant_service.cc +++ b/chrome/browser/search/instant_service.cc @@ -4,51 +4,39 @@ #include "chrome/browser/search/instant_service.h" -#include <vector> - -#include "base/logging.h" -#include "base/prefs/pref_service.h" -#include "base/strings/string_number_conversions.h" #include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/history/history_notifications.h" #include "chrome/browser/history/most_visited_tiles_experiment.h" #include "chrome/browser/history/top_sites.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/instant_io_context.h" -#include "chrome/browser/search/instant_service_factory.h" #include "chrome/browser/search/instant_service_observer.h" #include "chrome/browser/search/local_ntp_source.h" #include "chrome/browser/search/most_visited_iframe_source.h" #include "chrome/browser/search/search.h" #include "chrome/browser/search/suggestions/suggestions_source.h" -#include "chrome/browser/search_engines/template_url.h" +#include "chrome/browser/search_engines/search_terms_data.h" #include "chrome/browser/search_engines/template_url_service.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/themes/theme_properties.h" #include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/thumbnails/thumbnail_list_source.h" +#include "chrome/browser/ui/search/instant_search_prerenderer.h" #include "chrome/browser/ui/webui/favicon_source.h" #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" #include "chrome/browser/ui/webui/theme_source.h" -#include "chrome/common/pref_names.h" #include "chrome/common/render_messages.h" #include "content/public/browser/browser_thread.h" -#include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" -#include "content/public/browser/notification_source.h" #include "content/public/browser/notification_types.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/url_data_source.h" #include "grit/theme_resources.h" -#include "net/base/net_util.h" -#include "net/url_request/url_request.h" +#include "third_party/skia/include/core/SkColor.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/image/image_skia.h" #include "ui/gfx/sys_color_change_listener.h" -#include "url/gurl.h" -using content::BrowserThread; namespace { @@ -68,12 +56,32 @@ RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { InstantService::InstantService(Profile* profile) : profile_(profile), + template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), omnibox_start_margin_(chrome::kDisableStartMargin), weak_ptr_factory_(this) { - // Stub for unit tests. - if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) + // The initialization below depends on a typical set of browser threads. Skip + // it if we are running in a unit test without the full suite. + if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) return; + // This depends on the existence of the typical browser threads. Therefore it + // is only instantiated here (after the check for a UI thread above). + instant_io_context_ = new InstantIOContext(); + + previous_google_base_url_ = + GURL(UIThreadSearchTermsData(profile).GoogleBaseURLValue()); + + // TemplateURLService is NULL by default in tests. + if (template_url_service_) { + template_url_service_->AddObserver(this); + const TemplateURL* default_search_provider = + template_url_service_->GetDefaultSearchProvider(); + if (default_search_provider) { + previous_default_search_provider_.reset( + new TemplateURLData(default_search_provider->data())); + } + } + ResetInstantSearchPrerenderer(); registrar_.Add(this, @@ -89,11 +97,10 @@ InstantService::InstantService(Profile* profile) chrome::NOTIFICATION_TOP_SITES_CHANGED, content::Source<history::TopSites>(top_sites)); } - instant_io_context_ = new InstantIOContext(); if (profile_ && profile_->GetResourceContext()) { - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, base::Bind(&InstantIOContext::SetUserDataOnIO, profile->GetResourceContext(), instant_io_context_)); } @@ -122,29 +129,21 @@ InstantService::InstantService(Profile* profile) content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); content::URLDataSource::Add( profile_, new suggestions::SuggestionsSource(profile_)); - - profile_pref_registrar_.Init(profile_->GetPrefs()); - profile_pref_registrar_.Add( - prefs::kDefaultSearchProviderID, - base::Bind(&InstantService::OnDefaultSearchProviderChanged, - base::Unretained(this))); - - registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, - content::Source<Profile>(profile_->GetOriginalProfile())); } InstantService::~InstantService() { + if (template_url_service_) + template_url_service_->RemoveObserver(this); } void InstantService::AddInstantProcess(int process_id) { process_ids_.insert(process_id); if (instant_io_context_.get()) { - BrowserThread::PostTask(BrowserThread::IO, - FROM_HERE, - base::Bind(&InstantIOContext::AddInstantProcessOnIO, - instant_io_context_, - process_id)); + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, + base::Bind(&InstantIOContext::AddInstantProcessOnIO, + instant_io_context_, process_id)); } } @@ -201,9 +200,8 @@ void InstantService::Shutdown() { process_ids_.clear(); if (instant_io_context_.get()) { - BrowserThread::PostTask( - BrowserThread::IO, - FROM_HERE, + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, instant_io_context_)); } @@ -237,12 +235,6 @@ void InstantService::Observe(int type, break; } #endif // defined(ENABLE_THEMES) - case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: { - OnGoogleURLUpdated( - content::Source<Profile>(source).ptr(), - content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr()); - break; - } default: NOTREACHED() << "Unexpected notification type in InstantService."; } @@ -263,12 +255,10 @@ void InstantService::OnRendererProcessTerminated(int process_id) { process_ids_.erase(process_id); if (instant_io_context_.get()) { - BrowserThread::PostTask( - BrowserThread::IO, - FROM_HERE, + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, - instant_io_context_, - process_id)); + instant_io_context_, process_id)); } } @@ -403,47 +393,33 @@ void InstantService::OnThemeChanged(ThemeService* theme_service) { ThemeInfoChanged(*theme_info_)); } -void InstantService::OnGoogleURLUpdated( - Profile* profile, - GoogleURLTracker::UpdatedDetails* details) { - GURL last_prompted_url( - profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); - - // See GoogleURLTracker::OnURLFetchComplete(). - // last_prompted_url.is_empty() indicates very first run of Chrome. So there - // is no need to notify, as there won't be any old state. - if (last_prompted_url.is_empty()) - return; - - ResetInstantSearchPrerenderer(); - - // Only the scheme changed. Ignore it since we do not prompt the user in this - // case. - if (net::StripWWWFromHost(details->first) == - net::StripWWWFromHost(details->second)) - return; - - FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); -} - -void InstantService::OnDefaultSearchProviderChanged( - const std::string& pref_name) { - DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); - const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( - profile_)->GetDefaultSearchProvider(); - if (!template_url) { - // A NULL |template_url| could mean either this notification is sent during - // the browser start up operation or the user now has no default search - // provider. There is no way for the user to reach this state using the - // Chrome settings. Only explicitly poking at the DB or bugs in the Sync - // could cause that, neither of which we support. - return; +void InstantService::OnTemplateURLServiceChanged() { + // Check whether the default search provider was changed. + const TemplateURL* template_url = + template_url_service_->GetDefaultSearchProvider(); + bool default_search_provider_changed = !TemplateURL::MatchesData( + template_url, previous_default_search_provider_.get()); + if (default_search_provider_changed) { + previous_default_search_provider_.reset( + template_url ? new TemplateURLData(template_url->data()) : NULL); } - ResetInstantSearchPrerenderer(); + // Note that, even if the TemplateURL for the Default Search Provider has not + // changed, the effective URLs might change if they reference the Google base + // URL. The TemplateURLService will notify us when the effective URL changes + // in this way but it's up to us to do the work to check both. + GURL google_base_url(UIThreadSearchTermsData(profile_).GoogleBaseURLValue()); + if (google_base_url != previous_google_base_url_) { + previous_google_base_url_ = google_base_url; + if (template_url && template_url->HasGoogleBaseURLs()) + default_search_provider_changed = true; + } - FOR_EACH_OBSERVER( - InstantServiceObserver, observers_, DefaultSearchProviderChanged()); + if (default_search_provider_changed) { + ResetInstantSearchPrerenderer(); + FOR_EACH_OBSERVER(InstantServiceObserver, observers_, + DefaultSearchProviderChanged()); + } } void InstantService::ResetInstantSearchPrerenderer() { @@ -451,8 +427,6 @@ void InstantService::ResetInstantSearchPrerenderer() { return; GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); - if (url.is_valid()) - instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); - else - instant_prerenderer_.reset(); + instant_prerenderer_.reset( + url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); } diff --git a/chrome/browser/search/instant_service.h b/chrome/browser/search/instant_service.h index 393d072..02dc7c7 100644 --- a/chrome/browser/search/instant_service.h +++ b/chrome/browser/search/instant_service.h @@ -5,44 +5,37 @@ #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ -#include <map> #include <set> -#include <string> #include <vector> -#include "base/basictypes.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" +#include "base/gtest_prod_util.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" -#include "base/prefs/pref_change_registrar.h" -#include "chrome/browser/google/google_url_tracker.h" #include "chrome/browser/history/history_types.h" -#include "chrome/browser/ui/search/instant_search_prerenderer.h" -#include "chrome/common/instant_types.h" +#include "chrome/browser/search_engines/template_url_service_observer.h" #include "components/keyed_service/core/keyed_service.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" +#include "url/gurl.h" -class GURL; class InstantIOContext; +struct InstantMostVisitedItem; +class InstantSearchPrerenderer; class InstantServiceObserver; -class InstantTestBase; -class InstantServiceTest; class Profile; +struct TemplateURLData; +class TemplateURLService; +struct ThemeBackgroundInfo; class ThemeService; namespace content { class RenderProcessHost; } -namespace net { -class URLRequest; -} - // Tracks render process host IDs that are associated with Instant. class InstantService : public KeyedService, - public content::NotificationObserver { + public content::NotificationObserver, + public TemplateURLServiceObserver { public: explicit InstantService(Profile* profile); virtual ~InstantService(); @@ -108,16 +101,23 @@ class InstantService : public KeyedService, FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, MANUAL_SearchesFromFakebox); FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); - FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer); + FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest, + SendsSearchURLsToRenderer); - // Overridden from KeyedService: + // KeyedService: virtual void Shutdown() OVERRIDE; - // Overridden from content::NotificationObserver: + // content::NotificationObserver: virtual void Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; + // TemplateURLServiceObserver: + // Caches the previous value of the Default Search Provider and the Google + // base URL to filter out changes other than those affecting the Default + // Search Provider. + virtual void OnTemplateURLServiceChanged() OVERRIDE; + // Called when a renderer process is terminated. void OnRendererProcessTerminated(int process_id); @@ -132,15 +132,14 @@ class InstantService : public KeyedService, // Theme changed notification handler. void OnThemeChanged(ThemeService* theme_service); - void OnGoogleURLUpdated(Profile* profile, - GoogleURLTracker::UpdatedDetails* details); - - void OnDefaultSearchProviderChanged(const std::string& pref_name); - void ResetInstantSearchPrerenderer(); Profile* const profile_; + // The TemplateURLService that we are observing. It will outlive this + // InstantService due to the dependency declared in InstantServiceFactory. + TemplateURLService* template_url_service_; + // The process ids associated with Instant processes. std::set<int> process_ids_; @@ -158,8 +157,6 @@ class InstantService : public KeyedService, content::NotificationRegistrar registrar_; - PrefChangeRegistrar profile_pref_registrar_; - scoped_refptr<InstantIOContext> instant_io_context_; // Set to NULL if the default search provider does not support Instant. @@ -168,6 +165,11 @@ class InstantService : public KeyedService, // Used for Top Sites async retrieval. base::WeakPtrFactory<InstantService> weak_ptr_factory_; + // Used to check whether notifications from TemplateURLService indicate a + // change that affects the default search provider. + scoped_ptr<TemplateURLData> previous_default_search_provider_; + GURL previous_google_base_url_; + DISALLOW_COPY_AND_ASSIGN(InstantService); }; diff --git a/chrome/browser/search/instant_service_observer.cc b/chrome/browser/search/instant_service_observer.cc index efbd8f5..bda8ff0 100644 --- a/chrome/browser/search/instant_service_observer.cc +++ b/chrome/browser/search/instant_service_observer.cc @@ -14,9 +14,6 @@ void InstantServiceObserver::MostVisitedItemsChanged( void InstantServiceObserver::DefaultSearchProviderChanged() { } -void InstantServiceObserver::GoogleURLUpdated() { -} - void InstantServiceObserver::OmniboxStartMarginChanged( int omnibox_start_margin) { } diff --git a/chrome/browser/search/instant_service_observer.h b/chrome/browser/search/instant_service_observer.h index 248b689..68b4c97 100644 --- a/chrome/browser/search/instant_service_observer.h +++ b/chrome/browser/search/instant_service_observer.h @@ -23,11 +23,6 @@ class InstantServiceObserver { // Indicates that the default search provider changed. virtual void DefaultSearchProviderChanged(); - // Indicates that the Google URL has changed as a result of searchdomaincheck. - // Note that the search domain change triggers a yellow infobar at the top of - // the page, and the actual change is triggered after the user accepts. - virtual void GoogleURLUpdated(); - // Indicates that the omnibox start margin has changed. virtual void OmniboxStartMarginChanged(int omnibox_start_margin); diff --git a/chrome/browser/search/instant_service_unittest.cc b/chrome/browser/search/instant_service_unittest.cc index 0e9135c..760bb58 100644 --- a/chrome/browser/search/instant_service_unittest.cc +++ b/chrome/browser/search/instant_service_unittest.cc @@ -29,7 +29,6 @@ class MockInstantServiceObserver : public InstantServiceObserver { public: MOCK_METHOD0(DefaultSearchProviderChanged, void()); - MOCK_METHOD0(GoogleURLUpdated, void()); MOCK_METHOD1(OmniboxStartMarginChanged, void(int)); }; @@ -58,24 +57,45 @@ class InstantServiceTest : public InstantUnitTestBase { scoped_ptr<MockInstantServiceObserver> instant_service_observer_; }; -TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) { +class InstantServiceEnabledTest : public InstantServiceTest { + protected: + virtual void SetUp() OVERRIDE { + ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( + "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); + InstantServiceTest::SetUp(); + } +}; + +TEST_F(InstantServiceEnabledTest, DispatchDefaultSearchProviderChanged) { EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) .Times(1); - const std::string& new_base_url = "https://bar.com/"; + const std::string new_base_url = "https://bar.com/"; SetUserSelectedDefaultSearchProvider(new_base_url); } +TEST_F(InstantServiceTest, DontDispatchGoogleURLUpdatedForNonGoogleURLs) { + EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) + .Times(1); + const std::string new_dsp_url = "https://bar.com/"; + SetUserSelectedDefaultSearchProvider(new_dsp_url); + testing::Mock::VerifyAndClearExpectations(instant_service_observer_.get()); + + EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) + .Times(0); + const std::string new_base_url = "https://www.google.es/"; + NotifyGoogleBaseURLUpdate(new_base_url); +} + TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) { - EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); + EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) + .Times(1); - const std::string& new_base_url = "https://www.google.es/"; + const std::string new_base_url = "https://www.google.es/"; NotifyGoogleBaseURLUpdate(new_base_url); } -TEST_F(InstantServiceTest, SendsSearchURLsToRenderer) { - ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", - "Group1 use_cacheable_ntp:1")); +TEST_F(InstantServiceEnabledTest, SendsSearchURLsToRenderer) { scoped_ptr<content::MockRenderProcessHost> rph( new content::MockRenderProcessHost(profile())); rph->sink().ClearMessages(); @@ -102,10 +122,8 @@ TEST_F(InstantServiceTest, InstantSearchDisabled) { GetInstantSearchPrerenderer()); } -TEST_F(InstantServiceTest, +TEST_F(InstantServiceEnabledTest, ResetInstantSearchPrerenderer_DefaultProviderChanged) { - ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( - "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) .Times(2); @@ -127,18 +145,15 @@ TEST_F(InstantServiceTest, GetInstantSearchPrerenderer()); } -TEST_F(InstantServiceTest, ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) { - ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( - "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); +TEST_F(InstantServiceEnabledTest, + ResetInstantSearchPrerenderer_GoogleBaseURLUpdated) { EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged()) .Times(1); - EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1); - SetUserSelectedDefaultSearchProvider("https://google.com/"); InstantSearchPrerenderer* old_prerenderer = GetInstantSearchPrerenderer(); - EXPECT_NE(static_cast<InstantSearchPrerenderer*>(NULL), old_prerenderer); + EXPECT_TRUE(old_prerenderer != NULL); - const std::string& new_base_url = "https://www.google.es/"; + const std::string new_base_url = "https://www.google.es/"; NotifyGoogleBaseURLUpdate(new_base_url); EXPECT_NE(old_prerenderer, GetInstantSearchPrerenderer()); } diff --git a/chrome/browser/search/instant_unittest_base.cc b/chrome/browser/search/instant_unittest_base.cc index 6291009..d164e4b 100644 --- a/chrome/browser/search/instant_unittest_base.cc +++ b/chrome/browser/search/instant_unittest_base.cc @@ -5,7 +5,7 @@ #include "chrome/browser/search/instant_unittest_base.h" #include <string> -#include "base/basictypes.h" +#include "base/strings/utf_string_conversions.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/google/google_url_tracker.h" #include "chrome/browser/profiles/profile.h" @@ -52,6 +52,7 @@ void InstantUnitTestBase::SetUpWithoutQueryExtraction() { void InstantUnitTestBase::SetUserSelectedDefaultSearchProvider( const std::string& base_url) { TemplateURLData data; + data.SetKeyword(base::UTF8ToUTF16(base_url)); data.SetURL(base_url + "url?bar={searchTerms}"); data.instant_url = base_url + "instant?{google:omniboxStartMarginParameter}{google:forceInstantResults}" @@ -87,11 +88,16 @@ bool InstantUnitTestBase::IsInstantServiceObserver( return instant_service_->observers_.HasObserver(observer); } +TestingProfile* InstantUnitTestBase::CreateProfile() { + TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); + TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( + profile, &TemplateURLServiceFactory::BuildInstanceFor); + return profile; +} + void InstantUnitTestBase::SetUpHelper() { BrowserWithTestWindowTest::SetUp(); - TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( - profile(), &TemplateURLServiceFactory::BuildInstanceFor); template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile()); ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_); diff --git a/chrome/browser/search/instant_unittest_base.h b/chrome/browser/search/instant_unittest_base.h index ec786e5..87b8e33 100644 --- a/chrome/browser/search/instant_unittest_base.h +++ b/chrome/browser/search/instant_unittest_base.h @@ -49,6 +49,9 @@ class InstantUnitTestBase : public BrowserWithTestWindowTest { scoped_ptr<base::FieldTrialList> field_trial_list_; private: + // BrowserWithTestWindowTest override: + virtual TestingProfile* CreateProfile() OVERRIDE; + void SetUpHelper(); }; diff --git a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc index 5a1fa35..1813670 100644 --- a/chrome/browser/search_engines/default_search_pref_migration_unittest.cc +++ b/chrome/browser/search_engines/default_search_pref_migration_unittest.cc @@ -64,7 +64,7 @@ TEST_F(DefaultSearchPrefMigrationTest, MigrateUserSelectedValue) { scoped_ptr<TemplateURL> t_url( CreateKeyword("name1", "key1", "http://foo1/{searchTerms}")); // Store a value in the legacy location. - TemplateURLService::SaveDefaultSearchProviderToPrefs( + test_util()->model()->SaveDefaultSearchProviderToPrefs( t_url.get(), test_util()->profile()->GetPrefs()); // Run the migration. @@ -87,7 +87,7 @@ TEST_F(DefaultSearchPrefMigrationTest, ModernValuePresent) { scoped_ptr<TemplateURL> t_url2( CreateKeyword("name2", "key2", "http://foo2/{searchTerms}")); // Store a value in the legacy location. - TemplateURLService::SaveDefaultSearchProviderToPrefs( + test_util()->model()->SaveDefaultSearchProviderToPrefs( t_url.get(), test_util()->profile()->GetPrefs()); // Store another value in the modern location. diff --git a/chrome/browser/search_engines/template_url.cc b/chrome/browser/search_engines/template_url.cc index 01a15df..4963eb2 100644 --- a/chrome/browser/search_engines/template_url.cc +++ b/chrome/browser/search_engines/template_url.cc @@ -1122,6 +1122,33 @@ GURL TemplateURL::GenerateFaviconURL(const GURL& url) { return url.ReplaceComponents(rep); } +// static +bool TemplateURL::MatchesData(const TemplateURL* t_url, + const TemplateURLData* data) { + if (!t_url || !data) + return !t_url && !data; + + return (t_url->short_name() == data->short_name) && + t_url->HasSameKeywordAs(*data) && + (t_url->url() == data->url()) && + (t_url->suggestions_url() == data->suggestions_url) && + (t_url->instant_url() == data->instant_url) && + (t_url->image_url() == data->image_url) && + (t_url->new_tab_url() == data->new_tab_url) && + (t_url->search_url_post_params() == data->search_url_post_params) && + (t_url->suggestions_url_post_params() == + data->suggestions_url_post_params) && + (t_url->instant_url_post_params() == data->instant_url_post_params) && + (t_url->image_url_post_params() == data->image_url_post_params) && + (t_url->favicon_url() == data->favicon_url) && + (t_url->safe_for_autoreplace() == data->safe_for_autoreplace) && + (t_url->show_in_default_list() == data->show_in_default_list) && + (t_url->input_encodings() == data->input_encodings) && + (t_url->alternate_urls() == data->alternate_urls) && + (t_url->search_terms_replacement_key() == + data->search_terms_replacement_key); +} + base::string16 TemplateURL::AdjustedShortNameForLocaleDirection() const { base::string16 bidi_safe_short_name = data_.short_name; base::i18n::AdjustStringForLocaleDirection(&bidi_safe_short_name); @@ -1142,6 +1169,14 @@ bool TemplateURL::SupportsReplacementUsingTermsData( return url_ref_.SupportsReplacementUsingTermsData(search_terms_data); } +bool TemplateURL::HasGoogleBaseURLs() const { + return url_ref_.HasGoogleBaseURLs() || + suggestions_url_ref_.HasGoogleBaseURLs() || + instant_url_ref_.HasGoogleBaseURLs() || + image_url_ref_.HasGoogleBaseURLs() || + new_tab_url_ref_.HasGoogleBaseURLs(); +} + bool TemplateURL::IsGoogleSearchURLWithReplaceableKeyword() const { return (GetType() == NORMAL) && url_ref_.HasGoogleBaseURLs() && google_util::IsGoogleHostname(base::UTF16ToUTF8(data_.keyword()), diff --git a/chrome/browser/search_engines/template_url.h b/chrome/browser/search_engines/template_url.h index 5d1542b..9d04355 100644 --- a/chrome/browser/search_engines/template_url.h +++ b/chrome/browser/search_engines/template_url.h @@ -567,6 +567,11 @@ class TemplateURL { // Generates a favicon URL from the specified url. static GURL GenerateFaviconURL(const GURL& url); + // Returns true if |t_url| and |data| are equal in all meaningful respects. + // Static to allow either or both params to be NULL. + static bool MatchesData(const TemplateURL* t_url, + const TemplateURLData* data); + Profile* profile() { return profile_; } const TemplateURLData& data() const { return data_; } @@ -645,6 +650,9 @@ class TemplateURL { bool SupportsReplacementUsingTermsData( const SearchTermsData& search_terms_data) const; + // Returns true if any URLRefs use Googe base URLs. + bool HasGoogleBaseURLs() const; + // Returns true if this TemplateURL uses Google base URLs and has a keyword // of "google.TLD". We use this to decide whether we can automatically // update the keyword to reflect the current Google base URL TLD. diff --git a/chrome/browser/search_engines/template_url_service.cc b/chrome/browser/search_engines/template_url_service.cc index 1042ddb..c259791 100644 --- a/chrome/browser/search_engines/template_url_service.cc +++ b/chrome/browser/search_engines/template_url_service.cc @@ -57,32 +57,6 @@ typedef TemplateURLService::SyncDataMap SyncDataMap; namespace { -bool TemplateURLMatchesData(const TemplateURL* url1, - const TemplateURLData* url2) { - if (!url1 || !url2) - return !url1 && !url2; - - return (url1->short_name() == url2->short_name) && - url1->HasSameKeywordAs(*url2) && - (url1->url() == url2->url()) && - (url1->suggestions_url() == url2->suggestions_url) && - (url1->instant_url() == url2->instant_url) && - (url1->image_url() == url2->image_url) && - (url1->new_tab_url() == url2->new_tab_url) && - (url1->search_url_post_params() == url2->search_url_post_params) && - (url1->suggestions_url_post_params() == - url2->suggestions_url_post_params) && - (url1->instant_url_post_params() == url2->instant_url_post_params) && - (url1->image_url_post_params() == url2->image_url_post_params) && - (url1->favicon_url() == url2->favicon_url) && - (url1->safe_for_autoreplace() == url2->safe_for_autoreplace) && - (url1->show_in_default_list() == url2->show_in_default_list) && - (url1->input_encodings() == url2->input_encodings) && - (url1->alternate_urls() == url2->alternate_urls) && - (url1->search_terms_replacement_key() == - url2->search_terms_replacement_key); -} - const char kFirstPotentialEngineHistogramName[] = "Search.FirstPotentialEngineCalled"; @@ -420,79 +394,6 @@ bool TemplateURLService::LoadDefaultSearchProviderFromPrefs( } // static -void TemplateURLService::SaveDefaultSearchProviderToPrefs( - const TemplateURL* t_url, PrefService* prefs) { - if (!prefs) - return; - - bool enabled = false; - std::string search_url; - std::string suggest_url; - std::string instant_url; - std::string image_url; - std::string new_tab_url; - std::string search_url_post_params; - std::string suggest_url_post_params; - std::string instant_url_post_params; - std::string image_url_post_params; - std::string icon_url; - std::string encodings; - std::string short_name; - std::string keyword; - std::string id_string; - std::string prepopulate_id; - base::ListValue alternate_urls; - std::string search_terms_replacement_key; - if (t_url) { - DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType()); - enabled = true; - search_url = t_url->url(); - suggest_url = t_url->suggestions_url(); - instant_url = t_url->instant_url(); - image_url = t_url->image_url(); - new_tab_url = t_url->new_tab_url(); - search_url_post_params = t_url->search_url_post_params(); - suggest_url_post_params = t_url->suggestions_url_post_params(); - instant_url_post_params = t_url->instant_url_post_params(); - image_url_post_params = t_url->image_url_post_params(); - GURL icon_gurl = t_url->favicon_url(); - if (!icon_gurl.is_empty()) - icon_url = icon_gurl.spec(); - encodings = JoinString(t_url->input_encodings(), ';'); - short_name = base::UTF16ToUTF8(t_url->short_name()); - keyword = base::UTF16ToUTF8(t_url->keyword()); - id_string = base::Int64ToString(t_url->id()); - prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); - for (size_t i = 0; i < t_url->alternate_urls().size(); ++i) - alternate_urls.AppendString(t_url->alternate_urls()[i]); - search_terms_replacement_key = t_url->search_terms_replacement_key(); - } - prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); - prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); - prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); - prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); - prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url); - prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url); - prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams, - search_url_post_params); - prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams, - suggest_url_post_params); - prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams, - instant_url_post_params); - prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams, - image_url_post_params); - prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); - prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); - prefs->SetString(prefs::kDefaultSearchProviderName, short_name); - prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); - prefs->SetString(prefs::kDefaultSearchProviderID, id_string); - prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); - prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); - prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, - search_terms_replacement_key); -} - -// static base::string16 TemplateURLService::GenerateKeyword(const GURL& url) { DCHECK(url.is_valid()); // Strip "www." off the front of the keyword; otherwise the keyword won't work @@ -570,6 +471,79 @@ GURL TemplateURLService::GenerateSearchURLUsingTermsData( search_terms_data, NULL)); } +void TemplateURLService::SaveDefaultSearchProviderToPrefs( + const TemplateURL* t_url, + PrefService* prefs) const { + if (!prefs || load_failed_) + return; + + bool enabled = false; + std::string search_url; + std::string suggest_url; + std::string instant_url; + std::string image_url; + std::string new_tab_url; + std::string search_url_post_params; + std::string suggest_url_post_params; + std::string instant_url_post_params; + std::string image_url_post_params; + std::string icon_url; + std::string encodings; + std::string short_name; + std::string keyword; + std::string id_string; + std::string prepopulate_id; + base::ListValue alternate_urls; + std::string search_terms_replacement_key; + if (t_url) { + DCHECK_EQ(TemplateURL::NORMAL, t_url->GetType()); + enabled = true; + search_url = t_url->url(); + suggest_url = t_url->suggestions_url(); + instant_url = t_url->instant_url(); + image_url = t_url->image_url(); + new_tab_url = t_url->new_tab_url(); + search_url_post_params = t_url->search_url_post_params(); + suggest_url_post_params = t_url->suggestions_url_post_params(); + instant_url_post_params = t_url->instant_url_post_params(); + image_url_post_params = t_url->image_url_post_params(); + GURL icon_gurl = t_url->favicon_url(); + if (!icon_gurl.is_empty()) + icon_url = icon_gurl.spec(); + encodings = JoinString(t_url->input_encodings(), ';'); + short_name = base::UTF16ToUTF8(t_url->short_name()); + keyword = base::UTF16ToUTF8(t_url->keyword()); + id_string = base::Int64ToString(t_url->id()); + prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); + for (size_t i = 0; i < t_url->alternate_urls().size(); ++i) + alternate_urls.AppendString(t_url->alternate_urls()[i]); + search_terms_replacement_key = t_url->search_terms_replacement_key(); + } + prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); + prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); + prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); + prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); + prefs->SetString(prefs::kDefaultSearchProviderImageURL, image_url); + prefs->SetString(prefs::kDefaultSearchProviderNewTabURL, new_tab_url); + prefs->SetString(prefs::kDefaultSearchProviderSearchURLPostParams, + search_url_post_params); + prefs->SetString(prefs::kDefaultSearchProviderSuggestURLPostParams, + suggest_url_post_params); + prefs->SetString(prefs::kDefaultSearchProviderInstantURLPostParams, + instant_url_post_params); + prefs->SetString(prefs::kDefaultSearchProviderImageURLPostParams, + image_url_post_params); + prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); + prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); + prefs->SetString(prefs::kDefaultSearchProviderName, short_name); + prefs->SetString(prefs::kDefaultSearchProviderKeyword, keyword); + prefs->SetString(prefs::kDefaultSearchProviderID, id_string); + prefs->SetString(prefs::kDefaultSearchProviderPrepopulateID, prepopulate_id); + prefs->Set(prefs::kDefaultSearchProviderAlternateURLs, alternate_urls); + prefs->SetString(prefs::kDefaultSearchProviderSearchTermsReplacementKey, + search_terms_replacement_key); +} + bool TemplateURLService::CanReplaceKeyword( const base::string16& keyword, const GURL& url, @@ -812,31 +786,15 @@ void TemplateURLService::IncrementUsageCount(TemplateURL* url) { return; ++url->data_.usage_count; - if (service_.get()) - service_.get()->UpdateKeyword(url->data()); + if (service_) + service_->UpdateKeyword(url->data()); } void TemplateURLService::ResetTemplateURL(TemplateURL* url, const base::string16& title, const base::string16& keyword, const std::string& search_url) { - if (!loaded_) - return; - DCHECK(!keyword.empty()); - DCHECK(!search_url.empty()); - TemplateURLData data(url->data()); - data.short_name = title; - data.SetKeyword(keyword); - if (search_url != data.url()) { - data.SetURL(search_url); - // The urls have changed, reset the favicon url. - data.favicon_url = GURL(); - } - data.safe_for_autoreplace = false; - data.last_modified = time_provider_(); - TemplateURL new_url(url->profile(), data); - UIThreadSearchTermsData search_terms_data(url->profile()); - if (UpdateNoNotify(url, new_url, search_terms_data)) + if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) NotifyObservers(); } @@ -860,10 +818,8 @@ void TemplateURLService::SetUserSelectedDefaultSearchProvider( } TemplateURL* TemplateURLService::GetDefaultSearchProvider() { - if (loaded_ && !load_failed_) - return default_search_provider_; - // We're not loaded, rely on the default search provider stored in prefs. - return initial_default_search_provider_.get(); + return (loaded_ && !load_failed_) ? + default_search_provider_ : initial_default_search_provider_.get(); } bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( @@ -974,11 +930,10 @@ void TemplateURLService::Load() { if (loaded_ || load_handle_) return; - if (!service_.get()) { + if (!service_) service_ = WebDataService::FromBrowserContext(profile_); - } - if (service_.get()) { + if (service_) { load_handle_ = service_->GetKeywords(this); } else { ChangeToLoadedState(); @@ -1005,6 +960,7 @@ void TemplateURLService::OnWebDataServiceRequestDone( // Results are null if the database went away or (most likely) wasn't // loaded. load_failed_ = true; + service_ = NULL; ChangeToLoadedState(); on_loaded_callbacks_.Notify(); return; @@ -1282,6 +1238,15 @@ syncer::SyncMergeResult TemplateURLService::MergeDataAndStartSyncing( DCHECK(sync_processor.get()); DCHECK(sync_error_factory.get()); syncer::SyncMergeResult merge_result(type); + + // Disable sync if we failed to load. + if (load_failed_) { + merge_result.set_error(syncer::SyncError( + FROM_HERE, syncer::SyncError::DATATYPE_ERROR, + "Local database load failed.", syncer::SEARCH_ENGINES)); + return merge_result; + } + sync_processor_ = sync_processor.Pass(); sync_error_factory_ = sync_error_factory.Pass(); @@ -1671,8 +1636,8 @@ void TemplateURLService::Init(const Initializer* initializers, // Request a server check for the correct Google URL if Google is the // default search engine and not in headless mode. - if (profile_ && initial_default_search_provider_.get() && - initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { + if (profile_ && initial_default_search_provider_ && + initial_default_search_provider_->HasGoogleBaseURLs()) { scoped_ptr<base::Environment> env(base::Environment::Create()); if (!env->HasVar(env_vars::kHeadless)) GoogleURLTracker::RequestServerCheck(profile_, false); @@ -1887,7 +1852,7 @@ bool TemplateURLService::UpdateNoNotify( if (!existing_turl->sync_guid().empty()) guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; - if (service_.get()) + if (service_) service_->UpdateKeyword(existing_turl->data()); // Inform sync of the update. @@ -1990,8 +1955,7 @@ void TemplateURLService::GoogleBaseURLChanged(const GURL& old_base_url) { for (TemplateURLVector::iterator i(template_urls_.begin()); i != template_urls_.end(); ++i) { TemplateURL* t_url = *i; - if (t_url->url_ref().HasGoogleBaseURLs() || - t_url->suggestions_url_ref().HasGoogleBaseURLs()) { + if (t_url->HasGoogleBaseURLs()) { TemplateURL updated_turl(t_url->profile(), t_url->data()); updated_turl.ResetKeywordIfNecessary(false); KeywordToTemplateMap::const_iterator existing_entry = @@ -2060,13 +2024,12 @@ void TemplateURLService::UpdateDefaultSearch() { // The default was managed and remains managed. Update the default only // if it has changed; we don't want to respond to changes triggered by // SaveDefaultSearchProviderToPrefs. - if (TemplateURLMatchesData(default_search_provider_, - new_default_from_prefs.get())) + if (TemplateURL::MatchesData(default_search_provider_, + new_default_from_prefs.get())) return; if (!new_default_from_prefs) { - // default_search_provider_ can't be NULL otherwise - // TemplateURLMatchesData would have returned true. Remove this now - // invalid value. + // |default_search_provider_| can't be NULL or MatchesData() would have + // returned true. Remove this now invalid value. TemplateURL* old_default = default_search_provider_; bool success = SetDefaultSearchProviderNoNotify(NULL); DCHECK(success); @@ -2176,10 +2139,10 @@ bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL* url) { // Don't mark the url as edited, otherwise we won't be able to rev the // template urls we ship with. url->data_.show_in_default_list = true; - if (service_.get() && (url->GetType() == TemplateURL::NORMAL)) + if (service_ && (url->GetType() == TemplateURL::NORMAL)) service_->UpdateKeyword(url->data()); - if (url->url_ref().HasGoogleBaseURLs()) { + if (url->HasGoogleBaseURLs()) { GoogleURLTracker::RequestServerCheck(profile_, false); #if defined(ENABLE_RLZ) RLZTracker::RecordProductEvent(rlz_lib::CHROME, @@ -2211,7 +2174,7 @@ bool TemplateURLService::SetDefaultSearchProviderNoNotify(TemplateURL* url) { } } - if (service_.get()) + if (service_) service_->SetDefaultSearchProviderID(url ? url->id() : 0); // Inform sync the change to the show_in_default_list flag. @@ -2252,9 +2215,9 @@ bool TemplateURLService::AddNoNotify(TemplateURL* template_url, } else { base::string16 new_keyword = UniquifyKeyword(*existing_keyword_turl, false); - ResetTemplateURL(existing_keyword_turl, - existing_keyword_turl->short_name(), new_keyword, - existing_keyword_turl->url()); + ResetTemplateURLNoNotify(existing_keyword_turl, + existing_keyword_turl->short_name(), new_keyword, + existing_keyword_turl->url()); } } template_urls_.push_back(template_url); @@ -2263,7 +2226,7 @@ bool TemplateURLService::AddNoNotify(TemplateURL* template_url, if (newly_adding && (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { - if (service_.get()) + if (service_) service_->AddKeyword(template_url->data()); // Inform sync of the addition. Note that this will assign a GUID to @@ -2290,7 +2253,7 @@ void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { template_urls_.erase(i); if (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) { - if (service_.get()) + if (service_) service_->RemoveKeyword(template_url->id()); // Inform sync of the deletion. @@ -2315,6 +2278,30 @@ void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { delete template_url; } +bool TemplateURLService::ResetTemplateURLNoNotify( + TemplateURL* url, + const base::string16& title, + const base::string16& keyword, + const std::string& search_url) { + if (!loaded_) + return false; + DCHECK(!keyword.empty()); + DCHECK(!search_url.empty()); + TemplateURLData data(url->data()); + data.short_name = title; + data.SetKeyword(keyword); + if (search_url != data.url()) { + data.SetURL(search_url); + // The urls have changed, reset the favicon url. + data.favicon_url = GURL(); + } + data.safe_for_autoreplace = false; + data.last_modified = time_provider_(); + TemplateURL new_url(url->profile(), data); + UIThreadSearchTermsData search_terms_data(url->profile()); + return UpdateNoNotify(url, new_url, search_terms_data); +} + void TemplateURLService::NotifyObservers() { if (!loaded_) return; @@ -2343,7 +2330,7 @@ void TemplateURLService::RemoveProvidersCreatedByPolicy( if (template_url->created_by_policy()) { if (template_url == *default_search_provider && is_default_search_managed_ && - TemplateURLMatchesData(template_url, default_from_prefs)) { + TemplateURL::MatchesData(template_url, default_from_prefs)) { // If the database specified a default search provider that was set // by policy, and the default search provider from the preferences // is also set by policy and they are the same, keep the entry in the @@ -2360,7 +2347,7 @@ void TemplateURLService::RemoveProvidersCreatedByPolicy( *default_search_provider = NULL; i = template_urls->erase(i); - if (service_.get()) + if (service_) service_->RemoveKeyword(template_url->id()); delete template_url; } else { @@ -2584,7 +2571,7 @@ void TemplateURLService::PatchMissingSyncGUIDs( (template_url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)) { template_url->data_.sync_guid = base::GenerateGUID(); - if (service_.get()) + if (service_) service_->UpdateKeyword(template_url->data()); } } @@ -2613,8 +2600,8 @@ void TemplateURLService::AddTemplateURLsAndSetupDefaultEngine( if (is_default_search_managed_) { SetTemplateURLs(template_urls); - if (TemplateURLMatchesData(default_search_provider, - default_from_prefs.get())) { + if (TemplateURL::MatchesData(default_search_provider, + default_from_prefs.get())) { // The value from the preferences was previously stored in the database. // Reuse it. } else { diff --git a/chrome/browser/search_engines/template_url_service.h b/chrome/browser/search_engines/template_url_service.h index 1729323..a39e73f 100644 --- a/chrome/browser/search_engines/template_url_service.h +++ b/chrome/browser/search_engines/template_url_service.h @@ -112,11 +112,6 @@ class TemplateURLService : public WebDataServiceConsumer, scoped_ptr<TemplateURLData>* default_provider_data, bool* is_managed); - // Saves enough of url to |prefs| so that it can be loaded from preferences on - // start up. - static void SaveDefaultSearchProviderToPrefs(const TemplateURL* url, - PrefService* prefs); - // Generates a suitable keyword for the specified url, which must be valid. // This is guaranteed not to return an empty string, since TemplateURLs should // never have an empty keyword. @@ -140,6 +135,11 @@ class TemplateURLService : public WebDataServiceConsumer, const TemplateURL* t_url, const SearchTermsData& search_terms_data); + // Saves enough of url to |prefs| so that it can be loaded from preferences on + // start up. + void SaveDefaultSearchProviderToPrefs(const TemplateURL* url, + PrefService* prefs) const; + // Returns true if there is no TemplateURL that conflicts with the // keyword/url pair, or there is one but it can be replaced. If there is an // existing keyword that can be replaced and template_url_to_replace is @@ -409,16 +409,12 @@ class TemplateURLService : public WebDataServiceConsumer, DontUpdateKeywordSearchForNonReplaceable); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); - FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, - CreateSyncDataFromTemplateURL); - FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, - CreateTemplateURLFromSyncData); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, + IsLocalTemplateURLBetter); + FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes); - FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, - IsLocalTemplateURLBetter); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); friend class TemplateURLServiceTestUtilBase; @@ -568,6 +564,13 @@ class TemplateURLService : public WebDataServiceConsumer, // Caller is responsible for notifying observers. void RemoveNoNotify(TemplateURL* template_url); + // Like ResetTemplateURL(), but instead of notifying observers, returns + // whether anything has changed. + bool ResetTemplateURLNoNotify(TemplateURL* url, + const base::string16& title, + const base::string16& keyword, + const std::string& search_url); + // Notify the observers that the model has changed. This is done only if the // model is loaded. void NotifyObservers(); @@ -713,7 +716,9 @@ class TemplateURLService : public WebDataServiceConsumer, // Whether the keywords have been loaded. bool loaded_; - // Did loading fail? This is only valid if loaded_ is true. + // Set when the web data service fails to load properly. This prevents + // further communication with sync or writing to prefs, so we don't persist + // inconsistent state data anywhere. bool load_failed_; // If non-zero, we're waiting on a load. diff --git a/chrome/browser/search_engines/template_url_service_unittest.cc b/chrome/browser/search_engines/template_url_service_unittest.cc index d6ca738..5237b7e 100644 --- a/chrome/browser/search_engines/template_url_service_unittest.cc +++ b/chrome/browser/search_engines/template_url_service_unittest.cc @@ -500,7 +500,7 @@ TEST_F(TemplateURLServiceTest, AddSameKeyword) { data.safe_for_autoreplace = false; TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data); model()->Add(t_url2); - VerifyObserverCount(2); + VerifyObserverCount(1); EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc index d1d8e92..3e95e32 100644 --- a/chrome/browser/ui/browser_instant_controller.cc +++ b/chrome/browser/ui/browser_instant_controller.cc @@ -26,7 +26,8 @@ #include "content/public/browser/user_metrics.h" #include "content/public/browser/web_contents.h" -using base::UserMetricsAction; + +// Helpers -------------------------------------------------------------------- namespace { @@ -39,8 +40,8 @@ InstantSearchPrerenderer* GetInstantSearchPrerenderer(Profile* profile) { } // namespace -//////////////////////////////////////////////////////////////////////////////// -// BrowserInstantController, public: + +// BrowserInstantController --------------------------------------------------- BrowserInstantController::BrowserInstantController(Browser* browser) : browser_(browser), @@ -119,9 +120,6 @@ void BrowserInstantController::TabDeactivated(content::WebContents* contents) { prerenderer->Cancel(); } -//////////////////////////////////////////////////////////////////////////////// -// BrowserInstantController, SearchModelObserver implementation: - void BrowserInstantController::ModelChanged( const SearchModel::State& old_state, const SearchModel::State& new_state) { @@ -132,9 +130,9 @@ void BrowserInstantController::ModelChanged( // the full story, it's necessary to look at other UMA actions as well, // such as tab switches. if (new_mode.is_search_results()) - content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); + content::RecordAction(base::UserMetricsAction("InstantExtended.ShowSRP")); else if (new_mode.is_ntp()) - content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); + content::RecordAction(base::UserMetricsAction("InstantExtended.ShowNTP")); instant_.SearchModeChanged(old_state.mode, new_mode); } @@ -143,18 +141,7 @@ void BrowserInstantController::ModelChanged( instant_.InstantSupportChanged(new_state.instant_support); } -//////////////////////////////////////////////////////////////////////////////// -// BrowserInstantController, InstantServiceObserver implementation: - void BrowserInstantController::DefaultSearchProviderChanged() { - ReloadTabsInInstantProcess(); -} - -void BrowserInstantController::GoogleURLUpdated() { - ReloadTabsInInstantProcess(); -} - -void BrowserInstantController::ReloadTabsInInstantProcess() { InstantService* instant_service = InstantServiceFactory::GetForProfile(profile()); if (!instant_service) diff --git a/chrome/browser/ui/browser_instant_controller.h b/chrome/browser/ui/browser_instant_controller.h index d9c70ca..3953c69 100644 --- a/chrome/browser/ui/browser_instant_controller.h +++ b/chrome/browser/ui/browser_instant_controller.h @@ -48,17 +48,12 @@ class BrowserInstantController : public SearchModelObserver, void TabDeactivated(content::WebContents* contents); private: - // Overridden from search::SearchModelObserver: + // SearchModelObserver: virtual void ModelChanged(const SearchModel::State& old_state, const SearchModel::State& new_state) OVERRIDE; - // Overridden from InstantServiceObserver: + // InstantServiceObserver: virtual void DefaultSearchProviderChanged() OVERRIDE; - virtual void GoogleURLUpdated() OVERRIDE; - - // Reloads the tabs in instant process to ensure that their privileged status - // is still valid. - void ReloadTabsInInstantProcess(); // Replaces the contents at tab |index| with |new_contents| and deletes the // existing contents. diff --git a/chrome/browser/ui/browser_instant_controller_unittest.cc b/chrome/browser/ui/browser_instant_controller_unittest.cc index d93ef56..1a8c1e0 100644 --- a/chrome/browser/ui/browser_instant_controller_unittest.cc +++ b/chrome/browser/ui/browser_instant_controller_unittest.cc @@ -6,6 +6,7 @@ #include "base/basictypes.h" #include "base/memory/scoped_vector.h" +#include "base/metrics/field_trial.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/search/instant_service.h" #include "chrome/browser/search/instant_service_observer.h" @@ -25,6 +26,13 @@ namespace chrome { namespace { class BrowserInstantControllerTest : public InstantUnitTestBase { + public: + virtual void SetUp() OVERRIDE { + ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( + "EmbeddedSearch", "Group1 use_cacheable_ntp:1 prefetch_results:1")); + InstantUnitTestBase::SetUp(); + } + protected: friend class FakeWebContentsObserver; }; |