1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/DebuggerOnGCRunnable.h"
8 
9 #include <utility>
10 
11 #include "js/Debug.h"
12 #include "mozilla/CycleCollectedJSContext.h"
13 #include "mozilla/dom/ScriptSettings.h"
14 #include "mozilla/SchedulerGroup.h"
15 
16 namespace mozilla {
17 
18 /* static */
Enqueue(JSContext * aCx,const JS::GCDescription & aDesc)19 nsresult DebuggerOnGCRunnable::Enqueue(JSContext* aCx,
20                                        const JS::GCDescription& aDesc) {
21   auto gcEvent = aDesc.toGCEvent(aCx);
22   if (!gcEvent) {
23     return NS_ERROR_OUT_OF_MEMORY;
24   }
25 
26   RefPtr<DebuggerOnGCRunnable> runOnGC =
27       new DebuggerOnGCRunnable(std::move(gcEvent));
28   if (NS_IsMainThread()) {
29     return SchedulerGroup::Dispatch(TaskCategory::GarbageCollection,
30                                     runOnGC.forget());
31   } else {
32     return NS_DispatchToCurrentThread(runOnGC);
33   }
34 }
35 
36 NS_IMETHODIMP
Run()37 DebuggerOnGCRunnable::Run() {
38   dom::AutoJSAPI jsapi;
39   jsapi.Init();
40   if (!JS::dbg::FireOnGarbageCollectionHook(jsapi.cx(), std::move(mGCData))) {
41     return NS_ERROR_OUT_OF_MEMORY;
42   }
43   return NS_OK;
44 }
45 
Cancel()46 nsresult DebuggerOnGCRunnable::Cancel() {
47   mGCData = nullptr;
48   return NS_OK;
49 }
50 
51 }  // namespace mozilla
52