Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: chrome/browser/ui/omnibox/omnibox_edit_model.cc

Issue 14698028: Omnibox refactor. OmniboxController now holds an AutocompleteMatch. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed failing browser tests. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/omnibox/omnibox_edit_model.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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/ui/omnibox/omnibox_edit_model.h" 5 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // NOTE: Be sure and set keyword-related state BEFORE invoking 177 // NOTE: Be sure and set keyword-related state BEFORE invoking
178 // DisplayTextFromUserText(), as its result depends upon this state. 178 // DisplayTextFromUserText(), as its result depends upon this state.
179 keyword_ = state.keyword; 179 keyword_ = state.keyword;
180 is_keyword_hint_ = state.is_keyword_hint; 180 is_keyword_hint_ = state.is_keyword_hint;
181 view_->SetUserText(state.user_text, 181 view_->SetUserText(state.user_text,
182 DisplayTextFromUserText(state.user_text), false); 182 DisplayTextFromUserText(state.user_text), false);
183 view_->SetInstantSuggestion(state.instant_suggestion); 183 view_->SetInstantSuggestion(state.instant_suggestion);
184 } 184 }
185 } 185 }
186 186
187 AutocompleteMatch OmniboxEditModel::CurrentMatch() { 187 AutocompleteMatch OmniboxEditModel::CurrentMatch(
188 AutocompleteMatch match; 188 GURL* alternate_nav_url) const {
189 GetInfoForCurrentText(&match, NULL); 189 // If we have a valid match use it. Otherwise get one for the current text.
190 AutocompleteMatch match =
191 omnibox_controller_->CurrentMatch(alternate_nav_url);
192 if (!match.destination_url.is_valid())
193 GetInfoForCurrentText(&match, alternate_nav_url);
190 return match; 194 return match;
191 } 195 }
192 196
193 bool OmniboxEditModel::UpdatePermanentText(const string16& new_permanent_text) { 197 bool OmniboxEditModel::UpdatePermanentText(const string16& new_permanent_text) {
194 // When there's a new URL, and the user is not editing anything or the edit 198 // When there's a new URL, and the user is not editing anything or the edit
195 // doesn't have focus, we want to revert the edit to show the new URL. (The 199 // doesn't have focus, we want to revert the edit to show the new URL. (The
196 // common case where the edit doesn't have focus is when the user has started 200 // common case where the edit doesn't have focus is when the user has started
197 // an edit and then abandoned it and clicked a link on the page.) 201 // an edit and then abandoned it and clicked a link on the page.)
198 // 202 //
199 // If the page is auto-committing an instant suggestion, however, we generally 203 // If the page is auto-committing an instant suggestion, however, we generally
(...skipping 13 matching lines...) Expand all
213 return visibly_changed_permanent_text; 217 return visibly_changed_permanent_text;
214 } 218 }
215 219
216 GURL OmniboxEditModel::PermanentURL() { 220 GURL OmniboxEditModel::PermanentURL() {
217 return URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), std::string()); 221 return URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), std::string());
218 } 222 }
219 223
220 void OmniboxEditModel::SetUserText(const string16& text) { 224 void OmniboxEditModel::SetUserText(const string16& text) {
221 SetInputInProgress(true); 225 SetInputInProgress(true);
222 InternalSetUserText(text); 226 InternalSetUserText(text);
227 omnibox_controller_->InvalidateCurrentMatch();
223 paste_state_ = NONE; 228 paste_state_ = NONE;
224 has_temporary_text_ = false; 229 has_temporary_text_ = false;
225 is_temporary_text_set_by_instant_ = false; 230 is_temporary_text_set_by_instant_ = false;
226 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch; 231 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch;
227 is_instant_temporary_text_a_search_query_ = false; 232 is_instant_temporary_text_a_search_query_ = false;
228 } 233 }
229 234
230 void OmniboxEditModel::FinalizeInstantQuery(
231 const string16& input_text,
232 const InstantSuggestion& suggestion) {
233 // Should only get called for the HTML popup.
234 #if defined(HTML_INSTANT_EXTENDED_POPUP)
235 if (!popup_model()->result().empty()) {
236 // When a IME is active and a candidate window is open, we don't show
237 // the omnibox popup, though |result()| may be available. Thus we check
238 // whether result().empty() or not instead of whether IsOpen() or not.
239 // We need the finalization of instant query when result() is available.
240 SearchProvider* search_provider =
241 autocomplete_controller()->search_provider();
242 // There may be no providers during testing; guard against that.
243 if (search_provider)
244 search_provider->FinalizeInstantQuery(input_text, suggestion);
245 }
246 #endif
247 }
248
249 void OmniboxEditModel::SetInstantSuggestion( 235 void OmniboxEditModel::SetInstantSuggestion(
250 const InstantSuggestion& suggestion) { 236 const InstantSuggestion& suggestion) {
251 // Should only get called for the HTML popup. 237 // Should only get called for the HTML popup.
252 #if defined(HTML_INSTANT_EXTENDED_POPUP) 238 #if defined(HTML_INSTANT_EXTENDED_POPUP)
253 switch (suggestion.behavior) { 239 omnibox_controller_->SetInstantSuggestion(suggestion);
254 case INSTANT_COMPLETE_NOW:
255 view_->SetInstantSuggestion(string16());
256 if (!suggestion.text.empty())
257 FinalizeInstantQuery(view_->GetText(), suggestion);
258 break;
259
260 case INSTANT_COMPLETE_NEVER: {
261 DCHECK_EQ(INSTANT_SUGGESTION_SEARCH, suggestion.type);
262 view_->SetInstantSuggestion(suggestion.text);
263 autocomplete_controller()->search_provider()->ClearInstantSuggestion();
264 break;
265 }
266
267 case INSTANT_COMPLETE_REPLACE: {
268 const bool save_original_selection = !has_temporary_text_;
269 view_->SetInstantSuggestion(string16());
270 has_temporary_text_ = true;
271 is_temporary_text_set_by_instant_ = true;
272 selected_instant_autocomplete_match_index_ =
273 suggestion.autocomplete_match_index;
274 is_instant_temporary_text_a_search_query_ =
275 suggestion.type == INSTANT_SUGGESTION_SEARCH;
276 // Instant suggestions are never a keyword.
277 keyword_ = string16();
278 is_keyword_hint_ = false;
279 view_->OnTemporaryTextMaybeChanged(suggestion.text,
280 save_original_selection, true);
281 break;
282 }
283 }
284 #endif 240 #endif
285 } 241 }
286 242
287 bool OmniboxEditModel::CommitSuggestedText() { 243 bool OmniboxEditModel::CommitSuggestedText() {
288 const string16 suggestion = view_->GetInstantSuggestion(); 244 const string16 suggestion = view_->GetInstantSuggestion();
289 if (suggestion.empty()) 245 if (suggestion.empty())
290 return false; 246 return false;
291 247
292 // Assume that the gray text we are committing is a search suggestion. 248 // Assume that the gray text we are committing is a search suggestion.
293 const string16 final_text = view_->GetText() + suggestion; 249 const string16 final_text = view_->GetText() + suggestion;
294 view_->OnBeforePossibleChange(); 250 view_->OnBeforePossibleChange();
295 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false, 251 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false,
296 false); 252 false);
297 view_->OnAfterPossibleChange(); 253 view_->OnAfterPossibleChange();
298 return true; 254 return true;
299 } 255 }
300 256
301 void OmniboxEditModel::OnChanged() { 257 void OmniboxEditModel::OnChanged() {
302 // Don't call CurrentMatch() when there's no editing, as in this case we'll 258 // Don't call CurrentMatch() when there's no editing, as in this case we'll
303 // never actually use it. This avoids running the autocomplete providers (and 259 // never actually use it. This avoids running the autocomplete providers (and
304 // any systems they then spin up) during startup. 260 // any systems they then spin up) during startup.
305 const AutocompleteMatch& current_match = user_input_in_progress_ ? 261 const AutocompleteMatch& current_match = user_input_in_progress_ ?
306 CurrentMatch() : AutocompleteMatch(); 262 CurrentMatch(NULL) : AutocompleteMatch();
307 263
308 AutocompleteActionPredictor::Action recommended_action = 264 AutocompleteActionPredictor::Action recommended_action =
309 AutocompleteActionPredictor::ACTION_NONE; 265 AutocompleteActionPredictor::ACTION_NONE;
310 AutocompleteActionPredictor* action_predictor = 266 AutocompleteActionPredictor* action_predictor =
311 user_input_in_progress_ ? 267 user_input_in_progress_ ?
312 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL; 268 AutocompleteActionPredictorFactory::GetForProfile(profile_) : NULL;
313 if (action_predictor) { 269 if (action_predictor) {
314 action_predictor->RegisterTransitionalMatches(user_text_, result()); 270 action_predictor->RegisterTransitionalMatches(user_text_, result());
315 // Confer with the AutocompleteActionPredictor to determine what action, if 271 // Confer with the AutocompleteActionPredictor to determine what action, if
316 // any, we should take. Get the recommended action here even if we don't 272 // any, we should take. Get the recommended action here even if we don't
(...skipping 21 matching lines...) Expand all
338 user_input_in_progress_, in_escape_handler_, 294 user_input_in_progress_, in_escape_handler_,
339 view_->DeleteAtEndPressed() || just_deleted_text_, 295 view_->DeleteAtEndPressed() || just_deleted_text_,
340 KeywordIsSelected()); 296 KeywordIsSelected());
341 } 297 }
342 298
343 if (!performed_instant) { 299 if (!performed_instant) {
344 // Hide any suggestions we might be showing. 300 // Hide any suggestions we might be showing.
345 view_->SetInstantSuggestion(string16()); 301 view_->SetInstantSuggestion(string16());
346 302
347 // No need to wait any longer for Instant. 303 // No need to wait any longer for Instant.
348 FinalizeInstantQuery(string16(), InstantSuggestion()); 304 omnibox_controller_->FinalizeInstantQuery(string16(), InstantSuggestion());
349 } 305 }
350 306
351 switch (recommended_action) { 307 switch (recommended_action) {
352 case AutocompleteActionPredictor::ACTION_PRERENDER: 308 case AutocompleteActionPredictor::ACTION_PRERENDER:
353 // It's possible that there is no current page, for instance if the tab 309 // It's possible that there is no current page, for instance if the tab
354 // has been closed or on return from a sleep state. 310 // has been closed or on return from a sleep state.
355 // (http://crbug.com/105689) 311 // (http://crbug.com/105689)
356 if (!delegate_->CurrentPageExists()) 312 if (!delegate_->CurrentPageExists())
357 break; 313 break;
358 // Ask for prerendering if the destination URL is different than the 314 // Ask for prerendering if the destination URL is different than the
359 // current URL. 315 // current URL.
360 if (current_match.destination_url != PermanentURL()) 316 if (current_match.destination_url != PermanentURL())
361 delegate_->DoPrerender(current_match); 317 delegate_->DoPrerender(current_match);
362 break; 318 break;
363 case AutocompleteActionPredictor::ACTION_PRECONNECT: 319 case AutocompleteActionPredictor::ACTION_PRECONNECT:
364 omnibox_controller_->DoPreconnect(current_match); 320 omnibox_controller_->DoPreconnect(current_match);
365 break; 321 break;
366 case AutocompleteActionPredictor::ACTION_NONE: 322 case AutocompleteActionPredictor::ACTION_NONE:
367 break; 323 break;
368 } 324 }
369 325
370 controller_->OnChanged(); 326 controller_->OnChanged();
371 } 327 }
372 328
373 void OmniboxEditModel::GetDataForURLExport(GURL* url, 329 void OmniboxEditModel::GetDataForURLExport(GURL* url,
374 string16* title, 330 string16* title,
375 gfx::Image* favicon) { 331 gfx::Image* favicon) {
376 AutocompleteMatch match; 332 *url = CurrentMatch(NULL).destination_url;
377 GetInfoForCurrentText(&match, NULL);
378 *url = match.destination_url;
379 if (*url == URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_), 333 if (*url == URLFixerUpper::FixupURL(UTF16ToUTF8(permanent_text_),
380 std::string())) { 334 std::string())) {
381 *title = controller_->GetTitle(); 335 *title = controller_->GetTitle();
382 *favicon = controller_->GetFavicon(); 336 *favicon = controller_->GetFavicon();
383 } 337 }
384 } 338 }
385 339
386 bool OmniboxEditModel::CurrentTextIsURL() const { 340 bool OmniboxEditModel::CurrentTextIsURL() const {
387 if (view_->toolbar_model()->WouldReplaceSearchURLWithSearchTerms()) 341 if (view_->toolbar_model()->WouldReplaceSearchURLWithSearchTerms())
388 return false; 342 return false;
389 343
390 // If current text is not composed of replaced search terms and 344 // If current text is not composed of replaced search terms and
391 // !user_input_in_progress_, then permanent text is showing and should be a 345 // !user_input_in_progress_, then permanent text is showing and should be a
392 // URL, so no further checking is needed. By avoiding checking in this case, 346 // URL, so no further checking is needed. By avoiding checking in this case,
393 // we avoid calling into the autocomplete providers, and thus initializing the 347 // we avoid calling into the autocomplete providers, and thus initializing the
394 // history system, as long as possible, which speeds startup. 348 // history system, as long as possible, which speeds startup.
395 if (!user_input_in_progress_) 349 if (!user_input_in_progress_)
396 return true; 350 return true;
397 351
398 AutocompleteMatch match; 352 return !AutocompleteMatch::IsSearchType(CurrentMatch(NULL).type);
399 GetInfoForCurrentText(&match, NULL);
400 return !AutocompleteMatch::IsSearchType(match.type);
401 } 353 }
402 354
403 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const { 355 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const {
404 AutocompleteMatch match; 356 return CurrentMatch(NULL).type;
405 GetInfoForCurrentText(&match, NULL);
406 return match.type;
407 } 357 }
408 358
409 void OmniboxEditModel::AdjustTextForCopy(int sel_min, 359 void OmniboxEditModel::AdjustTextForCopy(int sel_min,
410 bool is_all_selected, 360 bool is_all_selected,
411 string16* text, 361 string16* text,
412 GURL* url, 362 GURL* url,
413 bool* write_url) { 363 bool* write_url) {
414 *write_url = false; 364 *write_url = false;
415 365
416 // Do not adjust if selection did not start at the beginning of the field, or 366 // Do not adjust if selection did not start at the beginning of the field, or
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 504
555 bool OmniboxEditModel::IsPasteAndSearch(const string16& text) const { 505 bool OmniboxEditModel::IsPasteAndSearch(const string16& text) const {
556 AutocompleteMatch match; 506 AutocompleteMatch match;
557 ClassifyStringForPasteAndGo(text, &match, NULL); 507 ClassifyStringForPasteAndGo(text, &match, NULL);
558 return AutocompleteMatch::IsSearchType(match.type); 508 return AutocompleteMatch::IsSearchType(match.type);
559 } 509 }
560 510
561 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition, 511 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition,
562 bool for_drop) { 512 bool for_drop) {
563 // Get the URL and transition type for the selected entry. 513 // Get the URL and transition type for the selected entry.
564 AutocompleteMatch match;
565 GURL alternate_nav_url; 514 GURL alternate_nav_url;
566 GetInfoForCurrentText(&match, &alternate_nav_url); 515 AutocompleteMatch match = CurrentMatch(&alternate_nav_url);
567 516
568 // If CTRL is down it means the user wants to append ".com" to the text he 517 // If CTRL is down it means the user wants to append ".com" to the text he
569 // typed. If we can successfully generate a URL_WHAT_YOU_TYPED match doing 518 // typed. If we can successfully generate a URL_WHAT_YOU_TYPED match doing
570 // that, then we use this. These matches are marked as generated by the 519 // that, then we use this. These matches are marked as generated by the
571 // HistoryURLProvider so we only generate them if this provider is present. 520 // HistoryURLProvider so we only generate them if this provider is present.
572 if (control_key_state_ == DOWN_WITHOUT_CHANGE && !KeywordIsSelected() && 521 if (control_key_state_ == DOWN_WITHOUT_CHANGE && !KeywordIsSelected() &&
573 autocomplete_controller()->history_url_provider()) { 522 autocomplete_controller()->history_url_provider()) {
574 // Generate a new AutocompleteInput, copying the latest one but using "com" 523 // Generate a new AutocompleteInput, copying the latest one but using "com"
575 // as the desired TLD. Then use this autocomplete input to generate a 524 // as the desired TLD. Then use this autocomplete input to generate a
576 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent 525 // URL_WHAT_YOU_TYPED AutocompleteMatch. Note that using the most recent
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 if (popup_model()->IsOpen()) { 585 if (popup_model()->IsOpen()) {
637 const base::TimeTicks& now(base::TimeTicks::Now()); 586 const base::TimeTicks& now(base::TimeTicks::Now());
638 base::TimeDelta elapsed_time_since_user_first_modified_omnibox( 587 base::TimeDelta elapsed_time_since_user_first_modified_omnibox(
639 now - time_user_first_modified_omnibox_); 588 now - time_user_first_modified_omnibox_);
640 base::TimeDelta elapsed_time_since_last_change_to_default_match( 589 base::TimeDelta elapsed_time_since_last_change_to_default_match(
641 now - autocomplete_controller()->last_time_default_match_changed()); 590 now - autocomplete_controller()->last_time_default_match_changed());
642 // These elapsed times don't really make sense for ZeroSuggest matches 591 // These elapsed times don't really make sense for ZeroSuggest matches
643 // (because the user does not modify the omnibox for ZeroSuggest), so for 592 // (because the user does not modify the omnibox for ZeroSuggest), so for
644 // those we set the elapsed times to something that will be ignored by 593 // those we set the elapsed times to something that will be ignored by
645 // metrics_log.cc. 594 // metrics_log.cc.
646 if (match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) { 595 if (match.provider &&
596 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) {
647 elapsed_time_since_user_first_modified_omnibox = 597 elapsed_time_since_user_first_modified_omnibox =
648 base::TimeDelta::FromMilliseconds(-1); 598 base::TimeDelta::FromMilliseconds(-1);
649 elapsed_time_since_last_change_to_default_match = 599 elapsed_time_since_last_change_to_default_match =
650 base::TimeDelta::FromMilliseconds(-1); 600 base::TimeDelta::FromMilliseconds(-1);
651 } 601 }
652 // TODO(sreeram): Handle is_temporary_text_set_by_instant_ correctly. 602 // TODO(sreeram): Handle is_temporary_text_set_by_instant_ correctly.
653 OmniboxLog log( 603 OmniboxLog log(
654 autocomplete_controller()->input().text(), 604 autocomplete_controller()->input().text(),
655 just_deleted_text_, 605 just_deleted_text_,
656 autocomplete_controller()->input().type(), 606 autocomplete_controller()->input().type(),
657 popup_model()->selected_line(), 607 popup_model()->selected_line(),
658 -1, // don't yet know tab ID; set later if appropriate 608 -1, // don't yet know tab ID; set later if appropriate
659 delegate_->CurrentPageExists() ? ClassifyPage(delegate_->GetURL()) : 609 delegate_->CurrentPageExists() ? ClassifyPage(delegate_->GetURL()) :
660 metrics::OmniboxEventProto_PageClassification_OTHER, 610 metrics::OmniboxEventProto_PageClassification_OTHER,
661 elapsed_time_since_user_first_modified_omnibox, 611 elapsed_time_since_user_first_modified_omnibox,
662 string16::npos, // completed_length; possibly set later 612 string16::npos, // completed_length; possibly set later
663 elapsed_time_since_last_change_to_default_match, 613 elapsed_time_since_last_change_to_default_match,
664 result()); 614 result());
665 DCHECK(user_input_in_progress_ || 615
666 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) 616 DCHECK(user_input_in_progress_ || (match.provider &&
617 match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST))
667 << "We didn't get here through the expected series of calls. " 618 << "We didn't get here through the expected series of calls. "
668 << "time_user_first_modified_omnibox_ is not set correctly and other " 619 << "time_user_first_modified_omnibox_ is not set correctly and other "
669 << "things may be wrong. Match provider: " << match.provider->GetName(); 620 << "things may be wrong. Match provider: "
621 << (match.provider ? match.provider->GetName() : "NULL");
670 DCHECK(log.elapsed_time_since_user_first_modified_omnibox >= 622 DCHECK(log.elapsed_time_since_user_first_modified_omnibox >=
671 log.elapsed_time_since_last_change_to_default_match) 623 log.elapsed_time_since_last_change_to_default_match)
672 << "We should've got the notification that the user modified the " 624 << "We should've got the notification that the user modified the "
673 << "omnibox text at same time or before the most recent time the " 625 << "omnibox text at same time or before the most recent time the "
674 << "default match changed."; 626 << "default match changed.";
627
675 if (index != OmniboxPopupModel::kNoMatch) 628 if (index != OmniboxPopupModel::kNoMatch)
676 log.selected_index = index; 629 log.selected_index = index;
677 if (match.inline_autocomplete_offset != string16::npos) { 630 if (match.inline_autocomplete_offset != string16::npos) {
678 DCHECK_GE(match.fill_into_edit.length(), 631 DCHECK_GE(match.fill_into_edit.length(),
679 match.inline_autocomplete_offset); 632 match.inline_autocomplete_offset);
680 log.completed_length = 633 log.completed_length =
681 match.fill_into_edit.length() - match.inline_autocomplete_offset; 634 match.fill_into_edit.length() - match.inline_autocomplete_offset;
682 } 635 }
683 636
684 if ((disposition == CURRENT_TAB) && delegate_->CurrentPageExists()) { 637 if ((disposition == CURRENT_TAB) && delegate_->CurrentPageExists()) {
685 // If we know the destination is being opened in the current tab, 638 // If we know the destination is being opened in the current tab,
686 // we can easily get the tab ID. (If it's being opened in a new 639 // we can easily get the tab ID. (If it's being opened in a new
687 // tab, we don't know the tab ID yet.) 640 // tab, we don't know the tab ID yet.)
688 log.tab_id = delegate_->GetSessionID().id(); 641 log.tab_id = delegate_->GetSessionID().id();
689 } 642 }
690 autocomplete_controller()->AddProvidersInfo(&log.providers_info); 643 autocomplete_controller()->AddProvidersInfo(&log.providers_info);
691 content::NotificationService::current()->Notify( 644 content::NotificationService::current()->Notify(
692 chrome::NOTIFICATION_OMNIBOX_OPENED_URL, 645 chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
693 content::Source<Profile>(profile_), 646 content::Source<Profile>(profile_),
694 content::Details<OmniboxLog>(&log)); 647 content::Details<OmniboxLog>(&log));
695 HISTOGRAM_ENUMERATION("Omnibox.EventCount", 1, 2); 648 HISTOGRAM_ENUMERATION("Omnibox.EventCount", 1, 2);
696 } 649 }
697 650
698 TemplateURL* template_url = match.GetTemplateURL(profile_, false); 651 TemplateURL* template_url = match.GetTemplateURL(profile_, false);
699 if (template_url) { 652 if (template_url) {
700 if (match.transition == content::PAGE_TRANSITION_KEYWORD) { 653 if (match.transition == content::PAGE_TRANSITION_KEYWORD) {
701 // The user is using a non-substituting keyword or is explicitly in 654 // The user is using a non-substituting keyword or is explicitly in
702 // keyword mode. 655 // keyword mode.
703
704 AutocompleteMatch current_match;
705 GetInfoForCurrentText(&current_match, NULL);
706 const AutocompleteMatch& match = (index == OmniboxPopupModel::kNoMatch) ? 656 const AutocompleteMatch& match = (index == OmniboxPopupModel::kNoMatch) ?
707 current_match : result().match_at(index); 657 CurrentMatch(NULL) : result().match_at(index);
708 658
709 // Don't increment usage count for extension keywords. 659 // Don't increment usage count for extension keywords.
710 if (delegate_->ProcessExtensionKeyword(template_url, match, 660 if (delegate_->ProcessExtensionKeyword(template_url, match,
711 disposition)) { 661 disposition)) {
712 if (disposition != NEW_BACKGROUND_TAB) 662 if (disposition != NEW_BACKGROUND_TAB)
713 view_->RevertAll(); 663 view_->RevertAll();
714 return; 664 return;
715 } 665 }
716 666
717 content::RecordAction(UserMetricsAction("AcceptedKeyword")); 667 content::RecordAction(UserMetricsAction("AcceptedKeyword"));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 733
784 // Ensure the current selection is saved before showing keyword mode 734 // Ensure the current selection is saved before showing keyword mode
785 // so that moving to another line and then reverting the text will restore 735 // so that moving to another line and then reverting the text will restore
786 // the current state properly. 736 // the current state properly.
787 bool save_original_selection = !has_temporary_text_; 737 bool save_original_selection = !has_temporary_text_;
788 has_temporary_text_ = true; 738 has_temporary_text_ = true;
789 is_temporary_text_set_by_instant_ = false; 739 is_temporary_text_set_by_instant_ = false;
790 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch; 740 selected_instant_autocomplete_match_index_ = OmniboxPopupModel::kNoMatch;
791 is_instant_temporary_text_a_search_query_ = false; 741 is_instant_temporary_text_a_search_query_ = false;
792 view_->OnTemporaryTextMaybeChanged( 742 view_->OnTemporaryTextMaybeChanged(
793 DisplayTextFromUserText(CurrentMatch().fill_into_edit), 743 DisplayTextFromUserText(CurrentMatch(NULL).fill_into_edit),
794 save_original_selection, true); 744 save_original_selection, true);
795 745
796 content::RecordAction(UserMetricsAction("AcceptedKeywordHint")); 746 content::RecordAction(UserMetricsAction("AcceptedKeywordHint"));
797 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method, 747 UMA_HISTOGRAM_ENUMERATION(kEnteredKeywordModeHistogram, entered_method,
798 ENTERED_KEYWORD_MODE_NUM_ITEMS); 748 ENTERED_KEYWORD_MODE_NUM_ITEMS);
799 749
800 return true; 750 return true;
801 } 751 }
802 752
803 void OmniboxEditModel::AcceptTemporaryTextAsUserText() { 753 void OmniboxEditModel::AcceptTemporaryTextAsUserText() {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 // TODO(samarth): determine if it is safe to move the call to 835 // TODO(samarth): determine if it is safe to move the call to
886 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us 836 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us
887 // just call SetFocusState() to handle the state change. 837 // just call SetFocusState() to handle the state change.
888 focus_state_ = OMNIBOX_FOCUS_NONE; 838 focus_state_ = OMNIBOX_FOCUS_NONE;
889 control_key_state_ = UP; 839 control_key_state_ = UP;
890 paste_state_ = NONE; 840 paste_state_ = NONE;
891 } 841 }
892 842
893 bool OmniboxEditModel::OnEscapeKeyPressed() { 843 bool OmniboxEditModel::OnEscapeKeyPressed() {
894 if (has_temporary_text_) { 844 if (has_temporary_text_) {
895 AutocompleteMatch match; 845 if (CurrentMatch(NULL).destination_url != original_url_) {
896 GetInfoForCurrentText(&match, NULL);
897 if (match.destination_url != original_url_) {
898 RevertTemporaryText(true); 846 RevertTemporaryText(true);
899 return true; 847 return true;
900 } 848 }
901 } 849 }
902 850
903 // We do not clear the pending entry from the omnibox when a load is first 851 // We do not clear the pending entry from the omnibox when a load is first
904 // stopped. If the user presses Escape while stopped, we clear it. 852 // stopped. If the user presses Escape while stopped, we clear it.
905 if (delegate_->CurrentPageExists() && !delegate_->IsLoading()) { 853 if (delegate_->CurrentPageExists() && !delegate_->IsLoading()) {
906 delegate_->GetNavigationController().DiscardNonCommittedEntries(); 854 delegate_->GetNavigationController().DiscardNonCommittedEntries();
907 view_->Update(NULL); 855 view_->Update(NULL);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 popup_model()->Move(count); 934 popup_model()->Move(count);
987 } 935 }
988 } 936 }
989 } 937 }
990 938
991 void OmniboxEditModel::OnPopupDataChanged( 939 void OmniboxEditModel::OnPopupDataChanged(
992 const string16& text, 940 const string16& text,
993 GURL* destination_for_temporary_text_change, 941 GURL* destination_for_temporary_text_change,
994 const string16& keyword, 942 const string16& keyword,
995 bool is_keyword_hint) { 943 bool is_keyword_hint) {
944 // The popup changed its data, the match in the controller is no longer valid.
945 omnibox_controller_->InvalidateCurrentMatch();
946
996 // Update keyword/hint-related local state. 947 // Update keyword/hint-related local state.
997 bool keyword_state_changed = (keyword_ != keyword) || 948 bool keyword_state_changed = (keyword_ != keyword) ||
998 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty()); 949 ((is_keyword_hint_ != is_keyword_hint) && !keyword.empty());
999 if (keyword_state_changed) { 950 if (keyword_state_changed) {
1000 keyword_ = keyword; 951 keyword_ = keyword;
1001 is_keyword_hint_ = is_keyword_hint; 952 is_keyword_hint_ = is_keyword_hint;
1002 953
1003 // |is_keyword_hint_| should always be false if |keyword_| is empty. 954 // |is_keyword_hint_| should always be false if |keyword_| is empty.
1004 DCHECK(!keyword_.empty() || !is_keyword_hint_); 955 DCHECK(!keyword_.empty() || !is_keyword_hint_);
1005 } 956 }
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 // determine what keyword, if any, is applicable. 1115 // determine what keyword, if any, is applicable.
1165 // 1116 //
1166 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that 1117 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that
1167 // will have updated our state already, so in that case we don't also return 1118 // will have updated our state already, so in that case we don't also return
1168 // true from this function. 1119 // true from this function.
1169 return !(text_differs && allow_keyword_ui_change && !just_deleted_text && 1120 return !(text_differs && allow_keyword_ui_change && !just_deleted_text &&
1170 no_selection && (selection_start == user_text_.length()) && 1121 no_selection && (selection_start == user_text_.length()) &&
1171 MaybeAcceptKeywordBySpace(user_text_)); 1122 MaybeAcceptKeywordBySpace(user_text_));
1172 } 1123 }
1173 1124
1174 void OmniboxEditModel::OnResultChanged(bool default_match_changed) { 1125 void OmniboxEditModel::OnCurrentMatchChanged(bool is_temporary_set_by_instant) {
1126 has_temporary_text_ = is_temporary_set_by_instant;
1127 is_temporary_text_set_by_instant_ = is_temporary_set_by_instant;
1128
1129 const AutocompleteMatch& match = omnibox_controller_->CurrentMatch(NULL);
1130
1131 if (is_temporary_set_by_instant) {
1132 view_->OnTemporaryTextMaybeChanged(
1133 DisplayTextFromUserText(match.fill_into_edit), !has_temporary_text_,
1134 false);
1135 } else {
1136 // We store |keyword| and |is_keyword_hint| in temporary variables since
1137 // OnPopupDataChanged use their previous state to detect changes.
1138 string16 keyword;
1139 bool is_keyword_hint;
1140 match.GetKeywordUIState(profile_, &keyword, &is_keyword_hint);
1141 string16 inline_autocomplete_text;
1142 if (match.inline_autocomplete_offset < match.fill_into_edit.length()) {
1143 // We have blue text, go through OnPopupDataChanged.
1144 // TODO(beaudoin): Merge OnPopupDataChanged with this method once the
1145 // popup handling has completely migrated to omnibox_controller.
1146 inline_autocomplete_text =
1147 match.fill_into_edit.substr(match.inline_autocomplete_offset);
1148 }
1149 popup_model()->OnResultChanged();
1150 OnPopupDataChanged(inline_autocomplete_text, NULL, keyword,
1151 is_keyword_hint);
1152 }
1153 }
1154
1155 void OmniboxEditModel::OnGrayTextChanged() {
1156 view_->SetInstantSuggestion(omnibox_controller_->gray_suggestion());
1157 }
1158
1159 string16 OmniboxEditModel::GetViewText() const {
1160 return view_->GetText();
1175 } 1161 }
1176 1162
1177 InstantController* OmniboxEditModel::GetInstantController() const { 1163 InstantController* OmniboxEditModel::GetInstantController() const {
1178 return controller_->GetInstant(); 1164 return controller_->GetInstant();
1179 } 1165 }
1180 1166
1181 bool OmniboxEditModel::query_in_progress() const { 1167 bool OmniboxEditModel::query_in_progress() const {
1182 return !autocomplete_controller()->done(); 1168 return !autocomplete_controller()->done();
1183 } 1169 }
1184 1170
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 instant->OmniboxFocusChanged(state, reason, NULL); 1411 instant->OmniboxFocusChanged(state, reason, NULL);
1426 1412
1427 // Update state and notify view if the omnibox has focus and the caret 1413 // Update state and notify view if the omnibox has focus and the caret
1428 // visibility changed. 1414 // visibility changed.
1429 const bool was_caret_visible = is_caret_visible(); 1415 const bool was_caret_visible = is_caret_visible();
1430 focus_state_ = state; 1416 focus_state_ = state;
1431 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1417 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1432 is_caret_visible() != was_caret_visible) 1418 is_caret_visible() != was_caret_visible)
1433 view_->ApplyCaretVisibility(); 1419 view_->ApplyCaretVisibility();
1434 } 1420 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/omnibox/omnibox_edit_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698