people_result.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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.
4
5#include "chrome/browser/ui/app_list/search/people/people_result.h"
6
7#include <vector>
8
9#include "base/bind.h"
10#include "base/memory/ref_counted.h"
11#include "base/strings/utf_string_conversions.h"
12#include "chrome/browser/extensions/event_router.h"
13#include "chrome/browser/extensions/extension_system.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/signin/profile_oauth2_token_service.h"
16#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
17#include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
18#include "chrome/browser/ui/app_list/search/people/person.h"
19#include "chrome/browser/ui/browser_navigator.h"
20#include "chrome/common/extensions/api/hangouts_private.h"
21#include "grit/generated_resources.h"
22#include "grit/theme_resources.h"
23#include "ui/base/l10n/l10n_util.h"
24#include "ui/base/resource/resource_bundle.h"
25
26namespace OnHangoutRequested =
27    extensions::api::hangouts_private::OnHangoutRequested;
28
29using extensions::api::hangouts_private::User;
30using extensions::api::hangouts_private::HangoutRequest;
31
32namespace {
33
34const int kIconSize = 32;
35const char kImageSizePath[] = "s32-p/";
36const char kEmailUrlPrefix[] = "mailto:";
37
38const char* const kHangoutsExtensionIds[] = {
39  "nckgahadagoaajjgafhacjanaoiihapd",
40  "ljclpkphhpbpinifbeabbhlfddcpfdde",
41  "ppleadejekpmccmnpjdimmlfljlkdfej",
42  "eggnbpckecmjlblplehfpjjdhhidfdoj"
43};
44
45// Add a query parameter to specify the size to fetch the image in. The
46// original profile image can be of an arbitrary size, we ask the server to
47// crop it to a square 32x32 using its smart cropping algorithm.
48GURL GetImageUrl(const GURL& url) {
49  std::string image_filename = url.ExtractFileName();
50  if (image_filename.empty())
51    return url;
52
53  return url.Resolve(kImageSizePath + image_filename);
54}
55
56}  // namespace
57
58namespace app_list {
59
60PeopleResult::PeopleResult(Profile* profile, scoped_ptr<Person> person)
61    : profile_(profile), person_(person.Pass()), weak_factory_(this) {
62  set_id(person_->id);
63  set_title(UTF8ToUTF16(person_->display_name));
64  set_relevance(person_->interaction_rank);
65  set_details(UTF8ToUTF16(person_->email));
66
67  RefreshHangoutsExtensionId();
68  SetDefaultActions();
69
70  image_ = gfx::ImageSkia(
71      new UrlIconSource(base::Bind(&PeopleResult::OnIconLoaded,
72                                   weak_factory_.GetWeakPtr()),
73                        profile_->GetRequestContext(),
74                        GetImageUrl(person_->image_url),
75                        kIconSize,
76                        IDR_PROFILE_PICTURE_LOADING),
77      gfx::Size(kIconSize, kIconSize));
78  SetIcon(image_);
79}
80
81PeopleResult::~PeopleResult() {
82}
83
84void PeopleResult::Open(int event_flags) {
85  // Action 0 will always be our default action.
86  InvokeAction(0, event_flags);
87}
88
89void PeopleResult::InvokeAction(int action_index, int event_flags) {
90  if (hangouts_extension_id_.empty()) {
91    // If the hangouts app is not available, the only option we are showing
92    // to the user is 'Send Email'.
93    SendEmail();
94  } else {
95    switch (action_index) {
96      case 0:
97        OpenChat();
98        break;
99      case 1:
100        SendEmail();
101        break;
102      default:
103        LOG(ERROR) << "Invalid people search action: " << action_index;
104    }
105  }
106}
107
108scoped_ptr<ChromeSearchResult> PeopleResult::Duplicate() {
109  return scoped_ptr<ChromeSearchResult>(
110      new PeopleResult(profile_, person_->Duplicate().Pass())).Pass();
111}
112
113void PeopleResult::OnIconLoaded() {
114  // Remove the existing image reps since the icon data is loaded and they
115  // need to be re-created.
116  const std::vector<gfx::ImageSkiaRep>& image_reps = image_.image_reps();
117  for (size_t i = 0; i < image_reps.size(); ++i)
118    image_.RemoveRepresentation(image_reps[i].scale());
119
120  SetIcon(image_);
121}
122
123void PeopleResult::SetDefaultActions() {
124  Actions actions;
125
126  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
127  if (!hangouts_extension_id_.empty()) {
128    actions.push_back(Action(
129        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT),
130        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_HOVER),
131        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_PRESSED),
132        l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_CHAT_TOOLTIP)));
133  }
134  actions.push_back(Action(
135      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL),
136      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_HOVER),
137      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_PRESSED),
138      l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_EMAIL_TOOLTIP)));
139  SetActions(actions);
140}
141
142void PeopleResult::OpenChat() {
143  HangoutRequest request;
144
145  request.type = extensions::api::hangouts_private::HANGOUT_TYPE_CHAT;
146
147  // from: the user this chat request is originating from.
148  ProfileOAuth2TokenService* token_service =
149      ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
150  DCHECK(token_service);
151  request.from = token_service->GetPrimaryAccountId();
152
153  // to: list of users with whom to start this hangout is with.
154  linked_ptr<User> target(new User());
155  target->id = person_->owner_id;
156  request.to.push_back(target);
157
158  scoped_ptr<extensions::Event> event(
159      new extensions::Event(OnHangoutRequested::kEventName,
160                            OnHangoutRequested::Create(request)));
161
162  // TODO(rkc): Change this once we remove the hangoutsPrivate API.
163  // See crbug.com/306672
164  extensions::ExtensionSystem::Get(
165      profile_)->event_router()->DispatchEventToExtension(
166          hangouts_extension_id_, event.Pass());
167}
168
169void PeopleResult::SendEmail() {
170  chrome::NavigateParams params(profile_,
171                                GURL(kEmailUrlPrefix + person_->email),
172                                content::PAGE_TRANSITION_LINK);
173  // If no window exists, this will open a new window this one tab.
174  params.disposition = NEW_FOREGROUND_TAB;
175  chrome::Navigate(&params);
176}
177
178void PeopleResult::RefreshHangoutsExtensionId() {
179  // TODO(rkc): Change this once we remove the hangoutsPrivate API.
180  // See crbug.com/306672
181  for (size_t i = 0; i < arraysize(kHangoutsExtensionIds); ++i) {
182    if (extensions::ExtensionSystem::Get(
183        profile_)->event_router()->ExtensionHasEventListener(
184            kHangoutsExtensionIds[i], OnHangoutRequested::kEventName)) {
185      hangouts_extension_id_ = kHangoutsExtensionIds[i];
186      return;
187    }
188  }
189  hangouts_extension_id_.clear();
190}
191
192ChromeSearchResultType PeopleResult::GetType() {
193  return SEARCH_PEOPLE_SEARCH_RESULT;
194}
195
196}  // namespace app_list
197