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
|
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2012 Intel Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "core/timing/Performance.h"
#include "bindings/core/v8/ScriptValue.h"
#include "bindings/core/v8/V8ObjectBuilder.h"
#include "core/dom/Document.h"
#include "core/dom/QualifiedName.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/UseCounter.h"
#include "core/html/HTMLFrameOwnerElement.h"
#include "core/loader/DocumentLoader.h"
#include "core/origin_trials/OriginTrials.h"
#include "core/timing/PerformanceTiming.h"
static const double kLongTaskThreshold = 0.05;
static const char kUnknownAttribution[] = "unknown";
static const char kAmbiguousAttribution[] = "multiple-contexts";
static const char kSameOriginAttribution[] = "same-origin";
static const char kSameOriginSelfAttribution[] = "self";
static const char kSameOriginAncestorAttribution[] = "same-origin-ancestor";
static const char kSameOriginDescendantAttribution[] = "same-origin-descendant";
static const char kCrossOriginAncestorAttribution[] = "cross-origin-ancestor";
static const char kCrossOriginDescendantAttribution[] =
"cross-origin-descendant";
static const char kCrossOriginAttribution[] = "cross-origin-unreachable";
namespace blink {
namespace {
String getFrameAttribute(HTMLFrameOwnerElement* frameOwner,
const QualifiedName& attrName,
bool truncate) {
String attrValue;
if (frameOwner->hasAttribute(attrName)) {
attrValue = frameOwner->getAttribute(attrName);
if (truncate && attrValue.length() > 100)
attrValue = attrValue.substring(0, 100); // Truncate to 100 chars
}
return attrValue;
}
const char* sameOriginAttribution(Frame* observerFrame, Frame* culpritFrame) {
if (observerFrame == culpritFrame)
return kSameOriginSelfAttribution;
if (observerFrame->tree().isDescendantOf(culpritFrame))
return kSameOriginAncestorAttribution;
if (culpritFrame->tree().isDescendantOf(observerFrame))
return kSameOriginDescendantAttribution;
return kSameOriginAttribution;
}
} // namespace
static double toTimeOrigin(LocalFrame* frame) {
if (!frame)
return 0.0;
Document* document = frame->document();
if (!document)
return 0.0;
DocumentLoader* loader = document->loader();
if (!loader)
return 0.0;
return loader->timing().referenceMonotonicTime();
}
Performance::Performance(LocalFrame* frame)
: PerformanceBase(toTimeOrigin(frame)),
ContextLifecycleObserver(frame ? frame->document() : nullptr) {}
Performance::~Performance() {
}
void Performance::contextDestroyed(ExecutionContext* destroyedContext) {
toDocument(destroyedContext)
->frame()
->performanceMonitor()
->unsubscribeAll(this);
}
ExecutionContext* Performance::getExecutionContext() const {
if (!frame())
return nullptr;
return frame()->document();
}
MemoryInfo* Performance::memory() {
return MemoryInfo::create();
}
PerformanceNavigation* Performance::navigation() const {
if (!m_navigation)
m_navigation = PerformanceNavigation::create(frame());
return m_navigation.get();
}
PerformanceTiming* Performance::timing() const {
if (!m_timing)
m_timing = PerformanceTiming::create(frame());
return m_timing.get();
}
void Performance::updateLongTaskInstrumentation() {
DCHECK(frame());
if (!frame()->document() ||
!OriginTrials::longTaskObserverEnabled(frame()->document()))
return;
if (hasObserverFor(PerformanceEntry::LongTask)) {
UseCounter::count(frame()->localFrameRoot(), UseCounter::LongTaskObserver);
frame()->performanceMonitor()->subscribe(PerformanceMonitor::kLongTask,
kLongTaskThreshold, this);
} else {
frame()->performanceMonitor()->unsubscribeAll(this);
}
}
ScriptValue Performance::toJSONForBinding(ScriptState* scriptState) const {
V8ObjectBuilder result(scriptState);
result.add("timing", timing()->toJSONForBinding(scriptState));
result.add("navigation", navigation()->toJSONForBinding(scriptState));
return result.scriptValue();
}
DEFINE_TRACE(Performance) {
visitor->trace(m_navigation);
visitor->trace(m_timing);
ContextLifecycleObserver::trace(visitor);
PerformanceBase::trace(visitor);
PerformanceMonitor::Client::trace(visitor);
}
static bool canAccessOrigin(Frame* frame1, Frame* frame2) {
SecurityOrigin* securityOrigin1 =
frame1->securityContext()->getSecurityOrigin();
SecurityOrigin* securityOrigin2 =
frame2->securityContext()->getSecurityOrigin();
return securityOrigin1->canAccess(securityOrigin2);
}
/**
* Report sanitized name based on cross-origin policy.
* See detailed Security doc here: http://bit.ly/2duD3F7
*/
// static
std::pair<String, DOMWindow*> Performance::sanitizedAttribution(
ExecutionContext* taskContext,
bool hasMultipleContexts,
Frame* observerFrame) {
if (hasMultipleContexts) {
// Unable to attribute, multiple script execution contents were involved.
return std::make_pair(kAmbiguousAttribution, nullptr);
}
if (!taskContext || !taskContext->isDocument() ||
!toDocument(taskContext)->frame()) {
// Unable to attribute as no script was involved.
return std::make_pair(kUnknownAttribution, nullptr);
}
// Exactly one culprit location, attribute based on origin boundary.
Frame* culpritFrame = toDocument(taskContext)->frame();
DCHECK(culpritFrame);
if (canAccessOrigin(observerFrame, culpritFrame)) {
// From accessible frames or same origin, return culprit location URL.
return std::make_pair(sameOriginAttribution(observerFrame, culpritFrame),
culpritFrame->domWindow());
}
// For cross-origin, if the culprit is the descendant or ancestor of
// observer then indicate the *closest* cross-origin frame between
// the observer and the culprit, in the corresponding direction.
if (culpritFrame->tree().isDescendantOf(observerFrame)) {
// If the culprit is a descendant of the observer, then walk up the tree
// from culprit to observer, and report the *last* cross-origin (from
// observer) frame. If no intermediate cross-origin frame is found, then
// report the culprit directly.
Frame* lastCrossOriginFrame = culpritFrame;
for (Frame* frame = culpritFrame; frame != observerFrame;
frame = frame->tree().parent()) {
if (!canAccessOrigin(observerFrame, frame)) {
lastCrossOriginFrame = frame;
}
}
return std::make_pair(kCrossOriginDescendantAttribution,
lastCrossOriginFrame->domWindow());
}
if (observerFrame->tree().isDescendantOf(culpritFrame)) {
return std::make_pair(kCrossOriginAncestorAttribution, nullptr);
}
return std::make_pair(kCrossOriginAttribution, nullptr);
}
void Performance::reportLongTask(double startTime,
double endTime,
ExecutionContext* taskContext,
bool hasMultipleContexts) {
std::pair<String, DOMWindow*> attribution = Performance::sanitizedAttribution(
taskContext, hasMultipleContexts, frame());
DOMWindow* culpritDomWindow = attribution.second;
if (!culpritDomWindow || !culpritDomWindow->document() ||
!culpritDomWindow->document()->localOwner()) {
addLongTaskTiming(startTime, endTime, attribution.first, "", "", "");
} else {
HTMLFrameOwnerElement* frameOwner =
culpritDomWindow->document()->localOwner();
addLongTaskTiming(startTime, endTime, attribution.first,
getFrameAttribute(frameOwner, HTMLNames::srcAttr, false),
getFrameAttribute(frameOwner, HTMLNames::idAttr, false),
getFrameAttribute(frameOwner, HTMLNames::nameAttr, true));
}
}
} // namespace blink
|