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