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/debug/trace_event.h"
11 #include "base/i18n/rtl.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "grit/app_locale_settings.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"
26 #include "base/win/windows_version.h"
31 std::string
GetBitmapDataUrl(const SkBitmap
& bitmap
) {
32 TRACE_EVENT2("oobe", "GetImageDataUrl",
33 "width", bitmap
.width(), "height", bitmap
.height());
34 std::vector
<unsigned char> output
;
35 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
, false, &output
);
37 str_url
.insert(str_url
.end(), output
.begin(), output
.end());
39 base::Base64Encode(str_url
, &str_url
);
40 str_url
.insert(0, "data:image/png;base64,");
44 std::string
GetBitmapDataUrlFromResource(int res
) {
45 // Load resource icon and covert to base64 encoded data url
46 base::RefCountedStaticMemory
* icon_data
=
47 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
48 res
, ui::SCALE_FACTOR_100P
);
51 scoped_refptr
<base::RefCountedMemory
> raw_icon(icon_data
);
53 str_url
.insert(str_url
.end(),
55 raw_icon
->front() + raw_icon
->size());
56 base::Base64Encode(str_url
, &str_url
);
57 str_url
.insert(0, "data:image/png;base64,");
61 WindowOpenDisposition
GetDispositionFromClick(const base::ListValue
* args
,
65 bool ctrl_key
= false;
66 bool meta_key
= false;
67 bool shift_key
= false;
69 CHECK(args
->GetDouble(start_index
++, &button
));
70 CHECK(args
->GetBoolean(start_index
++, &alt_key
));
71 CHECK(args
->GetBoolean(start_index
++, &ctrl_key
));
72 CHECK(args
->GetBoolean(start_index
++, &meta_key
));
73 CHECK(args
->GetBoolean(start_index
++, &shift_key
));
74 return ui::DispositionFromClick(
75 button
== 1.0, alt_key
, ctrl_key
, meta_key
, shift_key
);
78 bool ParseScaleFactor(const base::StringPiece
& identifier
,
79 float* scale_factor
) {
81 if (identifier
.empty()) {
82 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
86 if (*identifier
.rbegin() != 'x') {
87 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
93 identifier
.substr(0, identifier
.length() - 1).CopyToString(&stripped
);
94 if (!base::StringToDouble(stripped
, &scale
)) {
95 LOG(WARNING
) << "Invalid scale factor format: " << identifier
;
98 *scale_factor
= scale
;
102 void ParsePathAndScale(const GURL
& url
,
104 float* scale_factor
) {
105 *path
= net::UnescapeURLComponent(url
.path().substr(1),
106 (net::UnescapeRule::URL_SPECIAL_CHARS
|
107 net::UnescapeRule::SPACES
));
109 *scale_factor
= 1.0f
;
111 // Detect and parse resource string ending in @<scale>x.
112 std::size_t pos
= path
->rfind('@');
113 if (pos
!= std::string::npos
) {
114 base::StringPiece
stripped_path(*path
);
117 if (ParseScaleFactor(stripped_path
.substr(
118 pos
+ 1, stripped_path
.length() - pos
- 1), &factor
)) {
119 // Strip scale factor specification from path.
120 stripped_path
.remove_suffix(stripped_path
.length() - pos
);
121 stripped_path
.CopyToString(path
);
124 *scale_factor
= factor
;
129 void SetFontAndTextDirection(base::DictionaryValue
* localized_strings
) {
130 int web_font_family_id
= IDS_WEB_FONT_FAMILY
;
131 int web_font_size_id
= IDS_WEB_FONT_SIZE
;
133 // Vary font settings for Windows XP.
134 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
135 web_font_family_id
= IDS_WEB_FONT_FAMILY_XP
;
136 web_font_size_id
= IDS_WEB_FONT_SIZE_XP
;
140 std::string font_family
= l10n_util::GetStringUTF8(web_font_family_id
);
142 // TODO(dnicoara) Remove Ozone check when PlatformFont support is introduced
143 // into Ozone: crbug.com/320050
144 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
145 font_family
= ui::ResourceBundle::GetSharedInstance().GetFont(
146 ui::ResourceBundle::BaseFont
).GetFontName() + ", " + font_family
;
149 localized_strings
->SetString("fontfamily", font_family
);
150 localized_strings
->SetString("fontsize",
151 l10n_util::GetStringUTF8(web_font_size_id
));
152 localized_strings
->SetString("textdirection",
153 base::i18n::IsRTL() ? "rtl" : "ltr");