|
Line 0
a/Source/WebKit2/UIProcess/Plugins/gtk/PluginInfoCache.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2014 Igalia S.L. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "PluginInfoCache.h" |
| 28 |
|
| 29 |
#if ENABLE(NETSCAPE_PLUGIN_API) |
| 30 |
|
| 31 |
#include "NetscapePluginModule.h" |
| 32 |
#include <WebCore/FileSystem.h> |
| 33 |
#include <wtf/text/CString.h> |
| 34 |
|
| 35 |
namespace WebKit { |
| 36 |
|
| 37 |
static const unsigned gSchemaVersion = 1; |
| 38 |
|
| 39 |
PluginInfoCache& PluginInfoCache::shared() |
| 40 |
{ |
| 41 |
static NeverDestroyed<PluginInfoCache> pluginInfoCache; |
| 42 |
return pluginInfoCache; |
| 43 |
} |
| 44 |
|
| 45 |
PluginInfoCache::PluginInfoCache() |
| 46 |
: m_cacheFile(g_key_file_new()) |
| 47 |
, m_cachePath(g_build_filename(g_get_user_cache_dir(), "webkitgtk", "plugins", nullptr)) |
| 48 |
, m_saveToFileIdleId(0) |
| 49 |
{ |
| 50 |
g_key_file_load_from_file(m_cacheFile.get(), m_cachePath.get(), G_KEY_FILE_NONE, nullptr); |
| 51 |
|
| 52 |
if (g_key_file_has_group(m_cacheFile.get(), "schema")) { |
| 53 |
unsigned schemaVersion = static_cast<unsigned>(g_key_file_get_integer(m_cacheFile.get(), "schema", "version", nullptr)); |
| 54 |
if (schemaVersion == gSchemaVersion) |
| 55 |
return; |
| 56 |
|
| 57 |
// Cache file using an old schema, create a new empty file. |
| 58 |
m_cacheFile.reset(g_key_file_new()); |
| 59 |
} |
| 60 |
|
| 61 |
g_key_file_set_integer(m_cacheFile.get(), "schema", "version", static_cast<unsigned>(gSchemaVersion)); |
| 62 |
} |
| 63 |
|
| 64 |
PluginInfoCache::~PluginInfoCache() |
| 65 |
{ |
| 66 |
if (m_saveToFileIdleId) { |
| 67 |
g_source_remove(m_saveToFileIdleId); |
| 68 |
saveToFile(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
gboolean PluginInfoCache::saveToFileIdleCallback(PluginInfoCache* cache) |
| 73 |
{ |
| 74 |
cache->saveToFile(); |
| 75 |
return FALSE; |
| 76 |
} |
| 77 |
|
| 78 |
void PluginInfoCache::saveToFile() |
| 79 |
{ |
| 80 |
m_saveToFileIdleId = 0; |
| 81 |
|
| 82 |
gsize dataLength; |
| 83 |
GUniquePtr<char> data(g_key_file_to_data(m_cacheFile.get(), &dataLength, nullptr)); |
| 84 |
if (!data) |
| 85 |
return; |
| 86 |
|
| 87 |
g_file_set_contents(m_cachePath.get(), data.get(), dataLength, nullptr); |
| 88 |
} |
| 89 |
|
| 90 |
bool PluginInfoCache::getPluginInfo(const String& pluginPath, PluginModuleInfo& plugin) |
| 91 |
{ |
| 92 |
CString pluginGroup = pluginPath.utf8(); |
| 93 |
if (!g_key_file_has_group(m_cacheFile.get(), pluginGroup.data())) |
| 94 |
return false; |
| 95 |
|
| 96 |
time_t lastModified; |
| 97 |
if (!WebCore::getFileModificationTime(pluginPath, lastModified)) |
| 98 |
return false; |
| 99 |
time_t cachedLastModified = static_cast<time_t>(g_key_file_get_uint64(m_cacheFile.get(), pluginGroup.data(), "mtime", nullptr)); |
| 100 |
if (lastModified != cachedLastModified) |
| 101 |
return false; |
| 102 |
|
| 103 |
plugin.path = pluginPath; |
| 104 |
plugin.info.file = WebCore::pathGetFileName(pluginPath); |
| 105 |
|
| 106 |
GUniquePtr<char> stringValue(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "name", nullptr)); |
| 107 |
plugin.info.name = String::fromUTF8(stringValue.get()); |
| 108 |
|
| 109 |
stringValue.reset(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "description", nullptr)); |
| 110 |
plugin.info.desc = String::fromUTF8(stringValue.get()); |
| 111 |
|
| 112 |
stringValue.reset(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "mime-description", nullptr)); |
| 113 |
NetscapePluginModule::parseMIMEDescription(String::fromUTF8(stringValue.get()), plugin.info.mimes); |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
void PluginInfoCache::updatePluginInfo(const String& pluginPath, const PluginModuleInfo& plugin) |
| 119 |
{ |
| 120 |
time_t lastModified; |
| 121 |
if (!WebCore::getFileModificationTime(pluginPath, lastModified)) |
| 122 |
return; |
| 123 |
|
| 124 |
CString pluginGroup = pluginPath.utf8(); |
| 125 |
g_key_file_set_uint64(m_cacheFile.get(), pluginGroup.data(), "mtime", static_cast<guint64>(lastModified)); |
| 126 |
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "name", plugin.info.name.utf8().data()); |
| 127 |
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "description", plugin.info.desc.utf8().data()); |
| 128 |
|
| 129 |
String mimeDescription = NetscapePluginModule::buildMIMEDescription(plugin.info.mimes); |
| 130 |
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "mime-description", mimeDescription.utf8().data()); |
| 131 |
|
| 132 |
// Save the cache file in an idle to make sure it happens in the main thread and |
| 133 |
// it's done only once when this is called multiple times in a very short time. |
| 134 |
if (m_saveToFileIdleId) |
| 135 |
return; |
| 136 |
|
| 137 |
m_saveToFileIdleId = g_idle_add(reinterpret_cast<GSourceFunc>(PluginInfoCache::saveToFileIdleCallback), this); |
| 138 |
} |
| 139 |
|
| 140 |
} // namespace WebKit |
| 141 |
|
| 142 |
#endif // ENABLE(NETSCAPE_PLUGIN_API) |