| 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 } | 85 } |
| 86 | 86 |
| 87 } // namespace | 87 } // namespace |
| 88 | 88 |
| 89 InstantController::InstantController(InstantControllerDelegate* delegate, | 89 InstantController::InstantController(InstantControllerDelegate* delegate, |
| 90 Mode mode) | 90 Mode mode) |
| 91 : delegate_(delegate), | 91 : delegate_(delegate), |
| 92 mode_(mode), | 92 mode_(mode), |
| 93 last_active_tab_(NULL), | 93 last_active_tab_(NULL), |
| 94 last_verbatim_(false), | 94 last_verbatim_(false), |
| 95 last_complete_behavior_(INSTANT_COMPLETE_NOW), | |
| 96 last_transition_type_(content::PAGE_TRANSITION_LINK), | 95 last_transition_type_(content::PAGE_TRANSITION_LINK), |
| 97 is_showing_(false), | 96 is_showing_(false), |
| 98 loader_processed_last_update_(false) { | 97 loader_processed_last_update_(false) { |
| 99 } | 98 } |
| 100 | 99 |
| 101 InstantController::~InstantController() { | 100 InstantController::~InstantController() { |
| 102 if (GetPreviewContents()) | 101 if (GetPreviewContents()) |
| 103 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | 102 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); |
| 104 } | 103 } |
| 105 | 104 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 163 |
| 165 last_user_text_ = user_text; | 164 last_user_text_ = user_text; |
| 166 | 165 |
| 167 // Don't send an update to the loader if the query text hasn't changed. | 166 // Don't send an update to the loader if the query text hasn't changed. |
| 168 if (full_text == last_full_text_ && verbatim == last_verbatim_) { | 167 if (full_text == last_full_text_ && verbatim == last_verbatim_) { |
| 169 // Since we are updating |suggested_text|, shouldn't we also update | 168 // Since we are updating |suggested_text|, shouldn't we also update |
| 170 // |last_full_text_|? No. There's no guarantee that our suggestion will | 169 // |last_full_text_|? No. There's no guarantee that our suggestion will |
| 171 // actually be inline autocompleted. For example, it may get trumped by | 170 // actually be inline autocompleted. For example, it may get trumped by |
| 172 // a history suggestion. If our suggestion does make it, the omnibox will | 171 // a history suggestion. If our suggestion does make it, the omnibox will |
| 173 // call Update() again, at which time we'll update |last_full_text_|. | 172 // call Update() again, at which time we'll update |last_full_text_|. |
| 174 *suggested_text = last_suggestion_; | 173 *suggested_text = last_suggestion_.text; |
| 175 *complete_behavior = last_complete_behavior_; | 174 *complete_behavior = last_suggestion_.behavior; |
| 176 | 175 |
| 177 // We need to call Show() here because of this: | 176 // We need to call Show() here because of this: |
| 178 // 1. User has typed a query (say Q). Instant overlay is showing results. | 177 // 1. User has typed a query (say Q). Instant overlay is showing results. |
| 179 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of | 178 // 2. User arrows-down to a URL entry or erases all omnibox text. Both of |
| 180 // these cause the overlay to Hide(). | 179 // these cause the overlay to Hide(). |
| 181 // 3. User arrows-up to Q or types Q again. The last text we processed is | 180 // 3. User arrows-up to Q or types Q again. The last text we processed is |
| 182 // still Q, so we don't Update() the loader, but we do need to Show(). | 181 // still Q, so we don't Update() the loader, but we do need to Show(). |
| 183 if (loader_processed_last_update_ && mode_ == INSTANT) | 182 if (loader_processed_last_update_ && mode_ == INSTANT) |
| 184 Show(); | 183 Show(); |
| 185 return true; | 184 return true; |
| 186 } | 185 } |
| 187 | 186 |
| 188 last_full_text_ = full_text; | 187 last_full_text_ = full_text; |
| 189 last_verbatim_ = verbatim; | 188 last_verbatim_ = verbatim; |
| 190 loader_processed_last_update_ = false; | 189 loader_processed_last_update_ = false; |
| 191 | 190 |
| 192 // Reset the last suggestion, as it's no longer valid. | 191 // Reset the last suggestion, as it's no longer valid. |
| 193 suggested_text->clear(); | 192 suggested_text->clear(); |
| 194 last_suggestion_.clear(); | 193 last_suggestion_.Clear(); |
| 195 *complete_behavior = last_complete_behavior_ = INSTANT_COMPLETE_NOW; | 194 *complete_behavior = INSTANT_COMPLETE_NOW; |
| 196 | 195 |
| 197 if (mode_ != SILENT) { | 196 if (mode_ != SILENT) { |
| 198 loader_->Update(last_full_text_, last_verbatim_); | 197 loader_->Update(last_full_text_, last_verbatim_); |
| 199 | 198 |
| 200 content::NotificationService::current()->Notify( | 199 content::NotificationService::current()->Notify( |
| 201 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 200 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
| 202 content::Source<InstantController>(this), | 201 content::Source<InstantController>(this), |
| 203 content::NotificationService::NoDetails()); | 202 content::NotificationService::NoDetails()); |
| 204 } | 203 } |
| 205 | 204 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 | 398 |
| 400 ResetLoader(instant_url, active_tab); | 399 ResetLoader(instant_url, active_tab); |
| 401 } | 400 } |
| 402 | 401 |
| 403 bool InstantController::commit_on_pointer_release() const { | 402 bool InstantController::commit_on_pointer_release() const { |
| 404 return GetPreviewContents() && loader_->IsPointerDownFromActivate(); | 403 return GetPreviewContents() && loader_->IsPointerDownFromActivate(); |
| 405 } | 404 } |
| 406 | 405 |
| 407 void InstantController::SetSuggestions( | 406 void InstantController::SetSuggestions( |
| 408 InstantLoader* loader, | 407 InstantLoader* loader, |
| 409 const std::vector<string16>& suggestions, | 408 const std::vector<InstantSuggestion>& suggestions) { |
| 410 InstantCompleteBehavior behavior) { | |
| 411 DCHECK_EQ(loader_.get(), loader); | 409 DCHECK_EQ(loader_.get(), loader); |
| 412 if (loader_ != loader || IsOutOfDate() || mode_ == SILENT || mode_ == HIDDEN) | 410 if (loader_ != loader || IsOutOfDate() || mode_ == SILENT || mode_ == HIDDEN) |
| 413 return; | 411 return; |
| 414 | 412 |
| 415 loader_processed_last_update_ = true; | 413 loader_processed_last_update_ = true; |
| 416 | 414 |
| 417 string16 suggestion; | 415 InstantSuggestion suggestion; |
| 418 if (!suggestions.empty()) | 416 if (!suggestions.empty()) |
| 419 suggestion = suggestions[0]; | 417 suggestion = suggestions[0]; |
| 420 | 418 |
| 421 string16 suggestion_lower = base::i18n::ToLower(suggestion); | 419 string16 suggestion_lower = base::i18n::ToLower(suggestion.text); |
| 422 string16 user_text_lower = base::i18n::ToLower(last_user_text_); | 420 string16 user_text_lower = base::i18n::ToLower(last_user_text_); |
| 423 if (user_text_lower.size() >= suggestion_lower.size() || | 421 if (user_text_lower.size() >= suggestion_lower.size() || |
| 424 suggestion_lower.compare(0, user_text_lower.size(), user_text_lower)) { | 422 suggestion_lower.compare(0, user_text_lower.size(), user_text_lower)) { |
| 425 suggestion.clear(); | 423 suggestion.text.clear(); |
| 426 } else { | 424 } else { |
| 427 suggestion.erase(0, last_user_text_.size()); | 425 suggestion.text.erase(0, last_user_text_.size()); |
| 428 } | 426 } |
| 429 | 427 |
| 430 last_suggestion_ = suggestion; | 428 last_suggestion_ = suggestion; |
| 431 last_complete_behavior_ = behavior; | |
| 432 if (!last_verbatim_) | 429 if (!last_verbatim_) |
| 433 delegate_->SetSuggestedText(suggestion, behavior); | 430 delegate_->SetSuggestedText(suggestion.text, suggestion.behavior); |
| 434 | 431 |
| 435 if (mode_ != SUGGEST) | 432 if (mode_ != SUGGEST) |
| 436 Show(); | 433 Show(); |
| 437 } | 434 } |
| 438 | 435 |
| 439 void InstantController::CommitInstantLoader(InstantLoader* loader) { | 436 void InstantController::CommitInstantLoader(InstantLoader* loader) { |
| 440 DCHECK_EQ(loader_.get(), loader); | 437 DCHECK_EQ(loader_.get(), loader); |
| 441 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; | 438 DCHECK(is_showing_ && !IsOutOfDate()) << is_showing_; |
| 442 if (loader_ != loader || !is_showing_ || IsOutOfDate()) | 439 if (loader_ != loader || !is_showing_ || IsOutOfDate()) |
| 443 return; | 440 return; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 loader_->Init(); | 498 loader_->Init(); |
| 502 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); | 499 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); |
| 503 } | 500 } |
| 504 } | 501 } |
| 505 | 502 |
| 506 void InstantController::DeleteLoader() { | 503 void InstantController::DeleteLoader() { |
| 507 Hide(); | 504 Hide(); |
| 508 last_full_text_.clear(); | 505 last_full_text_.clear(); |
| 509 last_user_text_.clear(); | 506 last_user_text_.clear(); |
| 510 last_verbatim_ = false; | 507 last_verbatim_ = false; |
| 511 last_suggestion_.clear(); | 508 last_suggestion_.Clear(); |
| 512 last_complete_behavior_ = INSTANT_COMPLETE_NOW; | |
| 513 last_transition_type_ = content::PAGE_TRANSITION_LINK; | 509 last_transition_type_ = content::PAGE_TRANSITION_LINK; |
| 514 last_omnibox_bounds_ = gfx::Rect(); | 510 last_omnibox_bounds_ = gfx::Rect(); |
| 515 url_for_history_ = GURL(); | 511 url_for_history_ = GURL(); |
| 516 if (GetPreviewContents()) | 512 if (GetPreviewContents()) |
| 517 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | 513 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); |
| 518 loader_.reset(); | 514 loader_.reset(); |
| 519 } | 515 } |
| 520 | 516 |
| 521 void InstantController::Show() { | 517 void InstantController::Show() { |
| 522 if (!is_showing_) { | 518 if (!is_showing_) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 return false; | 576 return false; |
| 581 } | 577 } |
| 582 | 578 |
| 583 return true; | 579 return true; |
| 584 } | 580 } |
| 585 | 581 |
| 586 bool InstantController::IsOutOfDate() const { | 582 bool InstantController::IsOutOfDate() const { |
| 587 return !last_active_tab_ || | 583 return !last_active_tab_ || |
| 588 last_active_tab_ != delegate_->GetActiveTabContents(); | 584 last_active_tab_ != delegate_->GetActiveTabContents(); |
| 589 } | 585 } |
| OLD | NEW |