1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ProcessHangMonitor.h"
#include "mozilla/ProcessHangMonitorIPC.h"
#include "jsapi.h"
#include "xpcprivate.h"
#include "mozilla/Atomics.h"
#include "mozilla/BackgroundHangMonitor.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/CancelContentJSOptionsBinding.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/ipc/TaskFactory.h"
#include "mozilla/Monitor.h"
#include "mozilla/plugins/PluginBridge.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/Unused.h"
#include "mozilla/WeakPtr.h"
#include "nsExceptionHandler.h"
#include "nsFrameLoader.h"
#include "nsIHangReport.h"
#include "nsIRemoteTab.h"
#include "nsNetUtil.h"
#include "nsQueryObject.h"
#include "nsPluginHost.h"
#include "nsThreadUtils.h"
#include "base/task.h"
#include "base/thread.h"
#ifdef XP_WIN
// For IsDebuggerPresent()
# include <windows.h>
#endif
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::ipc;
/*
* Basic architecture:
*
* Each process has its own ProcessHangMonitor singleton. This singleton exists
* as long as there is at least one content process in the system. Each content
* process has a HangMonitorChild and the chrome process has one
* HangMonitorParent per process. Each process (including the chrome process)
* runs a hang monitoring thread. The PHangMonitor actors are bound to this
* thread so that they never block on the main thread.
*
* When the content process detects a hang, it posts a task to its hang thread,
* which sends an IPC message to the hang thread in the parent. The parent
* cancels any ongoing CPOW requests and then posts a runnable to the main
* thread that notifies Firefox frontend code of the hang. The frontend code is
* passed an nsIHangReport, which can be used to terminate the hang.
*
* If the user chooses to terminate a script, a task is posted to the chrome
* process's hang monitoring thread, which sends an IPC message to the hang
* thread in the content process. That thread sets a flag to indicate that JS
* execution should be terminated the next time it hits the interrupt
* callback. A similar scheme is used for debugging slow scripts. If a content
* process or plug-in needs to be terminated, the chrome process does so
* directly, without messaging the content process.
*/
namespace {
/* Child process objects */
class HangMonitorChild : public PProcessHangMonitorChild,
public BackgroundHangAnnotator {
public:
explicit HangMonitorChild(ProcessHangMonitor* aMonitor);
~HangMonitorChild() override;
void Bind(Endpoint<PProcessHangMonitorChild>&& aEndpoint);
typedef ProcessHangMonitor::SlowScriptAction SlowScriptAction;
SlowScriptAction NotifySlowScript(nsIBrowserChild* aBrowserChild,
const char* aFileName,
const nsString& aAddonId);
void NotifySlowScriptAsync(TabId aTabId, const nsCString& aFileName,
const nsString& aAddonId);
bool IsDebuggerStartupComplete();
void NotifyPluginHang(uint32_t aPluginId);
void NotifyPluginHangAsync(uint32_t aPluginId);
void ClearHang();
void ClearHangAsync();
void ClearPaintWhileInterruptingJS(const LayersObserverEpoch& aEpoch);
// MaybeStartPaintWhileInterruptingJS will notify the background hang monitor
// of activity if this is the first time calling it since
// ClearPaintWhileInterruptingJS. It should be callable from any thread, but
// you must be holding mMonitor if using it off the main thread, since it
// could race with ClearPaintWhileInterruptingJS.
void MaybeStartPaintWhileInterruptingJS();
mozilla::ipc::IPCResult RecvTerminateScript(
const bool& aTerminateGlobal) override;
mozilla::ipc::IPCResult RecvBeginStartingDebugger() override;
mozilla::ipc::IPCResult RecvEndStartingDebugger() override;
mozilla::ipc::IPCResult RecvPaintWhileInterruptingJS(
const TabId& aTabId, const LayersObserverEpoch& aEpoch) override;
mozilla::ipc::IPCResult RecvCancelContentJSExecutionIfRunning(
const TabId& aTabId, const nsIRemoteTab::NavigationType& aNavigationType,
const int32_t& aNavigationIndex,
const mozilla::Maybe<nsCString>& aNavigationURI,
const int32_t& aEpoch) override;
void ActorDestroy(ActorDestroyReason aWhy) override;
bool InterruptCallback();
void Shutdown();
static HangMonitorChild* Get() { return sInstance; }
void Dispatch(already_AddRefed<nsIRunnable> aRunnable) {
mHangMonitor->Dispatch(std::move(aRunnable));
}
bool IsOnThread() { return mHangMonitor->IsOnThread(); }
void AnnotateHang(BackgroundHangAnnotations& aAnnotations) override;
protected:
friend class mozilla::ProcessHangMonitor;
static Maybe<Monitor> sMonitor;
static Atomic<bool, SequentiallyConsistent> sInitializing;
private:
void ShutdownOnThread();
static Atomic<HangMonitorChild*, SequentiallyConsistent> sInstance;
const RefPtr<ProcessHangMonitor> mHangMonitor;
Monitor mMonitor;
// Main thread-only.
bool mSentReport;
// These fields must be accessed with mMonitor held.
bool mTerminateScript;
bool mTerminateGlobal;
bool mStartDebugger;
bool mFinishedStartingDebugger;
bool mPaintWhileInterruptingJS;
TabId mPaintWhileInterruptingJSTab;
MOZ_INIT_OUTSIDE_CTOR LayersObserverEpoch mPaintWhileInterruptingJSEpoch;
bool mCancelContentJS;
TabId mCancelContentJSTab;
nsIRemoteTab::NavigationType mCancelContentJSNavigationType;
int32_t mCancelContentJSNavigationIndex;
mozilla::Maybe<nsCString> mCancelContentJSNavigationURI;
int32_t mCancelContentJSEpoch;
JSContext* mContext;
bool mShutdownDone;
// This field is only accessed on the hang thread.
bool mIPCOpen;
// Allows us to ensure we NotifyActivity only once, allowing
// either thread to do so.
Atomic<bool> mPaintWhileInterruptingJSActive;
};
Maybe<Monitor> HangMonitorChild::sMonitor;
Atomic<bool, SequentiallyConsistent> HangMonitorChild::sInitializing;
Atomic<HangMonitorChild*, SequentiallyConsistent> HangMonitorChild::sInstance;
/* Parent process objects */
class HangMonitorParent;
class HangMonitoredProcess final : public nsIHangReport {
public:
NS_DECL_THREADSAFE_ISUPPORTS
HangMonitoredProcess(HangMonitorParent* aActor, ContentParent* aContentParent)
: mActor(aActor), mContentParent(aContentParent) {}
NS_DECL_NSIHANGREPORT
// Called when a content process shuts down.
void Clear() {
mContentParent = nullptr;
mActor = nullptr;
}
/**
* Sets the information associated with this hang: this includes the ID of
* the plugin which caused the hang as well as the content PID. The ID of
* a minidump taken during the hang can also be provided.
*
* @param aHangData The hang information
* @param aDumpId The ID of a minidump taken when the hang occurred
*/
void SetHangData(const HangData& aHangData, const nsAString& aDumpId) {
mHangData = aHangData;
mDumpId = aDumpId;
}
void ClearHang() {
mHangData = HangData();
mDumpId.Truncate();
}
private:
~HangMonitoredProcess() = default;
// Everything here is main thread-only.
HangMonitorParent* mActor;
ContentParent* mContentParent;
HangData mHangData;
nsAutoString mDumpId;
};
class HangMonitorParent : public PProcessHangMonitorParent,
public SupportsWeakPtr<HangMonitorParent> {
public:
explicit HangMonitorParent(ProcessHangMonitor* aMonitor);
~HangMonitorParent() override;
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(HangMonitorParent)
void Bind(Endpoint<PProcessHangMonitorParent>&& aEndpoint);
mozilla::ipc::IPCResult RecvHangEvidence(const HangData& aHangData) override;
mozilla::ipc::IPCResult RecvClearHang() override;
void ActorDestroy(ActorDestroyReason aWhy) override;
void SetProcess(HangMonitoredProcess* aProcess) { mProcess = aProcess; }
void Shutdown();
void PaintWhileInterruptingJS(dom::BrowserParent* aBrowserParent,
const LayersObserverEpoch& aEpoch);
void CancelContentJSExecutionIfRunning(
dom::BrowserParent* aBrowserParent,
nsIRemoteTab::NavigationType aNavigationType,
const dom::CancelContentJSOptions& aCancelContentJSOptions);
void TerminateScript(bool aTerminateGlobal);
void BeginStartingDebugger();
void EndStartingDebugger();
void CleanupPluginHang(uint32_t aPluginId, bool aRemoveFiles);
/**
* Update the dump for the specified plugin. This method is thread-safe and
* is used to replace a browser minidump with a full minidump. If aDumpId is
* empty this is a no-op.
*/
void UpdateMinidump(uint32_t aPluginId, const nsString& aDumpId);
void Dispatch(already_AddRefed<nsIRunnable> aRunnable) {
mHangMonitor->Dispatch(std::move(aRunnable));
}
bool IsOnThread() { return mHangMonitor->IsOnThread(); }
private:
bool TakeBrowserMinidump(const PluginHangData& aPhd, nsString& aCrashId);
void SendHangNotification(const HangData& aHangData,
const nsString& aBrowserDumpId, bool aTakeMinidump);
void ClearHangNotification();
void PaintWhileInterruptingJSOnThread(TabId aTabId,
const LayersObserverEpoch& aEpoch);
void CancelContentJSExecutionIfRunningOnThread(
TabId aTabId, nsIRemoteTab::NavigationType aNavigationType,
int32_t aNavigationIndex, nsIURI* aNavigationURI, int32_t aEpoch);
void ShutdownOnThread();
const RefPtr<ProcessHangMonitor> mHangMonitor;
// This field is only accessed on the hang thread.
bool mIPCOpen;
Monitor mMonitor;
// Must be accessed with mMonitor held.
RefPtr<HangMonitoredProcess> mProcess;
bool mShutdownDone;
// Map from plugin ID to crash dump ID. Protected by
// mBrowserCrashDumpHashLock.
nsDataHashtable<nsUint32HashKey, nsString> mBrowserCrashDumpIds;
Mutex mBrowserCrashDumpHashLock;
mozilla::ipc::TaskFactory<HangMonitorParent> mMainThreadTaskFactory;
};
} // namespace
/* HangMonitorChild implementation */
HangMonitorChild::HangMonitorChild(ProcessHangMonitor* aMonitor)
: mHangMonitor(aMonitor),
mMonitor("HangMonitorChild lock"),
mSentReport(false),
mTerminateScript(false),
mTerminateGlobal(false),
mStartDebugger(false),
mFinishedStartingDebugger(false),
mPaintWhileInterruptingJS(false),
mCancelContentJS(false),
mCancelContentJSNavigationType(nsIRemoteTab::NAVIGATE_BACK),
mCancelContentJSNavigationIndex(0),
mCancelContentJSEpoch(0),
mShutdownDone(false),
mIPCOpen(true),
mPaintWhileInterruptingJSActive(false) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!sInstance);
mContext = danger::GetJSContext();
BackgroundHangMonitor::RegisterAnnotator(*this);
MOZ_ASSERT(!sMonitor.isSome());
sMonitor.emplace("HangMonitorChild::sMonitor");
MonitorAutoLock mal(*sMonitor);
MOZ_ASSERT(!sInitializing);
sInitializing = true;
}
HangMonitorChild::~HangMonitorChild() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_ASSERT(sInstance == this);
sInstance = nullptr;
}
bool HangMonitorChild::InterruptCallback() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
// Don't start painting if we're not in a good place to run script. We run
// chrome script during layout and such, and it wouldn't be good to interrupt
// painting code from there.
if (!nsContentUtils::IsSafeToRunScript()) {
return true;
}
bool paintWhileInterruptingJS;
TabId paintWhileInterruptingJSTab;
LayersObserverEpoch paintWhileInterruptingJSEpoch;
{
MonitorAutoLock lock(mMonitor);
paintWhileInterruptingJS = mPaintWhileInterruptingJS;
paintWhileInterruptingJSTab = mPaintWhileInterruptingJSTab;
paintWhileInterruptingJSEpoch = mPaintWhileInterruptingJSEpoch;
mPaintWhileInterruptingJS = false;
}
if (paintWhileInterruptingJS) {
RefPtr<BrowserChild> browserChild =
BrowserChild::FindBrowserChild(paintWhileInterruptingJSTab);
if (browserChild) {
js::AutoAssertNoContentJS nojs(mContext);
browserChild->PaintWhileInterruptingJS(paintWhileInterruptingJSEpoch);
}
}
// Only handle the interrupt for cancelling content JS if we have a
// non-privileged script (i.e. not part of Gecko or an add-on).
JS::RootedObject global(mContext, JS::CurrentGlobalOrNull(mContext));
nsIPrincipal* principal = xpc::GetObjectPrincipal(global);
if (principal && (principal->IsSystemPrincipal() ||
principal->GetIsAddonOrExpandedAddonPrincipal())) {
return true;
}
nsCOMPtr<nsPIDOMWindowInner> win = xpc::WindowOrNull(global);
if (!win) {
return true;
}
bool cancelContentJS;
TabId cancelContentJSTab;
nsIRemoteTab::NavigationType cancelContentJSNavigationType;
int32_t cancelContentJSNavigationIndex;
mozilla::Maybe<nsCString> cancelContentJSNavigationURI;
int32_t cancelContentJSEpoch;
{
MonitorAutoLock lock(mMonitor);
cancelContentJS = mCancelContentJS;
cancelContentJSTab = mCancelContentJSTab;
cancelContentJSNavigationType = mCancelContentJSNavigationType;
cancelContentJSNavigationIndex = mCancelContentJSNavigationIndex;
cancelContentJSNavigationURI = std::move(mCancelContentJSNavigationURI);
cancelContentJSEpoch = mCancelContentJSEpoch;
mCancelContentJS = false;
}
if (cancelContentJS) {
js::AutoAssertNoContentJS nojs(mContext);
RefPtr<BrowserChild> browserChild =
BrowserChild::FindBrowserChild(cancelContentJSTab);
RefPtr<BrowserChild> browserChildFromWin = BrowserChild::GetFrom(win);
if (!browserChild || !browserChildFromWin) {
return true;
}
TabId tabIdFromWin = browserChildFromWin->GetTabId();
if (tabIdFromWin != cancelContentJSTab) {
// The currently-executing content JS doesn't belong to the tab that
// requested cancellation of JS. Just return and let the JS continue.
return true;
}
nsresult rv;
nsCOMPtr<nsIURI> uri;
if (cancelContentJSNavigationURI) {
rv = NS_NewURI(getter_AddRefs(uri), cancelContentJSNavigationURI.value());
if (NS_FAILED(rv)) {
return true;
}
}
bool canCancel;
rv = browserChild->CanCancelContentJS(cancelContentJSNavigationType,
cancelContentJSNavigationIndex, uri,
cancelContentJSEpoch, &canCancel);
if (NS_SUCCEEDED(rv) && canCancel) {
// Don't add this page to the BF cache, since we're cancelling its JS.
if (Document* doc = win->GetExtantDoc()) {
if (Document* topLevelDoc = doc->GetTopLevelContentDocument()) {
topLevelDoc->DisallowBFCaching();
}
}
return false;
}
}
return true;
}
void HangMonitorChild::AnnotateHang(BackgroundHangAnnotations& aAnnotations) {
if (mPaintWhileInterruptingJSActive) {
aAnnotations.AddAnnotation(NS_LITERAL_STRING("PaintWhileInterruptingJS"),
true);
}
}
void HangMonitorChild::Shutdown() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
BackgroundHangMonitor::UnregisterAnnotator(*this);
MonitorAutoLock lock(mMonitor);
while (!mShutdownDone) {
mMonitor.Wait();
}
}
void HangMonitorChild::ShutdownOnThread() {
MOZ_RELEASE_ASSERT(IsOnThread());
MonitorAutoLock lock(mMonitor);
mShutdownDone = true;
mMonitor.Notify();
}
void HangMonitorChild::ActorDestroy(ActorDestroyReason aWhy) {
MOZ_RELEASE_ASSERT(IsOnThread());
mIPCOpen = false;
// We use a task here to ensure that IPDL is finished with this
// HangMonitorChild before it gets deleted on the main thread.
Dispatch(NewNonOwningRunnableMethod("HangMonitorChild::ShutdownOnThread",
this,
&HangMonitorChild::ShutdownOnThread));
}
mozilla::ipc::IPCResult HangMonitorChild::RecvTerminateScript(
const bool& aTerminateGlobal) {
MOZ_RELEASE_ASSERT(IsOnThread());
MonitorAutoLock lock(mMonitor);
if (aTerminateGlobal) {
mTerminateGlobal = true;
} else {
mTerminateScript = true;
}
return IPC_OK();
}
mozilla::ipc::IPCResult HangMonitorChild::RecvBeginStartingDebugger() {
MOZ_RELEASE_ASSERT(IsOnThread());
MonitorAutoLock lock(mMonitor);
mStartDebugger = true;
return IPC_OK();
}
mozilla::ipc::IPCResult HangMonitorChild::RecvEndStartingDebugger() {
MOZ_RELEASE_ASSERT(IsOnThread());
MonitorAutoLock lock(mMonitor);
mFinishedStartingDebugger = true;
return IPC_OK();
}
mozilla::ipc::IPCResult HangMonitorChild::RecvPaintWhileInterruptingJS(
const TabId& aTabId, const LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(IsOnThread());
{
MonitorAutoLock lock(mMonitor);
MaybeStartPaintWhileInterruptingJS();
mPaintWhileInterruptingJS = true;
mPaintWhileInterruptingJSTab = aTabId;
mPaintWhileInterruptingJSEpoch = aEpoch;
}
JS_RequestInterruptCallback(mContext);
return IPC_OK();
}
void HangMonitorChild::MaybeStartPaintWhileInterruptingJS() {
mPaintWhileInterruptingJSActive = true;
}
void HangMonitorChild::ClearPaintWhileInterruptingJS(
const LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_RELEASE_ASSERT(XRE_IsContentProcess());
mPaintWhileInterruptingJSActive = false;
}
mozilla::ipc::IPCResult HangMonitorChild::RecvCancelContentJSExecutionIfRunning(
const TabId& aTabId, const nsIRemoteTab::NavigationType& aNavigationType,
const int32_t& aNavigationIndex,
const mozilla::Maybe<nsCString>& aNavigationURI, const int32_t& aEpoch) {
MOZ_RELEASE_ASSERT(IsOnThread());
{
MonitorAutoLock lock(mMonitor);
mCancelContentJS = true;
mCancelContentJSTab = aTabId;
mCancelContentJSNavigationType = aNavigationType;
mCancelContentJSNavigationIndex = aNavigationIndex;
mCancelContentJSNavigationURI = aNavigationURI;
mCancelContentJSEpoch = aEpoch;
}
JS_RequestInterruptCallback(mContext);
return IPC_OK();
}
void HangMonitorChild::Bind(Endpoint<PProcessHangMonitorChild>&& aEndpoint) {
MOZ_RELEASE_ASSERT(IsOnThread());
MonitorAutoLock mal(*sMonitor);
MOZ_ASSERT(!sInstance);
sInstance = this;
DebugOnly<bool> ok = aEndpoint.Bind(this);
MOZ_ASSERT(ok);
sInitializing = false;
mal.Notify();
}
void HangMonitorChild::NotifySlowScriptAsync(TabId aTabId,
const nsCString& aFileName,
const nsString& aAddonId) {
if (mIPCOpen) {
Unused << SendHangEvidence(SlowScriptData(aTabId, aFileName, aAddonId));
}
}
HangMonitorChild::SlowScriptAction HangMonitorChild::NotifySlowScript(
nsIBrowserChild* aBrowserChild, const char* aFileName,
const nsString& aAddonId) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mSentReport = true;
{
MonitorAutoLock lock(mMonitor);
if (mTerminateScript) {
mTerminateScript = false;
return SlowScriptAction::Terminate;
}
if (mTerminateGlobal) {
mTerminateGlobal = false;
return SlowScriptAction::TerminateGlobal;
}
if (mStartDebugger) {
mStartDebugger = false;
return SlowScriptAction::StartDebugger;
}
}
TabId id;
if (aBrowserChild) {
RefPtr<BrowserChild> browserChild =
static_cast<BrowserChild*>(aBrowserChild);
id = browserChild->GetTabId();
}
nsAutoCString filename(aFileName);
Dispatch(NewNonOwningRunnableMethod<TabId, nsCString, nsString>(
"HangMonitorChild::NotifySlowScriptAsync", this,
&HangMonitorChild::NotifySlowScriptAsync, id, filename, aAddonId));
return SlowScriptAction::Continue;
}
bool HangMonitorChild::IsDebuggerStartupComplete() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mMonitor);
if (mFinishedStartingDebugger) {
mFinishedStartingDebugger = false;
return true;
}
return false;
}
void HangMonitorChild::NotifyPluginHang(uint32_t aPluginId) {
// main thread in the child
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mSentReport = true;
// bounce to background thread
Dispatch(NewNonOwningRunnableMethod<uint32_t>(
"HangMonitorChild::NotifyPluginHangAsync", this,
&HangMonitorChild::NotifyPluginHangAsync, aPluginId));
}
void HangMonitorChild::NotifyPluginHangAsync(uint32_t aPluginId) {
MOZ_RELEASE_ASSERT(IsOnThread());
// bounce back to parent on background thread
if (mIPCOpen) {
Unused << SendHangEvidence(
PluginHangData(aPluginId, base::GetCurrentProcId()));
}
}
void HangMonitorChild::ClearHang() {
MOZ_ASSERT(NS_IsMainThread());
if (mSentReport) {
// bounce to background thread
Dispatch(NewNonOwningRunnableMethod("HangMonitorChild::ClearHangAsync",
this,
&HangMonitorChild::ClearHangAsync));
MonitorAutoLock lock(mMonitor);
mSentReport = false;
mTerminateScript = false;
mTerminateGlobal = false;
mStartDebugger = false;
mFinishedStartingDebugger = false;
}
}
void HangMonitorChild::ClearHangAsync() {
MOZ_RELEASE_ASSERT(IsOnThread());
// bounce back to parent on background thread
if (mIPCOpen) {
Unused << SendClearHang();
}
}
/* HangMonitorParent implementation */
HangMonitorParent::HangMonitorParent(ProcessHangMonitor* aMonitor)
: mHangMonitor(aMonitor),
mIPCOpen(true),
mMonitor("HangMonitorParent lock"),
mShutdownDone(false),
mBrowserCrashDumpHashLock("mBrowserCrashDumpIds lock"),
mMainThreadTaskFactory(this) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
}
HangMonitorParent::~HangMonitorParent() {
MutexAutoLock lock(mBrowserCrashDumpHashLock);
for (auto iter = mBrowserCrashDumpIds.Iter(); !iter.Done(); iter.Next()) {
nsString crashId = iter.UserData();
if (!crashId.IsEmpty()) {
CrashReporter::DeleteMinidumpFilesForID(crashId);
}
}
}
void HangMonitorParent::Shutdown() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mMonitor);
if (mProcess) {
mProcess->Clear();
mProcess = nullptr;
}
Dispatch(NewNonOwningRunnableMethod("HangMonitorParent::ShutdownOnThread",
this,
&HangMonitorParent::ShutdownOnThread));
while (!mShutdownDone) {
mMonitor.Wait();
}
}
void HangMonitorParent::ShutdownOnThread() {
MOZ_RELEASE_ASSERT(IsOnThread());
// mIPCOpen is only written from this thread, so need need to take the lock
// here. We'd be shooting ourselves in the foot, because ActorDestroy takes
// it.
if (mIPCOpen) {
Close();
}
MonitorAutoLock lock(mMonitor);
mShutdownDone = true;
mMonitor.Notify();
}
void HangMonitorParent::PaintWhileInterruptingJS(
dom::BrowserParent* aTab, const LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (StaticPrefs::browser_tabs_remote_force_paint()) {
TabId id = aTab->GetTabId();
Dispatch(NewNonOwningRunnableMethod<TabId, LayersObserverEpoch>(
"HangMonitorParent::PaintWhileInterruptingJSOnThread", this,
&HangMonitorParent::PaintWhileInterruptingJSOnThread, id, aEpoch));
}
}
void HangMonitorParent::PaintWhileInterruptingJSOnThread(
TabId aTabId, const LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(IsOnThread());
if (mIPCOpen) {
Unused << SendPaintWhileInterruptingJS(aTabId, aEpoch);
}
}
void HangMonitorParent::CancelContentJSExecutionIfRunning(
dom::BrowserParent* aBrowserParent,
nsIRemoteTab::NavigationType aNavigationType,
const dom::CancelContentJSOptions& aCancelContentJSOptions) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
TabId id = aBrowserParent->GetTabId();
Dispatch(NewNonOwningRunnableMethod<TabId, nsIRemoteTab::NavigationType,
int32_t, nsIURI*, int32_t>(
"HangMonitorParent::CancelContentJSExecutionIfRunningOnThread", this,
&HangMonitorParent::CancelContentJSExecutionIfRunningOnThread, id,
aNavigationType, aCancelContentJSOptions.mIndex,
aCancelContentJSOptions.mUri, aCancelContentJSOptions.mEpoch));
}
void HangMonitorParent::CancelContentJSExecutionIfRunningOnThread(
TabId aTabId, nsIRemoteTab::NavigationType aNavigationType,
int32_t aNavigationIndex, nsIURI* aNavigationURI, int32_t aEpoch) {
MOZ_RELEASE_ASSERT(IsOnThread());
mozilla::Maybe<nsCString> spec;
if (aNavigationURI) {
nsAutoCString tmp;
nsresult rv = aNavigationURI->GetSpec(tmp);
if (NS_SUCCEEDED(rv)) {
spec.emplace(tmp);
}
}
if (mIPCOpen) {
Unused << SendCancelContentJSExecutionIfRunning(
aTabId, aNavigationType, aNavigationIndex, spec, aEpoch);
}
}
void HangMonitorParent::ActorDestroy(ActorDestroyReason aWhy) {
MOZ_RELEASE_ASSERT(IsOnThread());
mIPCOpen = false;
}
void HangMonitorParent::Bind(Endpoint<PProcessHangMonitorParent>&& aEndpoint) {
MOZ_RELEASE_ASSERT(IsOnThread());
DebugOnly<bool> ok = aEndpoint.Bind(this);
MOZ_ASSERT(ok);
}
void HangMonitorParent::SendHangNotification(const HangData& aHangData,
const nsString& aBrowserDumpId,
bool aTakeMinidump) {
// chrome process, main thread
MOZ_RELEASE_ASSERT(NS_IsMainThread());
nsString dumpId;
if ((aHangData.type() == HangData::TPluginHangData) && aTakeMinidump) {
// We've been handed a partial minidump; complete it with plugin and
// content process dumps.
const PluginHangData& phd = aHangData.get_PluginHangData();
plugins::TakeFullMinidump(phd.pluginId(), phd.contentProcessId(),
aBrowserDumpId, dumpId);
UpdateMinidump(phd.pluginId(), dumpId);
} else {
// We already have a full minidump; go ahead and use it.
dumpId = aBrowserDumpId;
}
mProcess->SetHangData(aHangData, dumpId);
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
observerService->NotifyObservers(mProcess, "process-hang-report", nullptr);
}
void HangMonitorParent::ClearHangNotification() {
// chrome process, main thread
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mProcess->ClearHang();
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
observerService->NotifyObservers(mProcess, "clear-hang-report", nullptr);
}
// Take a minidump of the browser process if one wasn't already taken for the
// plugin that caused the hang. Return false if a dump was already available or
// true if new one has been taken.
bool HangMonitorParent::TakeBrowserMinidump(const PluginHangData& aPhd,
nsString& aCrashId) {
MutexAutoLock lock(mBrowserCrashDumpHashLock);
if (!mBrowserCrashDumpIds.Get(aPhd.pluginId(), &aCrashId)) {
nsCOMPtr<nsIFile> browserDump;
if (CrashReporter::TakeMinidump(getter_AddRefs(browserDump), true)) {
if (!CrashReporter::GetIDFromMinidump(browserDump, aCrashId) ||
aCrashId.IsEmpty()) {
browserDump->Remove(false);
NS_WARNING(
"Failed to generate timely browser stack, "
"this is bad for plugin hang analysis!");
} else {
mBrowserCrashDumpIds.Put(aPhd.pluginId(), aCrashId);
return true;
}
}
}
return false;
}
mozilla::ipc::IPCResult HangMonitorParent::RecvHangEvidence(
const HangData& aHangData) {
// chrome process, background thread
MOZ_RELEASE_ASSERT(IsOnThread());
if (!StaticPrefs::dom_ipc_reportProcessHangs()) {
return IPC_OK();
}
#ifdef XP_WIN
// Don't report hangs if we're debugging the process. You can comment this
// line out for testing purposes.
if (IsDebuggerPresent()) {
return IPC_OK();
}
#endif
// Before we wake up the browser main thread we want to take a
// browser minidump.
nsAutoString crashId;
bool takeMinidump = false;
if (aHangData.type() == HangData::TPluginHangData) {
takeMinidump = TakeBrowserMinidump(aHangData.get_PluginHangData(), crashId);
}
mHangMonitor->InitiateCPOWTimeout();
MonitorAutoLock lock(mMonitor);
NS_DispatchToMainThread(mMainThreadTaskFactory.NewRunnableMethod(
&HangMonitorParent::SendHangNotification, aHangData, crashId,
takeMinidump));
return IPC_OK();
}
mozilla::ipc::IPCResult HangMonitorParent::RecvClearHang() {
// chrome process, background thread
MOZ_RELEASE_ASSERT(IsOnThread());
if (!StaticPrefs::dom_ipc_reportProcessHangs()) {
return IPC_OK();
}
mHangMonitor->InitiateCPOWTimeout();
MonitorAutoLock lock(mMonitor);
NS_DispatchToMainThread(mMainThreadTaskFactory.NewRunnableMethod(
&HangMonitorParent::ClearHangNotification));
return IPC_OK();
}
void HangMonitorParent::TerminateScript(bool aTerminateGlobal) {
MOZ_RELEASE_ASSERT(IsOnThread());
if (mIPCOpen) {
Unused << SendTerminateScript(aTerminateGlobal);
}
}
void HangMonitorParent::BeginStartingDebugger() {
MOZ_RELEASE_ASSERT(IsOnThread());
if (mIPCOpen) {
Unused << SendBeginStartingDebugger();
}
}
void HangMonitorParent::EndStartingDebugger() {
MOZ_RELEASE_ASSERT(IsOnThread());
if (mIPCOpen) {
Unused << SendEndStartingDebugger();
}
}
void HangMonitorParent::CleanupPluginHang(uint32_t aPluginId,
bool aRemoveFiles) {
MutexAutoLock lock(mBrowserCrashDumpHashLock);
nsAutoString crashId;
if (!mBrowserCrashDumpIds.Get(aPluginId, &crashId)) {
return;
}
mBrowserCrashDumpIds.Remove(aPluginId);
if (aRemoveFiles && !crashId.IsEmpty()) {
CrashReporter::DeleteMinidumpFilesForID(crashId);
}
}
void HangMonitorParent::UpdateMinidump(uint32_t aPluginId,
const nsString& aDumpId) {
if (aDumpId.IsEmpty()) {
return;
}
MutexAutoLock lock(mBrowserCrashDumpHashLock);
mBrowserCrashDumpIds.Put(aPluginId, aDumpId);
}
/* HangMonitoredProcess implementation */
NS_IMPL_ISUPPORTS(HangMonitoredProcess, nsIHangReport)
NS_IMETHODIMP
HangMonitoredProcess::GetHangType(uint32_t* aHangType) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
switch (mHangData.type()) {
case HangData::TSlowScriptData:
*aHangType = SLOW_SCRIPT;
break;
case HangData::TPluginHangData:
*aHangType = PLUGIN_HANG;
break;
default:
MOZ_ASSERT_UNREACHABLE("Unexpected HangData type");
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetScriptBrowser(Element** aBrowser) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
TabId tabId = mHangData.get_SlowScriptData().tabId();
if (!mContentParent) {
return NS_ERROR_NOT_AVAILABLE;
}
nsTArray<PBrowserParent*> tabs;
mContentParent->ManagedPBrowserParent(tabs);
for (size_t i = 0; i < tabs.Length(); i++) {
BrowserParent* tp = BrowserParent::GetFrom(tabs[i]);
if (tp->GetTabId() == tabId) {
RefPtr<Element> node = tp->GetOwnerElement();
node.forget(aBrowser);
return NS_OK;
}
}
*aBrowser = nullptr;
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetScriptFileName(nsACString& aFileName) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
aFileName = mHangData.get_SlowScriptData().filename();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetAddonId(nsAString& aAddonId) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
aAddonId = mHangData.get_SlowScriptData().addonId();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetPluginName(nsACString& aPluginName) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_ERROR_NOT_AVAILABLE;
}
uint32_t id = mHangData.get_PluginHangData().pluginId();
RefPtr<nsPluginHost> host = nsPluginHost::GetInst();
nsPluginTag* tag = host->PluginWithId(id);
if (!tag) {
return NS_ERROR_UNEXPECTED;
}
aPluginName = tag->Name();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminateScript() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->Dispatch(NewNonOwningRunnableMethod<bool>(
"HangMonitorParent::TerminateScript", mActor,
&HangMonitorParent::TerminateScript, false));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminateGlobal() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->Dispatch(NewNonOwningRunnableMethod<bool>(
"HangMonitorParent::TerminateScript", mActor,
&HangMonitorParent::TerminateScript, true));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::BeginStartingDebugger() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->Dispatch(NewNonOwningRunnableMethod(
"HangMonitorParent::BeginStartingDebugger", mActor,
&HangMonitorParent::BeginStartingDebugger));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::EndStartingDebugger() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->Dispatch(NewNonOwningRunnableMethod(
"HangMonitorParent::EndStartingDebugger", mActor,
&HangMonitorParent::EndStartingDebugger));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminatePlugin() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_ERROR_UNEXPECTED;
}
// Use the multi-process crash report generated earlier.
uint32_t id = mHangData.get_PluginHangData().pluginId();
base::ProcessId contentPid =
mHangData.get_PluginHangData().contentProcessId();
plugins::TerminatePlugin(id, contentPid, NS_LITERAL_CSTRING("HangMonitor"),
mDumpId);
if (mActor) {
mActor->CleanupPluginHang(id, false);
}
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::IsReportForBrowser(nsFrameLoader* aFrameLoader,
bool* aResult) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!mActor) {
*aResult = false;
return NS_OK;
}
NS_ENSURE_STATE(aFrameLoader);
BrowserParent* tp = BrowserParent::GetFrom(aFrameLoader);
if (!tp) {
*aResult = false;
return NS_OK;
}
*aResult = mContentParent == tp->Manager();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::UserCanceled() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_OK;
}
if (mActor) {
uint32_t id = mHangData.get_PluginHangData().pluginId();
mActor->CleanupPluginHang(id, true);
}
return NS_OK;
}
static bool InterruptCallback(JSContext* cx) {
if (HangMonitorChild* child = HangMonitorChild::Get()) {
return child->InterruptCallback();
}
return true;
}
ProcessHangMonitor* ProcessHangMonitor::sInstance;
ProcessHangMonitor::ProcessHangMonitor() : mCPOWTimeout(false) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (XRE_IsContentProcess()) {
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
obs->AddObserver(this, "xpcom-shutdown", false);
}
if (NS_FAILED(NS_NewNamedThread("ProcessHangMon", getter_AddRefs(mThread)))) {
mThread = nullptr;
}
}
ProcessHangMonitor::~ProcessHangMonitor() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_ASSERT(sInstance == this);
sInstance = nullptr;
mThread->Shutdown();
mThread = nullptr;
}
ProcessHangMonitor* ProcessHangMonitor::GetOrCreate() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!sInstance) {
sInstance = new ProcessHangMonitor();
}
return sInstance;
}
NS_IMPL_ISUPPORTS(ProcessHangMonitor, nsIObserver)
NS_IMETHODIMP
ProcessHangMonitor::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!strcmp(aTopic, "xpcom-shutdown")) {
if (HangMonitorChild::sMonitor) {
MonitorAutoLock mal(*HangMonitorChild::sMonitor);
if (HangMonitorChild::sInitializing) {
mal.Wait();
}
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->Shutdown();
delete child;
}
}
HangMonitorChild::sMonitor.reset();
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
obs->RemoveObserver(this, "xpcom-shutdown");
}
return NS_OK;
}
ProcessHangMonitor::SlowScriptAction ProcessHangMonitor::NotifySlowScript(
nsIBrowserChild* aBrowserChild, const char* aFileName,
const nsString& aAddonId) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->NotifySlowScript(aBrowserChild, aFileName,
aAddonId);
}
bool ProcessHangMonitor::IsDebuggerStartupComplete() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->IsDebuggerStartupComplete();
}
bool ProcessHangMonitor::ShouldTimeOutCPOWs() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mCPOWTimeout) {
mCPOWTimeout = false;
return true;
}
return false;
}
void ProcessHangMonitor::InitiateCPOWTimeout() {
MOZ_RELEASE_ASSERT(IsOnThread());
mCPOWTimeout = true;
}
void ProcessHangMonitor::NotifyPluginHang(uint32_t aPluginId) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->NotifyPluginHang(aPluginId);
}
static PProcessHangMonitorParent* CreateHangMonitorParent(
ContentParent* aContentParent,
Endpoint<PProcessHangMonitorParent>&& aEndpoint) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
ProcessHangMonitor* monitor = ProcessHangMonitor::GetOrCreate();
auto* parent = new HangMonitorParent(monitor);
auto* process = new HangMonitoredProcess(parent, aContentParent);
parent->SetProcess(process);
monitor->Dispatch(
NewNonOwningRunnableMethod<Endpoint<PProcessHangMonitorParent>&&>(
"HangMonitorParent::Bind", parent, &HangMonitorParent::Bind,
std::move(aEndpoint)));
return parent;
}
void mozilla::CreateHangMonitorChild(
Endpoint<PProcessHangMonitorChild>&& aEndpoint) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
JSContext* cx = danger::GetJSContext();
JS_AddInterruptCallback(cx, InterruptCallback);
ProcessHangMonitor* monitor = ProcessHangMonitor::GetOrCreate();
auto* child = new HangMonitorChild(monitor);
monitor->Dispatch(
NewNonOwningRunnableMethod<Endpoint<PProcessHangMonitorChild>&&>(
"HangMonitorChild::Bind", child, &HangMonitorChild::Bind,
std::move(aEndpoint)));
}
void ProcessHangMonitor::Dispatch(already_AddRefed<nsIRunnable> aRunnable) {
mThread->Dispatch(std::move(aRunnable), nsIEventTarget::NS_DISPATCH_NORMAL);
}
bool ProcessHangMonitor::IsOnThread() {
bool on;
return NS_SUCCEEDED(mThread->IsOnCurrentThread(&on)) && on;
}
/* static */
PProcessHangMonitorParent* ProcessHangMonitor::AddProcess(
ContentParent* aContentParent) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!StaticPrefs::dom_ipc_processHangMonitor_AtStartup()) {
return nullptr;
}
Endpoint<PProcessHangMonitorParent> parent;
Endpoint<PProcessHangMonitorChild> child;
nsresult rv;
rv = PProcessHangMonitor::CreateEndpoints(
base::GetCurrentProcId(), aContentParent->OtherPid(), &parent, &child);
if (NS_FAILED(rv)) {
MOZ_ASSERT(false, "PProcessHangMonitor::CreateEndpoints failed");
return nullptr;
}
if (!aContentParent->SendInitProcessHangMonitor(std::move(child))) {
MOZ_ASSERT(false);
return nullptr;
}
return CreateHangMonitorParent(aContentParent, std::move(parent));
}
/* static */
void ProcessHangMonitor::RemoveProcess(PProcessHangMonitorParent* aParent) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
auto parent = static_cast<HangMonitorParent*>(aParent);
parent->Shutdown();
delete parent;
}
/* static */
void ProcessHangMonitor::ClearHang() {
MOZ_ASSERT(NS_IsMainThread());
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->ClearHang();
}
}
/* static */
void ProcessHangMonitor::PaintWhileInterruptingJS(
PProcessHangMonitorParent* aParent, dom::BrowserParent* aBrowserParent,
const layers::LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
auto parent = static_cast<HangMonitorParent*>(aParent);
parent->PaintWhileInterruptingJS(aBrowserParent, aEpoch);
}
/* static */
void ProcessHangMonitor::ClearPaintWhileInterruptingJS(
const layers::LayersObserverEpoch& aEpoch) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_RELEASE_ASSERT(XRE_IsContentProcess());
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->ClearPaintWhileInterruptingJS(aEpoch);
}
}
/* static */
void ProcessHangMonitor::MaybeStartPaintWhileInterruptingJS() {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_RELEASE_ASSERT(XRE_IsContentProcess());
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->MaybeStartPaintWhileInterruptingJS();
}
}
/* static */
void ProcessHangMonitor::CancelContentJSExecutionIfRunning(
PProcessHangMonitorParent* aParent, dom::BrowserParent* aBrowserParent,
nsIRemoteTab::NavigationType aNavigationType,
const dom::CancelContentJSOptions& aCancelContentJSOptions) {
MOZ_RELEASE_ASSERT(NS_IsMainThread());
auto parent = static_cast<HangMonitorParent*>(aParent);
parent->CancelContentJSExecutionIfRunning(aBrowserParent, aNavigationType,
aCancelContentJSOptions);
}
|