diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-21 00:56:07 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-21 00:56:07 +0000 |
commit | f38e25f12f542ed26b0b9d310d2e432297520995 (patch) | |
tree | 91968df6d61551cadf4ec12152f9790e5904f1b5 /base | |
parent | 53e45f18ee44a3b7ace0ebf96960c1af9f8f1ae6 (diff) | |
download | chromium_src-f38e25f12f542ed26b0b9d310d2e432297520995.zip chromium_src-f38e25f12f542ed26b0b9d310d2e432297520995.tar.gz chromium_src-f38e25f12f542ed26b0b9d310d2e432297520995.tar.bz2 |
plugins: move NativeLibrary into base.
NativeLibrary is used by some plugin code under chrome/.
Rather than including webkit/glue there, this relocation is the smallest
logical bite to take. :\
Review URL: http://codereview.chromium.org/87012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14071 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 4 | ||||
-rw-r--r-- | base/native_library.h | 47 | ||||
-rw-r--r-- | base/native_library_linux.cc | 36 | ||||
-rw-r--r-- | base/native_library_mac.mm | 38 | ||||
-rw-r--r-- | base/native_library_win.cc | 46 |
5 files changed, 171 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index e7c6adb..3180c70 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -162,6 +162,10 @@ 'message_pump_mac.mm', 'message_pump_win.cc', 'message_pump_win.h', + 'native_library.h', + 'native_library_linux.cc', + 'native_library_mac.mm', + 'native_library_win.cc', 'non_thread_safe.cc', 'non_thread_safe.h', 'nss_init.cc', diff --git a/base/native_library.h b/base/native_library.h new file mode 100644 index 0000000..ce85c23 --- /dev/null +++ b/base/native_library.h @@ -0,0 +1,47 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_NATIVE_LIBRARY_H_ +#define BASE_NATIVE_LIBRARY_H_ + +// This file defines a cross-platform "NativeLibrary" type which represents +// a loadable module. + +#include "build/build_config.h" + +#if defined(OS_WIN) +#include <windows.h> +#elif defined(OS_MACOSX) +#import <Carbon/Carbon.h> +#endif // OS_* + +class FilePath; + +namespace base { + +#if defined(OS_WIN) +typedef HMODULE NativeLibrary; +typedef char* NativeLibraryFunctionNameType; +#elif defined(OS_MACOSX) +typedef CFBundleRef NativeLibrary; +typedef CFStringRef NativeLibraryFunctionNameType; +#elif defined(OS_LINUX) +typedef void* NativeLibrary; +typedef const char* NativeLibraryFunctionNameType; +#endif // OS_* + +// Loads a native library from disk. Release it with UnloadNativeLibrary when +// you're done. +NativeLibrary LoadNativeLibrary(const FilePath& library_path); + +// Unloads a native library. +void UnloadNativeLibrary(NativeLibrary library); + +// Gets a function pointer from a native library. +void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, + NativeLibraryFunctionNameType name); + +} // namespace base + +#endif // BASE_NATIVE_LIBRARY_H_ diff --git a/base/native_library_linux.cc b/base/native_library_linux.cc new file mode 100644 index 0000000..1ec7aab --- /dev/null +++ b/base/native_library_linux.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/native_library.h" + +#include <dlfcn.h> + +#include "base/file_path.h" +#include "base/logging.h" + +namespace base { + +// static +NativeLibrary LoadNativeLibrary(const FilePath& library_path) { + void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY); + if (!dl) + NOTREACHED() << "dlopen failed: " << dlerror(); + + return dl; +} + +// static +void UnloadNativeLibrary(NativeLibrary library) { + int ret = dlclose(library); + if (ret < 0) + NOTREACHED() << "dlclose failed: " << dlerror(); +} + +// static +void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, + NativeLibraryFunctionNameType name) { + return dlsym(library, name); +} + +} // namespace base diff --git a/base/native_library_mac.mm b/base/native_library_mac.mm new file mode 100644 index 0000000..f817a01 --- /dev/null +++ b/base/native_library_mac.mm @@ -0,0 +1,38 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/native_library.h" + +#import <Carbon/Carbon.h> + +#include "base/file_path.h" +#include "base/scoped_cftyperef.h" + +namespace base { + +// static +NativeLibrary LoadNativeLibrary(const FilePath& library_path) { + scoped_cftyperef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( + kCFAllocatorDefault, + (const UInt8*)library_path.value().c_str(), + library_path.value().length(), + true)); + if (!url) + return NULL; + + return CFBundleCreate(kCFAllocatorDefault, url.get()); +} + +// static +void UnloadNativeLibrary(NativeLibrary library) { + CFRelease(library); +} + +// static +void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, + NativeLibraryFunctionNameType name) { + return CFBundleGetFunctionPointerForName(library, name); +} + +} // namespace base diff --git a/base/native_library_win.cc b/base/native_library_win.cc new file mode 100644 index 0000000..cf477fe --- /dev/null +++ b/base/native_library_win.cc @@ -0,0 +1,46 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/native_library.h" + +#include <windows.h> + +#include "base/file_path.h" +#include "base/path_service.h" + +namespace base { + +// static +NativeLibrary LoadNativeLibrary(const FilePath& library_path) { + // Switch the current directory to the library directory as the library + // may have dependencies on DLLs in this directory. + bool restore_directory = false; + std::wstring current_directory; + if (PathService::Get(base::DIR_CURRENT, ¤t_directory)) { + FilePath plugin_path = library_path.DirName(); + if (!plugin_path.value().empty()) { + PathService::SetCurrentDirectory(plugin_path.value()); + restore_directory = true; + } + } + + HMODULE module = LoadLibrary(library_path.value().c_str()); + if (restore_directory) + PathService::SetCurrentDirectory(current_directory); + + return module; +} + +// static +void UnloadNativeLibrary(NativeLibrary library) { + FreeLibrary(library); +} + +// static +void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, + NativeLibraryFunctionNameType name) { + return GetProcAddress(library, name); +} + +} // namespace base |