Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / webui / web_ui_util.cc
blob98f0ca9887e88abfbd98d526faa1e2b7113c7f8f
1 // Copyright 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 "ui/base/webui/web_ui_util.h"
7 #include <vector>
9 #include "base/base64.h"
10 #include "base/i18n/rtl.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/trace_event/trace_event.h"
16 #include "net/base/escape.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/window_open_disposition.h"
20 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/font.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/resources/grit/webui_resources.h"
24 #include "ui/strings/grit/app_locale_settings.h"
25 #include "url/gurl.h"
27 #if defined(OS_WIN)
28 #include "base/win/windows_version.h"
29 #endif
31 #if defined(OS_CHROMEOS)
32 #include "ui/base/font_helper_chromeos.h"
33 #endif
35 namespace webui {
37 std::string GetBitmapDataUrl(const SkBitmap& bitmap) {
38 TRACE_EVENT2("oobe", "GetImageDataUrl",
39 "width", bitmap.width(), "height", bitmap.height());
40 std::vector<unsigned char> output;
41 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output);
42 std::string str_url;
43 str_url.insert(str_url.end(), output.begin(), output.end());
45 base::Base64Encode(str_url, &str_url);
46 str_url.insert(0, "data:image/png;base64,");
47 return str_url;
50 std::string GetBitmapDataUrlFromResource(int res) {
51 // Load resource icon and covert to base64 encoded data url
52 base::RefCountedStaticMemory* icon_data =
53 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
54 res, ui::SCALE_FACTOR_100P);
55 if (!icon_data)
56 return std::string();
57 scoped_refptr<base::RefCountedMemory> raw_icon(icon_data);
58 std::string str_url;
59 str_url.insert(str_url.end(),
60 raw_icon->front(),
61 raw_icon->front() + raw_icon->size());
62 base::Base64Encode(str_url, &str_url);
63 str_url.insert(0, "data:image/png;base64,");
64 return str_url;
67 WindowOpenDisposition GetDispositionFromClick(const base::ListValue* args,
68 int start_index) {
69 double button = 0.0;
70 bool alt_key = false;
71 bool ctrl_key = false;
72 bool meta_key = false;
73 bool shift_key = false;
75 CHECK(args->GetDouble(start_index++, &button));
76 CHECK(args->GetBoolean(start_index++, &alt_key));
77 CHECK(args->GetBoolean(start_index++, &ctrl_key));
78 CHECK(args->GetBoolean(start_index++, &meta_key));
79 CHECK(args->GetBoolean(start_index++, &shift_key));
80 return ui::DispositionFromClick(
81 button == 1.0, alt_key, ctrl_key, meta_key, shift_key);
84 bool ParseScaleFactor(const base::StringPiece& identifier,
85 float* scale_factor) {
86 *scale_factor = 1.0f;
87 if (identifier.empty()) {
88 LOG(WARNING) << "Invalid scale factor format: " << identifier;
89 return false;
92 if (*identifier.rbegin() != 'x') {
93 LOG(WARNING) << "Invalid scale factor format: " << identifier;
94 return false;
97 double scale = 0;
98 std::string stripped;
99 identifier.substr(0, identifier.length() - 1).CopyToString(&stripped);
100 if (!base::StringToDouble(stripped, &scale)) {
101 LOG(WARNING) << "Invalid scale factor format: " << identifier;
102 return false;
104 *scale_factor = static_cast<float>(scale);
105 return true;
108 void ParsePathAndScale(const GURL& url,
109 std::string* path,
110 float* scale_factor) {
111 *path = net::UnescapeURLComponent(url.path().substr(1),
112 (net::UnescapeRule::URL_SPECIAL_CHARS |
113 net::UnescapeRule::SPACES));
114 if (scale_factor)
115 *scale_factor = 1.0f;
117 // Detect and parse resource string ending in @<scale>x.
118 std::size_t pos = path->rfind('@');
119 if (pos != std::string::npos) {
120 base::StringPiece stripped_path(*path);
121 float factor;
123 if (ParseScaleFactor(stripped_path.substr(
124 pos + 1, stripped_path.length() - pos - 1), &factor)) {
125 // Strip scale factor specification from path.
126 stripped_path.remove_suffix(stripped_path.length() - pos);
127 stripped_path.CopyToString(path);
129 if (scale_factor)
130 *scale_factor = factor;
134 void SetLoadTimeDataDefaults(const std::string& app_locale,
135 base::DictionaryValue* localized_strings) {
136 localized_strings->SetString("fontfamily", GetFontFamily());
137 localized_strings->SetString("fontsize", GetFontSize());
138 localized_strings->SetString("language", l10n_util::GetLanguage(app_locale));
139 localized_strings->SetString("textdirection", GetTextDirection());
142 std::string GetWebUiCssTextDefaults() {
143 std::vector<std::string> placeholders;
144 placeholders.push_back(GetTextDirection()); // $1
145 placeholders.push_back(GetFontFamily()); // $2
146 placeholders.push_back(GetFontSize()); // $3
148 const ui::ResourceBundle& resource_bundle =
149 ui::ResourceBundle::GetSharedInstance();
150 const std::string& css_template =
151 resource_bundle.GetRawDataResource(IDR_WEBUI_CSS_TEXT_DEFAULTS)
152 .as_string();
154 return ReplaceStringPlaceholders(css_template, placeholders, nullptr);
157 void AppendWebUiCssTextDefaults(std::string* html) {
158 html->append("<style>");
159 html->append(GetWebUiCssTextDefaults());
160 html->append("</style>");
163 std::string GetFontFamily() {
164 std::string font_family = l10n_util::GetStringUTF8(
165 #if defined(OS_WIN)
166 base::win::GetVersion() < base::win::VERSION_VISTA ?
167 IDS_WEB_FONT_FAMILY_XP :
168 #endif
169 IDS_WEB_FONT_FAMILY);
171 #if defined(OS_CHROMEOS)
172 ui::ReplaceNotoSansWithRobotoIfEnabled(&font_family);
173 #endif
175 // TODO(dnicoara) Remove Ozone check when PlatformFont support is introduced
176 // into Ozone: crbug.com/320050
177 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
178 font_family = ui::ResourceBundle::GetSharedInstance().GetFont(
179 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family;
180 #endif
182 return font_family;
185 std::string GetFontSize() {
186 return l10n_util::GetStringUTF8(
187 #if defined(OS_WIN)
188 base::win::GetVersion() < base::win::VERSION_VISTA ?
189 IDS_WEB_FONT_SIZE_XP :
190 #endif
191 IDS_WEB_FONT_SIZE);
194 std::string GetTextDirection() {
195 return base::i18n::IsRTL() ? "rtl" : "ltr";
198 } // namespace webui