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"
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"
22 // Maximum time (in milliseconds) to wait to the search providers to finish.
23 const int kStopTimeMS
= 1500;
28 SearchController::SearchController(SearchBoxModel
* search_box
,
29 AppListModel::SearchResults
* results
,
31 : search_box_(search_box
),
32 dispatching_query_(false),
33 mixer_(new Mixer(results
)),
35 is_voice_query_(false) {
39 SearchController::~SearchController() {
42 void SearchController::Start(bool is_voice_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();
52 (*it
)->Start(is_voice_query
, query
);
54 dispatching_query_
= false;
56 is_voice_query_
= is_voice_query
;
60 stop_timer_
.Start(FROM_HERE
,
61 base::TimeDelta::FromMilliseconds(kStopTimeMS
),
62 base::Bind(&SearchController::Stop
,
63 base::Unretained(this)));
66 void SearchController::Stop() {
69 for (Providers::iterator it
= providers_
.begin();
70 it
!= providers_
.end();
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()),
88 void SearchController::InvokeResultAction(SearchResult
* result
,
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_
)
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