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
)),
38 SearchController::~SearchController() {
41 void SearchController::Start() {
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();
53 dispatching_query_
= false;
57 stop_timer_
.Start(FROM_HERE
,
58 base::TimeDelta::FromMilliseconds(kStopTimeMS
),
59 base::Bind(&SearchController::Stop
,
60 base::Unretained(this)));
63 void SearchController::Stop() {
66 for (Providers::iterator it
= providers_
.begin();
67 it
!= providers_
.end();
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()),
85 void SearchController::InvokeResultAction(SearchResult
* result
,
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_
)
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