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/profiles/profile.h" | |
7 #include "chrome/browser/search/search.h" | 8 #include "chrome/browser/search/search.h" |
8 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
9 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
10 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
14 #include "content/public/browser/web_contents.h" | |
13 | 15 |
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); | 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 bool IsNTP(const content::WebContents* contents) { | 20 bool IsNTP(const content::WebContents* contents) { |
19 // We can't use WebContents::GetURL() because that uses the active entry, | 21 // We can't use WebContents::GetURL() because that uses the active entry, |
20 // whereas we want the visible entry. | 22 // whereas we want the visible entry. |
21 const content::NavigationEntry* entry = | 23 const content::NavigationEntry* entry = |
22 contents->GetController().GetVisibleEntry(); | 24 contents->GetController().GetVisibleEntry(); |
23 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) | 25 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) |
24 return true; | 26 return true; |
25 | 27 |
26 return chrome::IsInstantNTP(contents); | 28 return chrome::IsInstantNTP(contents); |
27 } | 29 } |
28 | 30 |
29 bool IsSearchResults(const content::WebContents* contents) { | 31 bool IsSearchResults(const content::WebContents* contents) { |
30 return !chrome::GetSearchTerms(contents).empty(); | 32 return !chrome::GetSearchTerms(contents).empty(); |
31 } | 33 } |
32 | 34 |
35 // TODO(kmadhusu): Move this helper from anonymous namespace to chrome | |
36 // namespace and remove InstantPage::IsLocal(). | |
37 bool IsLocal(const content::WebContents* contents) { | |
38 return contents && | |
39 (contents->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | |
40 contents->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | |
41 } | |
42 | |
33 } // namespace | 43 } // namespace |
34 | 44 |
35 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 45 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
36 : WebContentsObserver(web_contents), | 46 : WebContentsObserver(web_contents), |
37 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), | 47 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
38 user_input_in_progress_(false), | 48 user_input_in_progress_(false), |
39 popup_is_open_(false), | 49 popup_is_open_(false), |
40 user_text_is_empty_(true), | 50 user_text_is_empty_(true), |
41 web_contents_(web_contents) { | 51 web_contents_(web_contents) { |
42 if (!is_search_enabled_) | 52 if (!is_search_enabled_) |
(...skipping 25 matching lines...) Expand all Loading... | |
68 UpdateMode(); | 78 UpdateMode(); |
69 } | 79 } |
70 | 80 |
71 void SearchTabHelper::NavigationEntryUpdated() { | 81 void SearchTabHelper::NavigationEntryUpdated() { |
72 if (!is_search_enabled_) | 82 if (!is_search_enabled_) |
73 return; | 83 return; |
74 | 84 |
75 UpdateMode(); | 85 UpdateMode(); |
76 } | 86 } |
77 | 87 |
88 void SearchTabHelper::InstantSupportChanged(bool supports_instant) { | |
89 if (!is_search_enabled_) | |
90 return; | |
91 | |
92 model_.SetSupportsInstant(supports_instant); | |
93 } | |
94 | |
78 void SearchTabHelper::Observe( | 95 void SearchTabHelper::Observe( |
79 int type, | 96 int type, |
80 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
81 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
82 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 99 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
83 UpdateMode(); | 100 UpdateMode(); |
84 } | 101 } |
85 | 102 |
86 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { | 103 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { |
87 bool handled = true; | 104 bool handled = true; |
88 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) | 105 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) |
89 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, | 106 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, |
90 OnSearchBoxShowBars) | 107 OnSearchBoxShowBars) |
91 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, | 108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, |
92 OnSearchBoxHideBars) | 109 OnSearchBoxHideBars) |
110 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
111 OnInstantSupportDetermined) | |
93 IPC_MESSAGE_UNHANDLED(handled = false) | 112 IPC_MESSAGE_UNHANDLED(handled = false) |
94 IPC_END_MESSAGE_MAP() | 113 IPC_END_MESSAGE_MAP() |
95 return handled; | 114 return handled; |
96 } | 115 } |
97 | 116 |
117 void SearchTabHelper::DidFinishLoad( | |
118 int64 /* frame_id */, | |
119 const GURL& /* validated_url */, | |
120 bool is_main_frame, | |
121 content::RenderViewHost* /* render_view_host */) { | |
122 if (is_main_frame) | |
123 DetermineIfPageSupportsInstant(); | |
124 } | |
125 | |
98 void SearchTabHelper::UpdateMode() { | 126 void SearchTabHelper::UpdateMode() { |
99 SearchMode::Type type = SearchMode::MODE_DEFAULT; | 127 SearchMode::Type type = SearchMode::MODE_DEFAULT; |
100 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; | 128 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; |
101 if (IsNTP(web_contents_)) { | 129 if (IsNTP(web_contents_)) { |
102 type = SearchMode::MODE_NTP; | 130 type = SearchMode::MODE_NTP; |
103 origin = SearchMode::ORIGIN_NTP; | 131 origin = SearchMode::ORIGIN_NTP; |
104 } else if (IsSearchResults(web_contents_)) { | 132 } else if (IsSearchResults(web_contents_)) { |
105 type = SearchMode::MODE_SEARCH_RESULTS; | 133 type = SearchMode::MODE_SEARCH_RESULTS; |
106 origin = SearchMode::ORIGIN_SEARCH; | 134 origin = SearchMode::ORIGIN_SEARCH; |
107 } | 135 } |
108 if (user_input_in_progress_) | 136 if (user_input_in_progress_) |
109 type = SearchMode::MODE_SEARCH_SUGGESTIONS; | 137 type = SearchMode::MODE_SEARCH_SUGGESTIONS; |
110 | 138 |
111 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && | 139 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && |
112 !popup_is_open_ && !user_text_is_empty_) { | 140 !popup_is_open_ && !user_text_is_empty_) { |
113 // We're switching back (|popup_is_open_| is false) to an NTP (type and | 141 // We're switching back (|popup_is_open_| is false) to an NTP (type and |
114 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't | 142 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't |
115 // modify visibility of top bars. This specific omnibox state is set when | 143 // modify visibility of top bars. This specific omnibox state is set when |
116 // OmniboxEditModelChanged() is called from | 144 // OmniboxEditModelChanged() is called from |
117 // OmniboxEditModel::SetInputInProgress() which is called from | 145 // OmniboxEditModel::SetInputInProgress() which is called from |
118 // OmniboxEditModel::Revert(). | 146 // OmniboxEditModel::Revert(). |
119 model_.SetState(SearchModel::State(SearchMode(type, origin), | 147 model_.SetState(SearchModel::State(SearchMode(type, origin), |
120 model_.state().top_bars_visible)); | 148 model_.state().top_bars_visible, |
149 model_.instant_support())); | |
121 } else { | 150 } else { |
122 model_.SetMode(SearchMode(type, origin)); | 151 model_.SetMode(SearchMode(type, origin)); |
123 } | 152 } |
124 } | 153 } |
125 | 154 |
155 void SearchTabHelper::DetermineIfPageSupportsInstant() { | |
156 Profile* profile = | |
157 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
158 if (!chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), | |
samarth
2013/05/13 20:19:03
This seems reasonable but any particular reason yo
kmadhusu
2013/05/14 00:01:27
During a discussion, Sreeram mentioned about this
sreeram
2013/05/14 01:49:17
The idea is that if you send a message to a page t
samarth
2013/05/15 17:43:21
Ok, this check makes sense.
Sreeram: the reason I
| |
159 profile)) { | |
160 InstantSupportChanged(false); | |
161 } else if (IsLocal(web_contents_)) { | |
162 // Local pages always support Instant. | |
163 InstantSupportChanged(true); | |
164 } else { | |
165 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
166 } | |
167 } | |
168 | |
169 void SearchTabHelper::OnInstantSupportDetermined(int page_id, | |
170 bool supports_instant) { | |
171 if (web_contents()->IsActiveEntry(page_id)) | |
172 InstantSupportChanged(supports_instant); | |
173 } | |
174 | |
126 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { | 175 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { |
127 if (web_contents()->IsActiveEntry(page_id)) | 176 if (web_contents()->IsActiveEntry(page_id)) |
128 model_.SetTopBarsVisible(true); | 177 model_.SetTopBarsVisible(true); |
129 } | 178 } |
130 | 179 |
131 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { | 180 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { |
132 if (web_contents()->IsActiveEntry(page_id)) { | 181 if (web_contents()->IsActiveEntry(page_id)) { |
133 model_.SetTopBarsVisible(false); | 182 model_.SetTopBarsVisible(false); |
134 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); | 183 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); |
135 } | 184 } |
136 } | 185 } |
OLD | NEW |