- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / autofill_dialog_types.cc
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
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
26 DialogNotification::DialogNotification() : type_(NONE) {}
27
28 DialogNotification::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     size_t start = pieces[0].size();
37     size_t end = start + pieces[1].size();
38     link_range_ = gfx::Range(start, end);
39     display_text_ = JoinString(pieces, string16());
40   }
41 }
42
43 DialogNotification::~DialogNotification() {}
44
45 SkColor DialogNotification::GetBackgroundColor() const {
46   switch (type_) {
47     case DialogNotification::EXPLANATORY_MESSAGE:
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     case DialogNotification::VALIDATION_ERROR:
56       return kWarningColor;
57     case DialogNotification::NONE:
58       return SK_ColorTRANSPARENT;
59   }
60
61   NOTREACHED();
62   return SK_ColorTRANSPARENT;
63 }
64
65 SkColor DialogNotification::GetBorderColor() const {
66   switch (type_) {
67     case DialogNotification::EXPLANATORY_MESSAGE:
68     case DialogNotification::WALLET_USAGE_CONFIRMATION:
69       return SkColorSetRGB(0xe5, 0xe5, 0xe5);
70     case DialogNotification::REQUIRED_ACTION:
71     case DialogNotification::WALLET_ERROR:
72     case DialogNotification::DEVELOPER_WARNING:
73     case DialogNotification::SECURITY_WARNING:
74     case DialogNotification::VALIDATION_ERROR:
75     case DialogNotification::NONE:
76       return GetBackgroundColor();
77   }
78
79   NOTREACHED();
80   return SK_ColorTRANSPARENT;
81 }
82
83 SkColor DialogNotification::GetTextColor() const {
84   switch (type_) {
85     case DialogNotification::REQUIRED_ACTION:
86     case DialogNotification::WALLET_ERROR:
87     case DialogNotification::EXPLANATORY_MESSAGE:
88     case DialogNotification::WALLET_USAGE_CONFIRMATION:
89       return SkColorSetRGB(102, 102, 102);
90     case DialogNotification::DEVELOPER_WARNING:
91     case DialogNotification::SECURITY_WARNING:
92     case DialogNotification::VALIDATION_ERROR:
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::EXPLANATORY_MESSAGE ||
104          type_ == DialogNotification::WALLET_ERROR ||
105          type_ == DialogNotification::WALLET_USAGE_CONFIRMATION;
106 }
107
108 bool DialogNotification::HasCheckbox() const {
109   return type_ == DialogNotification::WALLET_USAGE_CONFIRMATION;
110 }
111
112 SkColor const kWarningColor = SkColorSetRGB(0xde, 0x49, 0x32);
113
114 SuggestionState::SuggestionState()
115     : visible(false) {}
116 SuggestionState::SuggestionState(bool visible,
117                                  const string16& vertically_compact_text,
118                                  const string16& horizontally_compact_text,
119                                  const gfx::Image& icon,
120                                  const 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   messages_.erase(field);
147   messages_.insert(MessageMap::value_type(field, message));
148 }
149
150 const ValidityMessage& ValidityMessages::GetMessageOrDefault(
151     ServerFieldType field) const {
152   MessageMap::const_iterator iter = messages_.find(field);
153   return iter != messages_.end() ? iter->second : default_message_;
154 }
155
156 bool ValidityMessages::HasSureError(ServerFieldType field) const {
157   return IsSureError(GetMessageOrDefault(field));
158 }
159
160 bool ValidityMessages::HasErrors() const {
161   for (MessageMap::const_iterator iter = messages_.begin();
162        iter != messages_.end(); ++iter) {
163     if (!iter->second.text.empty())
164       return true;
165   }
166   return false;
167 }
168
169 bool ValidityMessages::HasSureErrors() const {
170  for (MessageMap::const_iterator iter = messages_.begin();
171       iter != messages_.end(); ++iter) {
172     if (IsSureError(iter->second))
173       return true;
174   }
175   return false;
176 }
177
178 }  // namespace autofill