Workaround for xkbcommon dead keys.
[chromium-blink-merge.git] / ui / app_list / search_controller.cc
blob66d7593c704c440f75a0ecd86450a3a9c28be4bc
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 "ui/app_list/search_controller.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/user_metrics.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "ui/app_list/search/history.h"
16 #include "ui/app_list/search_box_model.h"
17 #include "ui/app_list/search_provider.h"
18 #include "ui/app_list/search_result.h"
20 namespace {
22 // Maximum time (in milliseconds) to wait to the search providers to finish.
23 const int kStopTimeMS = 1500;
26 namespace app_list {
28 SearchController::SearchController(SearchBoxModel* search_box,
29 AppListModel::SearchResults* results,
30 History* history)
31 : search_box_(search_box),
32 dispatching_query_(false),
33 mixer_(new Mixer(results)),
34 history_(history),
35 is_voice_query_(false) {
36 mixer_->Init();
39 SearchController::~SearchController() {
42 void SearchController::Start(bool is_voice_query) {
43 Stop();
45 base::string16 query;
46 base::TrimWhitespace(search_box_->text(), base::TRIM_ALL, &query);
48 dispatching_query_ = true;
49 for (Providers::iterator it = providers_.begin();
50 it != providers_.end();
51 ++it) {
52 (*it)->Start(is_voice_query, query);
54 dispatching_query_ = false;
56 is_voice_query_ = is_voice_query;
58 OnResultsChanged();
60 stop_timer_.Start(FROM_HERE,
61 base::TimeDelta::FromMilliseconds(kStopTimeMS),
62 base::Bind(&SearchController::Stop,
63 base::Unretained(this)));
66 void SearchController::Stop() {
67 stop_timer_.Stop();
69 for (Providers::iterator it = providers_.begin();
70 it != providers_.end();
71 ++it) {
72 (*it)->Stop();
76 void SearchController::OpenResult(SearchResult* result, int event_flags) {
77 // Count AppList.Search here because it is composed of search + action.
78 base::RecordAction(base::UserMetricsAction("AppList_Search"));
80 result->Open(event_flags);
82 if (history_ && history_->IsReady()) {
83 history_->AddLaunchEvent(base::UTF16ToUTF8(search_box_->text()),
84 result->id());
88 void SearchController::InvokeResultAction(SearchResult* result,
89 int action_index,
90 int event_flags) {
91 // TODO(xiyuan): Hook up with user learning.
92 result->InvokeAction(action_index, event_flags);
95 void SearchController::AddProvider(Mixer::GroupId group,
96 scoped_ptr<SearchProvider> provider) {
97 provider->set_result_changed_callback(base::Bind(
98 &SearchController::OnResultsChanged,
99 base::Unretained(this)));
100 mixer_->AddProviderToGroup(group, provider.get());
101 providers_.push_back(provider.release()); // Takes ownership.
104 void SearchController::OnResultsChanged() {
105 if (dispatching_query_)
106 return;
108 KnownResults known_results;
109 if (history_ && history_->IsReady()) {
110 history_->GetKnownResults(base::UTF16ToUTF8(search_box_->text()))
111 ->swap(known_results);
114 mixer_->MixAndPublish(is_voice_query_, known_results);
117 } // namespace app_list