dom/plugins/ipc/PluginHangUIParent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "PluginHangUI.h"
michael@0 8
michael@0 9 #include "PluginHangUIParent.h"
michael@0 10
michael@0 11 #include "mozilla/Telemetry.h"
michael@0 12 #include "mozilla/plugins/PluginModuleParent.h"
michael@0 13
michael@0 14 #include "nsContentUtils.h"
michael@0 15 #include "nsDirectoryServiceDefs.h"
michael@0 16 #include "nsIFile.h"
michael@0 17 #include "nsIProperties.h"
michael@0 18 #include "nsIWindowMediator.h"
michael@0 19 #include "nsIWinTaskbar.h"
michael@0 20 #include "nsServiceManagerUtils.h"
michael@0 21 #include "nsThreadUtils.h"
michael@0 22
michael@0 23 #include "WidgetUtils.h"
michael@0 24
michael@0 25 #define NS_TASKBAR_CONTRACTID "@mozilla.org/windows-taskbar;1"
michael@0 26
michael@0 27 using base::ProcessHandle;
michael@0 28
michael@0 29 using mozilla::widget::WidgetUtils;
michael@0 30
michael@0 31 using std::string;
michael@0 32 using std::vector;
michael@0 33
michael@0 34 namespace {
michael@0 35 class nsPluginHangUITelemetry : public nsRunnable
michael@0 36 {
michael@0 37 public:
michael@0 38 nsPluginHangUITelemetry(int aResponseCode, int aDontAskCode,
michael@0 39 uint32_t aResponseTimeMs, uint32_t aTimeoutMs)
michael@0 40 : mResponseCode(aResponseCode),
michael@0 41 mDontAskCode(aDontAskCode),
michael@0 42 mResponseTimeMs(aResponseTimeMs),
michael@0 43 mTimeoutMs(aTimeoutMs)
michael@0 44 {
michael@0 45 }
michael@0 46
michael@0 47 NS_IMETHOD
michael@0 48 Run()
michael@0 49 {
michael@0 50 mozilla::Telemetry::Accumulate(
michael@0 51 mozilla::Telemetry::PLUGIN_HANG_UI_USER_RESPONSE, mResponseCode);
michael@0 52 mozilla::Telemetry::Accumulate(
michael@0 53 mozilla::Telemetry::PLUGIN_HANG_UI_DONT_ASK, mDontAskCode);
michael@0 54 mozilla::Telemetry::Accumulate(
michael@0 55 mozilla::Telemetry::PLUGIN_HANG_UI_RESPONSE_TIME, mResponseTimeMs);
michael@0 56 mozilla::Telemetry::Accumulate(
michael@0 57 mozilla::Telemetry::PLUGIN_HANG_TIME, mTimeoutMs + mResponseTimeMs);
michael@0 58 return NS_OK;
michael@0 59 }
michael@0 60
michael@0 61 private:
michael@0 62 int mResponseCode;
michael@0 63 int mDontAskCode;
michael@0 64 uint32_t mResponseTimeMs;
michael@0 65 uint32_t mTimeoutMs;
michael@0 66 };
michael@0 67 } // anonymous namespace
michael@0 68
michael@0 69 namespace mozilla {
michael@0 70 namespace plugins {
michael@0 71
michael@0 72 PluginHangUIParent::PluginHangUIParent(PluginModuleParent* aModule,
michael@0 73 const int32_t aHangUITimeoutPref,
michael@0 74 const int32_t aChildTimeoutPref)
michael@0 75 : mMutex("mozilla::plugins::PluginHangUIParent::mMutex"),
michael@0 76 mModule(aModule),
michael@0 77 mTimeoutPrefMs(static_cast<uint32_t>(aHangUITimeoutPref) * 1000U),
michael@0 78 mIPCTimeoutMs(static_cast<uint32_t>(aChildTimeoutPref) * 1000U),
michael@0 79 mMainThreadMessageLoop(MessageLoop::current()),
michael@0 80 mIsShowing(false),
michael@0 81 mLastUserResponse(0),
michael@0 82 mHangUIProcessHandle(nullptr),
michael@0 83 mMainWindowHandle(nullptr),
michael@0 84 mRegWait(nullptr),
michael@0 85 mShowEvent(nullptr),
michael@0 86 mShowTicks(0),
michael@0 87 mResponseTicks(0)
michael@0 88 {
michael@0 89 }
michael@0 90
michael@0 91 PluginHangUIParent::~PluginHangUIParent()
michael@0 92 {
michael@0 93 { // Scope for lock
michael@0 94 MutexAutoLock lock(mMutex);
michael@0 95 UnwatchHangUIChildProcess(true);
michael@0 96 }
michael@0 97 if (mShowEvent) {
michael@0 98 ::CloseHandle(mShowEvent);
michael@0 99 }
michael@0 100 if (mHangUIProcessHandle) {
michael@0 101 ::CloseHandle(mHangUIProcessHandle);
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 bool
michael@0 106 PluginHangUIParent::DontShowAgain() const
michael@0 107 {
michael@0 108 return (mLastUserResponse & HANGUI_USER_RESPONSE_DONT_SHOW_AGAIN);
michael@0 109 }
michael@0 110
michael@0 111 bool
michael@0 112 PluginHangUIParent::WasLastHangStopped() const
michael@0 113 {
michael@0 114 return (mLastUserResponse & HANGUI_USER_RESPONSE_STOP);
michael@0 115 }
michael@0 116
michael@0 117 unsigned int
michael@0 118 PluginHangUIParent::LastShowDurationMs() const
michael@0 119 {
michael@0 120 // We only return something if there was a user response
michael@0 121 if (!mLastUserResponse) {
michael@0 122 return 0;
michael@0 123 }
michael@0 124 return static_cast<unsigned int>(mResponseTicks - mShowTicks);
michael@0 125 }
michael@0 126
michael@0 127 bool
michael@0 128 PluginHangUIParent::Init(const nsString& aPluginName)
michael@0 129 {
michael@0 130 if (mHangUIProcessHandle) {
michael@0 131 return false;
michael@0 132 }
michael@0 133
michael@0 134 nsresult rv;
michael@0 135 rv = mMiniShm.Init(this, ::IsDebuggerPresent() ? INFINITE : mIPCTimeoutMs);
michael@0 136 NS_ENSURE_SUCCESS(rv, false);
michael@0 137 nsCOMPtr<nsIProperties>
michael@0 138 directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
michael@0 139 if (!directoryService) {
michael@0 140 return false;
michael@0 141 }
michael@0 142 nsCOMPtr<nsIFile> greDir;
michael@0 143 rv = directoryService->Get(NS_GRE_DIR,
michael@0 144 NS_GET_IID(nsIFile),
michael@0 145 getter_AddRefs(greDir));
michael@0 146 if (NS_FAILED(rv)) {
michael@0 147 return false;
michael@0 148 }
michael@0 149 nsAutoString path;
michael@0 150 greDir->GetPath(path);
michael@0 151
michael@0 152 FilePath exePath(path.get());
michael@0 153 exePath = exePath.AppendASCII(MOZ_HANGUI_PROCESS_NAME);
michael@0 154 CommandLine commandLine(exePath.value());
michael@0 155
michael@0 156 nsXPIDLString localizedStr;
michael@0 157 const char16_t* formatParams[] = { aPluginName.get() };
michael@0 158 rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
michael@0 159 "PluginHangUIMessage",
michael@0 160 formatParams,
michael@0 161 localizedStr);
michael@0 162 if (NS_FAILED(rv)) {
michael@0 163 return false;
michael@0 164 }
michael@0 165 commandLine.AppendLooseValue(localizedStr.get());
michael@0 166
michael@0 167 const char* keys[] = { "PluginHangUITitle",
michael@0 168 "PluginHangUIWaitButton",
michael@0 169 "PluginHangUIStopButton",
michael@0 170 "DontAskAgain" };
michael@0 171 for (unsigned int i = 0; i < ArrayLength(keys); ++i) {
michael@0 172 rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
michael@0 173 keys[i],
michael@0 174 localizedStr);
michael@0 175 if (NS_FAILED(rv)) {
michael@0 176 return false;
michael@0 177 }
michael@0 178 commandLine.AppendLooseValue(localizedStr.get());
michael@0 179 }
michael@0 180
michael@0 181 rv = GetHangUIOwnerWindowHandle(mMainWindowHandle);
michael@0 182 if (NS_FAILED(rv)) {
michael@0 183 return false;
michael@0 184 }
michael@0 185 nsAutoString hwndStr;
michael@0 186 hwndStr.AppendPrintf("%p", mMainWindowHandle);
michael@0 187 commandLine.AppendLooseValue(hwndStr.get());
michael@0 188
michael@0 189 ScopedHandle procHandle(::OpenProcess(SYNCHRONIZE,
michael@0 190 TRUE,
michael@0 191 GetCurrentProcessId()));
michael@0 192 if (!procHandle.IsValid()) {
michael@0 193 return false;
michael@0 194 }
michael@0 195 nsAutoString procHandleStr;
michael@0 196 procHandleStr.AppendPrintf("%p", procHandle.Get());
michael@0 197 commandLine.AppendLooseValue(procHandleStr.get());
michael@0 198
michael@0 199 // On Win7+, pass the application user model to the child, so it can
michael@0 200 // register with it. This insures windows created by the Hang UI
michael@0 201 // properly group with the parent app on the Win7 taskbar.
michael@0 202 nsCOMPtr<nsIWinTaskbar> taskbarInfo = do_GetService(NS_TASKBAR_CONTRACTID);
michael@0 203 if (taskbarInfo) {
michael@0 204 bool isSupported = false;
michael@0 205 taskbarInfo->GetAvailable(&isSupported);
michael@0 206 nsAutoString appId;
michael@0 207 if (isSupported && NS_SUCCEEDED(taskbarInfo->GetDefaultGroupId(appId))) {
michael@0 208 commandLine.AppendLooseValue(appId.get());
michael@0 209 } else {
michael@0 210 commandLine.AppendLooseValue(L"-");
michael@0 211 }
michael@0 212 } else {
michael@0 213 commandLine.AppendLooseValue(L"-");
michael@0 214 }
michael@0 215
michael@0 216 nsAutoString ipcTimeoutStr;
michael@0 217 ipcTimeoutStr.AppendInt(mIPCTimeoutMs);
michael@0 218 commandLine.AppendLooseValue(ipcTimeoutStr.get());
michael@0 219
michael@0 220 std::wstring ipcCookie;
michael@0 221 rv = mMiniShm.GetCookie(ipcCookie);
michael@0 222 if (NS_FAILED(rv)) {
michael@0 223 return false;
michael@0 224 }
michael@0 225 commandLine.AppendLooseValue(ipcCookie);
michael@0 226
michael@0 227 ScopedHandle showEvent(::CreateEventW(nullptr, FALSE, FALSE, nullptr));
michael@0 228 if (!showEvent.IsValid()) {
michael@0 229 return false;
michael@0 230 }
michael@0 231 mShowEvent = showEvent.Get();
michael@0 232
michael@0 233 MutexAutoLock lock(mMutex);
michael@0 234 STARTUPINFO startupInfo = { sizeof(STARTUPINFO) };
michael@0 235 PROCESS_INFORMATION processInfo = { nullptr };
michael@0 236 BOOL isProcessCreated = ::CreateProcess(exePath.value().c_str(),
michael@0 237 const_cast<wchar_t*>(commandLine.command_line_string().c_str()),
michael@0 238 nullptr,
michael@0 239 nullptr,
michael@0 240 TRUE,
michael@0 241 DETACHED_PROCESS,
michael@0 242 nullptr,
michael@0 243 nullptr,
michael@0 244 &startupInfo,
michael@0 245 &processInfo);
michael@0 246 if (isProcessCreated) {
michael@0 247 ::CloseHandle(processInfo.hThread);
michael@0 248 mHangUIProcessHandle = processInfo.hProcess;
michael@0 249 ::RegisterWaitForSingleObject(&mRegWait,
michael@0 250 processInfo.hProcess,
michael@0 251 &SOnHangUIProcessExit,
michael@0 252 this,
michael@0 253 INFINITE,
michael@0 254 WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE);
michael@0 255 ::WaitForSingleObject(mShowEvent, ::IsDebuggerPresent() ? INFINITE
michael@0 256 : mIPCTimeoutMs);
michael@0 257 // Setting this to true even if we time out on mShowEvent. This timeout
michael@0 258 // typically occurs when the machine is thrashing so badly that
michael@0 259 // plugin-hang-ui.exe is taking a while to start. If we didn't set
michael@0 260 // this to true, Firefox would keep spawning additional plugin-hang-ui
michael@0 261 // processes, which is not what we want.
michael@0 262 mIsShowing = true;
michael@0 263 }
michael@0 264 mShowEvent = nullptr;
michael@0 265 return !(!isProcessCreated);
michael@0 266 }
michael@0 267
michael@0 268 // static
michael@0 269 VOID CALLBACK PluginHangUIParent::SOnHangUIProcessExit(PVOID aContext,
michael@0 270 BOOLEAN aIsTimer)
michael@0 271 {
michael@0 272 PluginHangUIParent* object = static_cast<PluginHangUIParent*>(aContext);
michael@0 273 MutexAutoLock lock(object->mMutex);
michael@0 274 // If the Hang UI child process died unexpectedly, act as if the UI cancelled
michael@0 275 if (object->IsShowing()) {
michael@0 276 object->RecvUserResponse(HANGUI_USER_RESPONSE_CANCEL);
michael@0 277 // Firefox window was disabled automatically when the Hang UI was shown.
michael@0 278 // If plugin-hang-ui.exe was unexpectedly terminated, we need to re-enable.
michael@0 279 ::EnableWindow(object->mMainWindowHandle, TRUE);
michael@0 280 }
michael@0 281 }
michael@0 282
michael@0 283 // A precondition for this function is that the caller has locked mMutex
michael@0 284 bool
michael@0 285 PluginHangUIParent::UnwatchHangUIChildProcess(bool aWait)
michael@0 286 {
michael@0 287 mMutex.AssertCurrentThreadOwns();
michael@0 288 if (mRegWait) {
michael@0 289 // If aWait is false then we want to pass a nullptr (i.e. default
michael@0 290 // constructor) completionEvent
michael@0 291 ScopedHandle completionEvent;
michael@0 292 if (aWait) {
michael@0 293 completionEvent.Set(::CreateEventW(nullptr, FALSE, FALSE, nullptr));
michael@0 294 if (!completionEvent.IsValid()) {
michael@0 295 return false;
michael@0 296 }
michael@0 297 }
michael@0 298
michael@0 299 // if aWait == false and UnregisterWaitEx fails with ERROR_IO_PENDING,
michael@0 300 // it is okay to clear mRegWait; Windows is telling us that the wait's
michael@0 301 // callback is running but will be cleaned up once the callback returns.
michael@0 302 if (::UnregisterWaitEx(mRegWait, completionEvent) ||
michael@0 303 !aWait && ::GetLastError() == ERROR_IO_PENDING) {
michael@0 304 mRegWait = nullptr;
michael@0 305 if (aWait) {
michael@0 306 // We must temporarily unlock mMutex while waiting for the registered
michael@0 307 // wait callback to complete, or else we could deadlock.
michael@0 308 MutexAutoUnlock unlock(mMutex);
michael@0 309 ::WaitForSingleObject(completionEvent, INFINITE);
michael@0 310 }
michael@0 311 return true;
michael@0 312 }
michael@0 313 }
michael@0 314 return false;
michael@0 315 }
michael@0 316
michael@0 317 bool
michael@0 318 PluginHangUIParent::Cancel()
michael@0 319 {
michael@0 320 MutexAutoLock lock(mMutex);
michael@0 321 bool result = mIsShowing && SendCancel();
michael@0 322 if (result) {
michael@0 323 mIsShowing = false;
michael@0 324 }
michael@0 325 return result;
michael@0 326 }
michael@0 327
michael@0 328 bool
michael@0 329 PluginHangUIParent::SendCancel()
michael@0 330 {
michael@0 331 PluginHangUICommand* cmd = nullptr;
michael@0 332 nsresult rv = mMiniShm.GetWritePtr(cmd);
michael@0 333 if (NS_FAILED(rv)) {
michael@0 334 return false;
michael@0 335 }
michael@0 336 cmd->mCode = PluginHangUICommand::HANGUI_CMD_CANCEL;
michael@0 337 return NS_SUCCEEDED(mMiniShm.Send());
michael@0 338 }
michael@0 339
michael@0 340 // A precondition for this function is that the caller has locked mMutex
michael@0 341 bool
michael@0 342 PluginHangUIParent::RecvUserResponse(const unsigned int& aResponse)
michael@0 343 {
michael@0 344 mMutex.AssertCurrentThreadOwns();
michael@0 345 if (!mIsShowing && !(aResponse & HANGUI_USER_RESPONSE_CANCEL)) {
michael@0 346 // Don't process a user response if a cancellation is already pending
michael@0 347 return true;
michael@0 348 }
michael@0 349 mLastUserResponse = aResponse;
michael@0 350 mResponseTicks = ::GetTickCount();
michael@0 351 mIsShowing = false;
michael@0 352 // responseCode: 1 = Stop, 2 = Continue, 3 = Cancel
michael@0 353 int responseCode;
michael@0 354 if (aResponse & HANGUI_USER_RESPONSE_STOP) {
michael@0 355 // User clicked Stop
michael@0 356 mModule->TerminateChildProcess(mMainThreadMessageLoop);
michael@0 357 responseCode = 1;
michael@0 358 } else if(aResponse & HANGUI_USER_RESPONSE_CONTINUE) {
michael@0 359 // User clicked Continue
michael@0 360 responseCode = 2;
michael@0 361 } else {
michael@0 362 // Dialog was cancelled
michael@0 363 responseCode = 3;
michael@0 364 }
michael@0 365 int dontAskCode = (aResponse & HANGUI_USER_RESPONSE_DONT_SHOW_AGAIN) ? 1 : 0;
michael@0 366 nsCOMPtr<nsIRunnable> workItem = new nsPluginHangUITelemetry(responseCode,
michael@0 367 dontAskCode,
michael@0 368 LastShowDurationMs(),
michael@0 369 mTimeoutPrefMs);
michael@0 370 NS_DispatchToMainThread(workItem, NS_DISPATCH_NORMAL);
michael@0 371 return true;
michael@0 372 }
michael@0 373
michael@0 374 nsresult
michael@0 375 PluginHangUIParent::GetHangUIOwnerWindowHandle(NativeWindowHandle& windowHandle)
michael@0 376 {
michael@0 377 windowHandle = nullptr;
michael@0 378
michael@0 379 nsresult rv;
michael@0 380 nsCOMPtr<nsIWindowMediator> winMediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID,
michael@0 381 &rv));
michael@0 382 NS_ENSURE_SUCCESS(rv, rv);
michael@0 383
michael@0 384 nsCOMPtr<nsIDOMWindow> navWin;
michael@0 385 rv = winMediator->GetMostRecentWindow(MOZ_UTF16("navigator:browser"),
michael@0 386 getter_AddRefs(navWin));
michael@0 387 NS_ENSURE_SUCCESS(rv, rv);
michael@0 388 if (!navWin) {
michael@0 389 return NS_ERROR_FAILURE;
michael@0 390 }
michael@0 391
michael@0 392 nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(navWin);
michael@0 393 if (!widget) {
michael@0 394 return NS_ERROR_FAILURE;
michael@0 395 }
michael@0 396
michael@0 397 windowHandle = reinterpret_cast<NativeWindowHandle>(widget->GetNativeData(NS_NATIVE_WINDOW));
michael@0 398 if (!windowHandle) {
michael@0 399 return NS_ERROR_FAILURE;
michael@0 400 }
michael@0 401
michael@0 402 return NS_OK;
michael@0 403 }
michael@0 404
michael@0 405 void
michael@0 406 PluginHangUIParent::OnMiniShmEvent(MiniShmBase *aMiniShmObj)
michael@0 407 {
michael@0 408 const PluginHangUIResponse* response = nullptr;
michael@0 409 nsresult rv = aMiniShmObj->GetReadPtr(response);
michael@0 410 NS_ASSERTION(NS_SUCCEEDED(rv),
michael@0 411 "Couldn't obtain read pointer OnMiniShmEvent");
michael@0 412 if (NS_SUCCEEDED(rv)) {
michael@0 413 // The child process has returned a response so we shouldn't worry about
michael@0 414 // its state anymore.
michael@0 415 MutexAutoLock lock(mMutex);
michael@0 416 UnwatchHangUIChildProcess(false);
michael@0 417 RecvUserResponse(response->mResponseBits);
michael@0 418 }
michael@0 419 }
michael@0 420
michael@0 421 void
michael@0 422 PluginHangUIParent::OnMiniShmConnect(MiniShmBase* aMiniShmObj)
michael@0 423 {
michael@0 424 PluginHangUICommand* cmd = nullptr;
michael@0 425 nsresult rv = aMiniShmObj->GetWritePtr(cmd);
michael@0 426 NS_ASSERTION(NS_SUCCEEDED(rv),
michael@0 427 "Couldn't obtain write pointer OnMiniShmConnect");
michael@0 428 if (NS_FAILED(rv)) {
michael@0 429 return;
michael@0 430 }
michael@0 431 cmd->mCode = PluginHangUICommand::HANGUI_CMD_SHOW;
michael@0 432 if (NS_SUCCEEDED(aMiniShmObj->Send())) {
michael@0 433 mShowTicks = ::GetTickCount();
michael@0 434 }
michael@0 435 ::SetEvent(mShowEvent);
michael@0 436 }
michael@0 437
michael@0 438 } // namespace plugins
michael@0 439 } // namespace mozilla

mercurial