Index: chrome/browser/ui/search/search_tab_helper.cc |
diff --git a/chrome/browser/ui/search/search_tab_helper.cc b/chrome/browser/ui/search/search_tab_helper.cc |
index 186834fab9203db3fbe73ab5e6bd1c4e9179640d..b320565d6be9d73ca2a248d2b908dbbb36cfc4fe 100644 |
--- a/chrome/browser/ui/search/search_tab_helper.cc |
+++ b/chrome/browser/ui/search/search_tab_helper.cc |
@@ -4,55 +4,32 @@ |
#include "chrome/browser/ui/search/search_tab_helper.h" |
-#include "chrome/browser/google/google_util.h" |
#include "chrome/browser/profiles/profile.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/search/search.h" |
#include "chrome/common/url_constants.h" |
-#include "content/public/browser/navigation_controller.h" |
-#include "content/public/browser/navigation_details.h" |
#include "content/public/browser/navigation_entry.h" |
#include "content/public/browser/notification_service.h" |
#include "content/public/browser/notification_types.h" |
-#include "content/public/browser/web_contents.h" |
DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome::search::SearchTabHelper); |
namespace { |
-bool IsNTP(const GURL& url) { |
- return url.SchemeIs(chrome::kChromeUIScheme) && |
- url.host() == chrome::kChromeUINewTabHost; |
-} |
- |
-Profile* ProfileFromWebContents(const content::WebContents* web_contents) { |
- return Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
-} |
- |
-bool IsSearchEnabled(Profile* profile) { |
+bool IsSearchEnabled(const content::WebContents* contents) { |
+ Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
return chrome::search::IsInstantExtendedAPIEnabled(profile); |
} |
+bool IsNTP(const content::WebContents* contents) { |
+ // We can't use WebContents::GetURL() because that uses the active entry, |
+ // whereas we want the visible entry. |
+ const content::NavigationEntry* entry = |
+ contents->GetController().GetVisibleEntry(); |
+ return entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL); |
+} |
-bool IsSearchResults(const GURL& url, Profile* profile) { |
- if (chrome::search::IsForcedInstantURL(url)) |
- return true; |
- |
- // Profile can be NULL in unit tests. |
- TemplateURLService* template_url_service = |
- TemplateURLServiceFactory::GetForProfile(profile); |
- if (!template_url_service) |
- return false; |
- |
- TemplateURL* template_url = template_url_service->GetDefaultSearchProvider(); |
- if (!template_url) |
- return false; |
- |
- string16 result; |
- return template_url->HasSearchTermsReplacementKey(url) && |
- template_url->ExtractSearchTermsFromURL(url, &result) && !result.empty(); |
+bool IsSearchResults(const content::WebContents* contents) { |
+ return !chrome::search::GetSearchTerms(contents).empty(); |
} |
} // namespace |
@@ -61,7 +38,7 @@ namespace chrome { |
namespace search { |
SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
- : is_search_enabled_(IsSearchEnabled(ProfileFromWebContents(web_contents))), |
+ : is_search_enabled_(IsSearchEnabled(web_contents)), |
user_input_in_progress_(false), |
model_(web_contents) { |
if (!is_search_enabled_) |
@@ -86,13 +63,14 @@ void SearchTabHelper::OmniboxEditModelChanged(bool user_input_in_progress, |
if (!user_input_in_progress && !cancelling) |
return; |
- UpdateModelBasedOnURL(web_contents()->GetURL()); |
+ UpdateModel(); |
} |
void SearchTabHelper::NavigationEntryUpdated() { |
if (!is_search_enabled_) |
return; |
- UpdateModelBasedOnURL(web_contents()->GetURL()); |
+ |
+ UpdateModel(); |
} |
void SearchTabHelper::Observe( |
@@ -100,18 +78,16 @@ void SearchTabHelper::Observe( |
const content::NotificationSource& source, |
const content::NotificationDetails& details) { |
DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
- content::LoadCommittedDetails* committed_details = |
- content::Details<content::LoadCommittedDetails>(details).ptr(); |
- UpdateModelBasedOnURL(committed_details->entry->GetVirtualURL()); |
+ UpdateModel(); |
} |
-void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { |
+void SearchTabHelper::UpdateModel() { |
Mode::Type type = Mode::MODE_DEFAULT; |
Mode::Origin origin = Mode::ORIGIN_DEFAULT; |
- if (IsNTP(url)) { |
+ if (IsNTP(web_contents())) { |
type = Mode::MODE_NTP; |
origin = Mode::ORIGIN_NTP; |
- } else if (IsSearchResults(url, ProfileFromWebContents(web_contents()))) { |
+ } else if (IsSearchResults(web_contents())) { |
type = Mode::MODE_SEARCH_RESULTS; |
origin = Mode::ORIGIN_SEARCH; |
} |