OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/search/search_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/google/google_util.h" | 7 #include "chrome/browser/google/google_util.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
9 #include "chrome/browser/ui/search/search.h" | 12 #include "chrome/browser/ui/search/search.h" |
10 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
11 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
12 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
13 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
16 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
17 | 20 |
18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome::search::SearchTabHelper); | 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome::search::SearchTabHelper); |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 bool IsNTP(const GURL& url) { | 25 bool IsNTP(const GURL& url) { |
23 return url.SchemeIs(chrome::kChromeUIScheme) && | 26 return url.SchemeIs(chrome::kChromeUIScheme) && |
24 url.host() == chrome::kChromeUINewTabHost; | 27 url.host() == chrome::kChromeUINewTabHost; |
25 } | 28 } |
26 | 29 |
27 bool IsSearchEnabled(content::WebContents* web_contents) { | 30 Profile* ProfileFromWebContents(const content::WebContents* web_contents) { |
28 Profile* profile = | 31 return Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
29 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 32 } |
| 33 |
| 34 bool IsSearchEnabled(Profile* profile) { |
30 return chrome::search::IsInstantExtendedAPIEnabled(profile); | 35 return chrome::search::IsInstantExtendedAPIEnabled(profile); |
31 } | 36 } |
32 | 37 |
| 38 |
| 39 bool IsSearchResults(const GURL& url, Profile* profile) { |
| 40 // Profile can be NULL in unit tests. |
| 41 TemplateURLService* template_url_service = |
| 42 TemplateURLServiceFactory::GetForProfile(profile); |
| 43 if (!template_url_service) |
| 44 return false; |
| 45 |
| 46 TemplateURL* template_url = template_url_service->GetDefaultSearchProvider(); |
| 47 if (!template_url) |
| 48 return false; |
| 49 |
| 50 string16 result; |
| 51 return template_url->HasSearchTermsReplacementKey(url) && |
| 52 template_url->ExtractSearchTermsFromURL(url, &result) && !result.empty(); |
| 53 } |
| 54 |
33 } // namespace | 55 } // namespace |
34 | 56 |
35 namespace chrome { | 57 namespace chrome { |
36 namespace search { | 58 namespace search { |
37 | 59 |
38 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 60 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
39 : WebContentsObserver(web_contents), | 61 : WebContentsObserver(web_contents), |
40 is_search_enabled_(IsSearchEnabled(web_contents)), | 62 is_search_enabled_(IsSearchEnabled(ProfileFromWebContents(web_contents))), |
41 user_input_in_progress_(false), | 63 user_input_in_progress_(false), |
42 model_(web_contents) { | 64 model_(web_contents) { |
43 if (!is_search_enabled_) | 65 if (!is_search_enabled_) |
44 return; | 66 return; |
45 | 67 |
46 registrar_.Add( | 68 registrar_.Add( |
47 this, | 69 this, |
48 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 70 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
49 content::Source<content::NavigationController>( | 71 content::Source<content::NavigationController>( |
50 &web_contents->GetController())); | 72 &web_contents->GetController())); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 UpdateModelBasedOnURL(committed_details->entry->GetURL()); | 118 UpdateModelBasedOnURL(committed_details->entry->GetURL()); |
97 } | 119 } |
98 } | 120 } |
99 | 121 |
100 void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { | 122 void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { |
101 Mode::Type type = Mode::MODE_DEFAULT; | 123 Mode::Type type = Mode::MODE_DEFAULT; |
102 Mode::Origin origin = Mode::ORIGIN_DEFAULT; | 124 Mode::Origin origin = Mode::ORIGIN_DEFAULT; |
103 if (IsNTP(url)) { | 125 if (IsNTP(url)) { |
104 type = Mode::MODE_NTP; | 126 type = Mode::MODE_NTP; |
105 origin = Mode::ORIGIN_NTP; | 127 origin = Mode::ORIGIN_NTP; |
106 } else if (google_util::IsInstantExtendedAPIGoogleSearchUrl(url.spec())) { | 128 } else if (IsSearchResults(url, ProfileFromWebContents(web_contents()))) { |
107 type = Mode::MODE_SEARCH_RESULTS; | 129 type = Mode::MODE_SEARCH_RESULTS; |
108 origin = Mode::ORIGIN_SEARCH; | 130 origin = Mode::ORIGIN_SEARCH; |
109 } | 131 } |
110 if (user_input_in_progress_) | 132 if (user_input_in_progress_) |
111 type = Mode::MODE_SEARCH_SUGGESTIONS; | 133 type = Mode::MODE_SEARCH_SUGGESTIONS; |
112 model_.SetMode(Mode(type, origin)); | 134 model_.SetMode(Mode(type, origin)); |
113 } | 135 } |
114 | 136 |
115 const content::WebContents* SearchTabHelper::web_contents() const { | 137 const content::WebContents* SearchTabHelper::web_contents() const { |
116 return model_.web_contents(); | 138 return model_.web_contents(); |
117 } | 139 } |
118 | 140 |
119 } // namespace search | 141 } // namespace search |
120 } // namespace chrome | 142 } // namespace chrome |
OLD | NEW |