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"
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 "build/build_config.h"
17 #include "net/base/escape.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/template_expressions.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "ui/gfx/codec/png_codec.h"
23 #include "ui/gfx/font.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/resources/grit/webui_resources.h"
26 #include "ui/strings/grit/app_locale_settings.h"
30 #include "base/win/windows_version.h"
35 std::string GetBitmapDataUrl(const SkBitmap& bitmap) {
36 TRACE_EVENT2("oobe", "GetImageDataUrl",
37 "width", bitmap.width(), "height", bitmap.height());
38 std::vector<unsigned char> output;
39 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output);
40 return GetPngDataUrl(output.data(), output.size());
43 std::string GetPngDataUrl(const unsigned char* data, size_t size) {
44 std::string str_url(reinterpret_cast<const char*>(data), size);
45 base::Base64Encode(str_url, &str_url);
46 str_url.insert(0, "data:image/png;base64,");
50 WindowOpenDisposition GetDispositionFromClick(const base::ListValue* args,
54 bool ctrl_key = false;
55 bool meta_key = false;
56 bool shift_key = false;
58 CHECK(args->GetDouble(start_index++, &button));
59 CHECK(args->GetBoolean(start_index++, &alt_key));
60 CHECK(args->GetBoolean(start_index++, &ctrl_key));
61 CHECK(args->GetBoolean(start_index++, &meta_key));
62 CHECK(args->GetBoolean(start_index++, &shift_key));
63 return ui::DispositionFromClick(
64 button == 1.0, alt_key, ctrl_key, meta_key, shift_key);
67 bool ParseScaleFactor(const base::StringPiece& identifier,
68 float* scale_factor) {
70 if (identifier.empty()) {
71 LOG(WARNING) << "Invalid scale factor format: " << identifier;
75 if (*identifier.rbegin() != 'x') {
76 LOG(WARNING) << "Invalid scale factor format: " << identifier;
82 identifier.substr(0, identifier.length() - 1).CopyToString(&stripped);
83 if (!base::StringToDouble(stripped, &scale)) {
84 LOG(WARNING) << "Invalid scale factor format: " << identifier;
87 *scale_factor = static_cast<float>(scale);
91 // Parse a formatted frame index string into int and sets to |frame_index|.
92 bool ParseFrameIndex(const base::StringPiece& identifier, int* frame_index) {
94 if (identifier.empty()) {
95 LOG(WARNING) << "Invalid frame index format: " << identifier;
99 if (*identifier.rbegin() != ']') {
100 LOG(WARNING) << "Invalid frame index format: " << identifier;
105 if (!base::StringToUint(identifier.substr(0, identifier.length() - 1),
107 LOG(WARNING) << "Invalid frame index format: " << identifier;
110 *frame_index = static_cast<int>(frame);
114 void ParsePathAndImageSpec(const GURL& url,
118 *path = net::UnescapeURLComponent(
119 url.path().substr(1),
120 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS |
121 net::UnescapeRule::SPACES);
123 *scale_factor = 1.0f;
127 // Detect and parse resource string ending in @<scale>x.
128 std::size_t pos = path->rfind('@');
129 if (pos != std::string::npos) {
130 base::StringPiece stripped_path(*path);
133 if (ParseScaleFactor(stripped_path.substr(
134 pos + 1, stripped_path.length() - pos - 1), &factor)) {
135 // Strip scale factor specification from path.
136 stripped_path.remove_suffix(stripped_path.length() - pos);
137 stripped_path.CopyToString(path);
140 *scale_factor = factor;
143 // Detect and parse resource string ending in [<frame>].
144 pos = path->rfind('[');
145 if (pos != std::string::npos) {
146 base::StringPiece stripped_path(*path);
150 stripped_path.substr(pos + 1, stripped_path.length() - pos - 1),
152 // Strip frame index specification from path.
153 stripped_path.remove_suffix(stripped_path.length() - pos);
154 stripped_path.CopyToString(path);
157 *frame_index = index;
161 void ParsePathAndScale(const GURL& url,
163 float* scale_factor) {
164 ParsePathAndImageSpec(url, path, scale_factor, nullptr);
167 void ParsePathAndFrame(const GURL& url, std::string* path, int* frame_index) {
168 ParsePathAndImageSpec(url, path, nullptr, frame_index);
171 void SetLoadTimeDataDefaults(const std::string& app_locale,
172 base::DictionaryValue* localized_strings) {
173 localized_strings->SetString("fontfamily", GetFontFamily());
174 localized_strings->SetString("fontsize", GetFontSize());
175 localized_strings->SetString("language", l10n_util::GetLanguage(app_locale));
176 localized_strings->SetString("textdirection", GetTextDirection());
179 void SetLoadTimeDataDefaults(const std::string& app_locale,
180 ui::TemplateReplacements* replacements) {
181 (*replacements)["fontfamily"] = GetFontFamily();
182 (*replacements)["fontsize"] = GetFontSize();
183 (*replacements)["language"] = l10n_util::GetLanguage(app_locale);
184 (*replacements)["textdirection"] = GetTextDirection();
187 std::string GetWebUiCssTextDefaults(base::StringPiece css_template) {
188 ui::TemplateReplacements placeholders;
189 placeholders["textDirection"] = GetTextDirection();
190 placeholders["fontFamily"] = GetFontFamily();
191 placeholders["fontSize"] = GetFontSize();
192 return ui::ReplaceTemplateExpressions(css_template, placeholders);
195 std::string GetWebUiCssTextDefaults() {
196 const ui::ResourceBundle& resource_bundle =
197 ui::ResourceBundle::GetSharedInstance();
198 return GetWebUiCssTextDefaults(
199 resource_bundle.GetRawDataResource(IDR_WEBUI_CSS_TEXT_DEFAULTS));
202 std::string GetWebUiCssTextDefaultsMd() {
203 const ui::ResourceBundle& resource_bundle =
204 ui::ResourceBundle::GetSharedInstance();
205 return GetWebUiCssTextDefaults(
206 resource_bundle.GetRawDataResource(IDR_WEBUI_CSS_TEXT_DEFAULTS_MD));
209 void AppendWebUiCssTextDefaults(std::string* html) {
210 html->append("<style>");
211 html->append(GetWebUiCssTextDefaults());
212 html->append("</style>");
215 std::string GetFontFamily() {
216 std::string font_family = l10n_util::GetStringUTF8(IDS_WEB_FONT_FAMILY);
218 // TODO(dnicoara) Remove Ozone check when PlatformFont support is introduced
219 // into Ozone: crbug.com/320050
220 #if (defined(OS_LINUX) || defined(OS_BSD)) && !defined(OS_CHROMEOS) && \
222 font_family = ui::ResourceBundle::GetSharedInstance().GetFont(
223 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family;
229 std::string GetFontSize() {
230 return l10n_util::GetStringUTF8(IDS_WEB_FONT_SIZE);
233 std::string GetTextDirection() {
234 return base::i18n::IsRTL() ? "rtl" : "ltr";