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/people/people_result.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
16 #include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
17 #include "chrome/browser/ui/app_list/search/people/person.h"
18 #include "chrome/browser/ui/app_list/search/search_util.h"
19 #include "chrome/common/extensions/api/hangouts_private.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/signin/core/browser/profile_oauth2_token_service.h"
22 #include "components/signin/core/browser/signin_manager.h"
23 #include "content/public/browser/user_metrics.h"
24 #include "extensions/browser/event_router.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
29 namespace OnHangoutRequested
=
30 extensions::api::hangouts_private::OnHangoutRequested
;
32 using extensions::api::hangouts_private::User
;
33 using extensions::api::hangouts_private::HangoutRequest
;
37 const char kImageSizePath
[] = "s64-p/";
38 const char kEmailUrlPrefix
[] = "mailto:";
40 // Add a query parameter to specify the size to fetch the image in. The
41 // original profile image can be of an arbitrary size, we ask the server to
42 // crop it to a square 64x64 using its smart cropping algorithm.
43 GURL
GetImageUrl(const GURL
& url
) {
44 std::string image_filename
= url
.ExtractFileName();
45 if (image_filename
.empty())
48 return url
.Resolve(kImageSizePath
+ image_filename
);
55 PeopleResult::PeopleResult(Profile
* profile
,
56 AppListControllerDelegate
* controller
,
57 scoped_ptr
<Person
> person
)
59 controller_(controller
),
60 person_(person
.Pass()),
63 set_title(base::UTF8ToUTF16(person_
->display_name
));
64 set_relevance(person_
->interaction_rank
);
65 set_details(base::UTF8ToUTF16(person_
->email
));
67 RefreshHangoutsExtensionId();
70 int icon_size
= GetPreferredIconDimension();
71 image_
= gfx::ImageSkia(
73 base::Bind(&PeopleResult::OnIconLoaded
, weak_factory_
.GetWeakPtr()),
74 profile_
->GetRequestContext(),
75 GetImageUrl(person_
->image_url
),
77 IDR_PROFILE_PICTURE_LOADING
),
78 gfx::Size(icon_size
, icon_size
));
82 PeopleResult::~PeopleResult() {
85 void PeopleResult::Open(int event_flags
) {
86 RecordHistogram(SEARCH_PEOPLE_SEARCH_RESULT
);
88 // Action 0 will always be our default action.
89 InvokeAction(0, event_flags
);
92 void PeopleResult::InvokeAction(int action_index
, int event_flags
) {
93 if (hangouts_extension_id_
.empty()) {
94 // If the hangouts app is not available, the only option we are showing
95 // to the user is 'Send Email'.
98 switch (action_index
) {
106 LOG(ERROR
) << "Invalid people search action: " << action_index
;
111 scoped_ptr
<SearchResult
> PeopleResult::Duplicate() const {
112 return scoped_ptr
<SearchResult
>(
113 new PeopleResult(profile_
, controller_
, person_
->Duplicate().Pass()));
116 void PeopleResult::OnIconLoaded() {
117 // Remove the existing image reps since the icon data is loaded and they
118 // need to be re-created.
119 const std::vector
<gfx::ImageSkiaRep
>& image_reps
= image_
.image_reps();
120 for (size_t i
= 0; i
< image_reps
.size(); ++i
)
121 image_
.RemoveRepresentation(image_reps
[i
].scale());
126 void PeopleResult::SetDefaultActions() {
129 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
130 if (!hangouts_extension_id_
.empty()) {
131 actions
.push_back(Action(
132 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT
),
133 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_HOVER
),
134 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_PRESSED
),
135 l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_CHAT_TOOLTIP
)));
137 actions
.push_back(Action(
138 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL
),
139 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_HOVER
),
140 *bundle
.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_PRESSED
),
141 l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_EMAIL_TOOLTIP
)));
145 void PeopleResult::OpenChat() {
146 HangoutRequest request
;
148 request
.type
= extensions::api::hangouts_private::HANGOUT_TYPE_CHAT
;
150 // from: the user this chat request is originating from.
151 SigninManagerBase
* signin_manager
=
152 SigninManagerFactory::GetInstance()->GetForProfile(profile_
);
153 DCHECK(signin_manager
);
154 request
.from
= signin_manager
->GetAuthenticatedAccountInfo().email
;
156 // to: list of users with whom to start this hangout is with.
157 linked_ptr
<User
> target(new User());
158 target
->id
= person_
->owner_id
;
159 request
.to
.push_back(target
);
161 scoped_ptr
<extensions::Event
> event(new extensions::Event(
162 extensions::events::HANGOUTS_PRIVATE_ON_HANGOUT_REQUESTED
,
163 OnHangoutRequested::kEventName
, OnHangoutRequested::Create(request
)));
165 // TODO(rkc): Change this once we remove the hangoutsPrivate API.
166 // See crbug.com/306672
167 extensions::EventRouter::Get(profile_
)
168 ->DispatchEventToExtension(hangouts_extension_id_
, event
.Pass());
170 content::RecordAction(base::UserMetricsAction("PeopleSearch_OpenChat"));
173 void PeopleResult::SendEmail() {
174 controller_
->OpenURL(profile_
,
175 GURL(kEmailUrlPrefix
+ person_
->email
),
176 ui::PAGE_TRANSITION_LINK
,
178 content::RecordAction(base::UserMetricsAction("PeopleSearch_SendEmail"));
181 void PeopleResult::RefreshHangoutsExtensionId() {
182 for (const char* id
: extension_misc::kHangoutsExtensionIds
) {
183 if (extensions::EventRouter::Get(profile_
)
184 ->ExtensionHasEventListener(id
, OnHangoutRequested::kEventName
)) {
185 hangouts_extension_id_
= id
;
189 hangouts_extension_id_
.clear();
192 } // namespace app_list