Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 cin et: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <stdlib.h> |
michael@0 | 7 | #include <unistd.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/DebugOnly.h" |
michael@0 | 10 | #include "mozilla/Endian.h" |
michael@0 | 11 | #include <algorithm> |
michael@0 | 12 | |
michael@0 | 13 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 14 | // Older versions of these headers seem to be missing an extern "C" |
michael@0 | 15 | extern "C" { |
michael@0 | 16 | #include <libgnome/libgnome.h> |
michael@0 | 17 | #include <libgnomeui/gnome-icon-theme.h> |
michael@0 | 18 | #include <libgnomeui/gnome-icon-lookup.h> |
michael@0 | 19 | |
michael@0 | 20 | #include <libgnomevfs/gnome-vfs-file-info.h> |
michael@0 | 21 | #include <libgnomevfs/gnome-vfs-ops.h> |
michael@0 | 22 | } |
michael@0 | 23 | #endif |
michael@0 | 24 | #ifdef MOZ_ENABLE_GIO |
michael@0 | 25 | #include <gio/gio.h> |
michael@0 | 26 | #endif |
michael@0 | 27 | |
michael@0 | 28 | #include <gtk/gtk.h> |
michael@0 | 29 | |
michael@0 | 30 | #include "nsMimeTypes.h" |
michael@0 | 31 | #include "nsIMIMEService.h" |
michael@0 | 32 | |
michael@0 | 33 | #include "nsIStringBundle.h" |
michael@0 | 34 | |
michael@0 | 35 | #include "nsNetUtil.h" |
michael@0 | 36 | #include "nsIURL.h" |
michael@0 | 37 | #include "prlink.h" |
michael@0 | 38 | |
michael@0 | 39 | #include "nsIconChannel.h" |
michael@0 | 40 | |
michael@0 | 41 | NS_IMPL_ISUPPORTS(nsIconChannel, |
michael@0 | 42 | nsIRequest, |
michael@0 | 43 | nsIChannel) |
michael@0 | 44 | |
michael@0 | 45 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 46 | // These let us have a soft dependency on libgnomeui rather than a hard one. These are just basically the prototypes |
michael@0 | 47 | // of the functions in the libraries. |
michael@0 | 48 | typedef char* (*_GnomeIconLookup_fn)(GtkIconTheme *icon_theme, GnomeThumbnailFactory *thumbnail_factory, |
michael@0 | 49 | const char *file_uri, const char *custom_icon, GnomeVFSFileInfo *file_info, |
michael@0 | 50 | const char *mime_type, GnomeIconLookupFlags flags, GnomeIconLookupResultFlags *result); |
michael@0 | 51 | typedef GnomeIconTheme* (*_GnomeIconThemeNew_fn)(void); |
michael@0 | 52 | typedef int (*_GnomeInit_fn)(const char *app_id, const char *app_version, int argc, char **argv, const struct poptOption *options, |
michael@0 | 53 | int flags, poptContext *return_ctx); |
michael@0 | 54 | typedef GnomeProgram* (*_GnomeProgramGet_fn)(void); |
michael@0 | 55 | typedef GnomeVFSResult (*_GnomeVFSGetFileInfo_fn)(const gchar *text_uri, GnomeVFSFileInfo *info, GnomeVFSFileInfoOptions options); |
michael@0 | 56 | typedef void (*_GnomeVFSFileInfoClear_fn)(GnomeVFSFileInfo *info); |
michael@0 | 57 | |
michael@0 | 58 | static PRLibrary* gLibGnomeUI = nullptr; |
michael@0 | 59 | static PRLibrary* gLibGnome = nullptr; |
michael@0 | 60 | static PRLibrary* gLibGnomeVFS = nullptr; |
michael@0 | 61 | static bool gTriedToLoadGnomeLibs = false; |
michael@0 | 62 | |
michael@0 | 63 | static _GnomeIconLookup_fn _gnome_icon_lookup = nullptr; |
michael@0 | 64 | static _GnomeIconThemeNew_fn _gnome_icon_theme_new = nullptr; |
michael@0 | 65 | static _GnomeInit_fn _gnome_init = nullptr; |
michael@0 | 66 | static _GnomeProgramGet_fn _gnome_program_get = nullptr; |
michael@0 | 67 | static _GnomeVFSGetFileInfo_fn _gnome_vfs_get_file_info = nullptr; |
michael@0 | 68 | static _GnomeVFSFileInfoClear_fn _gnome_vfs_file_info_clear = nullptr; |
michael@0 | 69 | #endif //MOZ_ENABLE_GNOMEUI |
michael@0 | 70 | |
michael@0 | 71 | static nsresult |
michael@0 | 72 | moz_gdk_pixbuf_to_channel(GdkPixbuf* aPixbuf, nsIURI *aURI, |
michael@0 | 73 | nsIChannel **aChannel) |
michael@0 | 74 | { |
michael@0 | 75 | int width = gdk_pixbuf_get_width(aPixbuf); |
michael@0 | 76 | int height = gdk_pixbuf_get_height(aPixbuf); |
michael@0 | 77 | NS_ENSURE_TRUE(height < 256 && width < 256 && height > 0 && width > 0 && |
michael@0 | 78 | gdk_pixbuf_get_colorspace(aPixbuf) == GDK_COLORSPACE_RGB && |
michael@0 | 79 | gdk_pixbuf_get_bits_per_sample(aPixbuf) == 8 && |
michael@0 | 80 | gdk_pixbuf_get_has_alpha(aPixbuf) && |
michael@0 | 81 | gdk_pixbuf_get_n_channels(aPixbuf) == 4, |
michael@0 | 82 | NS_ERROR_UNEXPECTED); |
michael@0 | 83 | |
michael@0 | 84 | const int n_channels = 4; |
michael@0 | 85 | gsize buf_size = 2 + n_channels * height * width; |
michael@0 | 86 | uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size); |
michael@0 | 87 | NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 88 | uint8_t *out = buf; |
michael@0 | 89 | |
michael@0 | 90 | *(out++) = width; |
michael@0 | 91 | *(out++) = height; |
michael@0 | 92 | |
michael@0 | 93 | const guchar * const pixels = gdk_pixbuf_get_pixels(aPixbuf); |
michael@0 | 94 | int rowextra = gdk_pixbuf_get_rowstride(aPixbuf) - width * n_channels; |
michael@0 | 95 | |
michael@0 | 96 | // encode the RGB data and the A data |
michael@0 | 97 | const guchar * in = pixels; |
michael@0 | 98 | for (int y = 0; y < height; ++y, in += rowextra) { |
michael@0 | 99 | for (int x = 0; x < width; ++x) { |
michael@0 | 100 | uint8_t r = *(in++); |
michael@0 | 101 | uint8_t g = *(in++); |
michael@0 | 102 | uint8_t b = *(in++); |
michael@0 | 103 | uint8_t a = *(in++); |
michael@0 | 104 | #define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255)) |
michael@0 | 105 | #if MOZ_LITTLE_ENDIAN |
michael@0 | 106 | *(out++) = DO_PREMULTIPLY(b); |
michael@0 | 107 | *(out++) = DO_PREMULTIPLY(g); |
michael@0 | 108 | *(out++) = DO_PREMULTIPLY(r); |
michael@0 | 109 | *(out++) = a; |
michael@0 | 110 | #else |
michael@0 | 111 | *(out++) = a; |
michael@0 | 112 | *(out++) = DO_PREMULTIPLY(r); |
michael@0 | 113 | *(out++) = DO_PREMULTIPLY(g); |
michael@0 | 114 | *(out++) = DO_PREMULTIPLY(b); |
michael@0 | 115 | #endif |
michael@0 | 116 | #undef DO_PREMULTIPLY |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | NS_ASSERTION(out == buf + buf_size, "size miscalculation"); |
michael@0 | 121 | |
michael@0 | 122 | nsresult rv; |
michael@0 | 123 | nsCOMPtr<nsIStringInputStream> stream = |
michael@0 | 124 | do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); |
michael@0 | 125 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 126 | |
michael@0 | 127 | rv = stream->AdoptData((char*)buf, buf_size); |
michael@0 | 128 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 129 | |
michael@0 | 130 | rv = NS_NewInputStreamChannel(aChannel, aURI, stream, |
michael@0 | 131 | NS_LITERAL_CSTRING(IMAGE_ICON_MS)); |
michael@0 | 132 | return rv; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | static GtkWidget *gProtoWindow = nullptr; |
michael@0 | 136 | static GtkWidget *gStockImageWidget = nullptr; |
michael@0 | 137 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 138 | static GnomeIconTheme *gIconTheme = nullptr; |
michael@0 | 139 | #endif //MOZ_ENABLE_GNOMEUI |
michael@0 | 140 | |
michael@0 | 141 | static void |
michael@0 | 142 | ensure_stock_image_widget() |
michael@0 | 143 | { |
michael@0 | 144 | // Only the style of the GtkImage needs to be used, but the widget is kept |
michael@0 | 145 | // to track dynamic style changes. |
michael@0 | 146 | if (!gProtoWindow) { |
michael@0 | 147 | gProtoWindow = gtk_window_new(GTK_WINDOW_POPUP); |
michael@0 | 148 | GtkWidget* protoLayout = gtk_fixed_new(); |
michael@0 | 149 | gtk_container_add(GTK_CONTAINER(gProtoWindow), protoLayout); |
michael@0 | 150 | |
michael@0 | 151 | gStockImageWidget = gtk_image_new(); |
michael@0 | 152 | gtk_container_add(GTK_CONTAINER(protoLayout), gStockImageWidget); |
michael@0 | 153 | |
michael@0 | 154 | gtk_widget_ensure_style(gStockImageWidget); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 159 | static nsresult |
michael@0 | 160 | ensure_libgnomeui() |
michael@0 | 161 | { |
michael@0 | 162 | // Attempt to get the libgnomeui symbol references. We do it this way so that stock icons from Init() |
michael@0 | 163 | // don't get held back by InitWithGnome()'s libgnomeui dependency. |
michael@0 | 164 | if (!gTriedToLoadGnomeLibs) { |
michael@0 | 165 | gLibGnomeUI = PR_LoadLibrary("libgnomeui-2.so.0"); |
michael@0 | 166 | if (!gLibGnomeUI) |
michael@0 | 167 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 168 | |
michael@0 | 169 | _gnome_init = (_GnomeInit_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_init_with_popt_table"); |
michael@0 | 170 | _gnome_icon_theme_new = (_GnomeIconThemeNew_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_theme_new"); |
michael@0 | 171 | _gnome_icon_lookup = (_GnomeIconLookup_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_lookup"); |
michael@0 | 172 | |
michael@0 | 173 | if (!_gnome_init || !_gnome_icon_theme_new || !_gnome_icon_lookup) { |
michael@0 | 174 | PR_UnloadLibrary(gLibGnomeUI); |
michael@0 | 175 | gLibGnomeUI = nullptr; |
michael@0 | 176 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | if (!gLibGnomeUI) |
michael@0 | 181 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 182 | |
michael@0 | 183 | return NS_OK; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | static nsresult |
michael@0 | 187 | ensure_libgnome() |
michael@0 | 188 | { |
michael@0 | 189 | if (!gTriedToLoadGnomeLibs) { |
michael@0 | 190 | gLibGnome = PR_LoadLibrary("libgnome-2.so.0"); |
michael@0 | 191 | if (!gLibGnome) |
michael@0 | 192 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 193 | |
michael@0 | 194 | _gnome_program_get = (_GnomeProgramGet_fn)PR_FindFunctionSymbol(gLibGnome, "gnome_program_get"); |
michael@0 | 195 | if (!_gnome_program_get) { |
michael@0 | 196 | PR_UnloadLibrary(gLibGnome); |
michael@0 | 197 | gLibGnome = nullptr; |
michael@0 | 198 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | if (!gLibGnome) |
michael@0 | 203 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 204 | |
michael@0 | 205 | return NS_OK; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | static nsresult |
michael@0 | 209 | ensure_libgnomevfs() |
michael@0 | 210 | { |
michael@0 | 211 | if (!gTriedToLoadGnomeLibs) { |
michael@0 | 212 | gLibGnomeVFS = PR_LoadLibrary("libgnomevfs-2.so.0"); |
michael@0 | 213 | if (!gLibGnomeVFS) |
michael@0 | 214 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 215 | |
michael@0 | 216 | _gnome_vfs_get_file_info = (_GnomeVFSGetFileInfo_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_get_file_info"); |
michael@0 | 217 | _gnome_vfs_file_info_clear = (_GnomeVFSFileInfoClear_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_file_info_clear"); |
michael@0 | 218 | if (!_gnome_vfs_get_file_info || !_gnome_vfs_file_info_clear) { |
michael@0 | 219 | PR_UnloadLibrary(gLibGnomeVFS); |
michael@0 | 220 | gLibGnomeVFS = nullptr; |
michael@0 | 221 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | if (!gLibGnomeVFS) |
michael@0 | 226 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 227 | |
michael@0 | 228 | return NS_OK; |
michael@0 | 229 | } |
michael@0 | 230 | #endif //MOZ_ENABLE_GNOMEUI |
michael@0 | 231 | |
michael@0 | 232 | static GtkIconSize |
michael@0 | 233 | moz_gtk_icon_size(const char *name) |
michael@0 | 234 | { |
michael@0 | 235 | if (strcmp(name, "button") == 0) |
michael@0 | 236 | return GTK_ICON_SIZE_BUTTON; |
michael@0 | 237 | |
michael@0 | 238 | if (strcmp(name, "menu") == 0) |
michael@0 | 239 | return GTK_ICON_SIZE_MENU; |
michael@0 | 240 | |
michael@0 | 241 | if (strcmp(name, "toolbar") == 0) |
michael@0 | 242 | return GTK_ICON_SIZE_LARGE_TOOLBAR; |
michael@0 | 243 | |
michael@0 | 244 | if (strcmp(name, "toolbarsmall") == 0) |
michael@0 | 245 | return GTK_ICON_SIZE_SMALL_TOOLBAR; |
michael@0 | 246 | |
michael@0 | 247 | if (strcmp(name, "dnd") == 0) |
michael@0 | 248 | return GTK_ICON_SIZE_DND; |
michael@0 | 249 | |
michael@0 | 250 | if (strcmp(name, "dialog") == 0) |
michael@0 | 251 | return GTK_ICON_SIZE_DIALOG; |
michael@0 | 252 | |
michael@0 | 253 | return GTK_ICON_SIZE_MENU; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | #if defined(MOZ_ENABLE_GNOMEUI) || defined(MOZ_ENABLE_GIO) |
michael@0 | 257 | static int32_t |
michael@0 | 258 | GetIconSize(nsIMozIconURI *aIconURI) |
michael@0 | 259 | { |
michael@0 | 260 | nsAutoCString iconSizeString; |
michael@0 | 261 | |
michael@0 | 262 | aIconURI->GetIconSize(iconSizeString); |
michael@0 | 263 | if (iconSizeString.IsEmpty()) { |
michael@0 | 264 | uint32_t size; |
michael@0 | 265 | mozilla::DebugOnly<nsresult> rv = aIconURI->GetImageSize(&size); |
michael@0 | 266 | NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed"); |
michael@0 | 267 | return size; |
michael@0 | 268 | } else { |
michael@0 | 269 | int size; |
michael@0 | 270 | |
michael@0 | 271 | GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); |
michael@0 | 272 | gtk_icon_size_lookup(icon_size, &size, nullptr); |
michael@0 | 273 | return size; |
michael@0 | 274 | } |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | /* Scale icon buffer to preferred size */ |
michael@0 | 278 | static nsresult |
michael@0 | 279 | ScaleIconBuf(GdkPixbuf **aBuf, int32_t iconSize) |
michael@0 | 280 | { |
michael@0 | 281 | // Scale buffer only if width or height differ from preferred size |
michael@0 | 282 | if (gdk_pixbuf_get_width(*aBuf) != iconSize && |
michael@0 | 283 | gdk_pixbuf_get_height(*aBuf) != iconSize) { |
michael@0 | 284 | GdkPixbuf *scaled = gdk_pixbuf_scale_simple(*aBuf, iconSize, iconSize, |
michael@0 | 285 | GDK_INTERP_BILINEAR); |
michael@0 | 286 | // replace original buffer by scaled |
michael@0 | 287 | g_object_unref(*aBuf); |
michael@0 | 288 | *aBuf = scaled; |
michael@0 | 289 | if (!scaled) |
michael@0 | 290 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 291 | } |
michael@0 | 292 | return NS_OK; |
michael@0 | 293 | } |
michael@0 | 294 | #endif |
michael@0 | 295 | |
michael@0 | 296 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 297 | nsresult |
michael@0 | 298 | nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI) |
michael@0 | 299 | { |
michael@0 | 300 | nsresult rv; |
michael@0 | 301 | |
michael@0 | 302 | if (NS_FAILED(ensure_libgnomeui()) || NS_FAILED(ensure_libgnome()) || NS_FAILED(ensure_libgnomevfs())) { |
michael@0 | 303 | gTriedToLoadGnomeLibs = true; |
michael@0 | 304 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | gTriedToLoadGnomeLibs = true; |
michael@0 | 308 | |
michael@0 | 309 | if (!_gnome_program_get()) { |
michael@0 | 310 | // Get the brandShortName from the string bundle to pass to GNOME |
michael@0 | 311 | // as the application name. This may be used for things such as |
michael@0 | 312 | // the title of grouped windows in the panel. |
michael@0 | 313 | nsCOMPtr<nsIStringBundleService> bundleService = |
michael@0 | 314 | do_GetService(NS_STRINGBUNDLE_CONTRACTID); |
michael@0 | 315 | |
michael@0 | 316 | NS_ASSERTION(bundleService, "String bundle service must be present!"); |
michael@0 | 317 | |
michael@0 | 318 | nsCOMPtr<nsIStringBundle> bundle; |
michael@0 | 319 | bundleService->CreateBundle("chrome://branding/locale/brand.properties", |
michael@0 | 320 | getter_AddRefs(bundle)); |
michael@0 | 321 | nsAutoString appName; |
michael@0 | 322 | |
michael@0 | 323 | if (bundle) { |
michael@0 | 324 | bundle->GetStringFromName(MOZ_UTF16("brandShortName"), |
michael@0 | 325 | getter_Copies(appName)); |
michael@0 | 326 | } else { |
michael@0 | 327 | NS_WARNING("brand.properties not present, using default application name"); |
michael@0 | 328 | appName.Assign(NS_LITERAL_STRING("Gecko")); |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | char* empty[] = { "" }; |
michael@0 | 332 | _gnome_init(NS_ConvertUTF16toUTF8(appName).get(), |
michael@0 | 333 | "1.0", 1, empty, nullptr, 0, nullptr); |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | uint32_t iconSize = GetIconSize(aIconURI); |
michael@0 | 337 | nsAutoCString type; |
michael@0 | 338 | aIconURI->GetContentType(type); |
michael@0 | 339 | |
michael@0 | 340 | GnomeVFSFileInfo fileInfo = {0}; |
michael@0 | 341 | fileInfo.refcount = 1; // In case some GnomeVFS function addrefs and releases it |
michael@0 | 342 | |
michael@0 | 343 | nsAutoCString spec; |
michael@0 | 344 | nsCOMPtr<nsIURL> url; |
michael@0 | 345 | rv = aIconURI->GetIconURL(getter_AddRefs(url)); |
michael@0 | 346 | if (url) { |
michael@0 | 347 | url->GetAsciiSpec(spec); |
michael@0 | 348 | // Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a |
michael@0 | 349 | // network request |
michael@0 | 350 | bool isFile; |
michael@0 | 351 | if (NS_SUCCEEDED(url->SchemeIs("file", &isFile)) && isFile) { |
michael@0 | 352 | _gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT); |
michael@0 | 353 | } |
michael@0 | 354 | else { |
michael@0 | 355 | // The filename we get is UTF-8-compatible, which matches gnome expectations. |
michael@0 | 356 | // See also: http://lists.gnome.org/archives/gnome-vfs-list/2004-March/msg00049.html |
michael@0 | 357 | // "Whenever we can detect the charset used for the URI type we try to |
michael@0 | 358 | // convert it to/from utf8 automatically inside gnome-vfs." |
michael@0 | 359 | // I'll interpret that as "otherwise, this field is random junk". |
michael@0 | 360 | nsAutoCString name; |
michael@0 | 361 | url->GetFileName(name); |
michael@0 | 362 | fileInfo.name = g_strdup(name.get()); |
michael@0 | 363 | |
michael@0 | 364 | if (!type.IsEmpty()) { |
michael@0 | 365 | fileInfo.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE; |
michael@0 | 366 | fileInfo.mime_type = g_strdup(type.get()); |
michael@0 | 367 | } |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | if (type.IsEmpty()) { |
michael@0 | 372 | nsCOMPtr<nsIMIMEService> ms(do_GetService("@mozilla.org/mime;1")); |
michael@0 | 373 | if (ms) { |
michael@0 | 374 | nsAutoCString fileExt; |
michael@0 | 375 | aIconURI->GetFileExtension(fileExt); |
michael@0 | 376 | if (!fileExt.IsEmpty()) { |
michael@0 | 377 | ms->GetTypeFromExtension(fileExt, type); |
michael@0 | 378 | } |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | // Get the icon theme |
michael@0 | 382 | if (!gIconTheme) { |
michael@0 | 383 | gIconTheme = _gnome_icon_theme_new(); |
michael@0 | 384 | |
michael@0 | 385 | if (!gIconTheme) { |
michael@0 | 386 | _gnome_vfs_file_info_clear(&fileInfo); |
michael@0 | 387 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 388 | } |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | char* name = _gnome_icon_lookup(gIconTheme, nullptr, spec.get(), nullptr, |
michael@0 | 392 | &fileInfo, type.get(), |
michael@0 | 393 | GNOME_ICON_LOOKUP_FLAGS_NONE, nullptr); |
michael@0 | 394 | |
michael@0 | 395 | _gnome_vfs_file_info_clear(&fileInfo); |
michael@0 | 396 | if (!name) |
michael@0 | 397 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 398 | |
michael@0 | 399 | // Get the default theme associated with the screen |
michael@0 | 400 | // Do NOT free. |
michael@0 | 401 | GtkIconTheme *theme = gtk_icon_theme_get_default(); |
michael@0 | 402 | if (!theme) { |
michael@0 | 403 | g_free(name); |
michael@0 | 404 | return NS_ERROR_UNEXPECTED; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | GError *err = nullptr; |
michael@0 | 408 | GdkPixbuf* buf = gtk_icon_theme_load_icon(theme, name, iconSize, (GtkIconLookupFlags)0, &err); |
michael@0 | 409 | g_free(name); |
michael@0 | 410 | |
michael@0 | 411 | if (!buf) { |
michael@0 | 412 | if (err) |
michael@0 | 413 | g_error_free(err); |
michael@0 | 414 | return NS_ERROR_UNEXPECTED; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | rv = ScaleIconBuf(&buf, iconSize); |
michael@0 | 418 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 419 | |
michael@0 | 420 | rv = moz_gdk_pixbuf_to_channel(buf, aIconURI, |
michael@0 | 421 | getter_AddRefs(mRealChannel)); |
michael@0 | 422 | g_object_unref(buf); |
michael@0 | 423 | return rv; |
michael@0 | 424 | } |
michael@0 | 425 | #endif // MOZ_ENABLE_GNOMEUI |
michael@0 | 426 | |
michael@0 | 427 | #ifdef MOZ_ENABLE_GIO |
michael@0 | 428 | nsresult |
michael@0 | 429 | nsIconChannel::InitWithGIO(nsIMozIconURI *aIconURI) |
michael@0 | 430 | { |
michael@0 | 431 | GIcon *icon = nullptr; |
michael@0 | 432 | nsCOMPtr<nsIURL> fileURI; |
michael@0 | 433 | |
michael@0 | 434 | // Read icon content |
michael@0 | 435 | aIconURI->GetIconURL(getter_AddRefs(fileURI)); |
michael@0 | 436 | |
michael@0 | 437 | // Get icon for file specified by URI |
michael@0 | 438 | if (fileURI) { |
michael@0 | 439 | bool isFile; |
michael@0 | 440 | nsAutoCString spec; |
michael@0 | 441 | fileURI->GetAsciiSpec(spec); |
michael@0 | 442 | if (NS_SUCCEEDED(fileURI->SchemeIs("file", &isFile)) && isFile) { |
michael@0 | 443 | GFile *file = g_file_new_for_uri(spec.get()); |
michael@0 | 444 | GFileInfo *fileInfo = g_file_query_info(file, |
michael@0 | 445 | G_FILE_ATTRIBUTE_STANDARD_ICON, |
michael@0 | 446 | G_FILE_QUERY_INFO_NONE, |
michael@0 | 447 | nullptr, nullptr); |
michael@0 | 448 | g_object_unref(file); |
michael@0 | 449 | if (fileInfo) { |
michael@0 | 450 | // icon from g_content_type_get_icon doesn't need unref |
michael@0 | 451 | icon = g_file_info_get_icon(fileInfo); |
michael@0 | 452 | if (icon) |
michael@0 | 453 | g_object_ref(icon); |
michael@0 | 454 | g_object_unref(fileInfo); |
michael@0 | 455 | } |
michael@0 | 456 | } |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | // Try to get icon by using MIME type |
michael@0 | 460 | if (!icon) { |
michael@0 | 461 | nsAutoCString type; |
michael@0 | 462 | aIconURI->GetContentType(type); |
michael@0 | 463 | // Try to get MIME type from file extension by using nsIMIMEService |
michael@0 | 464 | if (type.IsEmpty()) { |
michael@0 | 465 | nsCOMPtr<nsIMIMEService> ms(do_GetService("@mozilla.org/mime;1")); |
michael@0 | 466 | if (ms) { |
michael@0 | 467 | nsAutoCString fileExt; |
michael@0 | 468 | aIconURI->GetFileExtension(fileExt); |
michael@0 | 469 | ms->GetTypeFromExtension(fileExt, type); |
michael@0 | 470 | } |
michael@0 | 471 | } |
michael@0 | 472 | char *ctype = nullptr; // character representation of content type |
michael@0 | 473 | if (!type.IsEmpty()) { |
michael@0 | 474 | ctype = g_content_type_from_mime_type(type.get()); |
michael@0 | 475 | } |
michael@0 | 476 | if (ctype) { |
michael@0 | 477 | icon = g_content_type_get_icon(ctype); |
michael@0 | 478 | g_free(ctype); |
michael@0 | 479 | } |
michael@0 | 480 | } |
michael@0 | 481 | |
michael@0 | 482 | // Get default icon theme |
michael@0 | 483 | GtkIconTheme *iconTheme = gtk_icon_theme_get_default(); |
michael@0 | 484 | GtkIconInfo *iconInfo = nullptr; |
michael@0 | 485 | // Get icon size |
michael@0 | 486 | int32_t iconSize = GetIconSize(aIconURI); |
michael@0 | 487 | |
michael@0 | 488 | if (icon) { |
michael@0 | 489 | // Use icon and theme to get GtkIconInfo |
michael@0 | 490 | iconInfo = gtk_icon_theme_lookup_by_gicon(iconTheme, |
michael@0 | 491 | icon, iconSize, |
michael@0 | 492 | (GtkIconLookupFlags)0); |
michael@0 | 493 | g_object_unref(icon); |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | if (!iconInfo) { |
michael@0 | 497 | // Mozilla's mimetype lookup failed. Try the "unknown" icon. |
michael@0 | 498 | iconInfo = gtk_icon_theme_lookup_icon(iconTheme, |
michael@0 | 499 | "unknown", iconSize, |
michael@0 | 500 | (GtkIconLookupFlags)0); |
michael@0 | 501 | if (!iconInfo) { |
michael@0 | 502 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 503 | } |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | // Create a GdkPixbuf buffer containing icon and scale it |
michael@0 | 507 | GdkPixbuf* buf = gtk_icon_info_load_icon(iconInfo, nullptr); |
michael@0 | 508 | gtk_icon_info_free(iconInfo); |
michael@0 | 509 | if (!buf) { |
michael@0 | 510 | return NS_ERROR_UNEXPECTED; |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | nsresult rv = ScaleIconBuf(&buf, iconSize); |
michael@0 | 514 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 515 | |
michael@0 | 516 | rv = moz_gdk_pixbuf_to_channel(buf, aIconURI, |
michael@0 | 517 | getter_AddRefs(mRealChannel)); |
michael@0 | 518 | g_object_unref(buf); |
michael@0 | 519 | return rv; |
michael@0 | 520 | } |
michael@0 | 521 | #endif // MOZ_ENABLE_GIO |
michael@0 | 522 | |
michael@0 | 523 | nsresult |
michael@0 | 524 | nsIconChannel::Init(nsIURI* aURI) |
michael@0 | 525 | { |
michael@0 | 526 | nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI); |
michael@0 | 527 | NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); |
michael@0 | 528 | |
michael@0 | 529 | nsAutoCString stockIcon; |
michael@0 | 530 | iconURI->GetStockIcon(stockIcon); |
michael@0 | 531 | if (stockIcon.IsEmpty()) { |
michael@0 | 532 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 533 | return InitWithGnome(iconURI); |
michael@0 | 534 | #else |
michael@0 | 535 | #ifdef MOZ_ENABLE_GIO |
michael@0 | 536 | return InitWithGIO(iconURI); |
michael@0 | 537 | #else |
michael@0 | 538 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 539 | #endif |
michael@0 | 540 | #endif |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | // Search for stockIcon |
michael@0 | 544 | nsAutoCString iconSizeString; |
michael@0 | 545 | iconURI->GetIconSize(iconSizeString); |
michael@0 | 546 | |
michael@0 | 547 | nsAutoCString iconStateString; |
michael@0 | 548 | iconURI->GetIconState(iconStateString); |
michael@0 | 549 | |
michael@0 | 550 | GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); |
michael@0 | 551 | GtkStateType state = iconStateString.EqualsLiteral("disabled") ? |
michael@0 | 552 | GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL; |
michael@0 | 553 | |
michael@0 | 554 | // First lookup the icon by stock id and text direction. |
michael@0 | 555 | GtkTextDirection direction = GTK_TEXT_DIR_NONE; |
michael@0 | 556 | if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-ltr"))) { |
michael@0 | 557 | direction = GTK_TEXT_DIR_LTR; |
michael@0 | 558 | } else if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-rtl"))) { |
michael@0 | 559 | direction = GTK_TEXT_DIR_RTL; |
michael@0 | 560 | } |
michael@0 | 561 | |
michael@0 | 562 | bool forceDirection = direction != GTK_TEXT_DIR_NONE; |
michael@0 | 563 | nsAutoCString stockID; |
michael@0 | 564 | bool useIconName = false; |
michael@0 | 565 | if (!forceDirection) { |
michael@0 | 566 | direction = gtk_widget_get_default_direction(); |
michael@0 | 567 | stockID = stockIcon; |
michael@0 | 568 | } else { |
michael@0 | 569 | // GTK versions < 2.22 use icon names from concatenating stock id with |
michael@0 | 570 | // -(rtl|ltr), which is how the moz-icon stock name is interpreted here. |
michael@0 | 571 | stockID = Substring(stockIcon, 0, stockIcon.Length() - 4); |
michael@0 | 572 | // However, if we lookup bidi icons by the stock name, then GTK versions |
michael@0 | 573 | // >= 2.22 will use a bidi lookup convention that most icon themes do not |
michael@0 | 574 | // yet follow. Therefore, we first check to see if the theme supports the |
michael@0 | 575 | // old icon name as this will have bidi support (if found). |
michael@0 | 576 | GtkIconTheme *icon_theme = gtk_icon_theme_get_default(); |
michael@0 | 577 | // Micking what gtk_icon_set_render_icon does with sizes, though it's not |
michael@0 | 578 | // critical as icons will be scaled to suit size. It just means we follow |
michael@0 | 579 | // the same pathes and so share caches. |
michael@0 | 580 | gint width, height; |
michael@0 | 581 | if (gtk_icon_size_lookup(icon_size, &width, &height)) { |
michael@0 | 582 | gint size = std::min(width, height); |
michael@0 | 583 | // We use gtk_icon_theme_lookup_icon() without |
michael@0 | 584 | // GTK_ICON_LOOKUP_USE_BUILTIN instead of gtk_icon_theme_has_icon() so |
michael@0 | 585 | // we don't pick up fallback icons added by distributions for backward |
michael@0 | 586 | // compatibility. |
michael@0 | 587 | GtkIconInfo *icon = |
michael@0 | 588 | gtk_icon_theme_lookup_icon(icon_theme, stockIcon.get(), |
michael@0 | 589 | size, (GtkIconLookupFlags)0); |
michael@0 | 590 | if (icon) { |
michael@0 | 591 | useIconName = true; |
michael@0 | 592 | gtk_icon_info_free(icon); |
michael@0 | 593 | } |
michael@0 | 594 | } |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | ensure_stock_image_widget(); |
michael@0 | 598 | GtkStyle *style = gtk_widget_get_style(gStockImageWidget); |
michael@0 | 599 | GtkIconSet *icon_set = nullptr; |
michael@0 | 600 | if (!useIconName) { |
michael@0 | 601 | icon_set = gtk_style_lookup_icon_set(style, stockID.get()); |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | if (!icon_set) { |
michael@0 | 605 | // Either we have choosen icon-name lookup for a bidi icon, or stockIcon is |
michael@0 | 606 | // not a stock id so we assume it is an icon name. |
michael@0 | 607 | useIconName = true; |
michael@0 | 608 | // Creating a GtkIconSet is a convenient way to allow the style to |
michael@0 | 609 | // render the icon, possibly with variations suitable for insensitive |
michael@0 | 610 | // states. |
michael@0 | 611 | icon_set = gtk_icon_set_new(); |
michael@0 | 612 | GtkIconSource *icon_source = gtk_icon_source_new(); |
michael@0 | 613 | |
michael@0 | 614 | gtk_icon_source_set_icon_name(icon_source, stockIcon.get()); |
michael@0 | 615 | gtk_icon_set_add_source(icon_set, icon_source); |
michael@0 | 616 | gtk_icon_source_free(icon_source); |
michael@0 | 617 | } |
michael@0 | 618 | |
michael@0 | 619 | GdkPixbuf *icon = |
michael@0 | 620 | gtk_icon_set_render_icon(icon_set, style, direction, state, |
michael@0 | 621 | icon_size, gStockImageWidget, nullptr); |
michael@0 | 622 | if (useIconName) { |
michael@0 | 623 | gtk_icon_set_unref(icon_set); |
michael@0 | 624 | } |
michael@0 | 625 | |
michael@0 | 626 | // According to documentation, gtk_icon_set_render_icon() never returns |
michael@0 | 627 | // nullptr, but it does return nullptr when we have the problem reported |
michael@0 | 628 | // here: https://bugzilla.gnome.org/show_bug.cgi?id=629878#c13 |
michael@0 | 629 | if (!icon) |
michael@0 | 630 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 631 | |
michael@0 | 632 | nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI, |
michael@0 | 633 | getter_AddRefs(mRealChannel)); |
michael@0 | 634 | |
michael@0 | 635 | g_object_unref(icon); |
michael@0 | 636 | |
michael@0 | 637 | return rv; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | void |
michael@0 | 641 | nsIconChannel::Shutdown() { |
michael@0 | 642 | if (gProtoWindow) { |
michael@0 | 643 | gtk_widget_destroy(gProtoWindow); |
michael@0 | 644 | gProtoWindow = nullptr; |
michael@0 | 645 | gStockImageWidget = nullptr; |
michael@0 | 646 | } |
michael@0 | 647 | #ifdef MOZ_ENABLE_GNOMEUI |
michael@0 | 648 | if (gIconTheme) { |
michael@0 | 649 | g_object_unref(G_OBJECT(gIconTheme)); |
michael@0 | 650 | gIconTheme = nullptr; |
michael@0 | 651 | } |
michael@0 | 652 | gTriedToLoadGnomeLibs = false; |
michael@0 | 653 | if (gLibGnomeUI) { |
michael@0 | 654 | PR_UnloadLibrary(gLibGnomeUI); |
michael@0 | 655 | gLibGnomeUI = nullptr; |
michael@0 | 656 | } |
michael@0 | 657 | if (gLibGnome) { |
michael@0 | 658 | PR_UnloadLibrary(gLibGnome); |
michael@0 | 659 | gLibGnome = nullptr; |
michael@0 | 660 | } |
michael@0 | 661 | if (gLibGnomeVFS) { |
michael@0 | 662 | PR_UnloadLibrary(gLibGnomeVFS); |
michael@0 | 663 | gLibGnomeVFS = nullptr; |
michael@0 | 664 | } |
michael@0 | 665 | #endif //MOZ_ENABLE_GNOMEUI |
michael@0 | 666 | } |