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

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: Addressed review comments 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.
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 (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 28 matching lines...) Expand all
60 const gfx::Font& omnibox_font = 65 const gfx::Font& omnibox_font =
61 ui::ResourceBundle::GetSharedInstance().GetFont( 66 ui::ResourceBundle::GetSharedInstance().GetFont(
62 ui::ResourceBundle::MediumFont); 67 ui::ResourceBundle::MediumFont);
63 #endif 68 #endif
64 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); 69 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName());
65 size_t omnibox_font_size = omnibox_font.GetFontSize(); 70 size_t omnibox_font_size = omnibox_font.GetFontSize();
66 Send(new ChromeViewMsg_SearchBoxFontInformation( 71 Send(new ChromeViewMsg_SearchBoxFontInformation(
67 routing_id(), omnibox_font_name, omnibox_font_size)); 72 routing_id(), omnibox_font_name, omnibox_font_size));
68 } 73 }
69 74
70 void InstantPage::DetermineIfPageSupportsInstant() {
71 if (IsLocal()) {
72 // Local pages always support Instant. That's why we keep them around.
73 int page_id = contents()->GetController().GetActiveEntry()->GetPageID();
74 OnInstantSupportDetermined(page_id, true);
75 } else {
76 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
77 }
78 }
79
80 void InstantPage::SendAutocompleteResults( 75 void InstantPage::SendAutocompleteResults(
81 const std::vector<InstantAutocompleteResult>& results) { 76 const std::vector<InstantAutocompleteResult>& results) {
82 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); 77 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results));
83 } 78 }
84 79
85 void InstantPage::UpOrDownKeyPressed(int count) { 80 void InstantPage::UpOrDownKeyPressed(int count) {
86 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); 81 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count));
87 } 82 }
88 83
89 void InstantPage::EscKeyPressed() { 84 void InstantPage::EscKeyPressed() {
(...skipping 24 matching lines...) Expand all
114 } 109 }
115 110
116 void InstantPage::SendMostVisitedItems( 111 void InstantPage::SendMostVisitedItems(
117 const std::vector<InstantMostVisitedItemIDPair>& items) { 112 const std::vector<InstantMostVisitedItemIDPair>& items) {
118 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); 113 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items));
119 } 114 }
120 115
121 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) 116 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url)
122 : delegate_(delegate), 117 : delegate_(delegate),
123 instant_url_(instant_url), 118 instant_url_(instant_url),
124 supports_instant_(false), 119 supports_instant_(false) {
125 instant_support_determined_(false) {
126 } 120 }
127 121
128 void InstantPage::SetContents(content::WebContents* contents) { 122 void InstantPage::SetContents(content::WebContents* contents) {
123 if (web_contents())
124 GetSearchModel()->RemoveObserver(this);
125
129 Observe(contents); 126 Observe(contents);
127
128 if (!contents)
129 return;
130
131 SearchModel* model = GetSearchModel();
132 model->AddObserver(this);
133
134 // Already know whether the page supports instant.
135 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
136 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES);
130 } 137 }
131 138
132 bool InstantPage::ShouldProcessRenderViewCreated() { 139 bool InstantPage::ShouldProcessRenderViewCreated() {
133 return false; 140 return false;
134 } 141 }
135 142
136 bool InstantPage::ShouldProcessRenderViewGone() { 143 bool InstantPage::ShouldProcessRenderViewGone() {
137 return false; 144 return false;
138 } 145 }
139 146
(...skipping 15 matching lines...) Expand all
155 162
156 bool InstantPage::ShouldProcessNavigateToURL() { 163 bool InstantPage::ShouldProcessNavigateToURL() {
157 return false; 164 return false;
158 } 165 }
159 166
160 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { 167 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) {
161 if (ShouldProcessRenderViewCreated()) 168 if (ShouldProcessRenderViewCreated())
162 delegate_->InstantPageRenderViewCreated(contents()); 169 delegate_->InstantPageRenderViewCreated(contents());
163 } 170 }
164 171
165 void InstantPage::DidFinishLoad(
166 int64 /* frame_id */,
167 const GURL& /* validated_url */,
168 bool is_main_frame,
169 content::RenderViewHost* /* render_view_host */) {
170 if (is_main_frame && !supports_instant_)
171 DetermineIfPageSupportsInstant();
172 }
173
174 bool InstantPage::OnMessageReceived(const IPC::Message& message) { 172 bool InstantPage::OnMessageReceived(const IPC::Message& message) {
175 bool handled = true; 173 bool handled = true;
176 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) 174 IPC_BEGIN_MESSAGE_MAP(InstantPage, message)
177 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) 175 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
179 OnInstantSupportDetermined)
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, 176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay,
181 OnShowInstantOverlay) 177 OnShowInstantOverlay)
182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) 178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox)
183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, 179 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate,
184 OnSearchBoxNavigate); 180 OnSearchBoxNavigate);
185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, 181 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem,
186 OnDeleteMostVisitedItem); 182 OnDeleteMostVisitedItem);
187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, 183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion,
188 OnUndoMostVisitedDeletion); 184 OnUndoMostVisitedDeletion);
189 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, 185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 int64 /* frame_id */, 218 int64 /* frame_id */,
223 bool is_main_frame, 219 bool is_main_frame,
224 const GURL& /* validated_url */, 220 const GURL& /* validated_url */,
225 int /* error_code */, 221 int /* error_code */,
226 const string16& /* error_description */, 222 const string16& /* error_description */,
227 content::RenderViewHost* /* render_view_host */) { 223 content::RenderViewHost* /* render_view_host */) {
228 if (is_main_frame) 224 if (is_main_frame)
229 delegate_->InstantPageLoadFailed(contents()); 225 delegate_->InstantPageLoadFailed(contents());
230 } 226 }
231 227
228 void InstantPage::ModelChanged(const SearchModel::State& old_state,
229 const SearchModel::State& new_state) {
230 if (old_state.instant_support != new_state.instant_support)
231 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES);
232 }
233
234 SearchModel* InstantPage::GetSearchModel() {
235 return contents() ?
236 SearchTabHelper::FromWebContents(contents())->model() : NULL;
237 }
238
239 void InstantPage::SetSupportsInstant(bool supports_instant) {
240 // Nothing to do if the page already supports Instant.
241 if (supports_instant_)
242 return;
243
244 supports_instant_ = supports_instant;
245 delegate_->InstantSupportDetermined(contents(), supports_instant);
246
247 // If the page doesn't support Instant, stop listening to it.
248 if (!supports_instant)
249 SetContents(NULL);
250 }
251
232 void InstantPage::OnSetSuggestions( 252 void InstantPage::OnSetSuggestions(
233 int page_id, 253 int page_id,
234 const std::vector<InstantSuggestion>& suggestions) { 254 const std::vector<InstantSuggestion>& suggestions) {
235 if (contents()->IsActiveEntry(page_id)) { 255 if (contents()->IsActiveEntry(page_id)) {
236 OnInstantSupportDetermined(page_id, true); 256 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
237 if (ShouldProcessSetSuggestions()) 257 if (ShouldProcessSetSuggestions())
238 delegate_->SetSuggestions(contents(), suggestions); 258 delegate_->SetSuggestions(contents(), suggestions);
239 } 259 }
240 } 260 }
241 261
242 void InstantPage::OnInstantSupportDetermined(int page_id,
243 bool supports_instant) {
244 if (!contents()->IsActiveEntry(page_id) || supports_instant_) {
245 // Nothing to do if the page already supports Instant.
246 return;
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 }
257
258 void InstantPage::OnShowInstantOverlay(int page_id, 262 void InstantPage::OnShowInstantOverlay(int page_id,
259 int height, 263 int height,
260 InstantSizeUnits units) { 264 InstantSizeUnits units) {
261 if (contents()->IsActiveEntry(page_id)) { 265 if (contents()->IsActiveEntry(page_id)) {
262 OnInstantSupportDetermined(page_id, true); 266 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
263 if (ShouldProcessShowInstantOverlay()) 267 if (ShouldProcessShowInstantOverlay())
264 delegate_->ShowInstantOverlay(contents(), height, units); 268 delegate_->ShowInstantOverlay(contents(), height, units);
265 } 269 }
266 } 270 }
267 271
268 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { 272 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) {
269 if (contents()->IsActiveEntry(page_id)) { 273 if (contents()->IsActiveEntry(page_id)) {
270 OnInstantSupportDetermined(page_id, true); 274 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
271 if (ShouldProcessFocusOmnibox()) 275 if (ShouldProcessFocusOmnibox())
272 delegate_->FocusOmnibox(contents(), state); 276 delegate_->FocusOmnibox(contents(), state);
273 } 277 }
274 } 278 }
275 279
276 void InstantPage::OnSearchBoxNavigate(int page_id, 280 void InstantPage::OnSearchBoxNavigate(int page_id,
277 const GURL& url, 281 const GURL& url,
278 content::PageTransition transition, 282 content::PageTransition transition,
279 WindowOpenDisposition disposition) { 283 WindowOpenDisposition disposition) {
280 if (contents()->IsActiveEntry(page_id)) { 284 if (contents()->IsActiveEntry(page_id)) {
281 OnInstantSupportDetermined(page_id, true); 285 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true);
282 if (ShouldProcessNavigateToURL()) 286 if (ShouldProcessNavigateToURL())
283 delegate_->NavigateToURL(contents(), url, transition, disposition); 287 delegate_->NavigateToURL(contents(), url, transition, disposition);
284 } 288 }
285 } 289 }
286 290
287 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { 291 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) {
288 delegate_->DeleteMostVisitedItem(restricted_id); 292 delegate_->DeleteMostVisitedItem(restricted_id);
289 } 293 }
290 294
291 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { 295 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) {
292 delegate_->UndoMostVisitedDeletion(restricted_id); 296 delegate_->UndoMostVisitedDeletion(restricted_id);
293 } 297 }
294 298
295 void InstantPage::OnUndoAllMostVisitedDeletions() { 299 void InstantPage::OnUndoAllMostVisitedDeletions() {
296 delegate_->UndoAllMostVisitedDeletions(); 300 delegate_->UndoAllMostVisitedDeletions();
297 } 301 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698