1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2012 Intel Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_PERFORMANCE_ENTRY_H_
33 #define THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_PERFORMANCE_ENTRY_H_
34 
35 #include "third_party/blink/public/mojom/timing/performance_mark_or_measure.mojom-blink-forward.h"
36 #include "third_party/blink/renderer/core/core_export.h"
37 #include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
38 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
39 #include "third_party/blink/renderer/platform/heap/handle.h"
40 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
41 
42 namespace blink {
43 
44 class ScriptState;
45 class ScriptValue;
46 class V8ObjectBuilder;
47 
48 using PerformanceEntryType = unsigned;
49 using PerformanceEntryTypeMask = unsigned;
50 
51 class CORE_EXPORT PerformanceEntry : public ScriptWrappable {
52   DEFINE_WRAPPERTYPEINFO();
53 
54  public:
55   ~PerformanceEntry() override;
56 
57   enum EntryType : PerformanceEntryType {
58     kInvalid = 0,
59     kNavigation = 1 << 0,
60     kMark = 1 << 1,
61     kMeasure = 1 << 2,
62     kResource = 1 << 3,
63     kLongTask = 1 << 4,
64     kTaskAttribution = 1 << 5,
65     kPaint = 1 << 6,
66     kEvent = 1 << 7,
67     kFirstInput = 1 << 8,
68     kElement = 1 << 9,
69     kLayoutShift = 1 << 10,
70     kLargestContentfulPaint = 1 << 11,
71     kVisibilityState = 1 << 12,
72   };
73 
name()74   const AtomicString& name() const { return name_; }
75   DOMHighResTimeStamp startTime() const;
76   virtual AtomicString entryType() const = 0;
77   virtual PerformanceEntryType EntryTypeEnum() const = 0;
78   // PerformanceNavigationTiming will override this due to
79   // the nature of reporting it early, which means not having a
80   // finish time available at construction time.
81   // Other classes must NOT override this.
82   virtual DOMHighResTimeStamp duration() const;
83 
84   ScriptValue toJSONForBinding(ScriptState*) const;
85 
IsResource()86   bool IsResource() const { return EntryTypeEnum() == kResource; }
IsMark()87   bool IsMark() const { return EntryTypeEnum() == kMark; }
IsMeasure()88   bool IsMeasure() const { return EntryTypeEnum() == kMeasure; }
89 
StartTimeCompareLessThan(PerformanceEntry * a,PerformanceEntry * b)90   static bool StartTimeCompareLessThan(PerformanceEntry* a,
91                                        PerformanceEntry* b) {
92     if (a->startTime() == b->startTime())
93       return a->index_ < b->index_;
94     return a->startTime() < b->startTime();
95   }
96 
97   static PerformanceEntry::EntryType ToEntryTypeEnum(
98       const AtomicString& entry_type);
99 
100   // Entries of the types listed here will be accessible from the
101   // PerformanceTimeline or the PerformanceObserver.  Those not listed will
102   // only be available via the PerformanceObserver.
103   // Note: Currently buffered flags don't support long task entries
104   // so leaving it out of this list for now.
IsValidTimelineEntryType(const PerformanceEntryType & entry_type)105   static bool IsValidTimelineEntryType(const PerformanceEntryType& entry_type) {
106     if (entry_type == kInvalid) {
107       return true;
108     }
109     DEFINE_THREAD_SAFE_STATIC_LOCAL(HashSet<PerformanceEntryType>,
110                                     valid_timeline_entry_types,
111                                     ({kNavigation, kMark, kMeasure, kResource,
112                                       kTaskAttribution, kPaint, kFirstInput}));
113     return valid_timeline_entry_types.Contains(entry_type);
114   }
115 
116   // PerformanceMark/Measure override this and it returns Mojo structure pointer
117   // which has all members of PerformanceMark/Measure. Common data members are
118   // set by PerformanceMark/Measure calling
119   // PerformanceEntry::ToMojoPerformanceMarkOrMeasure().
120   virtual mojom::blink::PerformanceMarkOrMeasurePtr
121   ToMojoPerformanceMarkOrMeasure();
122 
123  protected:
124   PerformanceEntry(const AtomicString& name,
125                    double start_time,
126                    double finish_time);
127   virtual void BuildJSONValue(V8ObjectBuilder&) const;
128 
129   // Protected and not const because PerformanceEventTiming needs to modify it.
130   double duration_;
131 
132  private:
133   const AtomicString name_;
134   const double start_time_;
135   const int index_;
136 };
137 
138 }  // namespace blink
139 
140 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_PERFORMANCE_ENTRY_H_
141