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