Add ICU message format support
[chromium-blink-merge.git] / ui / base / webui / web_ui_util.cc
blob6483f077b72ad40ec717bb25caa5017be9bc3401
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/template_expressions.h"
20 #include "ui/base/window_open_disposition.h"
21 #include "ui/gfx/codec/png_codec.h"
22 #include "ui/gfx/font.h"
23 #include "ui/gfx/image/image_skia.h"
24 #include "ui/resources/grit/webui_resources.h"
25 #include "ui/strings/grit/app_locale_settings.h"
26 #include "url/gurl.h"
28 #if defined(OS_WIN)
29 #include "base/win/windows_version.h"
30 #endif
32 namespace webui {
34 std::string GetBitmapDataUrl(const SkBitmap& bitmap) {
35 TRACE_EVENT2("oobe", "GetImageDataUrl",
36 "width", bitmap.width(), "height", bitmap.height());
37 std::vector<unsigned char> output;
38 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output);
39 std::string str_url;
40 str_url.insert(str_url.end(), output.begin(), output.end());
42 base::Base64Encode(str_url, &str_url);
43 str_url.insert(0, "data:image/png;base64,");
44 return str_url;
47 WindowOpenDisposition GetDispositionFromClick(const base::ListValue* args,
48 int start_index) {
49 double button = 0.0;
50 bool alt_key = false;
51 bool ctrl_key = false;
52 bool meta_key = false;
53 bool shift_key = false;
55 CHECK(args->GetDouble(start_index++, &button));
56 CHECK(args->GetBoolean(start_index++, &alt_key));
57 CHECK(args->GetBoolean(start_index++, &ctrl_key));
58 CHECK(args->GetBoolean(start_index++, &meta_key));
59 CHECK(args->GetBoolean(start_index++, &shift_key));
60 return ui::DispositionFromClick(
61 button == 1.0, alt_key, ctrl_key, meta_key, shift_key);
64 bool ParseScaleFactor(const base::StringPiece& identifier,
65 float* scale_factor) {
66 *scale_factor = 1.0f;
67 if (identifier.empty()) {
68 LOG(WARNING) << "Invalid scale factor format: " << identifier;
69 return false;
72 if (*identifier.rbegin() != 'x') {
73 LOG(WARNING) << "Invalid scale factor format: " << identifier;
74 return false;
77 double scale = 0;
78 std::string stripped;
79 identifier.substr(0, identifier.length() - 1).CopyToString(&stripped);
80 if (!base::StringToDouble(stripped, &scale)) {
81 LOG(WARNING) << "Invalid scale factor format: " << identifier;
82 return false;
84 *scale_factor = static_cast<float>(scale);
85 return true;
88 void ParsePathAndScale(const GURL& url,
89 std::string* path,
90 float* scale_factor) {
91 *path = net::UnescapeURLComponent(url.path().substr(1),
92 (net::UnescapeRule::URL_SPECIAL_CHARS |
93 net::UnescapeRule::SPACES));
94 if (scale_factor)
95 *scale_factor = 1.0f;
97 // Detect and parse resource string ending in @<scale>x.
98 std::size_t pos = path->rfind('@');
99 if (pos != std::string::npos) {
100 base::StringPiece stripped_path(*path);
101 float factor;
103 if (ParseScaleFactor(stripped_path.substr(
104 pos + 1, stripped_path.length() - pos - 1), &factor)) {
105 // Strip scale factor specification from path.
106 stripped_path.remove_suffix(stripped_path.length() - pos);
107 stripped_path.CopyToString(path);
109 if (scale_factor)
110 *scale_factor = factor;
114 void SetLoadTimeDataDefaults(const std::string& app_locale,
115 base::DictionaryValue* localized_strings) {
116 localized_strings->SetString("fontfamily", GetFontFamily());
117 localized_strings->SetString("fontsize", GetFontSize());
118 localized_strings->SetString("language", l10n_util::GetLanguage(app_locale));
119 localized_strings->SetString("textdirection", GetTextDirection());
122 std::string GetWebUiCssTextDefaults() {
123 std::map<base::StringPiece, std::string> placeholders;
124 placeholders["textDirection"] = GetTextDirection();
125 placeholders["fontFamily"] = GetFontFamily();
126 placeholders["fontSize"] = GetFontSize();
128 const ui::ResourceBundle& resource_bundle =
129 ui::ResourceBundle::GetSharedInstance();
130 const std::string& css_template =
131 resource_bundle.GetRawDataResource(IDR_WEBUI_CSS_TEXT_DEFAULTS)
132 .as_string();
134 return ui::ReplaceTemplateExpressions(css_template, placeholders);
137 void AppendWebUiCssTextDefaults(std::string* html) {
138 html->append("<style>");
139 html->append(GetWebUiCssTextDefaults());
140 html->append("</style>");
143 std::string GetFontFamily() {
144 std::string font_family = l10n_util::GetStringUTF8(
145 #if defined(OS_WIN)
146 base::win::GetVersion() < base::win::VERSION_VISTA ?
147 IDS_WEB_FONT_FAMILY_XP :
148 #endif
149 IDS_WEB_FONT_FAMILY);
151 // TODO(dnicoara) Remove Ozone check when PlatformFont support is introduced
152 // into Ozone: crbug.com/320050
153 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
154 font_family = ui::ResourceBundle::GetSharedInstance().GetFont(
155 ui::ResourceBundle::BaseFont).GetFontName() + ", " + font_family;
156 #endif
158 return font_family;
161 std::string GetFontSize() {
162 return l10n_util::GetStringUTF8(
163 #if defined(OS_WIN)
164 base::win::GetVersion() < base::win::VERSION_VISTA ?
165 IDS_WEB_FONT_SIZE_XP :
166 #endif
167 IDS_WEB_FONT_SIZE);
170 std::string GetTextDirection() {
171 return base::i18n::IsRTL() ? "rtl" : "ltr";
174 } // namespace webui