summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/webui/user_image_source.cc
blob: 3a4b0aa024401397baf952c2cd1a18afd2453629 (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
// Copyright (c) 2011 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/chromeos/webui/user_image_source.h"

#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/common/url_constants.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/codec/png_codec.h"

namespace chromeos {

std::vector<unsigned char> UserImageSource::GetUserImage(
    const std::string& email) const {
  std::vector<unsigned char> user_image;
  chromeos::UserVector users = chromeos::UserManager::Get()->GetUsers();
  for (size_t i = 0; i < users.size(); ++i) {
    if (users[i].email() == email) {
      gfx::PNGCodec::EncodeBGRASkBitmap(users[i].image(), true, &user_image);
      return user_image;
    }
  }
  gfx::PNGCodec::EncodeBGRASkBitmap(
      *ResourceBundle::GetSharedInstance().GetBitmapNamed(
          IDR_LOGIN_DEFAULT_USER),
      true,
      &user_image);
  return user_image;
}

UserImageSource::UserImageSource()
    : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) {
}

UserImageSource::~UserImageSource() {}

void UserImageSource::StartDataRequest(const std::string& path,
                                       bool is_incognito,
                                       int request_id) {
  // Strip the query param value - we only use it as a hack to ensure our
  // image gets reloaded instead of being pulled from the browser cache
  std::string email = path.substr(0, path.find_first_of("?"));
  SendResponse(request_id, new RefCountedBytes(GetUserImage(email)));
}

std::string UserImageSource::GetMimeType(const std::string&) const {
  // We need to explicitly return a mime type, otherwise if the user tries to
  // drag the image they get no extension.
  return "image/png";
}

}  // namespace chromeos