Add testing/scripts/OWNERS
[chromium-blink-merge.git] / ui / app_list / search_controller.cc
blob3a4a52f59c2d96b45c1bda7c3efaf807e00f8eb2
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 mixer_->Init();
38 SearchController::~SearchController() {
41 void SearchController::Start() {
42 Stop();
44 base::string16 query;
45 base::TrimWhitespace(search_box_->text(), base::TRIM_ALL, &query);
47 dispatching_query_ = true;
48 for (Providers::iterator it = providers_.begin();
49 it != providers_.end();
50 ++it) {
51 (*it)->Start(query);
53 dispatching_query_ = false;
55 OnResultsChanged();
57 stop_timer_.Start(FROM_HERE,
58 base::TimeDelta::FromMilliseconds(kStopTimeMS),
59 base::Bind(&SearchController::Stop,
60 base::Unretained(this)));
63 void SearchController::Stop() {
64 stop_timer_.Stop();
66 for (Providers::iterator it = providers_.begin();
67 it != providers_.end();
68 ++it) {
69 (*it)->Stop();
73 void SearchController::OpenResult(SearchResult* result, int event_flags) {
74 // Count AppList.Search here because it is composed of search + action.
75 base::RecordAction(base::UserMetricsAction("AppList_Search"));
77 result->Open(event_flags);
79 if (history_ && history_->IsReady()) {
80 history_->AddLaunchEvent(base::UTF16ToUTF8(search_box_->text()),
81 result->id());
85 void SearchController::InvokeResultAction(SearchResult* result,
86 int action_index,
87 int event_flags) {
88 // TODO(xiyuan): Hook up with user learning.
89 result->InvokeAction(action_index, event_flags);
92 void SearchController::AddProvider(Mixer::GroupId group,
93 scoped_ptr<SearchProvider> provider) {
94 provider->set_result_changed_callback(base::Bind(
95 &SearchController::OnResultsChanged,
96 base::Unretained(this)));
97 mixer_->AddProviderToGroup(group, provider.get());
98 providers_.push_back(provider.release()); // Takes ownership.
101 void SearchController::OnResultsChanged() {
102 if (dispatching_query_)
103 return;
105 KnownResults known_results;
106 if (history_ && history_->IsReady()) {
107 history_->GetKnownResults(base::UTF16ToUTF8(search_box_->text()))
108 ->swap(known_results);
111 mixer_->MixAndPublish(known_results);
114 } // namespace app_list