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_controller.h" |
10 #include "chrome/browser/autocomplete/autocomplete_match.h" | |
11 #include "chrome/browser/net/predictor.h" | |
12 #include "chrome/browser/predictors/autocomplete_action_predictor.h" | |
13 #include "chrome/browser/prerender/prerender_field_trial.h" | |
14 #include "chrome/browser/prerender/prerender_manager.h" | |
15 #include "chrome/browser/prerender/prerender_manager_factory.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
9 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
18 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" | |
10 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 19 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
20 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | |
21 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h" | |
22 #include "chrome/browser/ui/search/instant_controller.h" | |
23 #include "extensions/common/constants.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. | |
Mathieu
2013/05/03 19:09:02
nit: necessary indent?
beaudoin
2013/05/04 03:06:51
Done.
| |
44 const bool was_open = popup_->IsOpen(); | |
45 if (default_match_changed) { | |
46 string16 inline_autocomplete_text; | |
47 string16 keyword; | |
48 bool is_keyword_hint = false; | |
49 const AutocompleteResult& result = this->result(); | |
50 const AutocompleteResult::const_iterator match(result.default_match()); | |
51 if (match != result.end()) { | |
Mathieu
2013/05/03 19:09:02
Generally, don't hesitate to add comments if you f
beaudoin
2013/05/04 03:06:51
Did some digging and added some comment. Also refa
| |
52 if ((match->inline_autocomplete_offset != string16::npos) && | |
53 (match->inline_autocomplete_offset < | |
54 match->fill_into_edit.length())) { | |
55 inline_autocomplete_text = | |
56 match->fill_into_edit.substr(match->inline_autocomplete_offset); | |
57 } | |
58 | |
59 if (!prerender::IsOmniboxEnabled(profile_)) | |
60 DoPreconnect(*match); | |
61 | |
62 // We could prefetch the alternate nav URL, if any, but because there | |
63 // can be many of these as a user types an initial series of characters, | |
64 // the OS DNS cache could suffer eviction problems for minimal gain. | |
65 | |
66 match->GetKeywordUIState(profile_, &keyword, &is_keyword_hint); | |
67 } | |
68 | |
69 popup_->OnResultChanged(); | |
70 omnibox_edit_model_->OnPopupDataChanged(inline_autocomplete_text, NULL, | |
71 keyword, is_keyword_hint); | |
72 } else { | |
73 popup_->OnResultChanged(); | |
74 } | |
75 | |
76 if (popup_->IsOpen()) { | |
77 omnibox_edit_model_->OnPopupBoundsChanged( | |
78 popup_->view()->GetTargetBounds()); | |
79 | |
80 InstantController* instant = | |
81 omnibox_edit_model_->controller()->GetInstant(); | |
82 if (instant && !omnibox_edit_model_->in_revert()) { | |
83 instant->HandleAutocompleteResults( | |
84 *autocomplete_controller()->providers()); | |
85 } | |
86 } else if (was_open) { | |
87 // Accepts the temporary text as the user text, because it makes little | |
88 // sense to have temporary text when the popup is closed. | |
89 omnibox_edit_model_->AcceptTemporaryTextAsUserText(); | |
90 } | |
27 } | 91 } |
92 | |
93 void OmniboxController::ClearPopupKeywordMode() const { | |
94 if (popup_->IsOpen() && | |
95 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD) | |
96 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL); | |
97 } | |
98 | |
99 void OmniboxController::InfoForCurrentSelection(AutocompleteMatch* match, | |
100 GURL* alternate_nav_url) const { | |
101 DCHECK(match != NULL); | |
102 const AutocompleteResult& result = autocomplete_controller_->result(); | |
103 if (!autocomplete_controller_->done()) { | |
104 // It's technically possible for |result| to be empty if no provider returns | |
105 // a synchronous result but the query has not completed synchronously; | |
106 // pratically, however, that should never actually happen. | |
107 if (result.empty()) | |
108 return; | |
109 // The user cannot have manually selected a match, or the query would have | |
110 // stopped. So the default match must be the desired selection. | |
111 *match = *result.default_match(); | |
112 } else { | |
113 CHECK(popup_->IsOpen()); | |
114 // If there are no results, the popup should be closed (so we should have | |
115 // failed the CHECK above), and URLsForDefaultMatch() should have been | |
116 // called instead. | |
117 CHECK(!result.empty()); | |
118 CHECK(popup_->selected_line() < result.size()); | |
119 *match = result.match_at(popup_->selected_line()); | |
120 } | |
121 if (alternate_nav_url && popup_->manually_selected_match().empty()) | |
122 *alternate_nav_url = result.alternate_nav_url(); | |
123 } | |
124 | |
125 const AutocompleteResult& OmniboxController::result() const { | |
126 return autocomplete_controller_->result(); | |
127 } | |
128 | |
129 void OmniboxController::DoPreconnect(const AutocompleteMatch& match) { | |
130 if (!match.destination_url.SchemeIs(extensions::kExtensionScheme)) { | |
131 // Warm up DNS Prefetch cache, or preconnect to a search service. | |
132 UMA_HISTOGRAM_ENUMERATION("Autocomplete.MatchType", match.type, | |
133 AutocompleteMatch::NUM_TYPES); | |
134 if (profile_->GetNetworkPredictor()) { | |
135 profile_->GetNetworkPredictor()->AnticipateOmniboxUrl( | |
136 match.destination_url, | |
137 AutocompleteActionPredictor::IsPreconnectable(match)); | |
138 } | |
139 // We could prefetch the alternate nav URL, if any, but because there | |
140 // can be many of these as a user types an initial series of characters, | |
141 // the OS DNS cache could suffer eviction problems for minimal gain. | |
142 } | |
143 } | |
OLD | NEW |