summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorskanuj@chromium.org <skanuj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-24 21:33:40 +0000
committerskanuj@chromium.org <skanuj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-24 21:33:40 +0000
commitc8a118e51bbdfb7a7a6e8e87e1926779edd0c4de (patch)
treef67e267d4a72c761e0bd355f3e263928682d9662
parent5ed562ab9243dc3a604aaad9c46f0102364e0098 (diff)
downloadchromium_src-c8a118e51bbdfb7a7a6e8e87e1926779edd0c4de.zip
chromium_src-c8a118e51bbdfb7a7a6e8e87e1926779edd0c4de.tar.gz
chromium_src-c8a118e51bbdfb7a7a6e8e87e1926779edd0c4de.tar.bz2
Reload Instant NTP and Instant-process tabs on search url change
. Add a default impl of instant_service_observer. . Move pref change listener for default search provider to instant_service and pass to browser_instant_controller and instant_ntp_prerenderer via instant_service_observer. . Add a search-domain-check listener to instant_service in a similar manner. . Replaced ReloadStaleNTP, PreloadInstantNTP etc with ReloadInstantNTP . Introduce InstantUnitTestBase - InstantServiceTest and BrowserInstantControllerTest are subclasses. . InstantService Unit Test - Use a mock observer to verify the search provider change and google url update is triggered on observers. - Verify the prerendered NTP is reloaded. . BrowserInstantController Unit Test - Moved the tab reload test from interactive ui test to BrowserInstantControllerTest. R=jered@chromium.org, samarth@chromium.org TBR=sky@chromium.org BUG=254206 Review URL: https://chromiumcodereview.appspot.com/20388003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225063 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/search/instant_service.cc80
-rw-r--r--chrome/browser/search/instant_service.h13
-rw-r--r--chrome/browser/search/instant_service_observer.cc18
-rw-r--r--chrome/browser/search/instant_service_observer.h12
-rw-r--r--chrome/browser/search/instant_service_unittest.cc101
-rw-r--r--chrome/browser/search/instant_unittest_base.cc85
-rw-r--r--chrome/browser/search/instant_unittest_base.h42
-rw-r--r--chrome/browser/ui/OWNERS6
-rw-r--r--chrome/browser/ui/browser_instant_controller.cc42
-rw-r--r--chrome/browser/ui/browser_instant_controller.h19
-rw-r--r--chrome/browser/ui/browser_instant_controller_unittest.cc193
-rw-r--r--chrome/browser/ui/search/instant_extended_interactive_uitest.cc115
-rw-r--r--chrome/browser/ui/search/instant_extended_manual_interactive_uitest.cc4
-rw-r--r--chrome/browser/ui/search/instant_ntp_prerenderer.cc47
-rw-r--r--chrome/browser/ui/search/instant_ntp_prerenderer.h21
-rw-r--r--chrome/browser/ui/search/instant_ntp_prerenderer_unittest.cc28
-rw-r--r--chrome/browser/ui/search/instant_test_utils.cc2
-rw-r--r--chrome/chrome_browser.gypi1
-rw-r--r--chrome/chrome_tests_unit.gypi6
19 files changed, 616 insertions, 219 deletions
diff --git a/chrome/browser/search/instant_service.cc b/chrome/browser/search/instant_service.cc
index 62e8580..c50d271 100644
--- a/chrome/browser/search/instant_service.cc
+++ b/chrome/browser/search/instant_service.cc
@@ -7,6 +7,7 @@
#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"
@@ -19,18 +20,25 @@
#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_engines/template_url.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/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 "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 "ui/gfx/color_utils.h"
#include "ui/gfx/image/image_skia.h"
@@ -57,7 +65,7 @@ RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
InstantService::InstantService(Profile* profile)
: profile_(profile),
- ntp_prerenderer_(profile, profile->GetPrefs()),
+ ntp_prerenderer_(profile, this, profile->GetPrefs()),
browser_instant_controller_object_count_(0),
weak_ptr_factory_(this) {
// Stub for unit tests.
@@ -90,15 +98,25 @@ InstantService::InstantService(Profile* profile)
content::Source<ThemeService>(
ThemeServiceFactory::GetForProfile(profile_)));
- content::URLDataSource::Add(profile, new ThemeSource(profile));
+ content::URLDataSource::Add(profile_, new ThemeSource(profile_));
#endif // defined(ENABLE_THEMES)
- content::URLDataSource::Add(profile, new ThumbnailSource(profile, false));
- content::URLDataSource::Add(profile, new ThumbnailSource(profile, true));
- content::URLDataSource::Add(profile, new FaviconSource(
- profile, FaviconSource::FAVICON));
- content::URLDataSource::Add(profile, new LocalNtpSource(profile));
- content::URLDataSource::Add(profile, new MostVisitedIframeSource());
+
+ content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false));
+ content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true));
+ content::URLDataSource::Add(
+ profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
+ content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
+ content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
+
+ 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()));
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(profile_));
}
@@ -195,7 +213,7 @@ void InstantService::OnBrowserInstantControllerCreated() {
++browser_instant_controller_object_count_;
if (browser_instant_controller_object_count_ == 1)
- ntp_prerenderer_.PreloadInstantNTP();
+ ntp_prerenderer_.ReloadInstantNTP();
}
void InstantService::OnBrowserInstantControllerDestroyed() {
@@ -255,6 +273,12 @@ void InstantService::Observe(int type,
ntp_prerenderer_.DeleteNTPContents();
break;
}
+ 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.";
}
@@ -391,6 +415,44 @@ 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;
+
+ // 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;
+ }
+ FOR_EACH_OBSERVER(
+ InstantServiceObserver, observers_, DefaultSearchProviderChanged());
+}
+
InstantNTPPrerenderer* InstantService::ntp_prerenderer() {
return &ntp_prerenderer_;
}
diff --git a/chrome/browser/search/instant_service.h b/chrome/browser/search/instant_service.h
index f8c88e5..f876c4a 100644
--- a/chrome/browser/search/instant_service.h
+++ b/chrome/browser/search/instant_service.h
@@ -17,6 +17,8 @@
#include "base/memory/scoped_ptr.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_ntp_prerenderer.h"
#include "chrome/common/instant_types.h"
@@ -25,10 +27,8 @@
#include "content/public/browser/notification_registrar.h"
class GURL;
-class InstantExtendedTest;
class InstantIOContext;
class InstantServiceObserver;
-class InstantTestBase;
class Profile;
class ThemeService;
@@ -104,7 +104,9 @@ class InstantService : public BrowserContextKeyedService,
private:
friend class InstantExtendedTest;
+ friend class InstantServiceTest;
friend class InstantTestBase;
+ friend class InstantUnitTestBase;
FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
NTPReactsToNetworkChanges);
@@ -133,6 +135,11 @@ class InstantService : public BrowserContextKeyedService,
// Theme changed notification handler.
void OnThemeChanged(ThemeService* theme_service);
+ void OnGoogleURLUpdated(Profile* profile,
+ GoogleURLTracker::UpdatedDetails* details);
+
+ void OnDefaultSearchProviderChanged(const std::string& pref_name);
+
// Used by tests.
InstantNTPPrerenderer* ntp_prerenderer();
@@ -151,6 +158,8 @@ class InstantService : public BrowserContextKeyedService,
content::NotificationRegistrar registrar_;
+ PrefChangeRegistrar profile_pref_registrar_;
+
scoped_refptr<InstantIOContext> instant_io_context_;
InstantNTPPrerenderer ntp_prerenderer_;
diff --git a/chrome/browser/search/instant_service_observer.cc b/chrome/browser/search/instant_service_observer.cc
new file mode 100644
index 0000000..02e8856
--- /dev/null
+++ b/chrome/browser/search/instant_service_observer.cc
@@ -0,0 +1,18 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/search/instant_service_observer.h"
+
+void InstantServiceObserver::ThemeInfoChanged(const ThemeBackgroundInfo&) {
+}
+
+void InstantServiceObserver::MostVisitedItemsChanged(
+ const std::vector<InstantMostVisitedItem>&) {
+}
+
+void InstantServiceObserver::DefaultSearchProviderChanged() {
+}
+
+void InstantServiceObserver::GoogleURLUpdated() {
+}
diff --git a/chrome/browser/search/instant_service_observer.h b/chrome/browser/search/instant_service_observer.h
index c282c93..7d88b1a 100644
--- a/chrome/browser/search/instant_service_observer.h
+++ b/chrome/browser/search/instant_service_observer.h
@@ -14,11 +14,19 @@ struct ThemeBackgroundInfo;
class InstantServiceObserver {
public:
// Indicates that the user's custom theme has changed in some way.
- virtual void ThemeInfoChanged(const ThemeBackgroundInfo&) = 0;
+ virtual void ThemeInfoChanged(const ThemeBackgroundInfo&);
// Indicates that the most visited items has changed.
virtual void MostVisitedItemsChanged(
- const std::vector<InstantMostVisitedItem>&) = 0;
+ const std::vector<InstantMostVisitedItem>&);
+
+ // 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();
protected:
virtual ~InstantServiceObserver() {}
diff --git a/chrome/browser/search/instant_service_unittest.cc b/chrome/browser/search/instant_service_unittest.cc
new file mode 100644
index 0000000..1ae497e
--- /dev/null
+++ b/chrome/browser/search/instant_service_unittest.cc
@@ -0,0 +1,101 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+#include <vector>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_util.h"
+#include "chrome/browser/search/instant_service.h"
+#include "chrome/browser/search/instant_service_observer.h"
+#include "chrome/browser/search/instant_unittest_base.h"
+#include "chrome/browser/search/search.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/browser_window.h"
+#include "chrome/browser/ui/host_desktop.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_observer.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+namespace chrome {
+
+namespace {
+
+class MockInstantServiceObserver : public InstantServiceObserver {
+ public:
+ MOCK_METHOD0(DefaultSearchProviderChanged, void());
+ MOCK_METHOD0(GoogleURLUpdated, void());
+};
+
+class MockWebContentsObserver : public content::WebContentsObserver {
+ public:
+ MOCK_METHOD1(WebContentsDestroyed, void(content::WebContents*));
+
+ // Dumb override to make MSVC happy.
+ void Observe_(content::WebContents* contents) {
+ content::WebContentsObserver::Observe(contents);
+ }
+
+ protected:
+ friend class InstantServiceTest;
+ FRIEND_TEST_ALL_PREFIXES(InstantServiceTest,
+ DispatchDefaultSearchProviderChanged);
+ FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DispatchGoogleURLUpdated);
+};
+
+class InstantServiceTest : public InstantUnitTestBase {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ InstantUnitTestBase::SetUp();
+
+ instant_service_observer_.reset(new MockInstantServiceObserver());
+ instant_service_->AddObserver(instant_service_observer_.get());
+
+ instant_ntp_contents_observer_.reset(new MockWebContentsObserver());
+ instant_ntp_contents_observer_->Observe_(
+ instant_service_->GetNTPContents());
+ }
+
+ virtual void TearDown() OVERRIDE {
+ instant_service_->RemoveObserver(instant_service_observer_.get());
+ instant_ntp_contents_observer_->Observe_(NULL);
+ InstantUnitTestBase::TearDown();
+ }
+
+ scoped_ptr<MockInstantServiceObserver> instant_service_observer_;
+ scoped_ptr<MockWebContentsObserver> instant_ntp_contents_observer_;
+};
+
+TEST_F(InstantServiceTest, DispatchDefaultSearchProviderChanged) {
+ EXPECT_CALL(*instant_service_observer_.get(), DefaultSearchProviderChanged())
+ .Times(1);
+ EXPECT_CALL(*instant_ntp_contents_observer_.get(),
+ WebContentsDestroyed(instant_service_->GetNTPContents()))
+ .Times(1);
+
+ GURL ntp_url = instant_service_->GetNTPContents()->GetURL();
+ const std::string& new_base_url = "https://bar.com/";
+ SetDefaultSearchProvider(new_base_url);
+ GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL();
+ EXPECT_NE(ntp_url, new_ntp_url);
+ EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true));
+}
+
+TEST_F(InstantServiceTest, DispatchGoogleURLUpdated) {
+ EXPECT_CALL(*instant_service_observer_.get(), GoogleURLUpdated()).Times(1);
+ EXPECT_CALL(*instant_ntp_contents_observer_.get(),
+ WebContentsDestroyed(instant_service_->GetNTPContents()))
+ .Times(1);
+
+ GURL ntp_url = instant_service_->GetNTPContents()->GetURL();
+ const std::string& new_base_url = "https://www.google.es/";
+ NotifyGoogleBaseURLUpdate(new_base_url);
+ GURL new_ntp_url = instant_service_->GetNTPContents()->GetURL();
+ EXPECT_NE(ntp_url, new_ntp_url);
+ EXPECT_TRUE(StartsWithASCII(new_ntp_url.spec(), new_base_url, true));
+}
+
+} // namespace
+
+} // namespace chrome
diff --git a/chrome/browser/search/instant_unittest_base.cc b/chrome/browser/search/instant_unittest_base.cc
new file mode 100644
index 0000000..1842ac3
--- /dev/null
+++ b/chrome/browser/search/instant_unittest_base.cc
@@ -0,0 +1,85 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/search/instant_unittest_base.h"
+#include <string>
+
+#include "base/basictypes.h"
+#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/google/google_url_tracker.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/search/instant_service.h"
+#include "chrome/browser/search/instant_service_factory.h"
+#include "chrome/browser/search/search.h"
+#include "chrome/browser/search_engines/search_terms_data.h"
+#include "chrome/browser/search_engines/template_url.h"
+#include "chrome/browser/search_engines/template_url_service.h"
+#include "chrome/browser/search_engines/template_url_service_factory.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/test/base/browser_with_test_window_test.h"
+#include "chrome/test/base/testing_pref_service_syncable.h"
+#include "chrome/test/base/ui_test_utils.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/notification_source.h"
+
+void InstantUnitTestBase::SetUp() {
+ chrome::EnableInstantExtendedAPIForTesting();
+ BrowserWithTestWindowTest::SetUp();
+
+ TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
+ profile(), &TemplateURLServiceFactory::BuildInstanceFor);
+ template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
+ ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_);
+
+ UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/");
+ TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
+ pref_service->SetUserPref(prefs::kLastPromptedGoogleURL,
+ new base::StringValue("https://www.google.com/"));
+ SetDefaultSearchProvider("{google:baseURL}");
+ instant_service_ = InstantServiceFactory::GetForProfile(profile());
+}
+
+void InstantUnitTestBase::TearDown() {
+ UIThreadSearchTermsData::SetGoogleBaseURL("");
+ BrowserWithTestWindowTest::TearDown();
+}
+
+void InstantUnitTestBase::SetDefaultSearchProvider(
+ const std::string& base_url) {
+ TemplateURLData data;
+ data.SetURL(base_url + "url?bar={searchTerms}");
+ data.instant_url = base_url + "instant?"
+ "{google:omniboxStartMarginParameter}foo=foo#foo=foo&strk";
+ data.alternate_urls.push_back(base_url + "alt#quux={searchTerms}");
+ data.search_terms_replacement_key = "strk";
+
+ TemplateURL* template_url = new TemplateURL(profile(), data);
+ // Takes ownership of |template_url|.
+ template_url_service_->Add(template_url);
+ template_url_service_->SetDefaultSearchProvider(template_url);
+}
+
+void InstantUnitTestBase::NotifyGoogleBaseURLUpdate(
+ const std::string& new_google_base_url) {
+ // GoogleURLTracker is not created in tests.
+ // (See GoogleURLTrackerFactory::ServiceIsNULLWhileTesting())
+ // For determining google:baseURL for NTP, the following is used:
+ // UIThreadSearchTermsData::GoogleBaseURLValue()
+ // For simulating test behavior, this is overridden below.
+ UIThreadSearchTermsData::SetGoogleBaseURL(new_google_base_url);
+ GoogleURLTracker::UpdatedDetails details(GURL("https://www.google.com/"),
+ GURL(new_google_base_url));
+ content::NotificationService::current()->Notify(
+ chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
+ content::Source<Profile>(profile()->GetOriginalProfile()),
+ content::Details<GoogleURLTracker::UpdatedDetails>(&details));
+}
+
+
+bool InstantUnitTestBase::IsInstantServiceObserver(
+ InstantServiceObserver* observer) {
+ return instant_service_->observers_.HasObserver(observer);
+}
+
diff --git a/chrome/browser/search/instant_unittest_base.h b/chrome/browser/search/instant_unittest_base.h
new file mode 100644
index 0000000..6e46cf9
--- /dev/null
+++ b/chrome/browser/search/instant_unittest_base.h
@@ -0,0 +1,42 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SEARCH_INSTANT_UNITTEST_BASE_H_
+#define CHROME_BROWSER_SEARCH_INSTANT_UNITTEST_BASE_H_
+
+#include <string>
+
+#include "chrome/browser/search/instant_service.h"
+#include "chrome/browser/search_engines/template_url_service.h"
+#include "chrome/test/base/browser_with_test_window_test.h"
+
+class InstantServiceObserver;
+
+// This class provides an extension on top of BrowserWithTestWindowTest, and
+// adds some utility methods which can be useful for various unit tests for
+// Embedded Search / Instant implementation classes.
+class InstantUnitTestBase : public BrowserWithTestWindowTest {
+ protected:
+ virtual void SetUp() OVERRIDE;
+ virtual void TearDown() OVERRIDE;
+
+ // Adds and sets the default search provider using the base_url.
+ // The base_url should have the http[s]:// prefix and a trailing / after the
+ // TLD.
+ // It will always use an instant-enabled configuration using a
+ // search_terms_replacement_key.
+ void SetDefaultSearchProvider(const std::string& base_url);
+
+ // Simulates a Google Base URL change as would happen in event of
+ // search-domain-check. Note that the GoogleURLTrackerFactory is disabled for
+ // tests, so this is required.
+ void NotifyGoogleBaseURLUpdate(const std::string& new_google_base_url);
+
+ bool IsInstantServiceObserver(InstantServiceObserver* observer);
+
+ InstantService* instant_service_;
+ TemplateURLService* template_url_service_;
+};
+
+#endif // CHROME_BROWSER_SEARCH_INSTANT_UNITTEST_BASE_H_
diff --git a/chrome/browser/ui/OWNERS b/chrome/browser/ui/OWNERS
index d7a02c7..dfe6fff 100644
--- a/chrome/browser/ui/OWNERS
+++ b/chrome/browser/ui/OWNERS
@@ -9,6 +9,6 @@ sky@chromium.org
per-file browser_tab_contents.*=avi@chromium.org
# Instant/Search files.
-per-file browser_instant_controller.*=brettw@chromium.org
-per-file browser_instant_controller.*=jered@chromium.org
-per-file browser_instant_controller.*=samarth@chromium.org
+per-file browser_instant_controller*=brettw@chromium.org
+per-file browser_instant_controller*=jered@chromium.org
+per-file browser_instant_controller*=samarth@chromium.org
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc
index 662eca7..378d276 100644
--- a/chrome/browser/ui/browser_instant_controller.cc
+++ b/chrome/browser/ui/browser_instant_controller.cc
@@ -5,16 +5,12 @@
#include "chrome/browser/ui/browser_instant_controller.h"
#include "base/bind.h"
-#include "base/prefs/pref_service.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_web_ui.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/search.h"
-#include "chrome/browser/search_engines/template_url.h"
-#include "chrome/browser/search_engines/template_url_service.h"
-#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
@@ -25,9 +21,7 @@
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
-#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
-#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
@@ -42,16 +36,12 @@ BrowserInstantController::BrowserInstantController(Browser* browser)
: browser_(browser),
instant_(this),
instant_unload_handler_(browser) {
- profile_pref_registrar_.Init(profile()->GetPrefs());
- profile_pref_registrar_.Add(
- prefs::kDefaultSearchProviderID,
- base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged,
- base::Unretained(this)));
browser_->search_model()->AddObserver(this);
InstantService* instant_service =
InstantServiceFactory::GetForProfile(profile());
instant_service->OnBrowserInstantControllerCreated();
+ instant_service->AddObserver(this);
}
BrowserInstantController::~BrowserInstantController() {
@@ -59,6 +49,7 @@ BrowserInstantController::~BrowserInstantController() {
InstantService* instant_service =
InstantServiceFactory::GetForProfile(profile());
+ instant_service->RemoveObserver(this);
instant_service->OnBrowserInstantControllerDestroyed();
}
@@ -273,25 +264,20 @@ void BrowserInstantController::ModelChanged(
instant_.InstantSupportChanged(new_state.instant_support);
}
-void BrowserInstantController::OnDefaultSearchProviderChanged(
- const std::string& pref_name) {
- DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
-
- Profile* browser_profile = profile();
- const TemplateURL* template_url =
- TemplateURLServiceFactory::GetForProfile(browser_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;
- }
+////////////////////////////////////////////////////////////////////////////////
+// BrowserInstantController, InstantServiceObserver implementation:
+
+void BrowserInstantController::DefaultSearchProviderChanged() {
+ ReloadTabsInInstantProcess();
+}
+
+void BrowserInstantController::GoogleURLUpdated() {
+ ReloadTabsInInstantProcess();
+}
+void BrowserInstantController::ReloadTabsInInstantProcess() {
InstantService* instant_service =
- InstantServiceFactory::GetForProfile(browser_profile);
+ InstantServiceFactory::GetForProfile(profile());
if (!instant_service)
return;
diff --git a/chrome/browser/ui/browser_instant_controller.h b/chrome/browser/ui/browser_instant_controller.h
index 66598ef..a187a8c 100644
--- a/chrome/browser/ui/browser_instant_controller.h
+++ b/chrome/browser/ui/browser_instant_controller.h
@@ -9,7 +9,7 @@
#include "base/basictypes.h"
#include "base/compiler_specific.h"
-#include "base/prefs/pref_change_registrar.h"
+#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/ui/search/instant_controller.h"
#include "chrome/browser/ui/search/instant_unload_handler.h"
#include "chrome/browser/ui/search/search_model_observer.h"
@@ -27,7 +27,8 @@ namespace gfx {
class Rect;
}
-class BrowserInstantController : public SearchModelObserver {
+class BrowserInstantController : public SearchModelObserver,
+ public InstantServiceObserver {
public:
explicit BrowserInstantController(Browser* browser);
virtual ~BrowserInstantController();
@@ -91,11 +92,13 @@ class BrowserInstantController : public SearchModelObserver {
virtual void ModelChanged(const SearchModel::State& old_state,
const SearchModel::State& new_state) OVERRIDE;
- // Called when the default search provider changes. Revokes the searchbox API
- // privileges for any existing WebContents (that belong to the erstwhile
- // default search provider) by simply reloading all such WebContents. This
- // ensures that they are reloaded in a non-privileged renderer process.
- void OnDefaultSearchProviderChanged(const std::string& pref_name);
+ // Overridden from 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.
@@ -107,8 +110,6 @@ class BrowserInstantController : public SearchModelObserver {
InstantController instant_;
InstantUnloadHandler instant_unload_handler_;
- PrefChangeRegistrar profile_pref_registrar_;
-
DISALLOW_COPY_AND_ASSIGN(BrowserInstantController);
};
diff --git a/chrome/browser/ui/browser_instant_controller_unittest.cc b/chrome/browser/ui/browser_instant_controller_unittest.cc
new file mode 100644
index 0000000..52540aa
--- /dev/null
+++ b/chrome/browser/ui/browser_instant_controller_unittest.cc
@@ -0,0 +1,193 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_vector.h"
+#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/search/instant_service.h"
+#include "chrome/browser/search/instant_service_observer.h"
+#include "chrome/browser/search/instant_unittest_base.h"
+#include "chrome/browser/search/search.h"
+#include "chrome/browser/ui/browser_instant_controller.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/url_constants.h"
+#include "content/public/browser/navigation_controller.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_observer.h"
+
+namespace chrome {
+
+namespace {
+
+class BrowserInstantControllerTest : public InstantUnitTestBase {
+ protected:
+ friend class FakeWebContentsObserver;
+};
+
+const struct TabReloadTestCase {
+ const char* description;
+ const char* start_url;
+ bool start_in_instant_process;
+ bool should_reload;
+ bool end_in_instant_process;
+} kTabReloadTestCases[] = {
+ {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl,
+ true, true, true},
+ {"Remote Embedded NTP", "https://www.google.com/instant?strk",
+ true, true, false},
+ {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
+ true, true, false},
+ {"Other NTP", "https://bar.com/instant?strk",
+ false, false, false}
+};
+
+class FakeWebContentsObserver : public content::WebContentsObserver {
+ public:
+ FakeWebContentsObserver(BrowserInstantControllerTest* base_test,
+ content::WebContents* contents)
+ : WebContentsObserver(contents),
+ contents_(contents),
+ base_test_(base_test),
+ url_(contents->GetURL()),
+ num_reloads_(0) {}
+
+ virtual void NavigateToPendingEntry(
+ const GURL& url,
+ content::NavigationController::ReloadType reload_type) OVERRIDE {
+ // The tab reload event doesn't work with BrowserWithTestWindowTest.
+ // So we capture the NavigateToPendingEntry, and use the
+ // BrowserWithTestWindowTest::NavigateAndCommit to simulate the complete
+ // reload. Note that this will again trigger NavigateToPendingEntry, so we
+ // remove this as observer.
+ content::NavigationController* controller =
+ &web_contents()->GetController();
+ Observe(NULL);
+
+ if (url_ == url)
+ num_reloads_++;
+
+ base_test_->NavigateAndCommit(controller, url);
+ }
+
+ int num_reloads() const {
+ return num_reloads_;
+ }
+
+ content::WebContents* contents() {
+ return contents_;
+ }
+
+ protected:
+ friend class BrowserInstantControllerTest;
+ FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
+ DefaultSearchProviderChanged);
+ FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
+ GoogleBaseURLUpdated);
+
+ private:
+ content::WebContents* contents_;
+ BrowserInstantControllerTest* base_test_;
+ const GURL& url_;
+ int num_reloads_;
+};
+
+TEST_F(BrowserInstantControllerTest, DefaultSearchProviderChanged) {
+ size_t num_tests = arraysize(kTabReloadTestCases);
+ ScopedVector<FakeWebContentsObserver> observers;
+ for (size_t i = 0; i < num_tests; ++i) {
+ const TabReloadTestCase& test = kTabReloadTestCases[i];
+ AddTab(browser(), GURL(test.start_url));
+ content::WebContents* contents =
+ browser()->tab_strip_model()->GetActiveWebContents();
+
+ // Validate initial instant state.
+ EXPECT_EQ(test.start_in_instant_process,
+ instant_service_->IsInstantProcess(
+ contents->GetRenderProcessHost()->GetID()))
+ << test.description;
+
+ // Setup an observer to verify reload or absence thereof.
+ observers.push_back(new FakeWebContentsObserver(this, contents));
+ }
+
+ SetDefaultSearchProvider("https://bar.com/");
+
+ for (size_t i = 0; i < num_tests; ++i) {
+ FakeWebContentsObserver* observer = observers[i];
+ const TabReloadTestCase& test = kTabReloadTestCases[i];
+ content::WebContents* contents = observer->contents();
+
+ // Validate final instant state.
+ EXPECT_EQ(test.end_in_instant_process,
+ instant_service_->IsInstantProcess(
+ contents->GetRenderProcessHost()->GetID()))
+ << test.description;
+
+ // Ensure only the expected tabs(contents) reloaded.
+ EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
+ << test.description;
+ }
+}
+
+TEST_F(BrowserInstantControllerTest, GoogleBaseURLUpdated) {
+ const size_t num_tests = arraysize(kTabReloadTestCases);
+ ScopedVector<FakeWebContentsObserver> observers;
+ for (size_t i = 0; i < num_tests; ++i) {
+ const TabReloadTestCase& test = kTabReloadTestCases[i];
+ AddTab(browser(), GURL(test.start_url));
+ content::WebContents* contents =
+ browser()->tab_strip_model()->GetActiveWebContents();
+
+ // Validate initial instant state.
+ EXPECT_EQ(test.start_in_instant_process,
+ instant_service_->IsInstantProcess(
+ contents->GetRenderProcessHost()->GetID()))
+ << test.description;
+
+ // Setup an observer to verify reload or absence thereof.
+ observers.push_back(new FakeWebContentsObserver(this, contents));
+ }
+
+ NotifyGoogleBaseURLUpdate("https://www.google.es/");
+
+ for (size_t i = 0; i < num_tests; ++i) {
+ const TabReloadTestCase& test = kTabReloadTestCases[i];
+ FakeWebContentsObserver* observer = observers[i];
+ content::WebContents* contents = observer->contents();
+
+ // Validate final instant state.
+ EXPECT_EQ(test.end_in_instant_process,
+ instant_service_->IsInstantProcess(
+ contents->GetRenderProcessHost()->GetID()))
+ << test.description;
+
+ // Ensure only the expected tabs(contents) reloaded.
+ EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
+ << test.description;
+ }
+}
+
+TEST_F(BrowserInstantControllerTest, BrowserWindowLifecycle) {
+ scoped_ptr<BrowserWindow> window(CreateBrowserWindow());
+ Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
+ params.window = window.get();
+ scoped_ptr<Browser> browser(new Browser(params));
+ InstantServiceObserver* bic;
+ bic = browser->instant_controller();
+ EXPECT_TRUE(IsInstantServiceObserver(bic))
+ << "New BrowserInstantController should register as InstantServiceObserver";
+
+ browser.reset(NULL);
+ window.reset(NULL);
+ EXPECT_FALSE(IsInstantServiceObserver(bic))
+ << "New BrowserInstantController should register as InstantServiceObserver";
+}
+
+} // namespace
+
+} // namespace chrome
diff --git a/chrome/browser/ui/search/instant_extended_interactive_uitest.cc b/chrome/browser/ui/search/instant_extended_interactive_uitest.cc
index 98b3ec7..a1142c5 100644
--- a/chrome/browser/ui/search/instant_extended_interactive_uitest.cc
+++ b/chrome/browser/ui/search/instant_extended_interactive_uitest.cc
@@ -898,121 +898,6 @@ IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
EXPECT_EQ(1, on_most_visited_change_calls_);
}
-// Flaky: crbug.com/267096
-IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
- DISABLED_OnDefaultSearchProviderChanged) {
- InstantService* instant_service =
- InstantServiceFactory::GetForProfile(browser()->profile());
- ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
-
- // Setup Instant.
- ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
- FocusOmniboxAndWaitForInstantNTPSupport();
- EXPECT_EQ(1, instant_service->GetInstantProcessCount());
-
- // Navigating to the NTP should use the Instant render process.
- content::WindowedNotificationObserver new_tab_observer(
- content::NOTIFICATION_NAV_ENTRY_COMMITTED,
- content::NotificationService::AllSources());
- ui_test_utils::NavigateToURLWithDisposition(
- browser(),
- GURL(chrome::kChromeUINewTabURL),
- CURRENT_TAB,
- ui_test_utils::BROWSER_TEST_NONE);
- new_tab_observer.Wait();
-
- content::WebContents* ntp_contents =
- browser()->tab_strip_model()->GetActiveWebContents();
- EXPECT_TRUE(chrome::IsInstantNTP(ntp_contents));
- EXPECT_TRUE(instant_service->IsInstantProcess(
- ntp_contents->GetRenderProcessHost()->GetID()));
- GURL ntp_url = ntp_contents->GetURL();
-
- AddBlankTabAndShow(browser());
- content::WebContents* active_tab =
- browser()->tab_strip_model()->GetActiveWebContents();
- EXPECT_FALSE(chrome::IsInstantNTP(active_tab));
- EXPECT_FALSE(instant_service->IsInstantProcess(
- active_tab->GetRenderProcessHost()->GetID()));
-
- TemplateURLData data;
- data.short_name = ASCIIToUTF16("t");
- data.SetURL("http://defaultturl/q={searchTerms}");
- data.suggestions_url = "http://defaultturl2/q={searchTerms}";
- data.instant_url = "http://does/not/exist";
- data.alternate_urls.push_back(data.instant_url + "#q={searchTerms}");
- data.search_terms_replacement_key = "strk";
-
- TemplateURL* template_url = new TemplateURL(browser()->profile(), data);
- TemplateURLService* service =
- TemplateURLServiceFactory::GetForProfile(browser()->profile());
- ui_test_utils::WaitForTemplateURLServiceToLoad(service);
- service->Add(template_url); // Takes ownership of |template_url|.
-
- // Change the default search provider.
- content::WindowedNotificationObserver observer(
- content::NOTIFICATION_LOAD_STOP,
- content::Source<content::NavigationController>(
- &ntp_contents->GetController()));
- service->SetDefaultSearchProvider(template_url);
- observer.Wait();
-
- // |ntp_contents| should not use the Instant render process.
- EXPECT_FALSE(chrome::IsInstantNTP(ntp_contents));
- EXPECT_FALSE(instant_service->IsInstantProcess(
- ntp_contents->GetRenderProcessHost()->GetID()));
- // Make sure the URL remains the same.
- EXPECT_EQ(ntp_url, ntp_contents->GetURL());
-}
-
-IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
- ReloadLocalNTPOnSearchProviderChange) {
- // Setup Instant.
- ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
- FocusOmniboxAndWaitForInstantNTPSupport();
-
- // Navigate to Local NTP.
- content::WindowedNotificationObserver new_tab_observer(
- content::NOTIFICATION_NAV_ENTRY_COMMITTED,
- content::NotificationService::AllSources());
- ui_test_utils::NavigateToURLWithDisposition(
- browser(),
- GURL(chrome::kChromeSearchLocalNtpUrl),
- CURRENT_TAB,
- ui_test_utils::BROWSER_TEST_NONE);
- new_tab_observer.Wait();
-
- content::WebContents* ntp_contents =
- browser()->tab_strip_model()->GetActiveWebContents();
- GURL ntp_url = ntp_contents->GetURL();
-
- TemplateURLData data;
- data.short_name = ASCIIToUTF16("t");
- data.SetURL("http://defaultturl/q={searchTerms}");
- data.suggestions_url = "http://defaultturl2/q={searchTerms}";
- data.instant_url = "http://does/not/exist";
- data.alternate_urls.push_back(data.instant_url + "#q={searchTerms}");
- data.search_terms_replacement_key = "strk";
-
- TemplateURL* template_url = new TemplateURL(browser()->profile(), data);
- TemplateURLService* service =
- TemplateURLServiceFactory::GetForProfile(browser()->profile());
- ui_test_utils::WaitForTemplateURLServiceToLoad(service);
- service->Add(template_url); // Takes ownership of |template_url|.
-
- // Change the default search provider. This will reload the local NTP and the
- // page URL will remain the same.
- content::WindowedNotificationObserver observer(
- content::NOTIFICATION_LOAD_STOP,
- content::Source<content::NavigationController>(
- &ntp_contents->GetController()));
- service->SetDefaultSearchProvider(template_url);
- observer.Wait();
-
- // Make sure the URL remains the same.
- EXPECT_EQ(ntp_url, ntp_contents->GetURL());
-}
-
IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest, SetPrefetchQuery) {
ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
FocusOmniboxAndWaitForInstantNTPSupport();
diff --git a/chrome/browser/ui/search/instant_extended_manual_interactive_uitest.cc b/chrome/browser/ui/search/instant_extended_manual_interactive_uitest.cc
index af3206f..0cc5290 100644
--- a/chrome/browser/ui/search/instant_extended_manual_interactive_uitest.cc
+++ b/chrome/browser/ui/search/instant_extended_manual_interactive_uitest.cc
@@ -98,7 +98,7 @@ IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, MANUAL_ShowsGoogleNTP) {
InstantService* instant_service =
InstantServiceFactory::GetForProfile(browser()->profile());
ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
- instant_service->ntp_prerenderer()->ReloadStaleNTP();
+ instant_service->ntp_prerenderer()->ReloadInstantNTP();
FocusOmniboxAndWaitForInstantNTPSupport();
content::WindowedNotificationObserver observer(
@@ -120,7 +120,7 @@ IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, MANUAL_SearchesFromFakebox) {
InstantService* instant_service =
InstantServiceFactory::GetForProfile(browser()->profile());
ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
- instant_service->ntp_prerenderer()->ReloadStaleNTP();
+ instant_service->ntp_prerenderer()->ReloadInstantNTP();
FocusOmniboxAndWaitForInstantNTPSupport();
// Open a new tab page.
diff --git a/chrome/browser/ui/search/instant_ntp_prerenderer.cc b/chrome/browser/ui/search/instant_ntp_prerenderer.cc
index 7b3957a..d25c0c0 100644
--- a/chrome/browser/ui/search/instant_ntp_prerenderer.cc
+++ b/chrome/browser/ui/search/instant_ntp_prerenderer.cc
@@ -45,6 +45,7 @@ void DeleteNTPSoon(scoped_ptr<InstantNTP> ntp) {
InstantNTPPrerenderer::InstantNTPPrerenderer(Profile* profile,
+ InstantService* instant_service,
PrefService* prefs)
: profile_(profile) {
DCHECK(profile);
@@ -54,23 +55,26 @@ InstantNTPPrerenderer::InstantNTPPrerenderer(Profile* profile,
profile_pref_registrar_.Init(prefs);
profile_pref_registrar_.Add(
prefs::kSearchSuggestEnabled,
- base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP,
- base::Unretained(this)));
- profile_pref_registrar_.Add(
- prefs::kDefaultSearchProviderID,
- base::Bind(&InstantNTPPrerenderer::OnDefaultSearchProviderChanged,
+ base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP,
base::Unretained(this)));
}
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
+
+ // Allow instant_service to be null for unit tets.
+ if (instant_service)
+ instant_service->AddObserver(this);
}
InstantNTPPrerenderer::~InstantNTPPrerenderer() {
+ InstantService* instant_service =
+ InstantServiceFactory::GetForProfile(profile_);
+ if (instant_service)
+ instant_service->RemoveObserver(this);
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
}
-void InstantNTPPrerenderer::PreloadInstantNTP() {
- DCHECK(!ntp());
- ReloadStaleNTP();
+void InstantNTPPrerenderer::ReloadInstantNTP() {
+ ResetNTP(GetInstantURL());
}
scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() {
@@ -84,7 +88,7 @@ scoped_ptr<content::WebContents> InstantNTPPrerenderer::ReleaseNTPContents() {
scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents();
// Preload a new InstantNTP.
- ResetNTP(GetInstantURL());
+ ReloadInstantNTP();
return ntp_contents.Pass();
}
@@ -175,7 +179,7 @@ void InstantNTPPrerenderer::OnNetworkChanged(
return;
if (!ntp() || ntp()->IsLocal())
- ResetNTP(GetInstantURL());
+ ReloadInstantNTP();
}
void InstantNTPPrerenderer::InstantSupportDetermined(
@@ -245,28 +249,15 @@ void InstantNTPPrerenderer::InstantPageLoadFailed(
ResetNTP(GetLocalInstantURL());
}
-void InstantNTPPrerenderer::OnDefaultSearchProviderChanged(
- const std::string& pref_name) {
- DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
- if (!ntp())
- return;
-
- ResetNTP(GetInstantURL());
-}
-
void InstantNTPPrerenderer::ResetNTP(const std::string& instant_url) {
// Instant NTP is only used in extended mode so we should always have a
// non-empty URL to use.
DCHECK(!instant_url.empty());
ntp_.reset(new InstantNTP(this, instant_url, profile_));
- ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadStaleNTP,
+ ntp_->InitContents(base::Bind(&InstantNTPPrerenderer::ReloadInstantNTP,
base::Unretained(this)));
}
-void InstantNTPPrerenderer::ReloadStaleNTP() {
- ResetNTP(GetInstantURL());
-}
-
bool InstantNTPPrerenderer::PageIsCurrent() const {
const std::string& instant_url = GetInstantURL();
if (instant_url.empty() ||
@@ -298,3 +289,11 @@ bool InstantNTPPrerenderer::ShouldSwitchToLocalNTP() const {
// (unless the finch flag to use the remote NTP is set).
return !(InStartup() && chrome::ShouldPreferRemoteNTPOnStartup());
}
+
+void InstantNTPPrerenderer::DefaultSearchProviderChanged() {
+ ReloadInstantNTP();
+}
+
+void InstantNTPPrerenderer::GoogleURLUpdated() {
+ ReloadInstantNTP();
+}
diff --git a/chrome/browser/ui/search/instant_ntp_prerenderer.h b/chrome/browser/ui/search/instant_ntp_prerenderer.h
index 933e0a8..10d3292 100644
--- a/chrome/browser/ui/search/instant_ntp_prerenderer.h
+++ b/chrome/browser/ui/search/instant_ntp_prerenderer.h
@@ -11,14 +11,14 @@
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_change_registrar.h"
+#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/ui/search/instant_page.h"
#include "content/public/browser/web_contents.h"
#include "net/base/network_change_notifier.h"
-class InstantExtendedTest;
class InstantNTP;
class InstantService;
-class InstantTestBase;
+class PrefService;
class Profile;
// InstantNTPPrerenderer maintains a prerendered instance of InstantNTP.
@@ -31,13 +31,15 @@ class Profile;
// InstantNTPPrerenderer is owned by InstantService.
class InstantNTPPrerenderer
: public InstantPage::Delegate,
- public net::NetworkChangeNotifier::NetworkChangeObserver {
+ public net::NetworkChangeNotifier::NetworkChangeObserver,
+ public InstantServiceObserver {
public:
- InstantNTPPrerenderer(Profile* profile, PrefService* prefs);
+ InstantNTPPrerenderer(Profile* profile, InstantService* instant_service,
+ PrefService* prefs);
virtual ~InstantNTPPrerenderer();
// Preloads |ntp_| with a new InstantNTP.
- void PreloadInstantNTP();
+ void ReloadInstantNTP();
// Releases and returns the InstantNTP WebContents. May be NULL. Loads a new
// WebContents for the InstantNTP.
@@ -137,16 +139,13 @@ class InstantNTPPrerenderer
virtual void UndoAllMostVisitedDeletions() OVERRIDE;
virtual void InstantPageLoadFailed(content::WebContents* contents) OVERRIDE;
- // Called when the default search provider changes. Resets InstantNTP.
- void OnDefaultSearchProviderChanged(const std::string& pref_name);
+ // Overridden from InstantServiceObserver:
+ virtual void DefaultSearchProviderChanged() OVERRIDE;
+ virtual void GoogleURLUpdated() OVERRIDE;
// Recreates |ntp_| using |instant_url|.
void ResetNTP(const std::string& instant_url);
- // Resets |ntp_| with a new InstantNTP. Called when |ntp_| is stale or when a
- // pref is changed.
- void ReloadStaleNTP();
-
// Returns true if |ntp_| has an up-to-date Instant URL and supports Instant.
// Note that local URLs will not pass this check.
bool PageIsCurrent() const;
diff --git a/chrome/browser/ui/search/instant_ntp_prerenderer_unittest.cc b/chrome/browser/ui/search/instant_ntp_prerenderer_unittest.cc
index 917e01a..c3899b7f 100644
--- a/chrome/browser/ui/search/instant_ntp_prerenderer_unittest.cc
+++ b/chrome/browser/ui/search/instant_ntp_prerenderer_unittest.cc
@@ -3,11 +3,9 @@
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
-#include "base/metrics/histogram.h"
-#include "base/metrics/histogram_samples.h"
-#include "base/metrics/statistics_recorder.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
+#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/search/instant_ntp.h"
#include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
@@ -17,10 +15,6 @@
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
-using base::HistogramBase;
-using base::HistogramSamples;
-using base::StatisticsRecorder;
-
class TestableInstantNTP : public InstantNTP {
public:
TestableInstantNTP(InstantNTPPrerenderer* ntp_prerenderer,
@@ -62,13 +56,15 @@ class TestableInstantNTP : public InstantNTP {
class TestableInstantNTPPrerenderer : public InstantNTPPrerenderer {
public:
- explicit TestableInstantNTPPrerenderer(TestingProfile* profile)
- : InstantNTPPrerenderer(profile, NULL),
+ explicit TestableInstantNTPPrerenderer(TestingProfile* profile,
+ InstantService* instant_service)
+ : InstantNTPPrerenderer(profile, instant_service, NULL),
test_instant_url_("http://test_url"),
override_javascript_enabled_(true),
test_javascript_enabled_(true),
test_in_startup_(false),
- test_ntp_(NULL) {}
+ test_ntp_(NULL) {
+ }
// Overrides from InstantNTPPrerenderer
virtual std::string GetInstantURL() const OVERRIDE {
@@ -125,10 +121,15 @@ private:
class InstantNTPPrerendererTest : public testing::Test {
public:
- InstantNTPPrerendererTest()
- : instant_ntp_prerenderer_(new TestableInstantNTPPrerenderer(&profile_)) {
- base::StatisticsRecorder::Initialize();
+ virtual void SetUp() OVERRIDE {
chrome::EnableInstantExtendedAPIForTesting();
+ instant_service_ = InstantServiceFactory::GetForProfile(&profile_);
+ instant_ntp_prerenderer_.reset(
+ new TestableInstantNTPPrerenderer(&profile_, instant_service_));
+ }
+
+ virtual void TearDown() OVERRIDE {
+ instant_ntp_prerenderer_.reset();
}
TestableInstantNTPPrerenderer* instant_ntp_prerenderer() {
@@ -142,6 +143,7 @@ class InstantNTPPrerendererTest : public testing::Test {
private:
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestableInstantNTPPrerenderer> instant_ntp_prerenderer_;
+ InstantService* instant_service_;
mutable TestingProfile profile_;
};
diff --git a/chrome/browser/ui/search/instant_test_utils.cc b/chrome/browser/ui/search/instant_test_utils.cc
index 67d1d19..c576e37 100644
--- a/chrome/browser/ui/search/instant_test_utils.cc
+++ b/chrome/browser/ui/search/instant_test_utils.cc
@@ -61,7 +61,7 @@ void InstantTestBase::SetupInstant(Browser* browser) {
InstantService* instant_service =
InstantServiceFactory::GetForProfile(browser_->profile());
ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
- instant_service->ntp_prerenderer()->ReloadStaleNTP();
+ instant_service->ntp_prerenderer()->ReloadInstantNTP();
}
void InstantTestBase::SetInstantURL(const std::string& url) {
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index b301980..ecd7a79 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -1874,6 +1874,7 @@
'browser/search/instant_service.h',
'browser/search/instant_service_factory.cc',
'browser/search/instant_service_factory.h',
+ 'browser/search/instant_service_observer.cc',
'browser/search/instant_service_observer.h',
'browser/search/local_ntp_source.cc',
'browser/search/local_ntp_source.h',
diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi
index 1ef8680..04d0ddd 100644
--- a/chrome/chrome_tests_unit.gypi
+++ b/chrome/chrome_tests_unit.gypi
@@ -1173,6 +1173,9 @@
'browser/safe_browsing/signature_util_win_unittest.cc',
'browser/safe_browsing/two_phase_uploader_unittest.cc',
'browser/search/iframe_source_unittest.cc',
+ 'browser/search/instant_service_unittest.cc',
+ 'browser/search/instant_unittest_base.cc',
+ 'browser/search/instant_unittest_base.h',
'browser/search/most_visited_iframe_source_unittest.cc',
'browser/search/search_unittest.cc',
'browser/search_engines/search_host_to_urls_map_unittest.cc',
@@ -1399,6 +1402,7 @@
'browser/ui/bookmarks/bookmark_unittest.cc',
'browser/ui/bookmarks/recently_used_folders_combo_model_unittest.cc',
'browser/ui/browser_command_controller_unittest.cc',
+ 'browser/ui/browser_instant_controller_unittest.cc',
'browser/ui/browser_iterator_unittest.cc',
'browser/ui/browser_unittest.cc',
'browser/ui/chrome_select_file_policy_unittest.cc',
@@ -2501,12 +2505,14 @@
'browser/profiles/off_the_record_profile_impl_unittest.cc',
'browser/profiles/profile_list_desktop_unittest.cc',
'browser/profiles/profile_loader_unittest.cc',
+ 'browser/search/instant_service_unittest.cc',
'browser/search/search_unittest.cc',
'browser/signin/profile_oauth2_token_service_unittest.cc',
'browser/sync/profile_sync_service_session_unittest.cc',
'browser/sync/sync_global_error_unittest.cc',
'browser/tab_contents/render_view_context_menu_unittest.cc',
'browser/ui/autofill/autofill_dialog_controller_unittest.cc',
+ 'browser/ui/browser_instant_controller_unittest.cc',
'browser/ui/bookmarks/bookmark_bubble_sign_in_delegate_unittest.cc',
'browser/ui/bookmarks/bookmark_context_menu_controller_unittest.cc',
'browser/ui/bookmarks/bookmark_prompt_controller_unittest.cc',