michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: sw=4 ts=4 et : 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: #include "mozilla/ipc/IOThreadChild.h" michael@0: #include "mozilla/plugins/PluginProcessChild.h" michael@0: michael@0: #include "prlink.h" michael@0: michael@0: #include "base/command_line.h" michael@0: #include "base/string_util.h" michael@0: #include "chrome/common/chrome_switches.h" michael@0: michael@0: #if defined(XP_MACOSX) michael@0: #include "nsCocoaFeatures.h" michael@0: // An undocumented CoreGraphics framework method, present in the same form michael@0: // since at least OS X 10.5. michael@0: extern "C" CGError CGSSetDebugOptions(int options); michael@0: #endif michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: bool ShouldProtectPluginCurrentDirectory(char16ptr_t pluginFilePath); michael@0: #endif michael@0: michael@0: using mozilla::ipc::IOThreadChild; michael@0: michael@0: #ifdef OS_WIN michael@0: #include "nsSetDllDirectory.h" michael@0: #include michael@0: michael@0: namespace { michael@0: michael@0: std::size_t caseInsensitiveFind(std::string aHaystack, std::string aNeedle) { michael@0: std::transform(aHaystack.begin(), aHaystack.end(), aHaystack.begin(), ::tolower); michael@0: std::transform(aNeedle.begin(), aNeedle.end(), aNeedle.begin(), ::tolower); michael@0: return aHaystack.find(aNeedle); michael@0: } michael@0: michael@0: } michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: namespace plugins { michael@0: michael@0: michael@0: bool michael@0: PluginProcessChild::Init() michael@0: { michael@0: #if defined(XP_MACOSX) michael@0: // Remove the trigger for "dyld interposing" that we added in michael@0: // GeckoChildProcessHost::PerformAsyncLaunchInternal(), in the host michael@0: // process just before we were launched. Dyld interposing will still michael@0: // happen in our process (the plugin child process). But we don't want michael@0: // it to happen in any processes that the plugin might launch from our michael@0: // process. michael@0: nsCString interpose(PR_GetEnv("DYLD_INSERT_LIBRARIES")); michael@0: if (!interpose.IsEmpty()) { michael@0: // If we added the path to libplugin_child_interpose.dylib to an michael@0: // existing DYLD_INSERT_LIBRARIES, we appended it to the end, after a michael@0: // ":" path seperator. michael@0: int32_t lastSeparatorPos = interpose.RFind(":"); michael@0: int32_t lastTriggerPos = interpose.RFind("libplugin_child_interpose.dylib"); michael@0: bool needsReset = false; michael@0: if (lastTriggerPos != -1) { michael@0: if (lastSeparatorPos == -1) { michael@0: interpose.Truncate(); michael@0: needsReset = true; michael@0: } else if (lastTriggerPos > lastSeparatorPos) { michael@0: interpose.SetLength(lastSeparatorPos); michael@0: needsReset = true; michael@0: } michael@0: } michael@0: if (needsReset) { michael@0: nsCString setInterpose("DYLD_INSERT_LIBRARIES="); michael@0: if (!interpose.IsEmpty()) { michael@0: setInterpose.Append(interpose); michael@0: } michael@0: // Values passed to PR_SetEnv() must be seperately allocated. michael@0: char* setInterposePtr = strdup(setInterpose.get()); michael@0: PR_SetEnv(setInterposePtr); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef XP_WIN michael@0: // Drag-and-drop needs OleInitialize to be called, and Silverlight depends michael@0: // on the host calling CoInitialize (which is called by OleInitialize). michael@0: ::OleInitialize(nullptr); michael@0: #endif michael@0: michael@0: // Certain plugins, such as flash, steal the unhandled exception filter michael@0: // thus we never get crash reports when they fault. This call fixes it. michael@0: message_loop()->set_exception_restoration(true); michael@0: michael@0: std::string pluginFilename; michael@0: michael@0: #if defined(OS_POSIX) michael@0: // NB: need to be very careful in ensuring that the first arg michael@0: // (after the binary name) here is indeed the plugin module path. michael@0: // Keep in sync with dom/plugins/PluginModuleParent. michael@0: std::vector values = CommandLine::ForCurrentProcess()->argv(); michael@0: NS_ABORT_IF_FALSE(values.size() >= 2, "not enough args"); michael@0: michael@0: pluginFilename = UnmungePluginDsoPath(values[1]); michael@0: michael@0: #elif defined(OS_WIN) michael@0: std::vector values = michael@0: CommandLine::ForCurrentProcess()->GetLooseValues(); michael@0: NS_ABORT_IF_FALSE(values.size() >= 1, "not enough loose args"); michael@0: michael@0: if (ShouldProtectPluginCurrentDirectory(values[0].c_str())) { michael@0: SanitizeEnvironmentVariables(); michael@0: SetDllDirectory(L""); michael@0: } michael@0: michael@0: pluginFilename = WideToUTF8(values[0]); michael@0: #else michael@0: # error Sorry michael@0: #endif michael@0: michael@0: if (NS_FAILED(nsRegion::InitStatic())) { michael@0: NS_ERROR("Could not initialize nsRegion"); michael@0: return false; michael@0: } michael@0: michael@0: bool retval = mPlugin.Init(pluginFilename, ParentHandle(), michael@0: IOThreadChild::message_loop(), michael@0: IOThreadChild::channel()); michael@0: #if defined(XP_MACOSX) michael@0: if (nsCocoaFeatures::OnYosemiteOrLater()) { michael@0: // Explicitly turn off CGEvent logging. This works around bug 1092855. michael@0: // If there are already CGEvents in the log, turning off logging also michael@0: // causes those events to be written to disk. But at this point no michael@0: // CGEvents have yet been processed. CGEvents are events (usually michael@0: // input events) pulled from the WindowServer. An option of 0x80000008 michael@0: // turns on CGEvent logging. michael@0: CGSSetDebugOptions(0x80000007); michael@0: } michael@0: #endif michael@0: return retval; michael@0: } michael@0: michael@0: void michael@0: PluginProcessChild::CleanUp() michael@0: { michael@0: #ifdef XP_WIN michael@0: ::OleUninitialize(); michael@0: #endif michael@0: nsRegion::ShutdownStatic(); michael@0: } michael@0: michael@0: } // namespace plugins michael@0: } // namespace mozilla