Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / omnibox / browser / omnibox_controller.cc
blob9cbe634dfc8eb0eabfeb49ddcdda3118a69f90ec
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
3 // found in the LICENSE file.
5 #include "components/omnibox/browser/omnibox_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "components/omnibox/browser/autocomplete_classifier.h"
9 #include "components/omnibox/browser/autocomplete_match.h"
10 #include "components/omnibox/browser/omnibox_client.h"
11 #include "components/omnibox/browser/omnibox_edit_controller.h"
12 #include "components/omnibox/browser/omnibox_edit_model.h"
13 #include "components/omnibox/browser/omnibox_popup_model.h"
14 #include "components/omnibox/browser/omnibox_popup_view.h"
15 #include "components/search/search.h"
16 #include "ui/gfx/geometry/rect.h"
18 OmniboxController::OmniboxController(OmniboxEditModel* omnibox_edit_model,
19 OmniboxClient* client)
20 : omnibox_edit_model_(omnibox_edit_model),
21 client_(client),
22 popup_(NULL),
23 autocomplete_controller_(new AutocompleteController(
24 client_->CreateAutocompleteProviderClient(),
25 this,
26 AutocompleteClassifier::kDefaultOmniboxProviders)),
27 weak_ptr_factory_(this) {}
29 OmniboxController::~OmniboxController() {
32 void OmniboxController::StartAutocomplete(
33 const AutocompleteInput& input) const {
34 ClearPopupKeywordMode();
35 popup_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
37 // We don't explicitly clear OmniboxPopupModel::manually_selected_match, as
38 // Start ends up invoking OmniboxPopupModel::OnResultChanged which clears it.
39 autocomplete_controller_->Start(input);
42 void OmniboxController::OnResultChanged(bool default_match_changed) {
43 const bool was_open = popup_->IsOpen();
44 if (default_match_changed) {
45 // The default match has changed, we need to let the OmniboxEditModel know
46 // about new inline autocomplete text (blue highlight).
47 const AutocompleteResult::const_iterator match(result().default_match());
48 if (match != result().end()) {
49 current_match_ = *match;
50 omnibox_edit_model_->OnCurrentMatchChanged();
51 } else {
52 InvalidateCurrentMatch();
53 popup_->OnResultChanged();
54 omnibox_edit_model_->OnPopupDataChanged(base::string16(), NULL,
55 base::string16(), false);
57 } else {
58 popup_->OnResultChanged();
61 if (!popup_->IsOpen() && was_open) {
62 // Accept the temporary text as the user text, because it makes little sense
63 // to have temporary text when the popup is closed.
64 omnibox_edit_model_->AcceptTemporaryTextAsUserText();
67 // Note: The client outlives |this|, so bind a weak pointer to the callback
68 // passed in to eliminate the potential for crashes on shutdown.
69 client_->OnResultChanged(result(), default_match_changed,
70 base::Bind(&OmniboxController::SetAnswerBitmap,
71 weak_ptr_factory_.GetWeakPtr()));
74 void OmniboxController::InvalidateCurrentMatch() {
75 current_match_ = AutocompleteMatch();
78 void OmniboxController::ClearPopupKeywordMode() const {
79 if (popup_->IsOpen() &&
80 popup_->selected_line_state() == OmniboxPopupModel::KEYWORD)
81 popup_->SetSelectedLineState(OmniboxPopupModel::NORMAL);
84 void OmniboxController::SetAnswerBitmap(const SkBitmap& bitmap) {
85 popup_->SetAnswerBitmap(bitmap);