SerializedStackHolder.h (3570B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_SerializedStackHolder_h 8 #define mozilla_dom_SerializedStackHolder_h 9 10 #include "mozilla/dom/StructuredCloneHolder.h" 11 #include "mozilla/dom/WorkerRef.h" 12 13 namespace mozilla::dom { 14 15 // Information about a main or worker thread stack trace that can be accessed 16 // from either kind of thread. When a worker thread stack is serialized, the 17 // worker is held alive until this holder is destroyed. 18 class SerializedStackHolder { 19 // Holds any encoded stack data. 20 StructuredCloneHolder mHolder; 21 22 // The worker associated with this stack, or null if this is a main thread 23 // stack. 24 RefPtr<ThreadSafeWorkerRef> mWorkerRef; 25 26 // Write aStack's data into mHolder. 27 void WriteStack(JSContext* aCx, JS::Handle<JSObject*> aStack); 28 29 public: 30 SerializedStackHolder(); 31 32 // Fill this holder with a main or worklet thread stack. 33 void SerializeMainThreadOrWorkletStack(JSContext* aCx, 34 JS::Handle<JSObject*> aStack); 35 36 // Fill this holder with a worker thread stack. 37 void SerializeWorkerStack(JSContext* aCx, WorkerPrivate* aWorkerPrivate, 38 JS::Handle<JSObject*> aStack); 39 40 // Fill this holder with the current thread's current stack. 41 void SerializeCurrentStack(JSContext* aCx); 42 43 // Read back a saved frame stack. This must be called on the main thread. 44 // This returns null on failure, and does not leave an exception on aCx. 45 JSObject* ReadStack(JSContext* aCx); 46 }; 47 48 // Construct a stack for the current thread, which may be consumed by the net 49 // monitor later on. This may be called on either the main or a worker thread. 50 // 51 // This always creates a stack, even if the net monitor isn't active for the 52 // associated window. The net monitor will only be active if the associated 53 // Browsing Context or worker's WatchedByDevTools flag is set, so this should 54 // be checked before creating the stack. 55 UniquePtr<SerializedStackHolder> GetCurrentStackForNetMonitor(JSContext* aCx); 56 57 // Construct a stack for the current thread. 58 UniquePtr<SerializedStackHolder> GetCurrentStack(JSContext* aCx); 59 60 // If aStackHolder is non-null, this notifies the net monitor that aStackHolder 61 // is the stack from which aChannel originates. This must be called on the main 62 // thread. This call is synchronous, and aChannel and aStackHolder will not be 63 // used afterward. aChannel is an nsISupports object because this can be used 64 // with either nsIChannel or nsIWebSocketChannel. 65 void NotifyNetworkMonitorAlternateStack( 66 nsISupports* aChannel, UniquePtr<SerializedStackHolder> aStackHolder); 67 68 // Read back the saved frame stack and store it in a string as JSON. 69 // This must be called on the main thread. 70 void ConvertSerializedStackToJSON(UniquePtr<SerializedStackHolder> aStackHolder, 71 nsAString& aStackString); 72 73 // As above, notify the net monitor for a stack that has already been converted 74 // to JSON. This can be used with ConvertSerializedStackToJSON when multiple 75 // notifications might be needed for a single stack. 76 void NotifyNetworkMonitorAlternateStack(nsISupports* aChannel, 77 const nsAString& aStackJSON); 78 79 } // namespace mozilla::dom 80 81 #endif // mozilla_dom_SerializedStackHolder_h