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/chromeos/login/default_user_images.h"
6
7#include "base/basictypes.h"
8#include "base/logging.h"
9#include "base/string_number_conversions.h"
10#include "base/string_util.h"
11#include "base/stringprintf.h"
12#include "grit/theme_resources.h"
13
14namespace chromeos {
15
16namespace {
17
18const char kDefaultPathPrefix[] = "default:";
19const char kDefaultUrlPrefix[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER_";
20const char kFirstDefaultUrl[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER";
21
22const char* kOldDefaultImageNames[] = {
23  "default:gray",
24  "default:green",
25  "default:blue",
26  "default:yellow",
27  "default:red",
28};
29
30// Returns a string consisting of the prefix specified and the index of the
31// image if its valid.
32std::string GetDefaultImageString(int index, const std::string& prefix) {
33  if (index < 0 || index >= kDefaultImagesCount) {
34    NOTREACHED();
35    return std::string();
36  }
37  return StringPrintf("%s%d", prefix.c_str(), index);
38}
39
40// Returns true if the string specified consists of the prefix and one of
41// the default images indices. Returns the index of the image in |image_id|
42// variable.
43bool IsDefaultImageString(const std::string& s,
44                          const std::string& prefix,
45                          int* image_id) {
46  DCHECK(image_id);
47  if (!StartsWithASCII(s, prefix, true))
48    return false;
49
50  int image_index = -1;
51  if (base::StringToInt(s.begin() + prefix.length(),
52                        s.end(),
53                        &image_index)) {
54    if (image_index < 0 || image_index >= kDefaultImagesCount)
55      return false;
56    *image_id = image_index;
57    return true;
58  }
59
60  return false;
61}
62}  // namespace
63
64std::string GetDefaultImagePath(int index) {
65  return GetDefaultImageString(index, kDefaultPathPrefix);
66}
67
68bool IsDefaultImagePath(const std::string& path, int* image_id) {
69  if (IsDefaultImageString(path, kDefaultPathPrefix, image_id))
70    return true;
71
72  // Check old default image names for back-compatibility.
73  for (size_t i = 0; i < arraysize(kOldDefaultImageNames); ++i) {
74    if (path == kOldDefaultImageNames[i]) {
75      *image_id = static_cast<int>(i);
76      return true;
77    }
78  }
79  return false;
80}
81
82std::string GetDefaultImageUrl(int index) {
83  if (index == 0)
84    return kFirstDefaultUrl;
85  return GetDefaultImageString(index, kDefaultUrlPrefix);
86}
87
88bool IsDefaultImageUrl(const std::string url, int* image_id) {
89  if (url == kFirstDefaultUrl) {
90    *image_id = 0;
91    return true;
92  }
93  return IsDefaultImageString(url, kDefaultUrlPrefix, image_id);
94}
95
96// Resource IDs of default user images.
97const int kDefaultImageResources[] = {
98  IDR_LOGIN_DEFAULT_USER,
99  IDR_LOGIN_DEFAULT_USER_1,
100  IDR_LOGIN_DEFAULT_USER_2,
101  IDR_LOGIN_DEFAULT_USER_3,
102  IDR_LOGIN_DEFAULT_USER_4,
103  IDR_LOGIN_DEFAULT_USER_5,
104  IDR_LOGIN_DEFAULT_USER_6,
105  IDR_LOGIN_DEFAULT_USER_7,
106  IDR_LOGIN_DEFAULT_USER_8,
107  IDR_LOGIN_DEFAULT_USER_9,
108  IDR_LOGIN_DEFAULT_USER_10,
109  IDR_LOGIN_DEFAULT_USER_11,
110  IDR_LOGIN_DEFAULT_USER_12,
111  IDR_LOGIN_DEFAULT_USER_13,
112  IDR_LOGIN_DEFAULT_USER_14,
113  IDR_LOGIN_DEFAULT_USER_15,
114  IDR_LOGIN_DEFAULT_USER_16,
115  IDR_LOGIN_DEFAULT_USER_17,
116  IDR_LOGIN_DEFAULT_USER_18,
117};
118
119const int kDefaultImagesCount = arraysize(kDefaultImageResources);
120
121}  // namespace chromeos
122
123