| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 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 #include "config.h" | |
| 33 #include "core/page/Performance.h" | |
| 34 | |
| 35 #include "core/dom/Document.h" | |
| 36 #include "core/loader/DocumentLoader.h" | |
| 37 #include "core/page/MemoryInfo.h" | |
| 38 #include "core/page/PerformanceEntry.h" | |
| 39 #include "core/page/PerformanceNavigation.h" | |
| 40 #include "core/page/PerformanceResourceTiming.h" | |
| 41 #include "core/page/PerformanceTiming.h" | |
| 42 #include "core/page/PerformanceUserTiming.h" | |
| 43 #include "core/page/ResourceTimingInfo.h" | |
| 44 #include "weborigin/SecurityOrigin.h" | |
| 45 #include "wtf/CurrentTime.h" | |
| 46 | |
| 47 #include "core/page/Frame.h" | |
| 48 | |
| 49 namespace WebCore { | |
| 50 | |
| 51 static const size_t defaultResourceTimingBufferSize = 150; | |
| 52 | |
| 53 Performance::Performance(Frame* frame) | |
| 54 : DOMWindowProperty(frame) | |
| 55 , m_resourceTimingBufferSize(defaultResourceTimingBufferSize) | |
| 56 , m_userTiming(0) | |
| 57 , m_referenceTime(frame->document()->loader()->timing()->referenceMonotonicT
ime()) | |
| 58 { | |
| 59 ASSERT(m_referenceTime); | |
| 60 ScriptWrappable::init(this); | |
| 61 } | |
| 62 | |
| 63 Performance::~Performance() | |
| 64 { | |
| 65 } | |
| 66 | |
| 67 const AtomicString& Performance::interfaceName() const | |
| 68 { | |
| 69 return eventNames().interfaceForPerformance; | |
| 70 } | |
| 71 | |
| 72 ScriptExecutionContext* Performance::scriptExecutionContext() const | |
| 73 { | |
| 74 if (!frame()) | |
| 75 return 0; | |
| 76 return frame()->document(); | |
| 77 } | |
| 78 | |
| 79 PassRefPtr<MemoryInfo> Performance::memory() const | |
| 80 { | |
| 81 return MemoryInfo::create(m_frame); | |
| 82 } | |
| 83 | |
| 84 PerformanceNavigation* Performance::navigation() const | |
| 85 { | |
| 86 if (!m_navigation) | |
| 87 m_navigation = PerformanceNavigation::create(m_frame); | |
| 88 | |
| 89 return m_navigation.get(); | |
| 90 } | |
| 91 | |
| 92 PerformanceTiming* Performance::timing() const | |
| 93 { | |
| 94 if (!m_timing) | |
| 95 m_timing = PerformanceTiming::create(m_frame); | |
| 96 | |
| 97 return m_timing.get(); | |
| 98 } | |
| 99 | |
| 100 Vector<RefPtr<PerformanceEntry> > Performance::getEntries() const | |
| 101 { | |
| 102 Vector<RefPtr<PerformanceEntry> > entries; | |
| 103 | |
| 104 entries.append(m_resourceTimingBuffer); | |
| 105 | |
| 106 if (m_userTiming) { | |
| 107 entries.append(m_userTiming->getMarks()); | |
| 108 entries.append(m_userTiming->getMeasures()); | |
| 109 } | |
| 110 | |
| 111 std::sort(entries.begin(), entries.end(), PerformanceEntry::startTimeCompare
LessThan); | |
| 112 return entries; | |
| 113 } | |
| 114 | |
| 115 Vector<RefPtr<PerformanceEntry> > Performance::getEntriesByType(const String& en
tryType) | |
| 116 { | |
| 117 Vector<RefPtr<PerformanceEntry> > entries; | |
| 118 | |
| 119 if (equalIgnoringCase(entryType, "resource")) | |
| 120 for (Vector<RefPtr<PerformanceEntry> >::const_iterator resource = m_reso
urceTimingBuffer.begin(); resource != m_resourceTimingBuffer.end(); ++resource) | |
| 121 entries.append(*resource); | |
| 122 | |
| 123 if (m_userTiming) { | |
| 124 if (equalIgnoringCase(entryType, "mark")) | |
| 125 entries.append(m_userTiming->getMarks()); | |
| 126 else if (equalIgnoringCase(entryType, "measure")) | |
| 127 entries.append(m_userTiming->getMeasures()); | |
| 128 } | |
| 129 | |
| 130 std::sort(entries.begin(), entries.end(), PerformanceEntry::startTimeCompare
LessThan); | |
| 131 return entries; | |
| 132 } | |
| 133 | |
| 134 Vector<RefPtr<PerformanceEntry> > Performance::getEntriesByName(const String& na
me, const String& entryType) | |
| 135 { | |
| 136 Vector<RefPtr<PerformanceEntry> > entries; | |
| 137 | |
| 138 if (entryType.isNull() || equalIgnoringCase(entryType, "resource")) | |
| 139 for (Vector<RefPtr<PerformanceEntry> >::const_iterator resource = m_reso
urceTimingBuffer.begin(); resource != m_resourceTimingBuffer.end(); ++resource) | |
| 140 if ((*resource)->name() == name) | |
| 141 entries.append(*resource); | |
| 142 | |
| 143 if (m_userTiming) { | |
| 144 if (entryType.isNull() || equalIgnoringCase(entryType, "mark")) | |
| 145 entries.append(m_userTiming->getMarks(name)); | |
| 146 if (entryType.isNull() || equalIgnoringCase(entryType, "measure")) | |
| 147 entries.append(m_userTiming->getMeasures(name)); | |
| 148 } | |
| 149 | |
| 150 std::sort(entries.begin(), entries.end(), PerformanceEntry::startTimeCompare
LessThan); | |
| 151 return entries; | |
| 152 } | |
| 153 | |
| 154 void Performance::webkitClearResourceTimings() | |
| 155 { | |
| 156 m_resourceTimingBuffer.clear(); | |
| 157 } | |
| 158 | |
| 159 void Performance::webkitSetResourceTimingBufferSize(unsigned size) | |
| 160 { | |
| 161 m_resourceTimingBufferSize = size; | |
| 162 if (isResourceTimingBufferFull()) | |
| 163 dispatchEvent(Event::create(EventTypeNames::webkitresourcetimingbufferfu
ll)); | |
| 164 } | |
| 165 | |
| 166 static bool passesTimingAllowCheck(const ResourceResponse& response, Document* r
equestingDocument) | |
| 167 { | |
| 168 AtomicallyInitializedStatic(AtomicString&, timingAllowOrigin = *new AtomicSt
ring("timing-allow-origin")); | |
| 169 | |
| 170 RefPtr<SecurityOrigin> resourceOrigin = SecurityOrigin::create(response.url(
)); | |
| 171 if (resourceOrigin->isSameSchemeHostPort(requestingDocument->securityOrigin(
))) | |
| 172 return true; | |
| 173 | |
| 174 const String& timingAllowOriginString = response.httpHeaderField(timingAllow
Origin); | |
| 175 if (timingAllowOriginString.isEmpty() || equalIgnoringCase(timingAllowOrigin
String, "null")) | |
| 176 return false; | |
| 177 | |
| 178 if (timingAllowOriginString == "*") | |
| 179 return true; | |
| 180 | |
| 181 const String& securityOrigin = requestingDocument->securityOrigin()->toStrin
g(); | |
| 182 Vector<String> timingAllowOrigins; | |
| 183 timingAllowOriginString.split(" ", timingAllowOrigins); | |
| 184 for (size_t i = 0; i < timingAllowOrigins.size(); ++i) { | |
| 185 if (timingAllowOrigins[i] == securityOrigin) | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 static bool allowsTimingRedirect(const Vector<ResourceResponse>& redirectChain,
const ResourceResponse& finalResponse, Document* initiatorDocument) | |
| 193 { | |
| 194 if (!passesTimingAllowCheck(finalResponse, initiatorDocument)) | |
| 195 return false; | |
| 196 | |
| 197 for (size_t i = 0; i < redirectChain.size(); i++) { | |
| 198 if (!passesTimingAllowCheck(redirectChain[i], initiatorDocument)) | |
| 199 return false; | |
| 200 } | |
| 201 | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 205 void Performance::addResourceTiming(const ResourceTimingInfo& info, Document* in
itiatorDocument) | |
| 206 { | |
| 207 if (isResourceTimingBufferFull()) | |
| 208 return; | |
| 209 | |
| 210 const ResourceResponse& finalResponse = info.finalResponse(); | |
| 211 bool allowTimingDetails = passesTimingAllowCheck(finalResponse, initiatorDoc
ument); | |
| 212 double startTime = info.initialTime(); | |
| 213 | |
| 214 if (info.redirectChain().isEmpty()) { | |
| 215 RefPtr<PerformanceEntry> entry = PerformanceResourceTiming::create(info,
initiatorDocument, startTime, allowTimingDetails); | |
| 216 addResourceTimingBuffer(entry); | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 const Vector<ResourceResponse>& redirectChain = info.redirectChain(); | |
| 221 bool allowRedirectDetails = allowsTimingRedirect(redirectChain, finalRespons
e, initiatorDocument); | |
| 222 | |
| 223 if (!allowRedirectDetails) { | |
| 224 ResourceLoadTiming* finalTiming = finalResponse.resourceLoadTiming(); | |
| 225 ASSERT(finalTiming); | |
| 226 if (finalTiming) | |
| 227 startTime = finalTiming->requestTime; | |
| 228 } | |
| 229 | |
| 230 ResourceLoadTiming* lastRedirectTiming = redirectChain.last().resourceLoadTi
ming(); | |
| 231 ASSERT(lastRedirectTiming); | |
| 232 double lastRedirectEndTime = lastRedirectTiming->receiveHeadersEnd; | |
| 233 | |
| 234 RefPtr<PerformanceEntry> entry = PerformanceResourceTiming::create(info, ini
tiatorDocument, startTime, lastRedirectEndTime, allowTimingDetails, allowRedirec
tDetails); | |
| 235 addResourceTimingBuffer(entry); | |
| 236 } | |
| 237 | |
| 238 void Performance::addResourceTimingBuffer(PassRefPtr<PerformanceEntry> entry) | |
| 239 { | |
| 240 m_resourceTimingBuffer.append(entry); | |
| 241 | |
| 242 if (isResourceTimingBufferFull()) | |
| 243 dispatchEvent(Event::create(EventTypeNames::webkitresourcetimingbufferfu
ll)); | |
| 244 } | |
| 245 | |
| 246 bool Performance::isResourceTimingBufferFull() | |
| 247 { | |
| 248 return m_resourceTimingBuffer.size() >= m_resourceTimingBufferSize; | |
| 249 } | |
| 250 | |
| 251 void Performance::mark(const String& markName, ExceptionState& es) | |
| 252 { | |
| 253 if (!m_userTiming) | |
| 254 m_userTiming = UserTiming::create(this); | |
| 255 m_userTiming->mark(markName, es); | |
| 256 } | |
| 257 | |
| 258 void Performance::clearMarks(const String& markName) | |
| 259 { | |
| 260 if (!m_userTiming) | |
| 261 m_userTiming = UserTiming::create(this); | |
| 262 m_userTiming->clearMarks(markName); | |
| 263 } | |
| 264 | |
| 265 void Performance::measure(const String& measureName, const String& startMark, co
nst String& endMark, ExceptionState& es) | |
| 266 { | |
| 267 if (!m_userTiming) | |
| 268 m_userTiming = UserTiming::create(this); | |
| 269 m_userTiming->measure(measureName, startMark, endMark, es); | |
| 270 } | |
| 271 | |
| 272 void Performance::clearMeasures(const String& measureName) | |
| 273 { | |
| 274 if (!m_userTiming) | |
| 275 m_userTiming = UserTiming::create(this); | |
| 276 m_userTiming->clearMeasures(measureName); | |
| 277 } | |
| 278 | |
| 279 double Performance::now() const | |
| 280 { | |
| 281 return 1000.0 * (monotonicallyIncreasingTime() - m_referenceTime); | |
| 282 } | |
| 283 | |
| 284 } // namespace WebCore | |
| OLD | NEW |