WebGtkExtensionManager.cpp 2.6 KB

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