| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 103 } |
| 104 | 104 |
| 105 } // namespace | 105 } // namespace |
| 106 | 106 |
| 107 InstantController::InstantController(InstantControllerDelegate* delegate, | 107 InstantController::InstantController(InstantControllerDelegate* delegate, |
| 108 Mode mode) | 108 Mode mode) |
| 109 : delegate_(delegate), | 109 : delegate_(delegate), |
| 110 mode_(mode), | 110 mode_(mode), |
| 111 last_active_tab_(NULL), | 111 last_active_tab_(NULL), |
| 112 last_verbatim_(false), | 112 last_verbatim_(false), |
| 113 last_complete_behavior_(INSTANT_COMPLETE_NOW), | |
| 114 last_transition_type_(content::PAGE_TRANSITION_LINK), | 113 last_transition_type_(content::PAGE_TRANSITION_LINK), |
| 115 is_showing_(false), | 114 is_showing_(false), |
| 116 loader_processed_last_update_(false) { | 115 loader_processed_last_update_(false) { |
| 117 } | 116 } |
| 118 | 117 |
| 119 InstantController::~InstantController() { | 118 InstantController::~InstantController() { |
| 120 if (GetPreviewContents()) | 119 if (GetPreviewContents()) |
| 121 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | 120 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); |
| 122 } | 121 } |
| 123 | 122 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 181 |
| 183 last_user_text_ = user_text; | 182 last_user_text_ = user_text; |
| 184 | 183 |
| 185 // Don't send an update to the loader if the query text hasn't changed. | 184 // Don't send an update to the loader if the query text hasn't changed. |
| 186 if (full_text == last_full_text_ && verbatim == last_verbatim_) { | 185 if (full_text == last_full_text_ && verbatim == last_verbatim_) { |
| 187 // Since we are updating |suggested_text|, shouldn't we also update | 186 // Since we are updating |suggested_text|, shouldn't we also update |
| 188 // |last_full_text_|? No. There's no guarantee that our suggestion will | 187 // |last_full_text_|? No. There's no guarantee that our suggestion will |
| 189 // actually be inline autocompleted. For example, it may get trumped by | 188 // actually be inline autocompleted. For example, it may get trumped by |
| 190 // a history suggestion. If our suggestion does make it, the omnibox will | 189 // a history suggestion. If our suggestion does make it, the omnibox will |
| 191 // call Update() again, at which time we'll update |last_full_text_|. | 190 // call Update() again, at which time we'll update |last_full_text_|. |
| 192 *suggested_text = last_suggestion_; | 191 *suggested_text = last_suggestion_.text; |
| 193 *complete_behavior = last_complete_behavior_; | 192 *complete_behavior = last_suggestion_.behavior; |
| 194 | 193 |
| 195 // We need to call Show() here because of this: | 194 // We need to call Show() here because of this: |
| 196 // 1. User has typed a query (say Q). Instant overlay is showing results. | 195 // 1. User has typed a query (say Q). Instant overlay is showing results. |
| 197 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of | 196 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of |
| 198 // these cause the overlay to Hide(). | 197 // these cause the overlay to Hide(). |
| 199 // 3. User arrows-up to Q or types Q again. The last text we processed is | 198 // 3. User arrows-up to Q or types Q again. The last text we processed is |
| 200 // still Q, so we don't Update() the loader, but we do need to Show(). | 199 // still Q, so we don't Update() the loader, but we do need to Show(). |
| 201 if (loader_processed_last_update_ && mode_ == INSTANT) | 200 if (loader_processed_last_update_ && mode_ == INSTANT) |
| 202 Show(); | 201 Show(); |
| 203 return true; | 202 return true; |
| 204 } | 203 } |
| 205 | 204 |
| 206 last_full_text_ = full_text; | 205 last_full_text_ = full_text; |
| 207 last_verbatim_ = verbatim; | 206 last_verbatim_ = verbatim; |
| 208 loader_processed_last_update_ = false; | 207 loader_processed_last_update_ = false; |
| 209 | 208 |
| 210 // Reset the last suggestion, as it's no longer valid. | 209 // Reset the last suggestion, as it's no longer valid. |
| 211 suggested_text->clear(); | 210 suggested_text->clear(); |
| 212 last_suggestion_.clear(); | 211 last_suggestion_ = InstantSuggestion(); |
| 213 *complete_behavior = last_complete_behavior_ = INSTANT_COMPLETE_NOW; | 212 *complete_behavior = INSTANT_COMPLETE_NOW; |
| 214 | 213 |
| 215 if (mode_ != SILENT) { | 214 if (mode_ != SILENT) { |
| 216 loader_->Update(last_full_text_, last_verbatim_); | 215 loader_->Update(last_full_text_, last_verbatim_); |
| 217 | 216 |
| 218 content::NotificationService::current()->Notify( | 217 content::NotificationService::current()->Notify( |
| 219 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 218 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
| 220 content::Source<InstantController>(this), | 219 content::Source<InstantController>(this), |
| 221 content::NotificationService::NoDetails()); | 220 content::NotificationService::NoDetails()); |
| 222 } | 221 } |
| 223 | 222 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 | 416 |
| 418 ResetLoader(instant_url, active_tab); | 417 ResetLoader(instant_url, active_tab); |
| 419 } | 418 } |
| 420 | 419 |
| 421 bool InstantController::commit_on_pointer_release() const { | 420 bool InstantController::commit_on_pointer_release() const { |
| 422 return GetPreviewContents() && loader_->IsPointerDownFromActivate(); | 421 return GetPreviewContents() && loader_->IsPointerDownFromActivate(); |
| 423 } | 422 } |
| 424 | 423 |
| 425 void InstantController::SetSuggestions( | 424 void InstantController::SetSuggestions( |
| 426 InstantLoader* loader, | 425 InstantLoader* loader, |
| 427 const std::vector<string16>& suggestions, | 426 const std::vector<InstantSuggestion>& suggestions) { |
| 428 InstantCompleteBehavior behavior) { | |
| 429 DCHECK_EQ(loader_.get(), loader); | 427 DCHECK_EQ(loader_.get(), loader); |
| 430 if (loader_ != loader || IsOutOfDate() || mode_ == SILENT || mode_ == HIDDEN) | 428 if (loader_ != loader || IsOutOfDate() || mode_ == SILENT || mode_ == HIDDEN) |
| 431 return; | 429 return; |
| 432 | 430 |
| 433 loader_processed_last_update_ = true; | 431 loader_processed_last_update_ = true; |
| 434 | 432 |
| 435 string16 suggestion; | 433 InstantSuggestion suggestion; |
| 436 if (!suggestions.empty()) | 434 if (!suggestions.empty()) |
| 437 suggestion = suggestions[0]; | 435 suggestion = suggestions[0]; |
| 438 | 436 |
| 439 string16 suggestion_lower = base::i18n::ToLower(suggestion); | 437 string16 suggestion_lower = base::i18n::ToLower(suggestion.text); |
| 440 string16 user_text_lower = base::i18n::ToLower(last_user_text_); | 438 string16 user_text_lower = base::i18n::ToLower(last_user_text_); |
| 441 if (user_text_lower.size() >= suggestion_lower.size() || | 439 if (user_text_lower.size() >= suggestion_lower.size() || |
| 442 suggestion_lower.compare(0, user_text_lower.size(), user_text_lower)) { | 440 suggestion_lower.compare(0, user_text_lower.size(), user_text_lower)) { |
| 443 suggestion.clear(); | 441 suggestion.text.clear(); |
| 444 } else { | 442 } else { |
| 445 suggestion.erase(0, last_user_text_.size()); | 443 suggestion.text.erase(0, last_user_text_.size()); |
| 446 } | 444 } |
| 447 | 445 |
| 448 last_suggestion_ = suggestion; | 446 last_suggestion_ = suggestion; |
| 449 last_complete_behavior_ = behavior; | |
| 450 if (!last_verbatim_) | 447 if (!last_verbatim_) |
| 451 delegate_->SetSuggestedText(suggestion, behavior); | 448 delegate_->SetSuggestedText(suggestion.text, suggestion.behavior); |
| 452 | 449 |
| 453 if (mode_ != SUGGEST) | 450 if (mode_ != SUGGEST) |
| 454 Show(); | 451 Show(); |
| 455 } | 452 } |
| 456 | 453 |
| 457 void InstantController::CommitInstantLoader(InstantLoader* loader) { | 454 void InstantController::CommitInstantLoader(InstantLoader* loader) { |
| 458 DCHECK_EQ(loader_.get(), loader); | 455 DCHECK_EQ(loader_.get(), loader); |
| 459 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; | 456 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; |
| 460 if (loader_ != loader || !is_showing_ || IsOutOfDate()) | 457 if (loader_ != loader || !is_showing_ || IsOutOfDate()) |
| 461 return; | 458 return; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 loader_->Init(); | 516 loader_->Init(); |
| 520 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); | 517 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); |
| 521 } | 518 } |
| 522 } | 519 } |
| 523 | 520 |
| 524 void InstantController::DeleteLoader() { | 521 void InstantController::DeleteLoader() { |
| 525 Hide(); | 522 Hide(); |
| 526 last_full_text_.clear(); | 523 last_full_text_.clear(); |
| 527 last_user_text_.clear(); | 524 last_user_text_.clear(); |
| 528 last_verbatim_ = false; | 525 last_verbatim_ = false; |
| 529 last_suggestion_.clear(); | 526 last_suggestion_ = InstantSuggestion(); |
| 530 last_complete_behavior_ = INSTANT_COMPLETE_NOW; | |
| 531 last_transition_type_ = content::PAGE_TRANSITION_LINK; | 527 last_transition_type_ = content::PAGE_TRANSITION_LINK; |
| 532 last_omnibox_bounds_ = gfx::Rect(); | 528 last_omnibox_bounds_ = gfx::Rect(); |
| 533 url_for_history_ = GURL(); | 529 url_for_history_ = GURL(); |
| 534 if (GetPreviewContents()) | 530 if (GetPreviewContents()) |
| 535 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | 531 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); |
| 536 loader_.reset(); | 532 loader_.reset(); |
| 537 } | 533 } |
| 538 | 534 |
| 539 void InstantController::Show() { | 535 void InstantController::Show() { |
| 540 if (!is_showing_) { | 536 if (!is_showing_) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 return false; | 594 return false; |
| 599 } | 595 } |
| 600 | 596 |
| 601 return true; | 597 return true; |
| 602 } | 598 } |
| 603 | 599 |
| 604 bool InstantController::IsOutOfDate() const { | 600 bool InstantController::IsOutOfDate() const { |
| 605 return !last_active_tab_ || | 601 return !last_active_tab_ || |
| 606 last_active_tab_ != delegate_->GetActiveTabContents(); | 602 last_active_tab_ != delegate_->GetActiveTabContents(); |
| 607 } | 603 } |
| OLD | NEW |