1 // Copyright (c) 2012 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/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 ui::ScaleFactor
* scale_factor
) {
80 *scale_factor
= ui::SCALE_FACTOR_100P
;
81 if (identifier
.empty()) {
82 LOG(ERROR
) << "Invalid scale factor format: " << identifier
;
86 if (*identifier
.rbegin() != 'x') {
87 LOG(ERROR
) << "Invalid scale factor format: " << identifier
;
93 identifier
.substr(0, identifier
.length() - 1).CopyToString(&stripped
);
94 if (!base::StringToDouble(stripped
, &scale
)) {
95 LOG(ERROR
) << "Invalid scale factor format: " << identifier
;
99 *scale_factor
= ui::GetScaleFactorFromScale(static_cast<float>(scale
));
103 void ParsePathAndScale(const GURL
& url
,
105 ui::ScaleFactor
* scale_factor
) {
106 *path
= net::UnescapeURLComponent(url
.path().substr(1),
107 (net::UnescapeRule::URL_SPECIAL_CHARS
|
108 net::UnescapeRule::SPACES
));
110 *scale_factor
= ui::SCALE_FACTOR_100P
;
112 // Detect and parse resource string ending in @<scale>x.
113 std::size_t pos
= path
->rfind('@');
114 if (pos
!= std::string::npos
) {
115 base::StringPiece
stripped_path(*path
);
116 ui::ScaleFactor factor
;
118 if (ParseScaleFactor(stripped_path
.substr(
119 pos
+ 1, stripped_path
.length() - pos
- 1), &factor
)) {
120 // Strip scale factor specification from path.
121 stripped_path
.remove_suffix(stripped_path
.length() - pos
);
122 stripped_path
.CopyToString(path
);
125 *scale_factor
= factor
;
130 void SetFontAndTextDirection(base::DictionaryValue
* localized_strings
) {
131 int web_font_family_id
= IDS_WEB_FONT_FAMILY
;
132 int web_font_size_id
= IDS_WEB_FONT_SIZE
;
134 // Vary font settings for Windows XP.
135 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
136 web_font_family_id
= IDS_WEB_FONT_FAMILY_XP
;
137 web_font_size_id
= IDS_WEB_FONT_SIZE_XP
;
141 std::string font_family
= l10n_util::GetStringUTF8(web_font_family_id
);
143 #if defined(TOOLKIT_GTK)
144 // Use the system font on Linux/GTK. Keep the hard-coded font families as
145 // backup in case for some crazy reason this one isn't available.
146 font_family
= ui::ResourceBundle::GetSharedInstance().GetFont(
147 ui::ResourceBundle::BaseFont
).GetFontName() + ", " + font_family
;
150 localized_strings
->SetString("fontfamily", font_family
);
151 localized_strings
->SetString("fontsize",
152 l10n_util::GetStringUTF8(web_font_size_id
));
153 localized_strings
->SetString("textdirection",
154 base::i18n::IsRTL() ? "rtl" : "ltr");