| 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 SearchTabHelper::FromWebContents(contents())->model()->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 ClearContents(); |
| 141 |
| 142 if (!web_contents) |
| 143 return; |
| 144 |
| 145 Observe(web_contents); |
| 146 SearchModel* model = SearchTabHelper::FromWebContents(contents())->model(); |
| 147 model->AddObserver(this); |
| 148 |
| 149 // Already know whether the page supports instant. |
| 150 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) |
| 151 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); |
| 147 } | 152 } |
| 148 | 153 |
| 149 bool InstantPage::ShouldProcessRenderViewCreated() { | 154 bool InstantPage::ShouldProcessRenderViewCreated() { |
| 150 return false; | 155 return false; |
| 151 } | 156 } |
| 152 | 157 |
| 153 bool InstantPage::ShouldProcessRenderViewGone() { | 158 bool InstantPage::ShouldProcessRenderViewGone() { |
| 154 return false; | 159 return false; |
| 155 } | 160 } |
| 156 | 161 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 184 | 189 |
| 185 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { | 190 bool InstantPage::ShouldProcessUndoAllMostVisitedDeletions() { |
| 186 return false; | 191 return false; |
| 187 } | 192 } |
| 188 | 193 |
| 189 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 194 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { |
| 190 if (ShouldProcessRenderViewCreated()) | 195 if (ShouldProcessRenderViewCreated()) |
| 191 delegate_->InstantPageRenderViewCreated(contents()); | 196 delegate_->InstantPageRenderViewCreated(contents()); |
| 192 } | 197 } |
| 193 | 198 |
| 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) { | 199 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
| 204 bool handled = true; | 200 bool handled = true; |
| 205 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 201 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
| 206 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 202 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
| 207 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 208 OnInstantSupportDetermined) | |
| 209 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 203 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, |
| 210 OnShowInstantOverlay) | 204 OnShowInstantOverlay) |
| 211 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) | 205 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) |
| 212 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | 206 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, |
| 213 OnSearchBoxNavigate); | 207 OnSearchBoxNavigate); |
| 214 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, | 208 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, |
| 215 OnDeleteMostVisitedItem); | 209 OnDeleteMostVisitedItem); |
| 216 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, | 210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, |
| 217 OnUndoMostVisitedDeletion); | 211 OnUndoMostVisitedDeletion); |
| 218 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, | 212 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 int64 /* frame_id */, | 245 int64 /* frame_id */, |
| 252 bool is_main_frame, | 246 bool is_main_frame, |
| 253 const GURL& /* validated_url */, | 247 const GURL& /* validated_url */, |
| 254 int /* error_code */, | 248 int /* error_code */, |
| 255 const string16& /* error_description */, | 249 const string16& /* error_description */, |
| 256 content::RenderViewHost* /* render_view_host */) { | 250 content::RenderViewHost* /* render_view_host */) { |
| 257 if (is_main_frame) | 251 if (is_main_frame) |
| 258 delegate_->InstantPageLoadFailed(contents()); | 252 delegate_->InstantPageLoadFailed(contents()); |
| 259 } | 253 } |
| 260 | 254 |
| 255 void InstantPage::ModelChanged(const SearchModel::State& old_state, |
| 256 const SearchModel::State& new_state) { |
| 257 if (old_state.instant_support != new_state.instant_support) |
| 258 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); |
| 259 } |
| 260 |
| 261 void InstantPage::InstantSupportDetermined(bool supports_instant) { |
| 262 delegate_->InstantSupportDetermined(contents(), supports_instant); |
| 263 |
| 264 // If the page doesn't support Instant, stop listening to it. |
| 265 if (!supports_instant) |
| 266 ClearContents(); |
| 267 } |
| 268 |
| 261 void InstantPage::OnSetSuggestions( | 269 void InstantPage::OnSetSuggestions( |
| 262 int page_id, | 270 int page_id, |
| 263 const std::vector<InstantSuggestion>& suggestions) { | 271 const std::vector<InstantSuggestion>& suggestions) { |
| 264 if (!contents()->IsActiveEntry(page_id)) | 272 if (!contents()->IsActiveEntry(page_id)) |
| 265 return; | 273 return; |
| 266 | 274 |
| 267 OnInstantSupportDetermined(page_id, true); | 275 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 268 if (!ShouldProcessSetSuggestions()) | 276 if (!ShouldProcessSetSuggestions()) |
| 269 return; | 277 return; |
| 270 | 278 |
| 271 delegate_->SetSuggestions(contents(), suggestions); | 279 delegate_->SetSuggestions(contents(), suggestions); |
| 272 } | 280 } |
| 273 | 281 |
| 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, | 282 void InstantPage::OnShowInstantOverlay(int page_id, |
| 291 int height, | 283 int height, |
| 292 InstantSizeUnits units) { | 284 InstantSizeUnits units) { |
| 293 if (!contents()->IsActiveEntry(page_id)) | 285 if (!contents()->IsActiveEntry(page_id)) |
| 294 return; | 286 return; |
| 295 | 287 |
| 296 OnInstantSupportDetermined(page_id, true); | 288 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 297 delegate_->LogDropdownShown(); | 289 delegate_->LogDropdownShown(); |
| 298 if (!ShouldProcessShowInstantOverlay()) | 290 if (!ShouldProcessShowInstantOverlay()) |
| 299 return; | 291 return; |
| 300 | 292 |
| 301 delegate_->ShowInstantOverlay(contents(), height, units); | 293 delegate_->ShowInstantOverlay(contents(), height, units); |
| 302 } | 294 } |
| 303 | 295 |
| 304 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { | 296 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { |
| 305 if (!contents()->IsActiveEntry(page_id)) | 297 if (!contents()->IsActiveEntry(page_id)) |
| 306 return; | 298 return; |
| 307 | 299 |
| 308 OnInstantSupportDetermined(page_id, true); | 300 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 309 if (!ShouldProcessFocusOmnibox()) | 301 if (!ShouldProcessFocusOmnibox()) |
| 310 return; | 302 return; |
| 311 | 303 |
| 312 delegate_->FocusOmnibox(contents(), state); | 304 delegate_->FocusOmnibox(contents(), state); |
| 313 } | 305 } |
| 314 | 306 |
| 315 void InstantPage::OnSearchBoxNavigate(int page_id, | 307 void InstantPage::OnSearchBoxNavigate(int page_id, |
| 316 const GURL& url, | 308 const GURL& url, |
| 317 content::PageTransition transition, | 309 content::PageTransition transition, |
| 318 WindowOpenDisposition disposition, | 310 WindowOpenDisposition disposition, |
| 319 bool is_search_type) { | 311 bool is_search_type) { |
| 320 if (!contents()->IsActiveEntry(page_id)) | 312 if (!contents()->IsActiveEntry(page_id)) |
| 321 return; | 313 return; |
| 322 | 314 |
| 323 OnInstantSupportDetermined(page_id, true); | 315 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 324 if (!ShouldProcessNavigateToURL()) | 316 if (!ShouldProcessNavigateToURL()) |
| 325 return; | 317 return; |
| 326 | 318 |
| 327 delegate_->NavigateToURL( | 319 delegate_->NavigateToURL( |
| 328 contents(), url, transition, disposition, is_search_type); | 320 contents(), url, transition, disposition, is_search_type); |
| 329 } | 321 } |
| 330 | 322 |
| 331 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { | 323 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { |
| 332 if (!contents()->IsActiveEntry(page_id)) | 324 if (!contents()->IsActiveEntry(page_id)) |
| 333 return; | 325 return; |
| 334 | 326 |
| 335 OnInstantSupportDetermined(page_id, true); | 327 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 336 if (!ShouldProcessDeleteMostVisitedItem()) | 328 if (!ShouldProcessDeleteMostVisitedItem()) |
| 337 return; | 329 return; |
| 338 | 330 |
| 339 delegate_->DeleteMostVisitedItem(url); | 331 delegate_->DeleteMostVisitedItem(url); |
| 340 } | 332 } |
| 341 | 333 |
| 342 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { | 334 void InstantPage::OnUndoMostVisitedDeletion(int page_id, const GURL& url) { |
| 343 if (!contents()->IsActiveEntry(page_id)) | 335 if (!contents()->IsActiveEntry(page_id)) |
| 344 return; | 336 return; |
| 345 | 337 |
| 346 OnInstantSupportDetermined(page_id, true); | 338 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 347 if (!ShouldProcessUndoMostVisitedDeletion()) | 339 if (!ShouldProcessUndoMostVisitedDeletion()) |
| 348 return; | 340 return; |
| 349 | 341 |
| 350 delegate_->UndoMostVisitedDeletion(url); | 342 delegate_->UndoMostVisitedDeletion(url); |
| 351 } | 343 } |
| 352 | 344 |
| 353 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { | 345 void InstantPage::OnUndoAllMostVisitedDeletions(int page_id) { |
| 354 if (!contents()->IsActiveEntry(page_id)) | 346 if (!contents()->IsActiveEntry(page_id)) |
| 355 return; | 347 return; |
| 356 | 348 |
| 357 OnInstantSupportDetermined(page_id, true); | 349 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
| 358 if (!ShouldProcessUndoAllMostVisitedDeletions()) | 350 if (!ShouldProcessUndoAllMostVisitedDeletions()) |
| 359 return; | 351 return; |
| 360 | 352 |
| 361 delegate_->UndoAllMostVisitedDeletions(); | 353 delegate_->UndoAllMostVisitedDeletions(); |
| 362 } | 354 } |
| 355 |
| 356 void InstantPage::ClearContents() { |
| 357 if (contents()) |
| 358 SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this); |
| 359 |
| 360 Observe(NULL); |
| 361 } |
| OLD | NEW |