people_result.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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/profiles/profile.h"
13#include "chrome/browser/ui/app_list/search/common/url_icon_source.h"
14#include "grit/theme_resources.h"
15#include "ui/base/l10n/l10n_util.h"
16
17namespace {
18
19const int kIconSize = 32;
20const char kImageSizePath[] = "s32-p/";
21
22// Add a query parameter to specify the size to fetch the image in. The
23// original profile image can be of an arbitrary size, we ask the server to
24// crop it to a square 32x32 using its smart cropping algorithm.
25GURL GetImageUrl(const GURL& url) {
26  std::string image_filename = url.ExtractFileName();
27  if (image_filename.empty())
28    return url;
29
30  return url.Resolve(kImageSizePath + image_filename);
31}
32
33}  // namespace
34
35namespace app_list {
36
37PeopleResult::PeopleResult(Profile* profile,
38                           const std::string& id,
39                           const std::string& display_name,
40                           double interaction_rank,
41                           const GURL& image_url)
42    : profile_(profile),
43      id_(id),
44      display_name_(display_name),
45      interaction_rank_(interaction_rank),
46      image_url_(image_url),
47      weak_factory_(this) {
48  set_id(id_);
49  set_title(UTF8ToUTF16(display_name_));
50  set_relevance(interaction_rank_);
51
52  image_ = gfx::ImageSkia(
53      new UrlIconSource(base::Bind(&PeopleResult::OnIconLoaded,
54                                   weak_factory_.GetWeakPtr()),
55                        profile_->GetRequestContext(),
56                        GetImageUrl(image_url_),
57                        kIconSize,
58                        IDR_PROFILE_PICTURE_LOADING),
59      gfx::Size(kIconSize, kIconSize));
60  SetIcon(image_);
61}
62
63PeopleResult::~PeopleResult() {
64}
65
66void PeopleResult::Open(int event_flags) {
67  // TODO(rkc): Navigate to the person's profile?
68}
69
70void PeopleResult::InvokeAction(int action_index, int event_flags) {
71  DCHECK_EQ(0, action_index);
72  // TODO(rkc): Decide what to do here.
73}
74
75scoped_ptr<ChromeSearchResult> PeopleResult::Duplicate() {
76  return scoped_ptr<ChromeSearchResult>(new PeopleResult(profile_, id_,
77                                                         display_name_,
78                                                         interaction_rank_,
79                                                         image_url_)).Pass();
80}
81
82void PeopleResult::OnIconLoaded() {
83  // Remove the existing image reps since the icon data is loaded and they
84  // need to be re-created.
85  const std::vector<gfx::ImageSkiaRep>& image_reps = image_.image_reps();
86  for (size_t i = 0; i < image_reps.size(); ++i)
87    image_.RemoveRepresentation(image_reps[i].scale_factor());
88
89  SetIcon(image_);
90}
91
92ChromeSearchResultType PeopleResult::GetType() {
93  return SEARCH_PEOPLE_SEARCH_RESULT;
94}
95
96}  // namespace app_list
97