Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/ui/search/instant_page.cc

Issue 14911005: Move instant support to SearchTabHelper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
Jered 2013/05/08 00:16:27 Excitingly, this file now has unit tests. Consider
samarth 2013/05/08 02:38:48 Yes, please.
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/instant_page.h" 5 #include "chrome/browser/ui/search/instant_page.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/search/search.h"
9 #include "chrome/browser/ui/search/search_model.h"
10 #include "chrome/browser/ui/search/search_tab_helper.h"
8 #include "chrome/common/render_messages.h" 11 #include "chrome/common/render_messages.h"
9 #include "chrome/common/url_constants.h" 12 #include "chrome/common/url_constants.h"
10 #include "content/public/browser/navigation_controller.h" 13 #include "content/public/browser/navigation_controller.h"
11 #include "content/public/browser/navigation_details.h" 14 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/navigation_entry.h" 15 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/frame_navigate_params.h" 17 #include "content/public/common/frame_navigate_params.h"
15 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/font.h" 19 #include "ui/gfx/font.h"
17 20
18 InstantPage::Delegate::~Delegate() { 21 InstantPage::Delegate::~Delegate() {
19 } 22 }
20 23
21 InstantPage::~InstantPage() { 24 InstantPage::~InstantPage() {
25 if (extended_enabled_ && contents())
26 GetSearchModel()->RemoveObserver(this);
22 } 27 }
23 28
24 bool InstantPage::IsLocal() const { 29 bool InstantPage::IsLocal() const {
25 return contents() && 30 return contents() &&
26 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || 31 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) ||
27 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); 32 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl));
28 } 33 }
29 34
30 void InstantPage::Update(const string16& text, 35 void InstantPage::Update(const string16& text,
31 size_t selection_start, 36 size_t selection_start,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 119 }
115 120
116 void InstantPage::SendMostVisitedItems( 121 void InstantPage::SendMostVisitedItems(
117 const std::vector<InstantMostVisitedItemIDPair>& items) { 122 const std::vector<InstantMostVisitedItemIDPair>& items) {
118 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); 123 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
119 } 124 }
120 125
121 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) 126 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url)
122 : delegate_(delegate), 127 : delegate_(delegate),
123 instant_url_(instant_url), 128 instant_url_(instant_url),
129 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()),
124 supports_instant_(false), 130 supports_instant_(false),
125 instant_support_determined_(false) { 131 instant_support_determined_(false) {
126 } 132 }
127 133
128 void InstantPage::SetContents(content::WebContents* contents) { 134 void InstantPage::SetContents(content::WebContents* contents) {
135 if (extended_enabled_ && web_contents())
136 GetSearchModel()->RemoveObserver(this);
137
129 Observe(contents); 138 Observe(contents);
139
140 if (extended_enabled_ && contents) {
141 SearchModel* model = GetSearchModel();
142 model->AddObserver(this);
143
144 // Already know whether the page supports instant.
145 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
146 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES);
147 }
130 } 148 }
131 149
132 bool InstantPage::ShouldProcessRenderViewCreated() { 150 bool InstantPage::ShouldProcessRenderViewCreated() {
133 return false; 151 return false;
134 } 152 }
135 153
136 bool InstantPage::ShouldProcessRenderViewGone() { 154 bool InstantPage::ShouldProcessRenderViewGone() {
137 return false; 155 return false;
138 } 156 }
139 157
(...skipping 20 matching lines...) Expand all
160 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { 178 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) {
161 if (ShouldProcessRenderViewCreated()) 179 if (ShouldProcessRenderViewCreated())
162 delegate_->InstantPageRenderViewCreated(contents()); 180 delegate_->InstantPageRenderViewCreated(contents());
163 } 181 }
164 182
165 void InstantPage::DidFinishLoad( 183 void InstantPage::DidFinishLoad(
166 int64 /* frame_id */, 184 int64 /* frame_id */,
167 const GURL& /* validated_url */, 185 const GURL& /* validated_url */,
168 bool is_main_frame, 186 bool is_main_frame,
169 content::RenderViewHost* /* render_view_host */) { 187 content::RenderViewHost* /* render_view_host */) {
170 if (is_main_frame && !supports_instant_) 188 // For Instant Extended, SearchTabHelper keeps track of Instant support.
189 if (!extended_enabled_ && is_main_frame && !supports_instant_)
171 DetermineIfPageSupportsInstant(); 190 DetermineIfPageSupportsInstant();
172 } 191 }
173 192
174 bool InstantPage::OnMessageReceived(const IPC::Message& message) { 193 bool InstantPage::OnMessageReceived(const IPC::Message& message) {
175 bool handled = true; 194 bool handled = true;
176 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) 195 IPC_BEGIN_MESSAGE_MAP(InstantPage, message)
177 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) 196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, 197 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
samarth 2013/05/08 02:38:48 As I mentioned before, if you wait for Sreeram's c
kmadhusu 2013/05/09 02:40:40 Sure. I will wait for that CL to get committed.
179 OnInstantSupportDetermined) 198 OnInstantSupportDetermined)
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, 199 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay,
181 OnShowInstantOverlay) 200 OnShowInstantOverlay)
182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) 201 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox)
183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, 202 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
184 OnSearchBoxNavigate); 203 OnSearchBoxNavigate);
185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, 204 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
186 OnDeleteMostVisitedItem); 205 OnDeleteMostVisitedItem);
187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, 206 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
188 OnUndoMostVisitedDeletion); 207 OnUndoMostVisitedDeletion);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 int64 /* frame_id */, 241 int64 /* frame_id */,
223 bool is_main_frame, 242 bool is_main_frame,
224 const GURL& /* validated_url */, 243 const GURL& /* validated_url */,
225 int /* error_code */, 244 int /* error_code */,
226 const string16& /* error_description */, 245 const string16& /* error_description */,
227 content::RenderViewHost* /* render_view_host */) { 246 content::RenderViewHost* /* render_view_host */) {
228 if (is_main_frame) 247 if (is_main_frame)
229 delegate_->InstantPageLoadFailed(contents()); 248 delegate_->InstantPageLoadFailed(contents());
230 } 249 }
231 250
251 void InstantPage::ModelChanged(const SearchModel::State& old_state,
252 const SearchModel::State& new_state) {
253 if (old_state.instant_support != new_state.instant_support)
254 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES);
255 }
256
257 SearchModel* InstantPage::GetSearchModel() {
258 return contents() ?
259 SearchTabHelper::FromWebContents(contents())->model() : NULL;
260 }
261
262 void InstantPage::SetSupportsInstant(bool supports_instant) {
263 if (supports_instant_ == supports_instant)
264 return;
265
266 supports_instant_ = supports_instant;
267 delegate_->InstantSupportDetermined(contents(), supports_instant);
268
269 // If the page doesn't support Instant, stop listening to it.
270 if (!supports_instant)
271 SetContents(NULL);
272 }
273
232 void InstantPage::OnSetSuggestions( 274 void InstantPage::OnSetSuggestions(
233 int page_id, 275 int page_id,
234 const std::vector<InstantSuggestion>& suggestions) { 276 const std::vector<InstantSuggestion>& suggestions) {
235 if (contents()->IsActiveEntry(page_id)) { 277 if (contents()->IsActiveEntry(page_id)) {
236 OnInstantSupportDetermined(page_id, true); 278 SetSupportsInstant(true);
samarth 2013/05/08 02:38:48 So in general, we consider a page to support Insta
kmadhusu 2013/05/09 02:40:40 Done.
237 if (ShouldProcessSetSuggestions()) 279 if (ShouldProcessSetSuggestions())
238 delegate_->SetSuggestions(contents(), suggestions); 280 delegate_->SetSuggestions(contents(), suggestions);
239 } 281 }
240 } 282 }
241 283
242 void InstantPage::OnInstantSupportDetermined(int page_id, 284 void InstantPage::OnInstantSupportDetermined(int page_id,
243 bool supports_instant) { 285 bool supports_instant) {
244 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { 286 // For Instant Extended, SearchTabHelper keeps track of Instant support.
245 // Nothing to do if the page already supports Instant. 287 if (!extended_enabled_ && contents()->IsActiveEntry(page_id))
246 return; 288 SetSupportsInstant(supports_instant);
247 }
248
249 instant_support_determined_ = true;
250 supports_instant_ = supports_instant;
251 delegate_->InstantSupportDetermined(contents(), supports_instant);
252
253 // If the page doesn't support Instant, stop listening to it.
254 if (!supports_instant)
255 Observe(NULL);
256 } 289 }
257 290
258 void InstantPage::OnShowInstantOverlay(int page_id, 291 void InstantPage::OnShowInstantOverlay(int page_id,
259 int height, 292 int height,
260 InstantSizeUnits units) { 293 InstantSizeUnits units) {
261 if (contents()->IsActiveEntry(page_id)) { 294 if (contents()->IsActiveEntry(page_id)) {
262 OnInstantSupportDetermined(page_id, true); 295 SetSupportsInstant(true);
263 if (ShouldProcessShowInstantOverlay()) 296 if (ShouldProcessShowInstantOverlay())
264 delegate_->ShowInstantOverlay(contents(), height, units); 297 delegate_->ShowInstantOverlay(contents(), height, units);
265 } 298 }
266 } 299 }
267 300
268 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { 301 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) {
269 if (contents()->IsActiveEntry(page_id)) { 302 if (contents()->IsActiveEntry(page_id)) {
270 OnInstantSupportDetermined(page_id, true); 303 SetSupportsInstant(true);
271 if (ShouldProcessFocusOmnibox()) 304 if (ShouldProcessFocusOmnibox())
272 delegate_->FocusOmnibox(contents(), state); 305 delegate_->FocusOmnibox(contents(), state);
273 } 306 }
274 } 307 }
275 308
276 void InstantPage::OnSearchBoxNavigate(int page_id, 309 void InstantPage::OnSearchBoxNavigate(int page_id,
277 const GURL& url, 310 const GURL& url,
278 content::PageTransition transition, 311 content::PageTransition transition,
279 WindowOpenDisposition disposition) { 312 WindowOpenDisposition disposition) {
280 if (contents()->IsActiveEntry(page_id)) { 313 if (contents()->IsActiveEntry(page_id)) {
281 OnInstantSupportDetermined(page_id, true); 314 SetSupportsInstant(true);
282 if (ShouldProcessNavigateToURL()) 315 if (ShouldProcessNavigateToURL())
283 delegate_->NavigateToURL(contents(), url, transition, disposition); 316 delegate_->NavigateToURL(contents(), url, transition, disposition);
284 } 317 }
285 } 318 }
286 319
287 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { 320 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) {
288 delegate_->DeleteMostVisitedItem(restricted_id); 321 delegate_->DeleteMostVisitedItem(restricted_id);
289 } 322 }
290 323
291 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { 324 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) {
292 delegate_->UndoMostVisitedDeletion(restricted_id); 325 delegate_->UndoMostVisitedDeletion(restricted_id);
293 } 326 }
294 327
295 void InstantPage::OnUndoAllMostVisitedDeletions() { 328 void InstantPage::OnUndoAllMostVisitedDeletions() {
296 delegate_->UndoAllMostVisitedDeletions(); 329 delegate_->UndoAllMostVisitedDeletions();
297 } 330 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698