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 "WebGtkExtensionManager.h"
22
23#include "InjectedBundle.h"
24#include "WKBundleAPICast.h"
25#include "WKString.h"
26#include "WKType.h"
27#include "WebKitWebExtensionPrivate.h"
28#include <WebCore/FileSystem.h>
29#include <wtf/OwnPtr.h>
30
31namespace WebKit {
32
33WebGtkExtensionManager& WebGtkExtensionManager::shared()
34{
35    DEFINE_STATIC_LOCAL(WebGtkExtensionManager, extensionManager, ());
36    return extensionManager;
37}
38
39WebGtkExtensionManager::WebGtkExtensionManager()
40{
41}
42
43void WebGtkExtensionManager::scanModules(const String& webExtensionsDirectory, Vector<String>& modules)
44{
45    Vector<String> modulePaths = WebCore::listDirectory(webExtensionsDirectory, String("*.so"));
46    for (size_t i = 0; i < modulePaths.size(); ++i) {
47        if (WebCore::fileExists(modulePaths[i]))
48            modules.append(modulePaths[i]);
49    }
50}
51
52void WebGtkExtensionManager::initialize(WKBundleRef bundle, WKTypeRef userData)
53{
54    m_extension = adoptGRef(webkitWebExtensionCreate(toImpl(bundle)));
55
56    String webExtensionsDirectory;
57    if (userData) {
58        ASSERT(WKGetTypeID(userData) == WKStringGetTypeID());
59        webExtensionsDirectory = toImpl(static_cast<WKStringRef>(userData))->string();
60    }
61
62    if (webExtensionsDirectory.isNull())
63        return;
64
65    Vector<String> modulePaths;
66    scanModules(webExtensionsDirectory, modulePaths);
67
68    for (size_t i = 0; i < modulePaths.size(); ++i) {
69        OwnPtr<Module> module = adoptPtr(new Module(modulePaths[i]));
70        if (!module->load())
71            continue;
72
73        WebKitWebExtensionInitializeFunction initializeFunction =
74            module->functionPointer<WebKitWebExtensionInitializeFunction>("webkit_web_extension_initialize");
75        if (!initializeFunction)
76            continue;
77
78        m_extensionModules.append(module.leakPtr());
79        initializeFunction(m_extension.get());
80    }
81}
82
83} // namespace WebKit
84