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/omnibox/omnibox_controller.h" | 5 #include "chrome/browser/ui/omnibox/omnibox_controller.h" |
6 | 6 |
| 7 #include "base/metrics/histogram.h" |
7 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 8 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
8 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 9 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 10 #include "chrome/browser/net/predictor.h" |
| 11 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
| 12 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 13 #include "chrome/browser/prerender/prerender_manager.h" |
| 14 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/search/search.h" | 16 #include "chrome/browser/search/search.h" |
| 17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
10 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 18 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
| 19 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| 20 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h" |
| 21 #include "chrome/browser/ui/search/instant_controller.h" |
| 22 #include "extensions/common/constants.h" |
| 23 #include "ui/gfx/rect.h" |
| 24 |
| 25 using predictors::AutocompleteActionPredictor; |
11 | 26 |
12 | 27 |
13 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model, | 28 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model, |
14 Profile* profile) | 29 Profile* profile) |
15 : omnibox_edit_model_(omnibox_edit_model) { | 30 : omnibox_edit_model_(omnibox_edit_model), |
| 31 profile_(profile) { |
16 autocomplete_controller_.reset(new AutocompleteController(profile, this, | 32 autocomplete_controller_.reset(new AutocompleteController(profile, this, |
17 chrome::IsInstantExtendedAPIEnabled() ? | 33 chrome::IsInstantExtendedAPIEnabled() ? |
18 AutocompleteClassifier::kInstantExtendedOmniboxProviders : | 34 AutocompleteClassifier::kInstantExtendedOmniboxProviders : |
19 AutocompleteClassifier::kDefaultOmniboxProviders)); | 35 AutocompleteClassifier::kDefaultOmniboxProviders)); |
20 } | 36 } |
21 | 37 |
22 OmniboxController::~OmniboxController() { | 38 OmniboxController::~OmniboxController() { |
23 } | 39 } |
24 | 40 |
25 void OmniboxController::OnResultChanged(bool default_match_changed) { | 41 void OmniboxController::OnResultChanged(bool default_match_changed) { |
26 omnibox_edit_model_->OnResultChanged(default_match_changed); | 42 // TODO(beaudoin): There should be no need to access the popup when using |
| 43 // instant extended, remove this reference. |
| 44 const bool was_open = popup_->IsOpen(); |
| 45 if (default_match_changed) { |
| 46 // The default match has changed, we need to let the OmniboxEditModel know |
| 47 // about new inline autocomplete text (blue highlight). |
| 48 string16 inline_autocomplete_text; |
| 49 string16 keyword; |
| 50 bool is_keyword_hint = false; |
| 51 const AutocompleteResult& result = this->result(); |
| 52 const AutocompleteResult::const_iterator match(result.default_match()); |
| 53 if (match != result.end()) { |
| 54 if ((match->inline_autocomplete_offset != string16::npos) && |
| 55 (match->inline_autocomplete_offset < |
| 56 match->fill_into_edit.length())) { |
| 57 inline_autocomplete_text = |
| 58 match->fill_into_edit.substr(match->inline_autocomplete_offset); |
| 59 } |
| 60 |
| 61 if (!prerender::IsOmniboxEnabled(profile_)) |
| 62 DoPreconnect(*match); |
| 63 |
| 64 // We could prefetch the alternate nav URL, if any, but because there |
| 65 // can be many of these as a user types an initial series of characters, |
| 66 // the OS DNS cache could suffer eviction problems for minimal gain. |
| 67 |
| 68 match->GetKeywordUIState(profile_, &keyword, &is_keyword_hint); |
| 69 } |
| 70 |
| 71 popup_->OnResultChanged(); |
| 72 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, |
| 73 keyword, is_keyword_hint); |
| 74 } else { |
| 75 popup_->OnResultChanged(); |
| 76 } |
| 77 |
| 78 if (popup_->IsOpen()) { |
| 79 // The popup size may have changed, let instant know. |
| 80 OnPopupBoundsChanged(popup_->view()->GetTargetBounds()); |
| 81 |
| 82 InstantController* instant = |
| 83 omnibox_edit_model_->controller()->GetInstant(); |
| 84 if (instant && !omnibox_edit_model_->in_revert()) { |
| 85 instant->HandleAutocompleteResults( |
| 86 *autocomplete_controller()->providers(), |
| 87 autocomplete_controller()->result()); |
| 88 } |
| 89 } else if (was_open) { |
| 90 // Accept the temporary text as the user text, because it makes little sense |
| 91 // to have temporary text when the popup is closed. |
| 92 omnibox_edit_model_->AcceptTemporaryTextAsUserText(); |
| 93 // The popup has been closed, let instant know. |
| 94 OnPopupBoundsChanged(gfx::Rect()); |
| 95 } |
27 } | 96 } |
| 97 |
| 98 void OmniboxController::ClearPopupKeywordMode() const { |
| 99 if (popup_->IsOpen() && |
| 100 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD) |
| 101 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL); |
| 102 } |
| 103 |
| 104 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) { |
| 105 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) { |
| 106 // Warm up DNS Prefetch cache, or preconnect to a search service. |
| 107 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type, |
| 108 AutocompleteMatchType::NUM_TYPES); |
| 109 if (profile_->GetNetworkPredictor()) { |
| 110 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl( |
| 111 match.destination_url, |
| 112 AutocompleteActionPredictor::IsPreconnectable(match)); |
| 113 } |
| 114 // We could prefetch the alternate nav URL, if any, but because there |
| 115 // can be many of these as a user types an initial series of characters, |
| 116 // the OS DNS cache could suffer eviction problems for minimal gain. |
| 117 } |
| 118 } |
| 119 |
| 120 void OmniboxController::OnPopupBoundsChanged(const gfx::Rect& bounds) { |
| 121 InstantController* instant = omnibox_edit_model_->controller()->GetInstant(); |
| 122 if (instant) |
| 123 instant->SetPopupBounds(bounds); |
| 124 } |
OLD | NEW |