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 #ifndef UI_BASE_WEBUI_WEB_UI_UTIL_H_ 6 #define UI_BASE_WEBUI_WEB_UI_UTIL_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 12 #include "base/component_export.h" 13 #include "base/strings/string_piece.h" 14 #include "base/values.h" 15 #include "ui/base/template_expressions.h" 16 #include "ui/base/window_open_disposition.h" 17 18 class GURL; 19 class SkBitmap; 20 21 namespace webui { 22 23 struct LocalizedString { 24 const char* name; 25 int id; 26 }; 27 28 // Convenience routine to convert SkBitmap object to data url 29 // so that it can be used in WebUI. 30 COMPONENT_EXPORT(UI_BASE) std::string GetBitmapDataUrl(const SkBitmap& bitmap); 31 32 // Convenience routine to convert an in-memory PNG to a data url for WebUI use. 33 COMPONENT_EXPORT(UI_BASE) 34 std::string GetPngDataUrl(const unsigned char* data, size_t size); 35 36 // Extracts a disposition from click event arguments. |args| should contain 37 // an integer button and booleans alt key, ctrl key, meta key, and shift key 38 // (in that order), starting at |start_index|. 39 COMPONENT_EXPORT(UI_BASE) 40 WindowOpenDisposition GetDispositionFromClick(const base::ListValue* args, 41 int start_index); 42 43 // Parse a formatted scale factor string into float and sets to |scale_factor|. 44 COMPONENT_EXPORT(UI_BASE) 45 bool ParseScaleFactor(const base::StringPiece& identifier, float* scale_factor); 46 47 // Parses a URL containing some path [{frame}]@{scale}x. If it contains a 48 // scale factor then it is returned and the associated part of the URL is 49 // removed from the returned |path|, otherwise the default scale factor is 50 // returned and |path| is left intact. If it contains a frame index then it 51 // is returned and the associated part of the URL is removed from the 52 // returned |path|, otherwise the default frame index is returned and |path| 53 // is left intact. 54 COMPONENT_EXPORT(UI_BASE) 55 void ParsePathAndImageSpec(const GURL& url, 56 std::string* path, 57 float* scale_factor, 58 int* frame_index); 59 60 // Parses a URL containing some path @{scale}x. If it does not contain a scale 61 // factor then the default scale factor is returned. 62 COMPONENT_EXPORT(UI_BASE) 63 void ParsePathAndScale(const GURL& url, std::string* path, float* scale_factor); 64 65 // Helper function to set some default values (e.g., font family, size, 66 // language, and text direction) into the given dictionary. Requires an 67 // application locale (i.e. g_browser_process->GetApplicationLocale()). 68 COMPONENT_EXPORT(UI_BASE) 69 void SetLoadTimeDataDefaults(const std::string& app_locale, 70 base::DictionaryValue* localized_strings); 71 COMPONENT_EXPORT(UI_BASE) 72 void SetLoadTimeDataDefaults(const std::string& app_locale, 73 ui::TemplateReplacements* replacements); 74 75 // Get a CSS declaration for common text styles for all of Web UI. 76 COMPONENT_EXPORT(UI_BASE) std::string GetWebUiCssTextDefaults(); 77 78 // Get a CSS declaration for common text styles for Web UI using 79 // Material Design. 80 COMPONENT_EXPORT(UI_BASE) std::string GetWebUiCssTextDefaultsMd(); 81 82 // Appends the CSS declaration returned by GetWebUiCssTextDefaults() as an 83 // inline stylesheet. 84 COMPONENT_EXPORT(UI_BASE) void AppendWebUiCssTextDefaults(std::string* html); 85 86 // Get some common font styles for all of WebUI. 87 COMPONENT_EXPORT(UI_BASE) std::string GetFontFamily(); 88 COMPONENT_EXPORT(UI_BASE) std::string GetFontSize(); 89 COMPONENT_EXPORT(UI_BASE) std::string GetTextDirection(); 90 91 } // namespace webui 92 93 #endif // UI_BASE_WEBUI_WEB_UI_UTIL_H_ 94