• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/base/webui/web_ui_util.h"
6 
7 #include <vector>
8 
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 "net/base/escape.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/base/window_open_disposition.h"
19 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/font.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/strings/grit/app_locale_settings.h"
23 #include "url/gurl.h"
24 
25 #if defined(OS_WIN)
26 #include "base/win/windows_version.h"
27 #endif
28 
29 namespace webui {
30 
GetBitmapDataUrl(const SkBitmap & bitmap)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);
36   std::string str_url;
37   str_url.insert(str_url.end(), output.begin(), output.end());
38 
39   base::Base64Encode(str_url, &str_url);
40   str_url.insert(0, "data:image/png;base64,");
41   return str_url;
42 }
43 
GetBitmapDataUrlFromResource(int res)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);
49   if (!icon_data)
50     return std::string();
51   scoped_refptr<base::RefCountedMemory> raw_icon(icon_data);
52   std::string str_url;
53   str_url.insert(str_url.end(),
54     raw_icon->front(),
55     raw_icon->front() + raw_icon->size());
56   base::Base64Encode(str_url, &str_url);
57   str_url.insert(0, "data:image/png;base64,");
58   return str_url;
59 }
60 
GetDispositionFromClick(const base::ListValue * args,int start_index)61 WindowOpenDisposition GetDispositionFromClick(const base::ListValue* args,
62                                               int start_index) {
63   double button = 0.0;
64   bool alt_key = false;
65   bool ctrl_key = false;
66   bool meta_key = false;
67   bool shift_key = false;
68 
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);
76 }
77 
ParseScaleFactor(const base::StringPiece & identifier,float * scale_factor)78 bool ParseScaleFactor(const base::StringPiece& identifier,
79                       float* scale_factor) {
80   *scale_factor = 1.0f;
81   if (identifier.empty()) {
82     LOG(WARNING) << "Invalid scale factor format: " << identifier;
83     return false;
84   }
85 
86   if (*identifier.rbegin() != 'x') {
87     LOG(WARNING) << "Invalid scale factor format: " << identifier;
88     return false;
89   }
90 
91   double scale = 0;
92   std::string stripped;
93   identifier.substr(0, identifier.length() - 1).CopyToString(&stripped);
94   if (!base::StringToDouble(stripped, &scale)) {
95     LOG(WARNING) << "Invalid scale factor format: " << identifier;
96     return false;
97   }
98   *scale_factor = scale;
99   return true;
100 }
101 
ParsePathAndScale(const GURL & url,std::string * path,float * scale_factor)102 void ParsePathAndScale(const GURL& url,
103                        std::string* path,
104                        float* scale_factor) {
105   *path = net::UnescapeURLComponent(url.path().substr(1),
106                                     (net::UnescapeRule::URL_SPECIAL_CHARS |
107                                      net::UnescapeRule::SPACES));
108   if (scale_factor)
109     *scale_factor = 1.0f;
110 
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);
115     float factor;
116 
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);
122     }
123     if (scale_factor)
124       *scale_factor = factor;
125   }
126 }
127 
128 // static
SetFontAndTextDirection(base::DictionaryValue * localized_strings)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;
132 #if defined(OS_WIN)
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;
137   }
138 #endif
139 
140   std::string font_family = l10n_util::GetStringUTF8(web_font_family_id);
141 
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;
147 #endif
148 
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");
154 }
155 
156 }  // namespace webui
157