summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/app_list/search/people/people_result.cc
blob: 8668d634be2a30133f4bc556b04c3694e01cb0e8 (plain)
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
// Copyright 2013 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/people/people_result.h"

#include <vector>

#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
#include "chrome/browser/ui/app_list/search/people/person.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/common/extensions/api/hangouts_private.h"
#include "content/public/browser/user_metrics.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

namespace OnHangoutRequested =
    extensions::api::hangouts_private::OnHangoutRequested;

using extensions::api::hangouts_private::User;
using extensions::api::hangouts_private::HangoutRequest;

namespace {

const int kIconSize = 32;
const char kImageSizePath[] = "s32-p/";
const char kEmailUrlPrefix[] = "mailto:";

const char* const kHangoutsExtensionIds[] = {
  "nckgahadagoaajjgafhacjanaoiihapd",
  "ljclpkphhpbpinifbeabbhlfddcpfdde",
  "ppleadejekpmccmnpjdimmlfljlkdfej",
  "eggnbpckecmjlblplehfpjjdhhidfdoj"
};

// Add a query parameter to specify the size to fetch the image in. The
// original profile image can be of an arbitrary size, we ask the server to
// crop it to a square 32x32 using its smart cropping algorithm.
GURL GetImageUrl(const GURL& url) {
  std::string image_filename = url.ExtractFileName();
  if (image_filename.empty())
    return url;

  return url.Resolve(kImageSizePath + image_filename);
}

}  // namespace

namespace app_list {

PeopleResult::PeopleResult(Profile* profile, scoped_ptr<Person> person)
    : profile_(profile), person_(person.Pass()), weak_factory_(this) {
  set_id(person_->id);
  set_title(base::UTF8ToUTF16(person_->display_name));
  set_relevance(person_->interaction_rank);
  set_details(base::UTF8ToUTF16(person_->email));

  RefreshHangoutsExtensionId();
  SetDefaultActions();

  image_ = gfx::ImageSkia(
      new UrlIconSource(base::Bind(&PeopleResult::OnIconLoaded,
                                   weak_factory_.GetWeakPtr()),
                        profile_->GetRequestContext(),
                        GetImageUrl(person_->image_url),
                        kIconSize,
                        IDR_PROFILE_PICTURE_LOADING),
      gfx::Size(kIconSize, kIconSize));
  SetIcon(image_);
}

PeopleResult::~PeopleResult() {
}

void PeopleResult::Open(int event_flags) {
  // Action 0 will always be our default action.
  InvokeAction(0, event_flags);
}

void PeopleResult::InvokeAction(int action_index, int event_flags) {
  if (hangouts_extension_id_.empty()) {
    // If the hangouts app is not available, the only option we are showing
    // to the user is 'Send Email'.
    SendEmail();
  } else {
    switch (action_index) {
      case 0:
        OpenChat();
        break;
      case 1:
        SendEmail();
        break;
      default:
        LOG(ERROR) << "Invalid people search action: " << action_index;
    }
  }
}

scoped_ptr<ChromeSearchResult> PeopleResult::Duplicate() {
  return scoped_ptr<ChromeSearchResult>(
      new PeopleResult(profile_, person_->Duplicate().Pass())).Pass();
}

void PeopleResult::OnIconLoaded() {
  // Remove the existing image reps since the icon data is loaded and they
  // need to be re-created.
  const std::vector<gfx::ImageSkiaRep>& image_reps = image_.image_reps();
  for (size_t i = 0; i < image_reps.size(); ++i)
    image_.RemoveRepresentation(image_reps[i].scale());

  SetIcon(image_);
}

void PeopleResult::SetDefaultActions() {
  Actions actions;

  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  if (!hangouts_extension_id_.empty()) {
    actions.push_back(Action(
        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT),
        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_HOVER),
        *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_CHAT_PRESSED),
        l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_CHAT_TOOLTIP)));
  }
  actions.push_back(Action(
      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL),
      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_HOVER),
      *bundle.GetImageSkiaNamed(IDR_PEOPLE_SEARCH_ACTION_EMAIL_PRESSED),
      l10n_util::GetStringUTF16(IDS_PEOPLE_SEARCH_ACTION_EMAIL_TOOLTIP)));
  SetActions(actions);
}

void PeopleResult::OpenChat() {
  HangoutRequest request;

  request.type = extensions::api::hangouts_private::HANGOUT_TYPE_CHAT;

  // from: the user this chat request is originating from.
  SigninManagerBase* signin_manager =
      SigninManagerFactory::GetInstance()->GetForProfile(profile_);
  DCHECK(signin_manager);
  request.from = signin_manager->GetAuthenticatedAccountId();

  // to: list of users with whom to start this hangout is with.
  linked_ptr<User> target(new User());
  target->id = person_->owner_id;
  request.to.push_back(target);

  scoped_ptr<extensions::Event> event(
      new extensions::Event(OnHangoutRequested::kEventName,
                            OnHangoutRequested::Create(request)));

  // TODO(rkc): Change this once we remove the hangoutsPrivate API.
  // See crbug.com/306672
  extensions::ExtensionSystem::Get(
      profile_)->event_router()->DispatchEventToExtension(
          hangouts_extension_id_, event.Pass());

  content::RecordAction(base::UserMetricsAction("PeopleSearch_OpenChat"));
}

void PeopleResult::SendEmail() {
  chrome::NavigateParams params(profile_,
                                GURL(kEmailUrlPrefix + person_->email),
                                content::PAGE_TRANSITION_LINK);
  // If no window exists, this will open a new window this one tab.
  params.disposition = NEW_FOREGROUND_TAB;
  chrome::Navigate(&params);
  content::RecordAction(base::UserMetricsAction("PeopleSearch_SendEmail"));
}

void PeopleResult::RefreshHangoutsExtensionId() {
  // TODO(rkc): Change this once we remove the hangoutsPrivate API.
  // See crbug.com/306672
  for (size_t i = 0; i < arraysize(kHangoutsExtensionIds); ++i) {
    if (extensions::ExtensionSystem::Get(
        profile_)->event_router()->ExtensionHasEventListener(
            kHangoutsExtensionIds[i], OnHangoutRequested::kEventName)) {
      hangouts_extension_id_ = kHangoutsExtensionIds[i];
      return;
    }
  }
  hangouts_extension_id_.clear();
}

ChromeSearchResultType PeopleResult::GetType() {
  return SEARCH_PEOPLE_SEARCH_RESULT;
}

}  // namespace app_list