Home | History | Annotate | Download | only in avatar
      1 // Copyright 2014 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/users/avatar/default_user_images.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/logging.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/string_piece.h"
     11 #include "base/strings/string_util.h"
     12 #include "base/strings/stringprintf.h"
     13 #include "base/sys_info.h"
     14 #include "grit/generated_resources.h"
     15 #include "grit/theme_resources.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 #include "ui/base/resource/resource_bundle.h"
     18 #include "ui/gfx/image/image_skia.h"
     19 
     20 namespace chromeos {
     21 
     22 namespace {
     23 
     24 const char kDefaultPathPrefix[] = "default:";
     25 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER_";
     26 const char kZeroDefaultUrl[] = "chrome://theme/IDR_LOGIN_DEFAULT_USER";
     27 
     28 const char* kOldDefaultImageNames[] = {
     29   "default:gray",
     30   "default:green",
     31   "default:blue",
     32   "default:yellow",
     33   "default:red",
     34 };
     35 
     36 // IDs of default user image descriptions.
     37 const int kDefaultImageDescriptions[] = {
     38   0,  // No description for deprecated user image 0.
     39   0,  // No description for deprecated user image 1.
     40   0,  // No description for deprecated user image 2.
     41   0,  // No description for deprecated user image 3.
     42   0,  // No description for deprecated user image 4.
     43   0,  // No description for deprecated user image 5.
     44   0,  // No description for deprecated user image 6.
     45   0,  // No description for deprecated user image 7.
     46   0,  // No description for deprecated user image 8.
     47   0,  // No description for deprecated user image 9.
     48   0,  // No description for deprecated user image 10.
     49   0,  // No description for deprecated user image 11.
     50   0,  // No description for deprecated user image 12.
     51   0,  // No description for deprecated user image 13.
     52   0,  // No description for deprecated user image 14.
     53   0,  // No description for deprecated user image 15.
     54   0,  // No description for deprecated user image 16.
     55   0,  // No description for deprecated user image 17.
     56   0,  // No description for deprecated user image 18.
     57   IDS_LOGIN_DEFAULT_USER_DESC_19,
     58   IDS_LOGIN_DEFAULT_USER_DESC_20,
     59   IDS_LOGIN_DEFAULT_USER_DESC_21,
     60   IDS_LOGIN_DEFAULT_USER_DESC_22,
     61   IDS_LOGIN_DEFAULT_USER_DESC_23,
     62   IDS_LOGIN_DEFAULT_USER_DESC_24,
     63   IDS_LOGIN_DEFAULT_USER_DESC_25,
     64   IDS_LOGIN_DEFAULT_USER_DESC_26,
     65   IDS_LOGIN_DEFAULT_USER_DESC_27,
     66   IDS_LOGIN_DEFAULT_USER_DESC_28,
     67   IDS_LOGIN_DEFAULT_USER_DESC_29,
     68   IDS_LOGIN_DEFAULT_USER_DESC_30,
     69   IDS_LOGIN_DEFAULT_USER_DESC_31,
     70   IDS_LOGIN_DEFAULT_USER_DESC_32,
     71 };
     72 
     73 // Returns a string consisting of the prefix specified and the index of the
     74 // image if its valid.
     75 std::string GetDefaultImageString(int index, const std::string& prefix) {
     76   if (index < 0 || index >= kDefaultImagesCount) {
     77     DCHECK(!base::SysInfo::IsRunningOnChromeOS());
     78     return std::string();
     79   }
     80   return base::StringPrintf("%s%d", prefix.c_str(), index);
     81 }
     82 
     83 // Returns true if the string specified consists of the prefix and one of
     84 // the default images indices. Returns the index of the image in |image_id|
     85 // variable.
     86 bool IsDefaultImageString(const std::string& s,
     87                           const std::string& prefix,
     88                           int* image_id) {
     89   DCHECK(image_id);
     90   if (!StartsWithASCII(s, prefix, true))
     91     return false;
     92 
     93   int image_index = -1;
     94   if (base::StringToInt(base::StringPiece(s.begin() + prefix.length(),
     95                                           s.end()),
     96                         &image_index)) {
     97     if (image_index < 0 || image_index >= kDefaultImagesCount)
     98       return false;
     99     *image_id = image_index;
    100     return true;
    101   }
    102 
    103   return false;
    104 }
    105 }  // namespace
    106 
    107 std::string GetDefaultImagePath(int index) {
    108   return GetDefaultImageString(index, kDefaultPathPrefix);
    109 }
    110 
    111 bool IsDefaultImagePath(const std::string& path, int* image_id) {
    112   if (IsDefaultImageString(path, kDefaultPathPrefix, image_id))
    113     return true;
    114 
    115   // Check old default image names for back-compatibility.
    116   for (size_t i = 0; i < arraysize(kOldDefaultImageNames); ++i) {
    117     if (path == kOldDefaultImageNames[i]) {
    118       *image_id = static_cast<int>(i);
    119       return true;
    120     }
    121   }
    122   return false;
    123 }
    124 
    125 std::string GetDefaultImageUrl(int index) {
    126   if (index == 0)
    127     return kZeroDefaultUrl;
    128   return GetDefaultImageString(index, kDefaultUrlPrefix);
    129 }
    130 
    131 bool IsDefaultImageUrl(const std::string& url, int* image_id) {
    132   if (url == kZeroDefaultUrl) {
    133     *image_id = 0;
    134     return true;
    135   }
    136   return IsDefaultImageString(url, kDefaultUrlPrefix, image_id);
    137 }
    138 
    139 const gfx::ImageSkia& GetDefaultImage(int index) {
    140   DCHECK(index >= 0 && index < kDefaultImagesCount);
    141   return *ResourceBundle::GetSharedInstance().
    142       GetImageSkiaNamed(kDefaultImageResourceIDs[index]);
    143 }
    144 
    145 base::string16 GetDefaultImageDescription(int index) {
    146   DCHECK(index >= 0 && index < kDefaultImagesCount);
    147   int string_id = kDefaultImageDescriptions[index];
    148   if (string_id)
    149     return l10n_util::GetStringUTF16(string_id);
    150   else
    151     return base::string16();
    152 }
    153 
    154 // Resource IDs of default user images.
    155 const int kDefaultImageResourceIDs[] = {
    156   IDR_LOGIN_DEFAULT_USER,
    157   IDR_LOGIN_DEFAULT_USER_1,
    158   IDR_LOGIN_DEFAULT_USER_2,
    159   IDR_LOGIN_DEFAULT_USER_3,
    160   IDR_LOGIN_DEFAULT_USER_4,
    161   IDR_LOGIN_DEFAULT_USER_5,
    162   IDR_LOGIN_DEFAULT_USER_6,
    163   IDR_LOGIN_DEFAULT_USER_7,
    164   IDR_LOGIN_DEFAULT_USER_8,
    165   IDR_LOGIN_DEFAULT_USER_9,
    166   IDR_LOGIN_DEFAULT_USER_10,
    167   IDR_LOGIN_DEFAULT_USER_11,
    168   IDR_LOGIN_DEFAULT_USER_12,
    169   IDR_LOGIN_DEFAULT_USER_13,
    170   IDR_LOGIN_DEFAULT_USER_14,
    171   IDR_LOGIN_DEFAULT_USER_15,
    172   IDR_LOGIN_DEFAULT_USER_16,
    173   IDR_LOGIN_DEFAULT_USER_17,
    174   IDR_LOGIN_DEFAULT_USER_18,
    175   IDR_LOGIN_DEFAULT_USER_19,
    176   IDR_LOGIN_DEFAULT_USER_20,
    177   IDR_LOGIN_DEFAULT_USER_21,
    178   IDR_LOGIN_DEFAULT_USER_22,
    179   IDR_LOGIN_DEFAULT_USER_23,
    180   IDR_LOGIN_DEFAULT_USER_24,
    181   IDR_LOGIN_DEFAULT_USER_25,
    182   IDR_LOGIN_DEFAULT_USER_26,
    183   IDR_LOGIN_DEFAULT_USER_27,
    184   IDR_LOGIN_DEFAULT_USER_28,
    185   IDR_LOGIN_DEFAULT_USER_29,
    186   IDR_LOGIN_DEFAULT_USER_30,
    187   IDR_LOGIN_DEFAULT_USER_31,
    188   IDR_LOGIN_DEFAULT_USER_32,
    189 };
    190 
    191 const int kDefaultImageAuthorIDs[] = {
    192   IDS_LOGIN_DEFAULT_USER_AUTHOR,
    193   IDS_LOGIN_DEFAULT_USER_AUTHOR_1,
    194   IDS_LOGIN_DEFAULT_USER_AUTHOR_2,
    195   IDS_LOGIN_DEFAULT_USER_AUTHOR_3,
    196   IDS_LOGIN_DEFAULT_USER_AUTHOR_4,
    197   IDS_LOGIN_DEFAULT_USER_AUTHOR_5,
    198   IDS_LOGIN_DEFAULT_USER_AUTHOR_6,
    199   IDS_LOGIN_DEFAULT_USER_AUTHOR_7,
    200   IDS_LOGIN_DEFAULT_USER_AUTHOR_8,
    201   IDS_LOGIN_DEFAULT_USER_AUTHOR_9,
    202   IDS_LOGIN_DEFAULT_USER_AUTHOR_10,
    203   IDS_LOGIN_DEFAULT_USER_AUTHOR_11,
    204   IDS_LOGIN_DEFAULT_USER_AUTHOR_12,
    205   IDS_LOGIN_DEFAULT_USER_AUTHOR_13,
    206   IDS_LOGIN_DEFAULT_USER_AUTHOR_14,
    207   IDS_LOGIN_DEFAULT_USER_AUTHOR_15,
    208   IDS_LOGIN_DEFAULT_USER_AUTHOR_16,
    209   IDS_LOGIN_DEFAULT_USER_AUTHOR_17,
    210   IDS_LOGIN_DEFAULT_USER_AUTHOR_18,
    211   IDS_LOGIN_DEFAULT_USER_AUTHOR_19,
    212   IDS_LOGIN_DEFAULT_USER_AUTHOR_20,
    213   IDS_LOGIN_DEFAULT_USER_AUTHOR_21,
    214   IDS_LOGIN_DEFAULT_USER_AUTHOR_22,
    215   IDS_LOGIN_DEFAULT_USER_AUTHOR_23,
    216   IDS_LOGIN_DEFAULT_USER_AUTHOR_24,
    217   IDS_LOGIN_DEFAULT_USER_AUTHOR_25,
    218   IDS_LOGIN_DEFAULT_USER_AUTHOR_26,
    219   IDS_LOGIN_DEFAULT_USER_AUTHOR_27,
    220   IDS_LOGIN_DEFAULT_USER_AUTHOR_28,
    221   IDS_LOGIN_DEFAULT_USER_AUTHOR_29,
    222   IDS_LOGIN_DEFAULT_USER_AUTHOR_30,
    223   IDS_LOGIN_DEFAULT_USER_AUTHOR_31,
    224   IDS_LOGIN_DEFAULT_USER_AUTHOR_32,
    225 };
    226 
    227 const int kDefaultImageWebsiteIDs[] = {
    228   IDS_LOGIN_DEFAULT_USER_WEBSITE,
    229   IDS_LOGIN_DEFAULT_USER_WEBSITE_1,
    230   IDS_LOGIN_DEFAULT_USER_WEBSITE_2,
    231   IDS_LOGIN_DEFAULT_USER_WEBSITE_3,
    232   IDS_LOGIN_DEFAULT_USER_WEBSITE_4,
    233   IDS_LOGIN_DEFAULT_USER_WEBSITE_5,
    234   IDS_LOGIN_DEFAULT_USER_WEBSITE_6,
    235   IDS_LOGIN_DEFAULT_USER_WEBSITE_7,
    236   IDS_LOGIN_DEFAULT_USER_WEBSITE_8,
    237   IDS_LOGIN_DEFAULT_USER_WEBSITE_9,
    238   IDS_LOGIN_DEFAULT_USER_WEBSITE_10,
    239   IDS_LOGIN_DEFAULT_USER_WEBSITE_11,
    240   IDS_LOGIN_DEFAULT_USER_WEBSITE_12,
    241   IDS_LOGIN_DEFAULT_USER_WEBSITE_13,
    242   IDS_LOGIN_DEFAULT_USER_WEBSITE_14,
    243   IDS_LOGIN_DEFAULT_USER_WEBSITE_15,
    244   IDS_LOGIN_DEFAULT_USER_WEBSITE_16,
    245   IDS_LOGIN_DEFAULT_USER_WEBSITE_17,
    246   IDS_LOGIN_DEFAULT_USER_WEBSITE_18,
    247   IDS_LOGIN_DEFAULT_USER_WEBSITE_19,
    248   IDS_LOGIN_DEFAULT_USER_WEBSITE_20,
    249   IDS_LOGIN_DEFAULT_USER_WEBSITE_21,
    250   IDS_LOGIN_DEFAULT_USER_WEBSITE_22,
    251   IDS_LOGIN_DEFAULT_USER_WEBSITE_23,
    252   IDS_LOGIN_DEFAULT_USER_WEBSITE_24,
    253   IDS_LOGIN_DEFAULT_USER_WEBSITE_25,
    254   IDS_LOGIN_DEFAULT_USER_WEBSITE_26,
    255   IDS_LOGIN_DEFAULT_USER_WEBSITE_27,
    256   IDS_LOGIN_DEFAULT_USER_WEBSITE_28,
    257   IDS_LOGIN_DEFAULT_USER_WEBSITE_29,
    258   IDS_LOGIN_DEFAULT_USER_WEBSITE_30,
    259   IDS_LOGIN_DEFAULT_USER_WEBSITE_31,
    260   IDS_LOGIN_DEFAULT_USER_WEBSITE_32,
    261 };
    262 
    263 const int kDefaultImagesCount = arraysize(kDefaultImageResourceIDs);
    264 
    265 const int kFirstDefaultImageIndex = 19;
    266 
    267 // The order and the values of these constants are important for histograms
    268 // of different Chrome OS versions to be merged smoothly.
    269 const int kHistogramImageFromCamera = 19;
    270 const int kHistogramImageFromFile = 20;
    271 const int kHistogramImageOld = 21;
    272 const int kHistogramImageFromProfile = 22;
    273 const int kHistogramVideoFromCamera = 23;
    274 const int kHistogramVideoFromFile = 24;
    275 const int kHistogramImagesCount = kDefaultImagesCount + 6;
    276 
    277 int GetDefaultImageHistogramValue(int index) {
    278   DCHECK(index >= 0 && index < kDefaultImagesCount);
    279   // Create a gap in histogram values for
    280   // [kHistogramImageFromCamera..kHistogramImageFromProfile] block to fit.
    281   if (index < kHistogramImageFromCamera)
    282     return index;
    283   return index + 6;
    284 }
    285 
    286 }  // namespace chromeos
    287