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 "chrome/browser/ui/app_list/search/search_controller.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/app_list/search/app_search_provider.h"
18 #include "chrome/browser/ui/app_list/search/chrome_search_result.h"
19 #include "chrome/browser/ui/app_list/search/history.h"
20 #include "chrome/browser/ui/app_list/search/history_factory.h"
21 #include "chrome/browser/ui/app_list/search/omnibox_provider.h"
22 #include "chrome/browser/ui/app_list/search/people/people_provider.h"
23 #include "chrome/browser/ui/app_list/search/search_provider.h"
24 #include "chrome/browser/ui/app_list/search/webstore/webstore_provider.h"
25 #include "chrome/browser/ui/app_list/start_page_service.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "content/public/browser/user_metrics.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/app_list/search_box_model.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
35 const char kAppListSearchResultOpenTypeHistogram
[] =
36 "Apps.AppListSearchResultOpenType";
41 SearchController::SearchController(Profile
* profile
,
42 SearchBoxModel
* search_box
,
43 AppListModel::SearchResults
* results
,
44 AppListControllerDelegate
* list_controller
)
46 search_box_(search_box
),
47 list_controller_(list_controller
),
48 dispatching_query_(false),
49 mixer_(new Mixer(results
)),
50 history_(HistoryFactory::GetForBrowserContext(profile
)) {
54 SearchController::~SearchController() {}
56 void SearchController::Init() {
57 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
58 search_box_
->SetHintText(
59 l10n_util::GetStringUTF16(IDS_SEARCH_BOX_HINT
));
60 search_box_
->SetIcon(*bundle
.GetImageSkiaNamed(IDR_OMNIBOX_SEARCH
));
61 StartPageService
* service
= StartPageService::Get(profile_
);
62 if (service
&& service
->GetSpeechRecognitionContents()) {
63 search_box_
->SetSpeechRecognitionButton(
64 scoped_ptr
<SearchBoxModel::ButtonProperty
>(
65 new SearchBoxModel::ButtonProperty(
66 *bundle
.GetImageSkiaNamed(IDR_OMNIBOX_MIC_SEARCH
),
67 l10n_util::GetStringUTF16(
68 IDS_APP_LIST_START_SPEECH_RECOGNITION
))));
73 AddProvider(Mixer::MAIN_GROUP
, scoped_ptr
<SearchProvider
>(
74 new AppSearchProvider(profile_
, list_controller_
)).Pass());
75 AddProvider(Mixer::OMNIBOX_GROUP
, scoped_ptr
<SearchProvider
>(
76 new OmniboxProvider(profile_
)).Pass());
77 AddProvider(Mixer::WEBSTORE_GROUP
, scoped_ptr
<SearchProvider
>(
78 new WebstoreProvider(profile_
, list_controller_
)).Pass());
79 if (!CommandLine::ForCurrentProcess()->HasSwitch(
80 switches::kDisablePeopleSearch
)) {
81 AddProvider(Mixer::PEOPLE_GROUP
, scoped_ptr
<SearchProvider
>(
82 new PeopleProvider(profile_
)).Pass());
86 void SearchController::Start() {
90 TrimWhitespace(search_box_
->text(), TRIM_ALL
, &query
);
92 dispatching_query_
= true;
93 for (Providers::iterator it
= providers_
.begin();
94 it
!= providers_
.end();
98 dispatching_query_
= false;
102 // Maximum time (in milliseconds) to wait to the search providers to finish.
103 const int kStopTimeMS
= 1500;
104 stop_timer_
.Start(FROM_HERE
,
105 base::TimeDelta::FromMilliseconds(kStopTimeMS
),
106 base::Bind(&SearchController::Stop
,
107 base::Unretained(this)));
110 void SearchController::Stop() {
113 for (Providers::iterator it
= providers_
.begin();
114 it
!= providers_
.end();
120 void SearchController::OpenResult(SearchResult
* result
, int event_flags
) {
121 // Count AppList.Search here because it is composed of search + action.
122 content::RecordAction(base::UserMetricsAction("AppList_Search"));
124 ChromeSearchResult
* chrome_result
=
125 static_cast<app_list::ChromeSearchResult
*>(result
);
126 UMA_HISTOGRAM_ENUMERATION(kAppListSearchResultOpenTypeHistogram
,
127 chrome_result
->GetType(),
128 SEARCH_RESULT_TYPE_BOUNDARY
);
129 chrome_result
->Open(event_flags
);
131 if (history_
&& history_
->IsReady()) {
132 history_
->AddLaunchEvent(base::UTF16ToUTF8(search_box_
->text()),
133 chrome_result
->id());
137 void SearchController::InvokeResultAction(SearchResult
* result
,
140 // TODO(xiyuan): Hook up with user learning.
141 static_cast<app_list::ChromeSearchResult
*>(result
)->InvokeAction(
142 action_index
, event_flags
);
145 void SearchController::AddProvider(Mixer::GroupId group
,
146 scoped_ptr
<SearchProvider
> provider
) {
147 provider
->set_result_changed_callback(base::Bind(
148 &SearchController::OnResultsChanged
,
149 base::Unretained(this)));
150 mixer_
->AddProviderToGroup(group
, provider
.get());
151 providers_
.push_back(provider
.release()); // Takes ownership.
154 void SearchController::OnResultsChanged() {
155 if (dispatching_query_
)
158 KnownResults known_results
;
159 if (history_
&& history_
->IsReady()) {
160 history_
->GetKnownResults(base::UTF16ToUTF8(search_box_
->text()))
161 ->swap(known_results
);
164 mixer_
->MixAndPublish(known_results
);
167 } // namespace app_list