summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/default_user_images.cc
blob: e015c5aadd88364c07511659043fa755187c4335 (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
// 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/login/default_user_images.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"

namespace chromeos {

namespace {

const char kDefaultPathPrefix[] = "default:";
const char kDefaultUrlPrefix[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER_";
const char kFirstDefaultUrl[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER";

const char* kOldDefaultImageNames[] = {
  "default:gray",
  "default:green",
  "default:blue",
  "default:yellow",
  "default:red",
};

// Returns a string consisting of the prefix specified and the index of the
// image if its valid.
std::string GetDefaultImageString(int index, const std::string& prefix) {
  if (index < 0 || index >= kDefaultImagesCount) {
    NOTREACHED();
    return std::string();
  }
  return StringPrintf("%s%d", prefix.c_str(), index);
}

// Returns true if the string specified consists of the prefix and one of
// the default images indices. Returns the index of the image in |image_id|
// variable.
bool IsDefaultImageString(const std::string& s,
                          const std::string& prefix,
                          int* image_id) {
  DCHECK(image_id);
  if (!StartsWithASCII(s, prefix, true))
    return false;

  int image_index = -1;
  if (base::StringToInt(base::StringPiece(s.begin() + prefix.length(),
                                          s.end()),
                        &image_index)) {
    if (image_index < 0 || image_index >= kDefaultImagesCount)
      return false;
    *image_id = image_index;
    return true;
  }

  return false;
}
}  // namespace

std::string GetDefaultImagePath(int index) {
  return GetDefaultImageString(index, kDefaultPathPrefix);
}

bool IsDefaultImagePath(const std::string& path, int* image_id) {
  if (IsDefaultImageString(path, kDefaultPathPrefix, image_id))
    return true;

  // Check old default image names for back-compatibility.
  for (size_t i = 0; i < arraysize(kOldDefaultImageNames); ++i) {
    if (path == kOldDefaultImageNames[i]) {
      *image_id = static_cast<int>(i);
      return true;
    }
  }
  return false;
}

std::string GetDefaultImageUrl(int index) {
  if (index == 0)
    return kFirstDefaultUrl;
  return GetDefaultImageString(index, kDefaultUrlPrefix);
}

bool IsDefaultImageUrl(const std::string url, int* image_id) {
  if (url == kFirstDefaultUrl) {
    *image_id = 0;
    return true;
  }
  return IsDefaultImageString(url, kDefaultUrlPrefix, image_id);
}

const gfx::ImageSkia& GetDefaultImage(int index) {
  DCHECK(index >= 0 && index < kDefaultImagesCount);
  return *ResourceBundle::GetSharedInstance().
      GetImageSkiaNamed(kDefaultImageResources[index]);
}

// Resource IDs of default user images.
const int kDefaultImageResources[] = {
  IDR_LOGIN_DEFAULT_USER,
  IDR_LOGIN_DEFAULT_USER_1,
  IDR_LOGIN_DEFAULT_USER_2,
  IDR_LOGIN_DEFAULT_USER_3,
  IDR_LOGIN_DEFAULT_USER_4,
  IDR_LOGIN_DEFAULT_USER_5,
  IDR_LOGIN_DEFAULT_USER_6,
  IDR_LOGIN_DEFAULT_USER_7,
  IDR_LOGIN_DEFAULT_USER_8,
  IDR_LOGIN_DEFAULT_USER_9,
  IDR_LOGIN_DEFAULT_USER_10,
  IDR_LOGIN_DEFAULT_USER_11,
  IDR_LOGIN_DEFAULT_USER_12,
  IDR_LOGIN_DEFAULT_USER_13,
  IDR_LOGIN_DEFAULT_USER_14,
  IDR_LOGIN_DEFAULT_USER_15,
  IDR_LOGIN_DEFAULT_USER_16,
  IDR_LOGIN_DEFAULT_USER_17,
  IDR_LOGIN_DEFAULT_USER_18,
};

const int kDefaultImagesCount = arraysize(kDefaultImageResources);

// The order and the values of these constants are important for histograms
// of different Chrome OS versions to be merged smoothly.
const int kHistogramImageFromCamera = kDefaultImagesCount;
const int kHistogramImageFromFile = kDefaultImagesCount + 1;
const int kHistogramImageOld = kDefaultImagesCount + 2;
const int kHistogramImageFromProfile = kDefaultImagesCount + 3;
const int kHistogramImagesCount = kDefaultImagesCount + 4;

}  // namespace chromeos