Home | History | Annotate | Download | only in autofill
      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/ui/autofill/autofill_dialog_types.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string_split.h"
      9 #include "base/strings/string_util.h"
     10 
     11 namespace {
     12 
     13 bool IsSureError(const autofill::ValidityMessage& message) {
     14   return message.sure && !message.text.empty();
     15 }
     16 
     17 }  // namespace
     18 
     19 namespace autofill {
     20 
     21 static const base::char16 kRangeSeparator = '|';
     22 
     23 // Street address is multi-line, except in countries where it shares a line
     24 // with other inputs (such as Coite d'Ivoire).
     25 bool DetailInput::IsMultiline() const {
     26   return (type == ADDRESS_HOME_STREET_ADDRESS ||
     27           type == ADDRESS_BILLING_STREET_ADDRESS) &&
     28       length == DetailInput::LONG;
     29 }
     30 
     31 DialogNotification::DialogNotification() : type_(NONE) {}
     32 
     33 DialogNotification::DialogNotification(Type type,
     34                                        const base::string16& display_text)
     35     : type_(type),
     36       display_text_(display_text),
     37       checked_(false) {
     38   // If there's a range separated by bars, mark that as the anchor text.
     39   std::vector<base::string16> pieces;
     40   base::SplitStringDontTrim(display_text, kRangeSeparator, &pieces);
     41   if (pieces.size() > 1) {
     42     size_t start = pieces[0].size();
     43     size_t end = start + pieces[1].size();
     44     link_range_ = gfx::Range(start, end);
     45     display_text_ = JoinString(pieces, base::string16());
     46   }
     47 }
     48 
     49 DialogNotification::~DialogNotification() {}
     50 
     51 SkColor DialogNotification::GetBackgroundColor() const {
     52   switch (type_) {
     53     case DialogNotification::WALLET_USAGE_CONFIRMATION:
     54       return SkColorSetRGB(0xf5, 0xf5, 0xf5);
     55     case DialogNotification::REQUIRED_ACTION:
     56     case DialogNotification::WALLET_ERROR:
     57       return SkColorSetRGB(0xfc, 0xf3, 0xbf);
     58     case DialogNotification::DEVELOPER_WARNING:
     59     case DialogNotification::SECURITY_WARNING:
     60       return kWarningColor;
     61     case DialogNotification::NONE:
     62       return SK_ColorTRANSPARENT;
     63   }
     64 
     65   NOTREACHED();
     66   return SK_ColorTRANSPARENT;
     67 }
     68 
     69 SkColor DialogNotification::GetBorderColor() const {
     70   switch (type_) {
     71     case DialogNotification::WALLET_USAGE_CONFIRMATION:
     72       return SkColorSetRGB(0xe5, 0xe5, 0xe5);
     73     case DialogNotification::REQUIRED_ACTION:
     74     case DialogNotification::WALLET_ERROR:
     75     case DialogNotification::DEVELOPER_WARNING:
     76     case DialogNotification::SECURITY_WARNING:
     77     case DialogNotification::NONE:
     78       return GetBackgroundColor();
     79   }
     80 
     81   NOTREACHED();
     82   return SK_ColorTRANSPARENT;
     83 }
     84 
     85 SkColor DialogNotification::GetTextColor() const {
     86   switch (type_) {
     87     case DialogNotification::REQUIRED_ACTION:
     88     case DialogNotification::WALLET_ERROR:
     89     case DialogNotification::WALLET_USAGE_CONFIRMATION:
     90       return SkColorSetRGB(102, 102, 102);
     91     case DialogNotification::DEVELOPER_WARNING:
     92     case DialogNotification::SECURITY_WARNING:
     93       return SK_ColorWHITE;
     94     case DialogNotification::NONE:
     95       return SK_ColorTRANSPARENT;
     96   }
     97 
     98   NOTREACHED();
     99   return SK_ColorTRANSPARENT;
    100 }
    101 
    102 bool DialogNotification::HasArrow() const {
    103   return type_ == DialogNotification::WALLET_ERROR ||
    104          type_ == DialogNotification::WALLET_USAGE_CONFIRMATION;
    105 }
    106 
    107 bool DialogNotification::HasCheckbox() const {
    108   return type_ == DialogNotification::WALLET_USAGE_CONFIRMATION;
    109 }
    110 
    111 SkColor const kWarningColor = SkColorSetRGB(0xde, 0x49, 0x32);
    112 
    113 SuggestionState::SuggestionState()
    114     : visible(false) {}
    115 SuggestionState::SuggestionState(
    116     bool visible,
    117     const base::string16& vertically_compact_text,
    118     const base::string16& horizontally_compact_text,
    119     const gfx::Image& icon,
    120     const base::string16& extra_text,
    121     const gfx::Image& extra_icon)
    122     : visible(visible),
    123       vertically_compact_text(vertically_compact_text),
    124       horizontally_compact_text(horizontally_compact_text),
    125       icon(icon),
    126       extra_text(extra_text),
    127       extra_icon(extra_icon) {}
    128 SuggestionState::~SuggestionState() {}
    129 
    130 DialogOverlayString::DialogOverlayString() {}
    131 DialogOverlayString::~DialogOverlayString() {}
    132 
    133 DialogOverlayState::DialogOverlayState() {}
    134 DialogOverlayState::~DialogOverlayState() {}
    135 
    136 ValidityMessage::ValidityMessage(const base::string16& text, bool sure)
    137     : text(text), sure(sure) {}
    138 ValidityMessage::~ValidityMessage() {}
    139 
    140 ValidityMessages::ValidityMessages()
    141     : default_message_(ValidityMessage(base::string16(), false)) {}
    142 ValidityMessages::~ValidityMessages() {}
    143 
    144 void ValidityMessages::Set(
    145     ServerFieldType field, const ValidityMessage& message) {
    146   MessageMap::iterator iter = messages_.find(field);
    147   if (iter != messages_.end()) {
    148     if (!iter->second.text.empty())
    149       return;
    150 
    151     messages_.erase(iter);
    152   }
    153 
    154   messages_.insert(MessageMap::value_type(field, message));
    155 }
    156 
    157 const ValidityMessage& ValidityMessages::GetMessageOrDefault(
    158     ServerFieldType field) const {
    159   MessageMap::const_iterator iter = messages_.find(field);
    160   return iter != messages_.end() ? iter->second : default_message_;
    161 }
    162 
    163 bool ValidityMessages::HasSureError(ServerFieldType field) const {
    164   return IsSureError(GetMessageOrDefault(field));
    165 }
    166 
    167 bool ValidityMessages::HasErrors() const {
    168   for (MessageMap::const_iterator iter = messages_.begin();
    169        iter != messages_.end(); ++iter) {
    170     if (!iter->second.text.empty())
    171       return true;
    172   }
    173   return false;
    174 }
    175 
    176 bool ValidityMessages::HasSureErrors() const {
    177  for (MessageMap::const_iterator iter = messages_.begin();
    178       iter != messages_.end(); ++iter) {
    179     if (IsSureError(iter->second))
    180       return true;
    181   }
    182   return false;
    183 }
    184 
    185 }  // namespace autofill
    186