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 "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"
29 #include "base/win/windows_version.h"
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
);
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,");
47 WindowOpenDisposition
GetDispositionFromClick(const base::ListValue
* args
,
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
) {
67 if (identifier
.empty()) {
68 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
72 if (*identifier
.rbegin() != 'x') {
73 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
79 identifier
.substr(0, identifier
.length() - 1).CopyToString(&stripped
);
80 if (!base::StringToDouble(stripped
, &scale
)) {
81 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
84 *scale_factor
= static_cast<float>(scale
);
88 void ParsePathAndScale(const GURL
& url
,
90 float* scale_factor
) {
91 *path
= net::UnescapeURLComponent(url
.path().substr(1),
92 (net::UnescapeRule::URL_SPECIAL_CHARS
|
93 net::UnescapeRule::SPACES
));
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
);
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
);
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
)
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(
146 base::win::GetVersion() < base::win::VERSION_VISTA
?
147 IDS_WEB_FONT_FAMILY_XP
:
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
;
161 std::string
GetFontSize() {
162 return l10n_util::GetStringUTF8(
164 base::win::GetVersion() < base::win::VERSION_VISTA
?
165 IDS_WEB_FONT_SIZE_XP
:
170 std::string
GetTextDirection() {
171 return base::i18n::IsRTL() ? "rtl" : "ltr";