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