Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / themes / theme_properties.cc
blob406aa3658bdae8c8a74148208832d84db5d32991
1 // Copyright (c) 2013 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.
5 #include "chrome/browser/themes/theme_properties.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/themes/browser_theme_pack.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/resource/material_design/material_design_controller.h"
13 #include "ui/resources/grit/ui_resources.h"
15 namespace {
17 // ----------------------------------------------------------------------------
18 // Defaults for properties which are stored in the browser theme pack. If you
19 // change these defaults, you must increment the version number in
20 // browser_theme_pack.h
22 // Default colors.
23 #if defined(OS_CHROMEOS)
24 // Used for theme fallback colors.
25 const SkColor kDefaultColorFrame[] = {
26 SkColorSetRGB(109, 109, 109), SkColorSetRGB(204, 204, 204)};
27 const SkColor kDefaultColorFrameInactive[] = {
28 SkColorSetRGB(176, 176, 176), SkColorSetRGB(220, 220, 220)};
29 #elif defined(OS_MACOSX)
30 const SkColor kDefaultColorFrame = SkColorSetRGB(224, 224, 224);
31 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(246, 246, 246);
32 #else
33 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
34 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
35 #endif // OS_CHROMEOS
37 const SkColor kDefaultColorFrameIncognito[] = {
38 SkColorSetRGB(83, 106, 139), SkColorSetRGB(160, 160, 162)};
39 const SkColor kDefaultColorFrameIncognitoInactive[] = {
40 SkColorSetRGB(126, 139, 156), SkColorSetRGB(120, 120, 122)};
42 #if defined(OS_MACOSX)
43 const SkColor kDefaultColorToolbar = SkColorSetRGB(230, 230, 230);
44 #else
45 const SkColor kDefaultColorToolbar[] = {
46 SkColorSetRGB(223, 223, 223), SkColorSetRGB(242, 242, 242)};
47 #endif // OS_MACOSX
49 const SkColor kDefaultColorTabText = SK_ColorBLACK;
51 #if defined(OS_MACOSX)
52 const SkColor kDefaultColorBackgroundTabText = SK_ColorBLACK;
53 #else
54 const SkColor kDefaultColorBackgroundTabText[] = {
55 SkColorSetRGB(64, 64, 64), SK_ColorBLACK };
56 #endif // OS_MACOSX
58 const SkColor kDefaultColorBookmarkText = SK_ColorBLACK;
60 #if defined(OS_WIN)
61 const SkColor kDefaultColorNTPBackground =
62 color_utils::GetSysSkColor(COLOR_WINDOW);
63 const SkColor kDefaultColorNTPText =
64 color_utils::GetSysSkColor(COLOR_WINDOWTEXT);
65 const SkColor kDefaultColorNTPLink =
66 color_utils::GetSysSkColor(COLOR_HOTLIGHT);
67 #else
68 // TODO(beng): source from theme provider.
69 const SkColor kDefaultColorNTPBackground = SK_ColorWHITE;
70 const SkColor kDefaultColorNTPText = SK_ColorBLACK;
71 const SkColor kDefaultColorNTPLink = SkColorSetRGB(6, 55, 116);
72 #endif // OS_WIN
74 const SkColor kDefaultColorNTPHeader = SkColorSetRGB(150, 150, 150);
75 const SkColor kDefaultColorNTPSection = SkColorSetRGB(229, 229, 229);
76 const SkColor kDefaultColorNTPSectionText = SK_ColorBLACK;
77 const SkColor kDefaultColorNTPSectionLink = SkColorSetRGB(6, 55, 116);
78 const SkColor kDefaultColorButtonBackground = SkColorSetARGB(0, 0, 0, 0);
80 // Default tints.
81 const color_utils::HSL kDefaultTintButtons = { -1, -1, -1 };
82 const color_utils::HSL kDefaultTintFrame = { -1, -1, -1 };
83 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f };
84 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f };
85 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f };
86 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 };
88 // ----------------------------------------------------------------------------
89 // Defaults for properties which are not stored in the browser theme pack.
91 const SkColor kDefaultColorControlBackground = SK_ColorWHITE;
92 const SkColor kDefaultColorToolbarSeparator[] = {
93 SkColorSetRGB(170, 170, 171), SkColorSetRGB(182, 180, 182)};
95 #if defined(OS_MACOSX)
96 const SkColor kDefaultColorToolbarButtonStroke = SkColorSetARGB(75, 81, 81, 81);
97 const SkColor kDefaultColorToolbarButtonStrokeInactive =
98 SkColorSetARGB(75, 99, 99, 99);
99 const SkColor kDefaultColorToolbarBezel = SkColorSetRGB(204, 204, 204);
100 const SkColor kDefaultColorToolbarStroke = SkColorSetRGB(103, 103, 103);
101 const SkColor kDefaultColorToolbarStrokeInactive = SkColorSetRGB(163, 163, 163);
102 #endif // OS_MACOSX
104 // ----------------------------------------------------------------------------
106 // Strings used in alignment properties.
107 const char kAlignmentCenter[] = "center";
108 const char kAlignmentTop[] = "top";
109 const char kAlignmentBottom[] = "bottom";
110 const char kAlignmentLeft[] = "left";
111 const char kAlignmentRight[] = "right";
113 // Strings used in background tiling repetition properties.
114 const char kTilingNoRepeat[] = "no-repeat";
115 const char kTilingRepeatX[] = "repeat-x";
116 const char kTilingRepeatY[] = "repeat-y";
117 const char kTilingRepeat[] = "repeat";
119 // The image resources that will be tinted by the 'button' tint value.
120 // If you change this list, you must increment the version number in
121 // browser_theme_pack.cc, and you should assign persistent IDs to the
122 // data table at the start of said file or else tinted versions of
123 // these resources will not be created.
125 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
126 // should be removed once the cocoa port no longer uses them.
127 const int kToolbarButtonIDs[] = {
128 IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P,
129 IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P,
130 IDR_HOME, IDR_HOME_H, IDR_HOME_P,
131 IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P,
132 IDR_STOP, IDR_STOP_D, IDR_STOP_H, IDR_STOP_P,
133 IDR_BROWSER_ACTIONS_OVERFLOW, IDR_BROWSER_ACTIONS_OVERFLOW_H,
134 IDR_BROWSER_ACTIONS_OVERFLOW_P,
135 IDR_TOOLS, IDR_TOOLS_H, IDR_TOOLS_P,
136 IDR_MENU_DROPARROW,
137 IDR_TOOLBAR_BEZEL_HOVER, IDR_TOOLBAR_BEZEL_PRESSED, IDR_TOOLS_BAR,
140 SkColor TintForUnderline(SkColor input) {
141 return SkColorSetA(input, SkColorGetA(input) / 3);
144 } // namespace
146 // static
147 int ThemeProperties::StringToAlignment(const std::string& alignment) {
148 int alignment_mask = 0;
149 for (const std::string& component : base::SplitString(
150 alignment, base::kWhitespaceASCII,
151 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
152 if (base::LowerCaseEqualsASCII(component, kAlignmentTop))
153 alignment_mask |= ALIGN_TOP;
154 else if (base::LowerCaseEqualsASCII(component, kAlignmentBottom))
155 alignment_mask |= ALIGN_BOTTOM;
156 else if (base::LowerCaseEqualsASCII(component, kAlignmentLeft))
157 alignment_mask |= ALIGN_LEFT;
158 else if (base::LowerCaseEqualsASCII(component, kAlignmentRight))
159 alignment_mask |= ALIGN_RIGHT;
161 return alignment_mask;
164 // static
165 int ThemeProperties::StringToTiling(const std::string& tiling) {
166 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeatX))
167 return REPEAT_X;
168 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeatY))
169 return REPEAT_Y;
170 if (base::LowerCaseEqualsASCII(tiling, kTilingRepeat))
171 return REPEAT;
172 // NO_REPEAT is the default choice.
173 return NO_REPEAT;
176 // static
177 std::string ThemeProperties::AlignmentToString(int alignment) {
178 // Convert from an AlignmentProperty back into a string.
179 std::string vertical_string(kAlignmentCenter);
180 std::string horizontal_string(kAlignmentCenter);
182 if (alignment & ALIGN_TOP)
183 vertical_string = kAlignmentTop;
184 else if (alignment & ALIGN_BOTTOM)
185 vertical_string = kAlignmentBottom;
187 if (alignment & ALIGN_LEFT)
188 horizontal_string = kAlignmentLeft;
189 else if (alignment & ALIGN_RIGHT)
190 horizontal_string = kAlignmentRight;
192 return horizontal_string + " " + vertical_string;
195 // static
196 std::string ThemeProperties::TilingToString(int tiling) {
197 // Convert from a TilingProperty back into a string.
198 if (tiling == REPEAT_X)
199 return kTilingRepeatX;
200 if (tiling == REPEAT_Y)
201 return kTilingRepeatY;
202 if (tiling == REPEAT)
203 return kTilingRepeat;
204 return kTilingNoRepeat;
207 // static
208 const std::set<int>& ThemeProperties::GetTintableToolbarButtons() {
209 CR_DEFINE_STATIC_LOCAL(std::set<int>, button_set, ());
210 if (button_set.empty()) {
211 button_set = std::set<int>(
212 kToolbarButtonIDs,
213 kToolbarButtonIDs + arraysize(kToolbarButtonIDs));
216 return button_set;
219 // static
220 color_utils::HSL ThemeProperties::GetDefaultTint(int id) {
221 switch (id) {
222 case TINT_FRAME:
223 return kDefaultTintFrame;
224 case TINT_FRAME_INACTIVE:
225 return kDefaultTintFrameInactive;
226 case TINT_FRAME_INCOGNITO:
227 return kDefaultTintFrameIncognito;
228 case TINT_FRAME_INCOGNITO_INACTIVE:
229 return kDefaultTintFrameIncognitoInactive;
230 case TINT_BUTTONS:
231 return kDefaultTintButtons;
232 case TINT_BACKGROUND_TAB:
233 return kDefaultTintBackgroundTab;
234 default:
235 color_utils::HSL result = {-1, -1, -1};
236 return result;
240 // static
241 SkColor ThemeProperties::GetDefaultColor(int id) {
242 int mode = ui::MaterialDesignController::IsModeMaterial();
243 switch (id) {
244 // Properties stored in theme pack.
245 case COLOR_FRAME:
246 #if defined(OS_CHROMEOS)
247 return kDefaultColorFrame[mode];
248 #else
249 return kDefaultColorFrame;
250 #endif // OS_CHROMEOS
251 case COLOR_FRAME_INACTIVE:
252 #if defined(OS_CHROMEOS)
253 return kDefaultColorFrameInactive[mode];
254 #else
255 return kDefaultColorFrameInactive;
256 #endif // OS_CHROMEOS
257 case COLOR_FRAME_INCOGNITO:
258 return kDefaultColorFrameIncognito[mode];
259 case COLOR_FRAME_INCOGNITO_INACTIVE:
260 return kDefaultColorFrameIncognitoInactive[mode];
261 case COLOR_TOOLBAR:
262 #if defined(OS_MACOSX)
263 return kDefaultColorToolbar;
264 #else
265 return kDefaultColorToolbar[mode];
266 #endif // OS_MACOSX
267 case COLOR_TAB_TEXT:
268 return kDefaultColorTabText;
269 case COLOR_BACKGROUND_TAB_TEXT:
270 #if defined(OS_MACOSX)
271 return kDefaultColorBackgroundTabText;
272 #else
273 return kDefaultColorBackgroundTabText[mode];
274 #endif // OS_MACOSX
275 case COLOR_BOOKMARK_TEXT:
276 return kDefaultColorBookmarkText;
277 case COLOR_NTP_BACKGROUND:
278 return kDefaultColorNTPBackground;
279 case COLOR_NTP_TEXT:
280 return kDefaultColorNTPText;
281 case COLOR_NTP_LINK:
282 return kDefaultColorNTPLink;
283 case COLOR_NTP_LINK_UNDERLINE:
284 return TintForUnderline(kDefaultColorNTPLink);
285 case COLOR_NTP_HEADER:
286 return kDefaultColorNTPHeader;
287 case COLOR_NTP_SECTION:
288 return kDefaultColorNTPSection;
289 case COLOR_NTP_SECTION_TEXT:
290 return kDefaultColorNTPSectionText;
291 case COLOR_NTP_SECTION_LINK:
292 return kDefaultColorNTPSectionLink;
293 case COLOR_NTP_SECTION_LINK_UNDERLINE:
294 return TintForUnderline(kDefaultColorNTPSectionLink);
295 case COLOR_BUTTON_BACKGROUND:
296 return kDefaultColorButtonBackground;
298 // Properties not stored in theme pack.
299 case COLOR_CONTROL_BACKGROUND:
300 return kDefaultColorControlBackground;
301 case COLOR_TOOLBAR_SEPARATOR:
302 return kDefaultColorToolbarSeparator[mode];
303 #if defined(OS_MACOSX)
304 case COLOR_TOOLBAR_BUTTON_STROKE:
305 return kDefaultColorToolbarButtonStroke;
306 case COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE:
307 return kDefaultColorToolbarButtonStrokeInactive;
308 case COLOR_TOOLBAR_BEZEL:
309 return kDefaultColorToolbarBezel;
310 case COLOR_TOOLBAR_STROKE:
311 return kDefaultColorToolbarStroke;
312 case COLOR_TOOLBAR_STROKE_INACTIVE:
313 return kDefaultColorToolbarStrokeInactive;
314 #endif
315 default:
316 // Return a debugging red color.
317 return SK_ColorRED;