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/histogram_macros.h"
13 #include "base/metrics/user_metrics.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "ui/app_list/app_list_constants.h"
17 #include "ui/app_list/search/history.h"
18 #include "ui/app_list/search_box_model.h"
19 #include "ui/app_list/search_provider.h"
20 #include "ui/app_list/search_result.h"
24 // Maximum time (in milliseconds) to wait to the search providers to finish.
25 const int kStopTimeMS
= 1500;
30 SearchController::SearchController(SearchBoxModel
* search_box
,
31 AppListModel::SearchResults
* results
,
33 : search_box_(search_box
),
34 dispatching_query_(false),
35 mixer_(new Mixer(results
)),
37 is_voice_query_(false) {
41 SearchController::~SearchController() {
44 void SearchController::Start(bool is_voice_query
) {
48 base::TrimWhitespace(search_box_
->text(), base::TRIM_ALL
, &query
);
50 dispatching_query_
= true;
51 for (Providers::iterator it
= providers_
.begin();
52 it
!= providers_
.end();
54 (*it
)->Start(is_voice_query
, query
);
56 dispatching_query_
= false;
58 is_voice_query_
= is_voice_query
;
62 stop_timer_
.Start(FROM_HERE
,
63 base::TimeDelta::FromMilliseconds(kStopTimeMS
),
64 base::Bind(&SearchController::Stop
,
65 base::Unretained(this)));
68 void SearchController::Stop() {
71 for (Providers::iterator it
= providers_
.begin();
72 it
!= providers_
.end();
78 void SearchController::OpenResult(SearchResult
* result
, int event_flags
) {
79 // Count AppList.Search here because it is composed of search + action.
80 base::RecordAction(base::UserMetricsAction("AppList_Search"));
82 UMA_HISTOGRAM_ENUMERATION(kSearchResultOpenDisplayTypeHistogram
,
83 result
->display_type(),
84 SearchResult::DISPLAY_TYPE_LAST
);
86 result
->Open(event_flags
);
88 if (history_
&& history_
->IsReady()) {
89 history_
->AddLaunchEvent(base::UTF16ToUTF8(search_box_
->text()),
94 void SearchController::InvokeResultAction(SearchResult
* result
,
97 // TODO(xiyuan): Hook up with user learning.
98 result
->InvokeAction(action_index
, event_flags
);
101 void SearchController::AddProvider(Mixer::GroupId group
,
102 scoped_ptr
<SearchProvider
> provider
) {
103 provider
->set_result_changed_callback(base::Bind(
104 &SearchController::OnResultsChanged
,
105 base::Unretained(this)));
106 mixer_
->AddProviderToGroup(group
, provider
.get());
107 providers_
.push_back(provider
.release()); // Takes ownership.
110 void SearchController::OnResultsChanged() {
111 if (dispatching_query_
)
114 KnownResults known_results
;
115 if (history_
&& history_
->IsReady()) {
116 history_
->GetKnownResults(base::UTF16ToUTF8(search_box_
->text()))
117 ->swap(known_results
);
120 mixer_
->MixAndPublish(is_voice_query_
, known_results
);
123 } // namespace app_list