1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/app_list/search_builder.h"
#include <string>
#include <vector>
#include "base/command_line.h"
#include "chrome/browser/autocomplete/autocomplete_classifier.h"
#include "chrome/browser/autocomplete/autocomplete_controller.h"
#include "chrome/browser/autocomplete/autocomplete_input.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/autocomplete/autocomplete_provider.h"
#include "chrome/browser/autocomplete/autocomplete_result.h"
#include "chrome/browser/event_disposition.h"
#include "chrome/browser/extensions/extension_icon_image.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/app_list_controller.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/app_list/app_list_switches.h"
#include "ui/app_list/search_box_model.h"
#include "ui/app_list/search_result.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_CHROMEOS)
#include "base/memory/ref_counted.h"
#include "chrome/browser/autocomplete/contact_provider_chromeos.h"
#include "chrome/browser/chromeos/contacts/contact.pb.h"
#include "chrome/browser/chromeos/contacts/contact_manager.h"
#include "chrome/browser/extensions/api/rtc_private/rtc_private_api.h"
#include "chrome/browser/image_decoder.h"
#include "chrome/common/chrome_switches.h"
#endif
namespace {
int ACMatchStyleToTagStyle(int styles) {
int tag_styles = 0;
if (styles & ACMatchClassification::URL)
tag_styles |= app_list::SearchResult::Tag::URL;
if (styles & ACMatchClassification::MATCH)
tag_styles |= app_list::SearchResult::Tag::MATCH;
if (styles & ACMatchClassification::DIM)
tag_styles |= app_list::SearchResult::Tag::DIM;
return tag_styles;
}
// Translates ACMatchClassifications into SearchResult tags.
void ACMatchClassificationsToTags(
const string16& text,
const ACMatchClassifications& text_classes,
app_list::SearchResult::Tags* tags) {
int tag_styles = app_list::SearchResult::Tag::NONE;
size_t tag_start = 0;
for (size_t i = 0; i < text_classes.size(); ++i) {
const ACMatchClassification& text_class = text_classes[i];
// Closes current tag.
if (tag_styles != app_list::SearchResult::Tag::NONE) {
tags->push_back(app_list::SearchResult::Tag(
tag_styles, tag_start, text_class.offset));
tag_styles = app_list::SearchResult::Tag::NONE;
}
if (text_class.style == ACMatchClassification::NONE)
continue;
tag_start = text_class.offset;
tag_styles = ACMatchStyleToTagStyle(text_class.style);
}
if (tag_styles != app_list::SearchResult::Tag::NONE) {
tags->push_back(app_list::SearchResult::Tag(
tag_styles, tag_start, text.length()));
}
}
const extensions::Extension* GetExtensionByURL(Profile* profile,
const GURL& url) {
ExtensionService* service = profile->GetExtensionService();
// Need to explicitly get chrome app because it does not override new tab and
// not having a web extent to include new tab url, thus GetInstalledApp does
// not find it.
return url.spec() == chrome::kChromeUINewTabURL ?
service->extensions()->GetByID(extension_misc::kChromeAppId) :
service->GetInstalledApp(url);
}
// SearchBuildResult is an app list SearchResult built from an
// AutocompleteMatch.
class SearchBuilderResult : public app_list::SearchResult {
public:
SearchBuilderResult() : profile_(NULL) {}
virtual ~SearchBuilderResult() {}
Profile* profile() const { return profile_; }
const AutocompleteMatch& match() const { return match_; }
virtual void Init(Profile* profile, const AutocompleteMatch& match) {
profile_ = profile;
match_ = match;
UpdateIcon();
UpdateTitleAndDetails();
}
protected:
virtual void UpdateIcon() {
int resource_id = match_.starred ?
IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type);
SetIcon(*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
resource_id));
}
virtual void UpdateTitleAndDetails() {
set_title(match_.contents);
app_list::SearchResult::Tags title_tags;
ACMatchClassificationsToTags(match_.contents,
match_.contents_class,
&title_tags);
set_title_tags(title_tags);
set_details(match_.description);
app_list::SearchResult::Tags details_tags;
ACMatchClassificationsToTags(match_.description,
match_.description_class,
&details_tags);
set_details_tags(details_tags);
}
private:
Profile* profile_; // not owned
AutocompleteMatch match_;
DISALLOW_COPY_AND_ASSIGN(SearchBuilderResult);
};
// SearchBuilderResult implementation for AutocompleteMatch::EXTENSION_APP
// results.
class ExtensionAppResult : public SearchBuilderResult,
public extensions::IconImage::Observer {
public:
ExtensionAppResult() {}
virtual ~ExtensionAppResult() {}
protected:
virtual void UpdateIcon() OVERRIDE {
const extensions::Extension* extension =
GetExtensionByURL(profile(), match().destination_url);
if (extension)
LoadExtensionIcon(extension);
else
SearchBuilderResult::UpdateIcon();
}
private:
void LoadExtensionIcon(const extensions::Extension* extension) {
const gfx::ImageSkia default_icon = profile()->GetExtensionService()->
GetOmniboxPopupIcon(extension->id()).AsImageSkia();
icon_.reset(new extensions::IconImage(
extension,
extension->icons(),
extension_misc::EXTENSION_ICON_SMALL,
default_icon,
this));
SetIcon(icon_->image_skia());
}
// Overridden from extensions::IconImage::Observer:
virtual void OnExtensionIconImageChanged(
extensions::IconImage* image) OVERRIDE {
DCHECK_EQ(icon_.get(), image);
SetIcon(icon_->image_skia());
}
scoped_ptr<extensions::IconImage> icon_;
DISALLOW_COPY_AND_ASSIGN(ExtensionAppResult);
};
#if defined(OS_CHROMEOS)
// SearchBuilderResult implementation for AutocompleteMatch::CONTACT results.
class ContactResult : public SearchBuilderResult,
public ImageDecoder::Delegate {
public:
ContactResult() {}
virtual ~ContactResult() {
if (photo_decoder_.get())
photo_decoder_->set_delegate(NULL);
}
// Returns the contact represented by this result, or NULL if the contact
// can't be found (e.g. it's been deleted in the meantime). The returned
// object is only valid until the UI message loop continues running.
const contacts::Contact* GetContact() const {
AutocompleteMatch::AdditionalInfo::const_iterator it =
match().additional_info.find(ContactProvider::kMatchContactIdKey);
DCHECK(it != match().additional_info.end());
return contacts::ContactManager::GetInstance()->GetContactById(
profile(), it->second);
}
// Overridden from SearchBuilderResult:
virtual void Init(Profile* profile, const AutocompleteMatch& match) OVERRIDE {
SearchBuilderResult::Init(profile, match);
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
std::vector<app_list::SearchResult::ActionIconSet> icons;
icons.push_back(
app_list::SearchResult::ActionIconSet(
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_CHAT),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_CHAT_H),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_CHAT),
l10n_util::GetStringUTF16(IDS_APP_LIST_CONTACT_CHAT_TOOLTIP)));
icons.push_back(
app_list::SearchResult::ActionIconSet(
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_VIDEO),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_VIDEO_H),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_VIDEO),
l10n_util::GetStringUTF16(IDS_APP_LIST_CONTACT_VIDEO_TOOLTIP)));
icons.push_back(
app_list::SearchResult::ActionIconSet(
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_PHONE),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_PHONE_H),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_PHONE),
l10n_util::GetStringUTF16(IDS_APP_LIST_CONTACT_PHONE_TOOLTIP)));
icons.push_back(
app_list::SearchResult::ActionIconSet(
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_EMAIL),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_EMAIL_H),
*bundle.GetImageSkiaNamed(IDR_CONTACT_ACTION_EMAIL),
l10n_util::GetStringUTF16(IDS_APP_LIST_CONTACT_EMAIL_TOOLTIP)));
SetActionIcons(icons);
}
protected:
// Overridden from SearchBuilderResult:
virtual void UpdateIcon() OVERRIDE {
const contacts::Contact* contact = GetContact();
if (contact && contact->has_raw_untrusted_photo()) {
photo_decoder_ =
new ImageDecoder(
this,
contact->raw_untrusted_photo(),
ImageDecoder::DEFAULT_CODEC);
photo_decoder_->Start();
} else {
SetIcon(
*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_CONTACT_DEFAULT_PHOTO));
}
}
private:
// Overridden from ImageDecoder::Delegate:
virtual void OnImageDecoded(const ImageDecoder* decoder,
const SkBitmap& decoded_image) OVERRIDE {
DCHECK_EQ(decoder, photo_decoder_);
SetIcon(gfx::ImageSkia(decoded_image));
}
virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE {
DCHECK_EQ(decoder, photo_decoder_);
SetIcon(
*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_CONTACT_DEFAULT_PHOTO));
}
scoped_refptr<ImageDecoder> photo_decoder_;
DISALLOW_COPY_AND_ASSIGN(ContactResult);
};
#endif
} // namespace
SearchBuilder::SearchBuilder(
Profile* profile,
app_list::SearchBoxModel* search_box,
app_list::AppListModel::SearchResults* results,
AppListControllerDelegate* list_controller)
: profile_(profile),
search_box_(search_box),
results_(results),
list_controller_(list_controller) {
search_box_->SetHintText(
l10n_util::GetStringUTF16(IDS_SEARCH_BOX_HINT));
search_box_->SetIcon(*ui::ResourceBundle::GetSharedInstance().
GetImageSkiaNamed(IDR_OMNIBOX_SEARCH));
int providers = AutocompleteProvider::TYPE_EXTENSION_APP;
if (!CommandLine::ForCurrentProcess()->HasSwitch(
app_list::switches::kAppListShowAppsOnly)) {
// TODO(xiyuan): Consider requesting fewer providers in the non-apps-only
// case.
providers |= AutocompleteClassifier::kDefaultOmniboxProviders;
}
#if defined(OS_CHROMEOS)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableContacts))
providers |= AutocompleteProvider::TYPE_CONTACT;
#endif
controller_.reset(new AutocompleteController(profile, this, providers));
}
SearchBuilder::~SearchBuilder() {
}
void SearchBuilder::StartSearch() {
// Omnibox features such as keyword selection/accepting and instant query
// are not implemented.
// TODO(xiyuan): Figure out the features that need to support here.
controller_->Start(search_box_->text(), string16(), false, false, true,
AutocompleteInput::ALL_MATCHES);
}
void SearchBuilder::StopSearch() {
controller_->Stop(true);
}
void SearchBuilder::OpenResult(const app_list::SearchResult& result,
int event_flags) {
const SearchBuilderResult* builder_result =
static_cast<const SearchBuilderResult*>(&result);
const AutocompleteMatch& match = builder_result->match();
// Count AppList.Search here because it is composed of search + action.
content::RecordAction(content::UserMetricsAction("AppList_Search"));
if (match.type == AutocompleteMatch::EXTENSION_APP) {
const extensions::Extension* extension =
GetExtensionByURL(profile_, match.destination_url);
if (extension) {
content::RecordAction(
content::UserMetricsAction("AppList_ClickOnAppFromSearch"));
list_controller_->ActivateApp(profile_, extension->id(), event_flags);
}
#if defined(OS_CHROMEOS)
} else if (match.type == AutocompleteMatch::CONTACT) {
// Pass 0 for |action_index| to trigger a chat request.
InvokeResultAction(result, 0, event_flags);
#endif
} else {
// TODO(xiyuan): What should we do for alternate url case?
chrome::NavigateParams params(profile_,
match.destination_url,
match.transition);
params.disposition = chrome::DispositionFromEventFlags(event_flags);
chrome::Navigate(¶ms);
}
}
void SearchBuilder::InvokeResultAction(const app_list::SearchResult& result,
int action_index,
int event_flags) {
#if defined(OS_CHROMEOS)
const SearchBuilderResult* builder_result =
static_cast<const SearchBuilderResult*>(&result);
const AutocompleteMatch& match = builder_result->match();
if (match.type == AutocompleteMatch::CONTACT) {
const contacts::Contact* contact =
static_cast<const ContactResult*>(builder_result)->GetContact();
if (!contact)
return;
// Cases match the order in which actions were added in
// ContactResult::Init().
extensions::RtcPrivateEventRouter::LaunchAction launch_action =
extensions::RtcPrivateEventRouter::LAUNCH_CHAT;
switch (action_index) {
case 0:
launch_action = extensions::RtcPrivateEventRouter::LAUNCH_CHAT;
break;
case 1:
launch_action = extensions::RtcPrivateEventRouter::LAUNCH_VIDEO;
break;
case 2:
launch_action = extensions::RtcPrivateEventRouter::LAUNCH_VOICE;
break;
case 3:
// TODO(derat): Send email.
NOTIMPLEMENTED();
break;
default:
NOTREACHED() << "Unexpected action index " << action_index;
}
extensions::RtcPrivateEventRouter::DispatchLaunchEvent(
builder_result->profile(), launch_action, contact);
return;
}
#endif
NOTIMPLEMENTED();
}
void SearchBuilder::PopulateFromACResult(const AutocompleteResult& ac_result) {
results_->DeleteAll();
for (ACMatches::const_iterator it = ac_result.begin();
it != ac_result.end();
++it) {
SearchBuilderResult* result = NULL;
switch (it->type) {
case AutocompleteMatch::EXTENSION_APP:
result = new ExtensionAppResult();
break;
#if defined(OS_CHROMEOS)
case AutocompleteMatch::CONTACT:
result = new ContactResult();
break;
#endif
default:
result = new SearchBuilderResult();
}
result->Init(profile_, *it);
results_->Add(result);
}
}
void SearchBuilder::OnResultChanged(bool default_match_changed) {
// TODO(xiyuan): Handle default match properly.
const AutocompleteResult& ac_result = controller_->result();
PopulateFromACResult(ac_result);
}
|