Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/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::supports_instant() const { | 29 bool InstantPage::supports_instant() const { |
| 25 return supports_instant_; | 30 return contents() ? |
| 31 SearchTabHelper::FromWebContents(contents())->SupportsInstant() : false; | |
| 26 } | 32 } |
| 27 | 33 |
| 28 const std::string& InstantPage::instant_url() const { | 34 const std::string& InstantPage::instant_url() const { |
| 29 return instant_url_; | 35 return instant_url_; |
| 30 } | 36 } |
| 31 | 37 |
| 32 bool InstantPage::IsLocal() const { | 38 bool InstantPage::IsLocal() const { |
| 33 return contents() && | 39 return contents() && |
| 34 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | 40 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || |
| 35 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 41 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 const gfx::Font& omnibox_font = | 74 const gfx::Font& omnibox_font = |
| 69 ui::ResourceBundle::GetSharedInstance().GetFont( | 75 ui::ResourceBundle::GetSharedInstance().GetFont( |
| 70 ui::ResourceBundle::MediumFont); | 76 ui::ResourceBundle::MediumFont); |
| 71 #endif | 77 #endif |
| 72 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); | 78 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); |
| 73 size_t omnibox_font_size = omnibox_font.GetFontSize(); | 79 size_t omnibox_font_size = omnibox_font.GetFontSize(); |
| 74 Send(new ChromeViewMsg_SearchBoxFontInformation( | 80 Send(new ChromeViewMsg_SearchBoxFontInformation( |
| 75 routing_id(), omnibox_font_name, omnibox_font_size)); | 81 routing_id(), omnibox_font_name, omnibox_font_size)); |
| 76 } | 82 } |
| 77 | 83 |
| 78 void InstantPage::DetermineIfPageSupportsInstant() { | |
| 79 if (IsLocal()) { | |
| 80 // Local pages always support Instant. That's why we keep them around. | |
| 81 int page_id = contents()->GetController().GetActiveEntry()->GetPageID(); | |
| 82 OnInstantSupportDetermined(page_id, true); | |
| 83 } else { | |
| 84 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void InstantPage::SendAutocompleteResults( | 84 void InstantPage::SendAutocompleteResults( |
| 89 const std::vector<InstantAutocompleteResult>& results) { | 85 const std::vector<InstantAutocompleteResult>& results) { |
| 90 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | 86 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); |
| 91 } | 87 } |
| 92 | 88 |
| 93 void InstantPage::UpOrDownKeyPressed(int count) { | 89 void InstantPage::UpOrDownKeyPressed(int count) { |
| 94 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | 90 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); |
| 95 } | 91 } |
| 96 | 92 |
| 97 void InstantPage::EscKeyPressed() { | 93 void InstantPage::EscKeyPressed() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 const std::vector<InstantMostVisitedItem>& items) { | 126 const std::vector<InstantMostVisitedItem>& items) { |
| 131 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); | 127 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); |
| 132 } | 128 } |
| 133 | 129 |
| 134 void InstantPage::ToggleVoiceSearch() { | 130 void InstantPage::ToggleVoiceSearch() { |
| 135 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id())); | 131 Send(new ChromeViewMsg_SearchBoxToggleVoiceSearch(routing_id())); |
| 136 } | 132 } |
| 137 | 133 |
| 138 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) | 134 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) |
| 139 : delegate_(delegate), | 135 : delegate_(delegate), |
| 140 instant_url_(instant_url), | 136 instant_url_(instant_url) { |
| 141 supports_instant_(false), | |
| 142 instant_support_determined_(false) { | |
| 143 } | 137 } |
| 144 | 138 |
| 145 void InstantPage::SetContents(content::WebContents* contents) { | 139 void InstantPage::SetContents(content::WebContents* web_contents) { |
| 146 Observe(contents); | 140 if (contents()) |
| 141 GetSearchModel()->RemoveObserver(this); | |
| 142 | |
| 143 Observe(web_contents); | |
| 144 | |
| 145 if (!web_contents) | |
| 146 return; | |
| 147 | |
| 148 SearchModel* model = GetSearchModel(); | |
|
Jered
2013/06/19 17:58:17
nit: Remove GetSearchModel() and inline it. It wou
kmadhusu
2013/06/19 18:41:28
Done.
| |
| 149 model->AddObserver(this); | |
| 150 | |
| 151 // Already know whether the page supports instant. | |
| 152 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) | |
| 153 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); | |
|
Jered
2013/06/19 17:58:17
nit: It's confusing that SetContents() calls Insta
kmadhusu
2013/06/19 18:41:28
Done.
| |
| 147 } | 154 } |
| 148 | 155 |
| 149 bool InstantPage::ShouldProcessRenderViewCreated() { | 156 bool InstantPage::ShouldProcessRenderViewCreated() { |
| 150 return false; | 157 return false; |
| 151 } | 158 } |
| 152 | 159 |
| 153 bool InstantPage::ShouldProcessRenderViewGone() { | 160 bool InstantPage::ShouldProcessRenderViewGone() { |
| 154 return false; | 161 return false; |
| 155 } | 162 } |
| 156 | 163 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 184 | 191 |
| 185 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { | 192 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { |
| 186 return false; | 193 return false; |
| 187 } | 194 } |
| 188 | 195 |
| 189 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 196 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { |
| 190 if (ShouldProcessRenderViewCreated()) | 197 if (ShouldProcessRenderViewCreated()) |
| 191 delegate_->InstantPageRenderViewCreated(contents()); | 198 delegate_->InstantPageRenderViewCreated(contents()); |
| 192 } | 199 } |
| 193 | 200 |
| 194 void InstantPage::DidFinishLoad( | |
| 195 int64 /* frame_id */, | |
| 196 const GURL& /* validated_url */, | |
| 197 bool is_main_frame, | |
| 198 content::RenderViewHost* /* render_view_host */) { | |
| 199 if (is_main_frame && !supports_instant_) | |
| 200 DetermineIfPageSupportsInstant(); | |
| 201 } | |
| 202 | |
| 203 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 201 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
| 204 bool handled = true; | 202 bool handled = true; |
| 205 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 203 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
| 206 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 204 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
| 207 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 208 OnInstantSupportDetermined) | |
| 209 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 205 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, |
| 210 OnShowInstantOverlay) | 206 OnShowInstantOverlay) |
| 211 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) | 207 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) |
| 212 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | 208 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, |
| 213 OnSearchBoxNavigate); | 209 OnSearchBoxNavigate); |
| 214 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, | 210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, |
| 215 OnDeleteMostVisitedItem); | 211 OnDeleteMostVisitedItem); |
| 216 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, | 212 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, |
| 217 OnUndoMostVisitedDeletion); | 213 OnUndoMostVisitedDeletion); |
| 218 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, | 214 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 int64 /* frame_id */, | 247 int64 /* frame_id */, |
| 252 bool is_main_frame, | 248 bool is_main_frame, |
| 253 const GURL& /* validated_url */, | 249 const GURL& /* validated_url */, |
| 254 int /* error_code */, | 250 int /* error_code */, |
| 255 const string16& /* error_description */, | 251 const string16& /* error_description */, |
| 256 content::RenderViewHost* /* render_view_host */) { | 252 content::RenderViewHost* /* render_view_host */) { |
| 257 if (is_main_frame) | 253 if (is_main_frame) |
| 258 delegate_->InstantPageLoadFailed(contents()); | 254 delegate_->InstantPageLoadFailed(contents()); |
| 259 } | 255 } |
| 260 | 256 |
| 257 void InstantPage::ModelChanged(const SearchModel::State& old_state, | |
| 258 const SearchModel::State& new_state) { | |
| 259 if (old_state.instant_support != new_state.instant_support) | |
| 260 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); | |
| 261 } | |
| 262 | |
| 263 SearchModel* InstantPage::GetSearchModel() { | |
| 264 return contents() ? | |
| 265 SearchTabHelper::FromWebContents(contents())->model() : NULL; | |
| 266 } | |
| 267 | |
| 268 void InstantPage::InstantSupportDetermined(bool supports_instant) { | |
| 269 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
| 270 | |
| 271 // If the page doesn't support Instant, stop listening to it. | |
| 272 if (!supports_instant) | |
| 273 SetContents(NULL); | |
| 274 } | |
| 275 | |
| 261 void InstantPage::OnSetSuggestions( | 276 void InstantPage::OnSetSuggestions( |
| 262 int page_id, | 277 int page_id, |
| 263 const std::vector<InstantSuggestion>& suggestions) { | 278 const std::vector<InstantSuggestion>& suggestions) { |
| 264 if (!contents()->IsActiveEntry(page_id)) | 279 if (!contents()->IsActiveEntry(page_id)) |
| 265 return; | 280 return; |
| 266 | 281 |
| 267 OnInstantSupportDetermined(page_id, true); | 282 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 268 if (!ShouldProcessSetSuggestions()) | 283 if (!ShouldProcessSetSuggestions()) |
| 269 return; | 284 return; |
| 270 | 285 |
| 271 delegate_->SetSuggestions(contents(), suggestions); | 286 delegate_->SetSuggestions(contents(), suggestions); |
| 272 } | 287 } |
| 273 | 288 |
| 274 void InstantPage::OnInstantSupportDetermined(int page_id, | |
| 275 bool supports_instant) { | |
| 276 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | |
| 277 // Nothing to do if the page already supports Instant. | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 instant_support_determined_ = true; | |
| 282 supports_instant_ = supports_instant; | |
| 283 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
| 284 | |
| 285 // If the page doesn't support Instant, stop listening to it. | |
| 286 if (!supports_instant) | |
| 287 Observe(NULL); | |
| 288 } | |
| 289 | |
| 290 void InstantPage::OnShowInstantOverlay(int page_id, | 289 void InstantPage::OnShowInstantOverlay(int page_id, |
| 291 int height, | 290 int height, |
| 292 InstantSizeUnits units) { | 291 InstantSizeUnits units) { |
| 293 if (!contents()->IsActiveEntry(page_id)) | 292 if (!contents()->IsActiveEntry(page_id)) |
| 294 return; | 293 return; |
| 295 | 294 |
| 296 OnInstantSupportDetermined(page_id, true); | 295 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 297 delegate_->LogDropdownShown(); | 296 delegate_->LogDropdownShown(); |
| 298 if (!ShouldProcessShowInstantOverlay()) | 297 if (!ShouldProcessShowInstantOverlay()) |
| 299 return; | 298 return; |
| 300 | 299 |
| 301 delegate_->ShowInstantOverlay(contents(), height, units); | 300 delegate_->ShowInstantOverlay(contents(), height, units); |
| 302 } | 301 } |
| 303 | 302 |
| 304 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { | 303 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { |
| 305 if (!contents()->IsActiveEntry(page_id)) | 304 if (!contents()->IsActiveEntry(page_id)) |
| 306 return; | 305 return; |
| 307 | 306 |
| 308 OnInstantSupportDetermined(page_id, true); | 307 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 309 if (!ShouldProcessFocusOmnibox()) | 308 if (!ShouldProcessFocusOmnibox()) |
| 310 return; | 309 return; |
| 311 | 310 |
| 312 delegate_->FocusOmnibox(contents(), state); | 311 delegate_->FocusOmnibox(contents(), state); |
| 313 } | 312 } |
| 314 | 313 |
| 315 void InstantPage::OnSearchBoxNavigate(int page_id, | 314 void InstantPage::OnSearchBoxNavigate(int page_id, |
| 316 const GURL& url, | 315 const GURL& url, |
| 317 content::PageTransition transition, | 316 content::PageTransition transition, |
| 318 WindowOpenDisposition disposition, | 317 WindowOpenDisposition disposition, |
| 319 bool is_search_type) { | 318 bool is_search_type) { |
| 320 if (!contents()->IsActiveEntry(page_id)) | 319 if (!contents()->IsActiveEntry(page_id)) |
| 321 return; | 320 return; |
| 322 | 321 |
| 323 OnInstantSupportDetermined(page_id, true); | 322 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 324 if (!ShouldProcessNavigateToURL()) | 323 if (!ShouldProcessNavigateToURL()) |
| 325 return; | 324 return; |
| 326 | 325 |
| 327 delegate_->NavigateToURL( | 326 delegate_->NavigateToURL( |
| 328 contents(), url, transition, disposition, is_search_type); | 327 contents(), url, transition, disposition, is_search_type); |
| 329 } | 328 } |
| 330 | 329 |
| 331 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { | 330 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { |
| 332 if (!contents()->IsActiveEntry(page_id)) | 331 if (!contents()->IsActiveEntry(page_id)) |
| 333 return; | 332 return; |
| 334 | 333 |
| 335 OnInstantSupportDetermined(page_id, true); | 334 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 336 if (!ShouldProcessDeleteMostVisitedItem()) | 335 if (!ShouldProcessDeleteMostVisitedItem()) |
| 337 return; | 336 return; |
| 338 | 337 |
| 339 delegate_->DeleteMostVisitedItem(url); | 338 delegate_->DeleteMostVisitedItem(url); |
| 340 } | 339 } |
| 341 | 340 |
| 342 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { | 341 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { |
| 343 if (!contents()->IsActiveEntry(page_id)) | 342 if (!contents()->IsActiveEntry(page_id)) |
| 344 return; | 343 return; |
| 345 | 344 |
| 346 OnInstantSupportDetermined(page_id, true); | 345 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 347 if (!ShouldProcessUndoMostVisitedDeletion()) | 346 if (!ShouldProcessUndoMostVisitedDeletion()) |
| 348 return; | 347 return; |
| 349 | 348 |
| 350 delegate_->UndoMostVisitedDeletion(url); | 349 delegate_->UndoMostVisitedDeletion(url); |
| 351 } | 350 } |
| 352 | 351 |
| 353 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { | 352 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { |
| 354 if (!contents()->IsActiveEntry(page_id)) | 353 if (!contents()->IsActiveEntry(page_id)) |
| 355 return; | 354 return; |
| 356 | 355 |
| 357 OnInstantSupportDetermined(page_id, true); | 356 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 358 if (!ShouldProcessUndoAllMostVisitedDeletions()) | 357 if (!ShouldProcessUndoAllMostVisitedDeletions()) |
| 359 return; | 358 return; |
| 360 | 359 |
| 361 delegate_->UndoAllMostVisitedDeletions(); | 360 delegate_->UndoAllMostVisitedDeletions(); |
| 362 } | 361 } |
| OLD | NEW |