michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Use "dyld interposing" to hook methods imported from other libraries in the michael@0: // plugin child process. The basic technique is described at michael@0: // http://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73&lpg=PA73&dq=__interpose&source=bl&ots=OJnnXZYpZC&sig=o7I3lXvoduUi13SrPfOON7o3do4&hl=en&ei=AoehS9brCYGQNrvsmeUM&sa=X&oi=book_result&ct=result&resnum=6&ved=0CBsQ6AEwBQ#v=onepage&q=__interpose&f=false. michael@0: // The idea of doing it for the plugin child process comes from Chromium code, michael@0: // particularly from plugin_carbon_interpose_mac.cc michael@0: // (http://codesearch.google.com/codesearch/p?hl=en#OAMlx_jo-ck/src/chrome/browser/plugin_carbon_interpose_mac.cc&q=nscursor&exact_package=chromium&d=1&l=168) michael@0: // and from PluginProcessHost::Init() in plugin_process_host.cc michael@0: // (http://codesearch.google.com/codesearch/p?hl=en#OAMlx_jo-ck/src/content/browser/plugin_process_host.cc&q=nscursor&exact_package=chromium&d=1&l=222). michael@0: michael@0: // These hooks are needed to make certain OS calls work from the child process michael@0: // (a background process) that would normally only work when called in the michael@0: // parent process (the foreground process). They allow us to serialize michael@0: // information from the child process to the parent process, so that the same michael@0: // (or equivalent) calls can be made from the parent process. michael@0: michael@0: // This file lives in a seperate module (libplugin_child_interpose.dylib), michael@0: // which will get loaded by the OS before any other modules when the plugin michael@0: // child process is launched (from GeckoChildProcessHost:: michael@0: // PerformAsyncLaunchInternal()). For this reason it shouldn't link in other michael@0: // browser modules when loaded. Instead it should use dlsym() to load michael@0: // pointers to the methods it wants to call in other modules. michael@0: michael@0: #if !defined(__LP64__) michael@0: michael@0: #include michael@0: #import michael@0: michael@0: // The header file QuickdrawAPI.h is missing on OS X 10.7 and up (though the michael@0: // QuickDraw APIs defined in it are still present) -- so we need to supply the michael@0: // relevant parts of its contents here. It's likely that Apple will eventually michael@0: // remove the APIs themselves (probably in OS X 10.8), so we need to make them michael@0: // weak imports, and test for their presence before using them. michael@0: #if !defined(__QUICKDRAWAPI__) michael@0: michael@0: struct Cursor; michael@0: extern "C" void SetCursor(const Cursor * crsr) __attribute__((weak_import)); michael@0: michael@0: #endif /* __QUICKDRAWAPI__ */ michael@0: michael@0: BOOL (*OnSetThemeCursorPtr) (ThemeCursor) = NULL; michael@0: BOOL (*OnSetCursorPtr) (const Cursor*) = NULL; michael@0: BOOL (*OnHideCursorPtr) () = NULL; michael@0: BOOL (*OnShowCursorPtr) () = NULL; michael@0: michael@0: static BOOL loadXULPtrs() michael@0: { michael@0: if (!OnSetThemeCursorPtr) { michael@0: // mac_plugin_interposing_child_OnSetThemeCursor(ThemeCursor cursor) is in michael@0: // PluginInterposeOSX.mm michael@0: OnSetThemeCursorPtr = (BOOL(*)(ThemeCursor)) michael@0: dlsym(RTLD_DEFAULT, "mac_plugin_interposing_child_OnSetThemeCursor"); michael@0: } michael@0: if (!OnSetCursorPtr) { michael@0: // mac_plugin_interposing_child_OnSetCursor(const Cursor* cursor) is in michael@0: // PluginInterposeOSX.mm michael@0: OnSetCursorPtr = (BOOL(*)(const Cursor*)) michael@0: dlsym(RTLD_DEFAULT, "mac_plugin_interposing_child_OnSetCursor"); michael@0: } michael@0: if (!OnHideCursorPtr) { michael@0: // mac_plugin_interposing_child_OnHideCursor() is in PluginInterposeOSX.mm michael@0: OnHideCursorPtr = (BOOL(*)()) michael@0: dlsym(RTLD_DEFAULT, "mac_plugin_interposing_child_OnHideCursor"); michael@0: } michael@0: if (!OnShowCursorPtr) { michael@0: // mac_plugin_interposing_child_OnShowCursor() is in PluginInterposeOSX.mm michael@0: OnShowCursorPtr = (BOOL(*)()) michael@0: dlsym(RTLD_DEFAULT, "mac_plugin_interposing_child_OnShowCursor"); michael@0: } michael@0: return (OnSetCursorPtr && OnSetThemeCursorPtr && OnHideCursorPtr && OnShowCursorPtr); michael@0: } michael@0: michael@0: static OSStatus MacPluginChildSetThemeCursor(ThemeCursor cursor) michael@0: { michael@0: if (loadXULPtrs()) { michael@0: OnSetThemeCursorPtr(cursor); michael@0: } michael@0: return ::SetThemeCursor(cursor); michael@0: } michael@0: michael@0: static void MacPluginChildSetCursor(const Cursor* cursor) michael@0: { michael@0: if (::SetCursor) { michael@0: if (loadXULPtrs()) { michael@0: OnSetCursorPtr(cursor); michael@0: } michael@0: ::SetCursor(cursor); michael@0: } michael@0: } michael@0: michael@0: static CGError MacPluginChildCGDisplayHideCursor(CGDirectDisplayID display) michael@0: { michael@0: if (loadXULPtrs()) { michael@0: OnHideCursorPtr(); michael@0: } michael@0: return ::CGDisplayHideCursor(display); michael@0: } michael@0: michael@0: static CGError MacPluginChildCGDisplayShowCursor(CGDirectDisplayID display) michael@0: { michael@0: if (loadXULPtrs()) { michael@0: OnShowCursorPtr(); michael@0: } michael@0: return ::CGDisplayShowCursor(display); michael@0: } michael@0: michael@0: #pragma mark - michael@0: michael@0: struct interpose_substitution { michael@0: const void* replacement; michael@0: const void* original; michael@0: }; michael@0: michael@0: #define INTERPOSE_FUNCTION(function) \ michael@0: { reinterpret_cast(MacPluginChild##function), \ michael@0: reinterpret_cast(function) } michael@0: michael@0: __attribute__((used)) static const interpose_substitution substitutions[] michael@0: __attribute__((section("__DATA, __interpose"))) = { michael@0: INTERPOSE_FUNCTION(SetThemeCursor), michael@0: INTERPOSE_FUNCTION(CGDisplayHideCursor), michael@0: INTERPOSE_FUNCTION(CGDisplayShowCursor), michael@0: // SetCursor() and other QuickDraw APIs will probably be removed in OS X michael@0: // 10.8. But this will make 'SetCursor' NULL, which will just stop the OS michael@0: // from interposing it (tested using an INTERPOSE_FUNCTION_BROKEN macro michael@0: // that just sets the second address of each tuple to NULL). michael@0: INTERPOSE_FUNCTION(SetCursor), michael@0: }; michael@0: michael@0: #endif // !__LP64__