| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2017 Sony Interactive Entertainment Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <wtf/Noncopyable.h> |
| 30 | #include <wtf/text/WTFString.h> |
| 31 | |
| 32 | #if USE(CF) |
| 33 | #include <wtf/RetainPtr.h> |
| 34 | #endif |
| 35 | |
| 36 | #if USE(GLIB) |
| 37 | typedef struct _GModule GModule; |
| 38 | #endif |
| 39 | |
| 40 | #if OS(WINDOWS) |
| 41 | #include <windows.h> |
| 42 | #endif |
| 43 | |
| 44 | namespace WebKit { |
| 45 | |
| 46 | class Module { |
| 47 | WTF_MAKE_NONCOPYABLE(Module); |
| 48 | public: |
| 49 | Module(const String& path); |
| 50 | ~Module(); |
| 51 | |
| 52 | bool load(); |
| 53 | // Note: On Mac this leaks the CFBundle to avoid crashes when a bundle is unloaded and there are |
| 54 | // live Objective-C objects whose methods come from that bundle. |
| 55 | void unload(); |
| 56 | |
| 57 | #if USE(CF) |
| 58 | String bundleIdentifier() const; |
| 59 | #endif |
| 60 | |
| 61 | template<typename FunctionType> FunctionType functionPointer(const char* functionName) const; |
| 62 | |
| 63 | private: |
| 64 | void* platformFunctionPointer(const char* functionName) const; |
| 65 | |
| 66 | String m_path; |
| 67 | #if OS(WINDOWS) |
| 68 | HMODULE m_module; |
| 69 | #endif |
| 70 | #if USE(CF) |
| 71 | RetainPtr<CFBundleRef> m_bundle; |
| 72 | #elif USE(GLIB) |
| 73 | GModule* m_handle; |
| 74 | #endif |
| 75 | }; |
| 76 | |
| 77 | template<typename FunctionType> FunctionType Module::functionPointer(const char* functionName) const |
| 78 | { |
| 79 | return reinterpret_cast<FunctionType>(platformFunctionPointer(functionName)); |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |