PluginInfoCache.cpp [plain text]
#include "config.h"
#include "PluginInfoCache.h"
#if ENABLE(NETSCAPE_PLUGIN_API)
#include "NetscapePluginModule.h"
#include <WebCore/FileSystem.h>
#include <WebCore/PlatformDisplay.h>
#include <wtf/text/CString.h>
namespace WebKit {
static const unsigned gSchemaVersion = 3;
PluginInfoCache& PluginInfoCache::singleton()
{
static NeverDestroyed<PluginInfoCache> pluginInfoCache;
return pluginInfoCache;
}
static inline const char* cacheFilenameForCurrentDisplay()
{
#if PLATFORM(X11)
if (WebCore::PlatformDisplay::sharedDisplay().type() == WebCore::PlatformDisplay::Type::X11)
return "plugins-x11";
#endif
#if PLATFORM(WAYLAND)
if (WebCore::PlatformDisplay::sharedDisplay().type() == WebCore::PlatformDisplay::Type::Wayland)
return "plugins-wayland";
#endif
ASSERT_NOT_REACHED();
return "plugins";
}
PluginInfoCache::PluginInfoCache()
: m_cacheFile(g_key_file_new())
, m_saveToFileIdle(RunLoop::main(), this, &PluginInfoCache::saveToFile)
, m_readOnlyMode(false)
{
m_saveToFileIdle.setPriority(G_PRIORITY_DEFAULT_IDLE);
GUniquePtr<char> cacheDirectory(g_build_filename(g_get_user_cache_dir(), "webkitgtk", nullptr));
if (WebCore::makeAllDirectories(cacheDirectory.get())) {
GUniquePtr<char> oldCachePath(g_build_filename(cacheDirectory.get(), "plugins", nullptr));
WebCore::deleteFile(WebCore::stringFromFileSystemRepresentation(oldCachePath.get()));
m_cachePath.reset(g_build_filename(cacheDirectory.get(), cacheFilenameForCurrentDisplay(), nullptr));
g_key_file_load_from_file(m_cacheFile.get(), m_cachePath.get(), G_KEY_FILE_NONE, nullptr);
}
if (g_key_file_has_group(m_cacheFile.get(), "schema")) {
unsigned schemaVersion = static_cast<unsigned>(g_key_file_get_integer(m_cacheFile.get(), "schema", "version", nullptr));
if (schemaVersion < gSchemaVersion) {
m_cacheFile.reset(g_key_file_new());
} else if (schemaVersion > gSchemaVersion) {
m_readOnlyMode = true;
} else {
return;
}
}
g_key_file_set_integer(m_cacheFile.get(), "schema", "version", static_cast<unsigned>(gSchemaVersion));
}
PluginInfoCache::~PluginInfoCache()
{
}
void PluginInfoCache::saveToFile()
{
gsize dataLength;
GUniquePtr<char> data(g_key_file_to_data(m_cacheFile.get(), &dataLength, nullptr));
if (!data)
return;
g_file_set_contents(m_cachePath.get(), data.get(), dataLength, nullptr);
}
bool PluginInfoCache::getPluginInfo(const String& pluginPath, PluginModuleInfo& plugin)
{
CString pluginGroup = pluginPath.utf8();
if (!g_key_file_has_group(m_cacheFile.get(), pluginGroup.data()))
return false;
time_t lastModified;
if (!WebCore::getFileModificationTime(pluginPath, lastModified))
return false;
time_t cachedLastModified = static_cast<time_t>(g_key_file_get_uint64(m_cacheFile.get(), pluginGroup.data(), "mtime", nullptr));
if (lastModified != cachedLastModified)
return false;
plugin.path = pluginPath;
plugin.info.file = WebCore::pathGetFileName(pluginPath);
GUniquePtr<char> stringValue(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "name", nullptr));
plugin.info.name = String::fromUTF8(stringValue.get());
stringValue.reset(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "description", nullptr));
plugin.info.desc = String::fromUTF8(stringValue.get());
#if PLUGIN_ARCHITECTURE(X11)
stringValue.reset(g_key_file_get_string(m_cacheFile.get(), pluginGroup.data(), "mime-description", nullptr));
NetscapePluginModule::parseMIMEDescription(String::fromUTF8(stringValue.get()), plugin.info.mimes);
#endif
plugin.requiresGtk2 = g_key_file_get_boolean(m_cacheFile.get(), pluginGroup.data(), "requires-gtk2", nullptr);
return true;
}
void PluginInfoCache::updatePluginInfo(const String& pluginPath, const PluginModuleInfo& plugin)
{
time_t lastModified;
if (!WebCore::getFileModificationTime(pluginPath, lastModified))
return;
CString pluginGroup = pluginPath.utf8();
g_key_file_set_uint64(m_cacheFile.get(), pluginGroup.data(), "mtime", static_cast<guint64>(lastModified));
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "name", plugin.info.name.utf8().data());
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "description", plugin.info.desc.utf8().data());
#if PLUGIN_ARCHITECTURE(X11)
String mimeDescription = NetscapePluginModule::buildMIMEDescription(plugin.info.mimes);
g_key_file_set_string(m_cacheFile.get(), pluginGroup.data(), "mime-description", mimeDescription.utf8().data());
#endif
g_key_file_set_boolean(m_cacheFile.get(), pluginGroup.data(), "requires-gtk2", plugin.requiresGtk2);
if (m_cachePath && !m_readOnlyMode) {
if (m_saveToFileIdle.isActive())
return;
m_saveToFileIdle.startOneShot(0_s);
}
}
}
#endif // ENABLE(NETSCAPE_PLUGIN_API)