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 "TimelineMarker.h"
8 
9 #include "jsapi.h"
10 #include "js/Exception.h"
11 #include "mozilla/dom/ProfileTimelineMarkerBinding.h"
12 #include "nsContentUtils.h"
13 
14 namespace mozilla {
15 
TimelineMarker(const char * aName,MarkerTracingType aTracingType,MarkerStackRequest aStackRequest)16 TimelineMarker::TimelineMarker(const char* aName,
17                                MarkerTracingType aTracingType,
18                                MarkerStackRequest aStackRequest)
19     : AbstractTimelineMarker(aName, aTracingType) {
20   CaptureStackIfNecessary(aTracingType, aStackRequest);
21 }
22 
TimelineMarker(const char * aName,const TimeStamp & aTime,MarkerTracingType aTracingType,MarkerStackRequest aStackRequest)23 TimelineMarker::TimelineMarker(const char* aName, const TimeStamp& aTime,
24                                MarkerTracingType aTracingType,
25                                MarkerStackRequest aStackRequest)
26     : AbstractTimelineMarker(aName, aTime, aTracingType) {
27   CaptureStackIfNecessary(aTracingType, aStackRequest);
28 }
29 
AddDetails(JSContext * aCx,dom::ProfileTimelineMarker & aMarker)30 void TimelineMarker::AddDetails(JSContext* aCx,
31                                 dom::ProfileTimelineMarker& aMarker) {
32   if (GetTracingType() == MarkerTracingType::START) {
33     aMarker.mProcessType.Construct(GetProcessType());
34     aMarker.mIsOffMainThread.Construct(IsOffMainThread());
35   }
36 }
37 
GetStack()38 JSObject* TimelineMarker::GetStack() {
39   if (mStackTrace.initialized()) {
40     return mStackTrace;
41   }
42   return nullptr;
43 }
44 
CaptureStack()45 void TimelineMarker::CaptureStack() {
46   JSContext* ctx = nsContentUtils::GetCurrentJSContext();
47   if (ctx) {
48     JS::RootedObject stack(ctx);
49     if (JS::CaptureCurrentStack(ctx, &stack)) {
50       mStackTrace.init(ctx, stack.get());
51     } else {
52       JS_ClearPendingException(ctx);
53     }
54   }
55 }
56 
CaptureStackIfNecessary(MarkerTracingType aTracingType,MarkerStackRequest aStackRequest)57 void TimelineMarker::CaptureStackIfNecessary(MarkerTracingType aTracingType,
58                                              MarkerStackRequest aStackRequest) {
59   if ((aTracingType == MarkerTracingType::START ||
60        aTracingType == MarkerTracingType::TIMESTAMP) &&
61       aStackRequest != MarkerStackRequest::NO_STACK) {
62     CaptureStack();
63   }
64 }
65 
66 }  // namespace mozilla
67