1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
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 "NetscapePluginModule.h"
28
29#include <WebCore/FileSystem.h>
30#include <wtf/OwnArrayPtr.h>
31
32using namespace WebCore;
33
34namespace WebKit {
35
36static String getVersionInfo(const LPVOID versionInfoData, const String& info)
37{
38    LPVOID buffer;
39    UINT bufferLength;
40    String subInfo = "\\StringfileInfo\\040904E4\\" + info;
41    if (!::VerQueryValueW(versionInfoData, const_cast<UChar*>(subInfo.charactersWithNullTermination()), &buffer, &bufferLength) || !bufferLength)
42        return String();
43
44    // Subtract 1 from the length; we don't want the trailing null character.
45    return String(reinterpret_cast<UChar*>(buffer), bufferLength - 1);
46}
47
48static uint64_t fileVersion(DWORD leastSignificant, DWORD mostSignificant)
49{
50    ULARGE_INTEGER version;
51    version.LowPart = leastSignificant;
52    version.HighPart = mostSignificant;
53    return version.QuadPart;
54}
55
56bool NetscapePluginModule::getPluginInfo(const String& pluginPath, PluginInfoStore::Plugin& plugin)
57{
58        String pathCopy = pluginPath;
59    DWORD versionInfoSize = ::GetFileVersionInfoSizeW(pathCopy.charactersWithNullTermination(), 0);
60    if (!versionInfoSize)
61        return false;
62
63    OwnArrayPtr<char> versionInfoData = adoptArrayPtr(new char[versionInfoSize]);
64    if (!::GetFileVersionInfoW(pathCopy.charactersWithNullTermination(), 0, versionInfoSize, versionInfoData.get()))
65        return false;
66
67    String name = getVersionInfo(versionInfoData.get(), "ProductName");
68    String description = getVersionInfo(versionInfoData.get(), "FileDescription");
69    if (name.isNull() || description.isNull())
70        return false;
71
72    VS_FIXEDFILEINFO* info;
73    UINT infoSize;
74    if (!::VerQueryValueW(versionInfoData.get(), L"\\", reinterpret_cast<void**>(&info), &infoSize) || infoSize < sizeof(VS_FIXEDFILEINFO))
75        return false;
76
77    Vector<String> types;
78    getVersionInfo(versionInfoData.get(), "MIMEType").split('|', types);
79    Vector<String> extensionLists;
80    getVersionInfo(versionInfoData.get(), "FileExtents").split('|', extensionLists);
81    Vector<String> descriptions;
82    getVersionInfo(versionInfoData.get(), "FileOpenName").split('|', descriptions);
83
84    Vector<MimeClassInfo> mimes(types.size());
85    for (size_t i = 0; i < types.size(); i++) {
86        String type = types[i].lower();
87        String description = i < descriptions.size() ? descriptions[i] : "";
88        String extensionList = i < extensionLists.size() ? extensionLists[i] : "";
89
90        Vector<String> extensionsVector;
91        extensionList.split(',', extensionsVector);
92
93        // Get rid of the extension list that may be at the end of the description string.
94        int pos = description.find("(*");
95        if (pos != -1) {
96            // There might be a space that we need to get rid of.
97            if (pos > 1 && description[pos - 1] == ' ')
98                pos--;
99            description = description.left(pos);
100        }
101
102        mimes[i].type = type;
103        mimes[i].desc = description;
104        mimes[i].extensions.swap(extensionsVector);
105    }
106
107    plugin.path = pluginPath;
108    plugin.info.desc = description;
109    plugin.info.name = name;
110    plugin.info.file = pathGetFileName(pluginPath);
111    plugin.info.mimes.swap(mimes);
112    plugin.fileVersion = fileVersion(info->dwFileVersionLS, info->dwFileVersionMS);
113
114    return true;
115}
116
117void NetscapePluginModule::determineQuirks()
118{
119}
120
121} // namespace WebKit
122
123