1// Copyright (c) 2011 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/webui/options/chromeos/user_image_source.h"
6
7#include "base/memory/ref_counted_memory.h"
8#include "chrome/browser/chromeos/login/user_manager.h"
9#include "chrome/common/url_constants.h"
10#include "grit/theme_resources.h"
11#include "ui/base/resource/resource_bundle.h"
12#include "ui/gfx/codec/png_codec.h"
13
14namespace chromeos {
15
16std::vector<unsigned char> UserImageSource::GetUserImage(
17    const std::string& email) const {
18  std::vector<unsigned char> user_image;
19  chromeos::UserVector users = chromeos::UserManager::Get()->GetUsers();
20  for (size_t i = 0; i < users.size(); ++i) {
21    if (users[i].email() == email) {
22      gfx::PNGCodec::EncodeBGRASkBitmap(users[i].image(), false, &user_image);
23      return user_image;
24    }
25  }
26  gfx::PNGCodec::EncodeBGRASkBitmap(
27      *ResourceBundle::GetSharedInstance().GetBitmapNamed(
28          IDR_LOGIN_DEFAULT_USER),
29      false,
30      &user_image);
31  return user_image;
32}
33
34UserImageSource::UserImageSource()
35    : DataSource(chrome::kChromeUIUserImageHost, MessageLoop::current()) {
36}
37
38UserImageSource::~UserImageSource() {}
39
40void UserImageSource::StartDataRequest(const std::string& path,
41                                       bool is_incognito,
42                                       int request_id) {
43  // Strip the query param value - we only use it as a hack to ensure our
44  // image gets reloaded instead of being pulled from the browser cache
45  std::string email = path.substr(0, path.find_first_of("?"));
46  SendResponse(request_id, new RefCountedBytes(GetUserImage(email)));
47}
48
49std::string UserImageSource::GetMimeType(const std::string&) const {
50  // We need to explicitly return a mime type, otherwise if the user tries to
51  // drag the image they get no extension.
52  return "image/png";
53}
54
55}  // namespace chromeos
56