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 "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" 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 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 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 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 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(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 99 *scale_factor = ui::GetSupportedScaleFactor(static_cast<float>(scale)); 100 return true; 101 } 102 103 void ParsePathAndScale(const GURL& url, 104 std::string* path, 105 ui::ScaleFactor* scale_factor) { 106 *path = net::UnescapeURLComponent(url.path().substr(1), 107 (net::UnescapeRule::URL_SPECIAL_CHARS | 108 net::UnescapeRule::SPACES)); 109 if (scale_factor) 110 *scale_factor = ui::SCALE_FACTOR_100P; 111 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; 117 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); 123 } 124 if (scale_factor) 125 *scale_factor = factor; 126 } 127 } 128 129 // static 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; 133 #if defined(OS_WIN) 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; 138 } 139 #endif 140 141 std::string font_family = l10n_util::GetStringUTF8(web_font_family_id); 142 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; 148 #endif 149 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"); 155 } 156 157 } // namespace webui 158