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" | |
8 #include "chrome/browser/profiles/profile.h" | 7 #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" | |
12 #include "chrome/browser/ui/search/search.h" | 8 #include "chrome/browser/ui/search/search.h" |
13 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
14 #include "content/public/browser/navigation_controller.h" | |
15 #include "content/public/browser/navigation_details.h" | |
16 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
19 #include "content/public/browser/web_contents.h" | |
20 | 13 |
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome::search::SearchTabHelper); | 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome::search::SearchTabHelper); |
22 | 15 |
23 namespace { | 16 namespace { |
24 | 17 |
25 bool IsNTP(const GURL& url) { | 18 bool IsSearchEnabled(const content::WebContents* contents) { |
26 return url.SchemeIs(chrome::kChromeUIScheme) && | 19 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
27 url.host() == chrome::kChromeUINewTabHost; | |
28 } | |
29 | |
30 Profile* ProfileFromWebContents(const content::WebContents* web_contents) { | |
31 return Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
32 } | |
33 | |
34 bool IsSearchEnabled(Profile* profile) { | |
35 return chrome::search::IsInstantExtendedAPIEnabled(profile); | 20 return chrome::search::IsInstantExtendedAPIEnabled(profile); |
36 } | 21 } |
37 | 22 |
| 23 bool IsNTP(const content::WebContents* contents) { |
| 24 // We can't use WebContents::GetURL() because that uses the active entry, |
| 25 // whereas we want the visible entry. |
| 26 const content::NavigationEntry* entry = |
| 27 contents->GetController().GetVisibleEntry(); |
| 28 return entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL); |
| 29 } |
38 | 30 |
39 bool IsSearchResults(const GURL& url, Profile* profile) { | 31 bool IsSearchResults(const content::WebContents* contents) { |
40 if (chrome::search::IsForcedInstantURL(url)) | 32 return !chrome::search::GetSearchTerms(contents).empty(); |
41 return true; | |
42 | |
43 // Profile can be NULL in unit tests. | |
44 TemplateURLService* template_url_service = | |
45 TemplateURLServiceFactory::GetForProfile(profile); | |
46 if (!template_url_service) | |
47 return false; | |
48 | |
49 TemplateURL* template_url = template_url_service->GetDefaultSearchProvider(); | |
50 if (!template_url) | |
51 return false; | |
52 | |
53 string16 result; | |
54 return template_url->HasSearchTermsReplacementKey(url) && | |
55 template_url->ExtractSearchTermsFromURL(url, &result) && !result.empty(); | |
56 } | 33 } |
57 | 34 |
58 } // namespace | 35 } // namespace |
59 | 36 |
60 namespace chrome { | 37 namespace chrome { |
61 namespace search { | 38 namespace search { |
62 | 39 |
63 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 40 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
64 : is_search_enabled_(IsSearchEnabled(ProfileFromWebContents(web_contents))), | 41 : is_search_enabled_(IsSearchEnabled(web_contents)), |
65 user_input_in_progress_(false), | 42 user_input_in_progress_(false), |
66 model_(web_contents) { | 43 model_(web_contents) { |
67 if (!is_search_enabled_) | 44 if (!is_search_enabled_) |
68 return; | 45 return; |
69 | 46 |
70 registrar_.Add( | 47 registrar_.Add( |
71 this, | 48 this, |
72 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 49 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
73 content::Source<content::NavigationController>( | 50 content::Source<content::NavigationController>( |
74 &web_contents->GetController())); | 51 &web_contents->GetController())); |
75 } | 52 } |
76 | 53 |
77 SearchTabHelper::~SearchTabHelper() { | 54 SearchTabHelper::~SearchTabHelper() { |
78 } | 55 } |
79 | 56 |
80 void SearchTabHelper::OmniboxEditModelChanged(bool user_input_in_progress, | 57 void SearchTabHelper::OmniboxEditModelChanged(bool user_input_in_progress, |
81 bool cancelling) { | 58 bool cancelling) { |
82 if (!is_search_enabled_) | 59 if (!is_search_enabled_) |
83 return; | 60 return; |
84 | 61 |
85 user_input_in_progress_ = user_input_in_progress; | 62 user_input_in_progress_ = user_input_in_progress; |
86 if (!user_input_in_progress && !cancelling) | 63 if (!user_input_in_progress && !cancelling) |
87 return; | 64 return; |
88 | 65 |
89 UpdateModelBasedOnURL(web_contents()->GetURL()); | 66 UpdateModel(); |
90 } | 67 } |
91 | 68 |
92 void SearchTabHelper::NavigationEntryUpdated() { | 69 void SearchTabHelper::NavigationEntryUpdated() { |
93 if (!is_search_enabled_) | 70 if (!is_search_enabled_) |
94 return; | 71 return; |
95 UpdateModelBasedOnURL(web_contents()->GetURL()); | 72 |
| 73 UpdateModel(); |
96 } | 74 } |
97 | 75 |
98 void SearchTabHelper::Observe( | 76 void SearchTabHelper::Observe( |
99 int type, | 77 int type, |
100 const content::NotificationSource& source, | 78 const content::NotificationSource& source, |
101 const content::NotificationDetails& details) { | 79 const content::NotificationDetails& details) { |
102 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 80 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
103 content::LoadCommittedDetails* committed_details = | 81 UpdateModel(); |
104 content::Details<content::LoadCommittedDetails>(details).ptr(); | |
105 UpdateModelBasedOnURL(committed_details->entry->GetVirtualURL()); | |
106 } | 82 } |
107 | 83 |
108 void SearchTabHelper::UpdateModelBasedOnURL(const GURL& url) { | 84 void SearchTabHelper::UpdateModel() { |
109 Mode::Type type = Mode::MODE_DEFAULT; | 85 Mode::Type type = Mode::MODE_DEFAULT; |
110 Mode::Origin origin = Mode::ORIGIN_DEFAULT; | 86 Mode::Origin origin = Mode::ORIGIN_DEFAULT; |
111 if (IsNTP(url)) { | 87 if (IsNTP(web_contents())) { |
112 type = Mode::MODE_NTP; | 88 type = Mode::MODE_NTP; |
113 origin = Mode::ORIGIN_NTP; | 89 origin = Mode::ORIGIN_NTP; |
114 } else if (IsSearchResults(url, ProfileFromWebContents(web_contents()))) { | 90 } else if (IsSearchResults(web_contents())) { |
115 type = Mode::MODE_SEARCH_RESULTS; | 91 type = Mode::MODE_SEARCH_RESULTS; |
116 origin = Mode::ORIGIN_SEARCH; | 92 origin = Mode::ORIGIN_SEARCH; |
117 } | 93 } |
118 if (user_input_in_progress_) | 94 if (user_input_in_progress_) |
119 type = Mode::MODE_SEARCH_SUGGESTIONS; | 95 type = Mode::MODE_SEARCH_SUGGESTIONS; |
120 model_.SetMode(Mode(type, origin)); | 96 model_.SetMode(Mode(type, origin)); |
121 } | 97 } |
122 | 98 |
123 const content::WebContents* SearchTabHelper::web_contents() const { | 99 const content::WebContents* SearchTabHelper::web_contents() const { |
124 return model_.web_contents(); | 100 return model_.web_contents(); |
125 } | 101 } |
126 | 102 |
127 } // namespace search | 103 } // namespace search |
128 } // namespace chrome | 104 } // namespace chrome |
OLD | NEW |