1/* 2 * Copyright (C) 2012 Igalia S.L. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20#include "config.h" 21#include "WebKitMimeInfo.h" 22 23#include "WebKitMimeInfoPrivate.h" 24#include <wtf/gobject/GRefPtr.h> 25#include <wtf/text/CString.h> 26 27struct _WebKitMimeInfo { 28 _WebKitMimeInfo(const WebCore::MimeClassInfo& mimeInfo) 29 : mimeInfo(mimeInfo) 30 { 31 } 32 33 WebCore::MimeClassInfo mimeInfo; 34 CString mimeType; 35 CString description; 36 GRefPtr<GPtrArray> extensions; 37 38 int referenceCount; 39}; 40 41G_DEFINE_BOXED_TYPE(WebKitMimeInfo, webkit_mime_info, webkit_mime_info_ref, webkit_mime_info_unref) 42 43WebKitMimeInfo* webkitMimeInfoCreate(const WebCore::MimeClassInfo& mimeInfo) 44{ 45 WebKitMimeInfo* info = g_slice_new(WebKitMimeInfo); 46 new (info) WebKitMimeInfo(mimeInfo); 47 return info; 48} 49 50/** 51 * webkit_mime_info_ref: 52 * @info: a #WebKitMimeInfo 53 * 54 * Atomically increments the reference count of @info by one. This 55 * function is MT-safe and may be called from any thread. 56 * 57 * Returns: The passed in #WebKitMimeInfo 58 */ 59WebKitMimeInfo* webkit_mime_info_ref(WebKitMimeInfo* info) 60{ 61 g_atomic_int_inc(&info->referenceCount); 62 return info; 63} 64 65/** 66 * webkit_mime_info_unref: 67 * @info: a #WebKitMimeInfo 68 * 69 * Atomically decrements the reference count of @info by one. If the 70 * reference count drops to 0, all memory allocated by the #WebKitMimeInfo is 71 * released. This function is MT-safe and may be called from any 72 * thread. 73 */ 74void webkit_mime_info_unref(WebKitMimeInfo* info) 75{ 76 if (g_atomic_int_dec_and_test(&info->referenceCount)) { 77 info->~WebKitMimeInfo(); 78 g_slice_free(WebKitMimeInfo, info); 79 } 80} 81 82/** 83 * webkit_mime_info_get_mime_type: 84 * @info: a #WebKitMimeInfo 85 * 86 * Returns: the MIME type of @info 87 */ 88const char* webkit_mime_info_get_mime_type(WebKitMimeInfo* info) 89{ 90 if (!info->mimeType.isNull()) 91 return info->mimeType.data(); 92 93 if (info->mimeInfo.type.isEmpty()) 94 return 0; 95 96 info->mimeType = info->mimeInfo.type.utf8(); 97 return info->mimeType.data(); 98} 99 100/** 101 * webkit_mime_info_get_description: 102 * @info: a #WebKitMimeInfo 103 * 104 * Returns: the description of the MIME type of @info 105 */ 106const char* webkit_mime_info_get_description(WebKitMimeInfo* info) 107{ 108 if (!info->description.isNull()) 109 return info->description.data(); 110 111 if (info->mimeInfo.desc.isEmpty()) 112 return 0; 113 114 info->description = info->mimeInfo.desc.utf8(); 115 return info->description.data(); 116} 117 118/** 119 * webkit_mime_info_get_extensions: 120 * @info: a #WebKitMimeInfo 121 * 122 * Get the list of file extensions associated to the 123 * MIME type of @info 124 * 125 * Returns: (array zero-terminated=1) (transfer none): a 126 * %NULL-terminated array of strings 127 */ 128const char* const* webkit_mime_info_get_extensions(WebKitMimeInfo* info) 129{ 130 if (info->extensions) 131 return reinterpret_cast<gchar**>(info->extensions->pdata); 132 133 if (info->mimeInfo.extensions.isEmpty()) 134 return 0; 135 136 info->extensions = adoptGRef(g_ptr_array_new_with_free_func(g_free)); 137 for (size_t i = 0; i < info->mimeInfo.extensions.size(); ++i) { 138 if (info->mimeInfo.extensions[i].isEmpty()) 139 continue; 140 g_ptr_array_add(info->extensions.get(), g_strdup(info->mimeInfo.extensions[i].utf8().data())); 141 } 142 g_ptr_array_add(info->extensions.get(), 0); 143 144 return reinterpret_cast<gchar**>(info->extensions->pdata); 145} 146