PerformanceTiming.cpp 10.9 KB
Newer Older
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
/*
 * Copyright (C) 2010 Google 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 "config.h"
32
#include "PerformanceTiming.h"
33 34 35

#if ENABLE(WEB_TIMING)

36
#include "Document.h"
37
#include "DocumentLoadTiming.h"
38
#include "DocumentLoader.h"
39
#include "DocumentTiming.h"
40
#include "Frame.h"
41
#include "FrameLoader.h"
42 43
#include "ResourceLoadTiming.h"
#include "ResourceResponse.h"
44
#include <wtf/CurrentTime.h>
45 46 47

namespace WebCore {

48
static unsigned long long toIntegerMilliseconds(double seconds)
49
{
50 51 52 53
    ASSERT(seconds >= 0);
    return static_cast<unsigned long long>(seconds * 1000.0);
}

54
PerformanceTiming::PerformanceTiming(Frame* frame)
55
    : DOMWindowProperty(frame)
56 57 58
{
}

59
unsigned long long PerformanceTiming::navigationStart() const
60
{
61 62
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
63 64
        return 0;

65
    return monotonicTimeToIntegerMilliseconds(timing->navigationStart());
66 67
}

68 69 70 71 72 73
unsigned long long PerformanceTiming::unloadEventStart() const
{
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
        return 0;

74
    if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
75 76
        return 0;

77
    return monotonicTimeToIntegerMilliseconds(timing->unloadEventStart());
78 79
}

80
unsigned long long PerformanceTiming::unloadEventEnd() const
81
{
82 83
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
84 85
        return 0;

86
    if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
87 88
        return 0;

89
    return monotonicTimeToIntegerMilliseconds(timing->unloadEventEnd());
90
}
91

92
unsigned long long PerformanceTiming::redirectStart() const
93
{
94 95
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
96
        return 0;
97

98
    if (timing->hasCrossOriginRedirect())
99 100
        return 0;

101
    return monotonicTimeToIntegerMilliseconds(timing->redirectStart());
102
}
103

104
unsigned long long PerformanceTiming::redirectEnd() const
105
{
106 107
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
108
        return 0;
109

110
    if (timing->hasCrossOriginRedirect())
111 112
        return 0;

113
    return monotonicTimeToIntegerMilliseconds(timing->redirectEnd());
114
}
115

116
unsigned long long PerformanceTiming::fetchStart() const
117
{
118 119
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
120 121
        return 0;

122
    return monotonicTimeToIntegerMilliseconds(timing->fetchStart());
123 124
}

125
unsigned long long PerformanceTiming::domainLookupStart() const
126 127 128
{
    ResourceLoadTiming* timing = resourceLoadTiming();
    if (!timing)
129
        return fetchStart();
130

131 132 133 134 135 136
    // This will be -1 when a DNS request is not performed.
    // Rather than exposing a special value that indicates no DNS, we "backfill" with fetchStart.
    int dnsStart = timing->dnsStart;
    if (dnsStart < 0)
        return fetchStart();

137
    return resourceLoadTimeRelativeToAbsolute(dnsStart);
138 139
}

140
unsigned long long PerformanceTiming::domainLookupEnd() const
141 142 143
{
    ResourceLoadTiming* timing = resourceLoadTiming();
    if (!timing)
144
        return domainLookupStart();
145

146 147 148 149 150 151
    // This will be -1 when a DNS request is not performed.
    // Rather than exposing a special value that indicates no DNS, we "backfill" with domainLookupStart.
    int dnsEnd = timing->dnsEnd;
    if (dnsEnd < 0)
        return domainLookupStart();

152
    return resourceLoadTimeRelativeToAbsolute(dnsEnd);
153 154
}

155
unsigned long long PerformanceTiming::connectStart() const
156
{
157 158
    DocumentLoader* loader = documentLoader();
    if (!loader)
159
        return domainLookupEnd();
160 161

    ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
162
    if (!timing)
163
        return domainLookupEnd();
164

165
    // connectStart will be -1 when a network request is not made.
166 167
    // Rather than exposing a special value that indicates no new connection, we "backfill" with domainLookupEnd.
    int connectStart = timing->connectStart;
168
    if (connectStart < 0 || loader->response().connectionReused())
169 170
        return domainLookupEnd();

171
    // ResourceLoadTiming's connect phase includes DNS, however Navigation Timing's
172 173 174 175
    // connect phase should not. So if there is DNS time, trim it from the start.
    if (timing->dnsEnd >= 0 && timing->dnsEnd > connectStart)
        connectStart = timing->dnsEnd;

176
    return resourceLoadTimeRelativeToAbsolute(connectStart);
177 178
}

179
unsigned long long PerformanceTiming::connectEnd() const
180
{
181 182
    DocumentLoader* loader = documentLoader();
    if (!loader)
183
        return connectStart();
184 185

    ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
186
    if (!timing)
187
        return connectStart();
188

189
    // connectEnd will be -1 when a network request is not made.
190 191
    // Rather than exposing a special value that indicates no new connection, we "backfill" with connectStart.
    int connectEnd = timing->connectEnd;
192
    if (connectEnd < 0 || loader->response().connectionReused())
193 194
        return connectStart();

195
    return resourceLoadTimeRelativeToAbsolute(connectEnd);
196 197
}

198
unsigned long long PerformanceTiming::secureConnectionStart() const
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
    DocumentLoader* loader = documentLoader();
    if (!loader)
        return 0;

    ResourceLoadTiming* timing = loader->response().resourceLoadTiming();
    if (!timing)
        return 0;

    int sslStart = timing->sslStart;
    if (sslStart < 0)
        return 0;

    return resourceLoadTimeRelativeToAbsolute(sslStart);
}

215
unsigned long long PerformanceTiming::requestStart() const
216 217 218
{
    ResourceLoadTiming* timing = resourceLoadTiming();
    if (!timing)
219
        return connectEnd();
220

221
    ASSERT(timing->sendStart >= 0);
222
    return resourceLoadTimeRelativeToAbsolute(timing->sendStart);
223 224
}

225
unsigned long long PerformanceTiming::responseStart() const
226 227 228
{
    ResourceLoadTiming* timing = resourceLoadTiming();
    if (!timing)
229
        return requestStart();
230 231 232 233 234 235 236

    // FIXME: Response start needs to be the time of the first received byte.
    // However, the ResourceLoadTiming API currently only supports the time
    // the last header byte was received. For many responses with reasonable
    // sized cookies, the HTTP headers fit into a single packet so this time
    // is basically equivalent. But for some responses, particularly those with
    // headers larger than a single packet, this time will be too late.
237
    ASSERT(timing->receiveHeadersEnd >= 0);
238
    return resourceLoadTimeRelativeToAbsolute(timing->receiveHeadersEnd);
239 240
}

241
unsigned long long PerformanceTiming::responseEnd() const
242
{
243 244
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
245 246
        return 0;

247
    return monotonicTimeToIntegerMilliseconds(timing->responseEnd());
248 249
}

250
unsigned long long PerformanceTiming::domLoading() const
251 252 253
{
    const DocumentTiming* timing = documentTiming();
    if (!timing)
254
        return fetchStart();
255

256
    return monotonicTimeToIntegerMilliseconds(timing->domLoading);
257 258
}

259
unsigned long long PerformanceTiming::domInteractive() const
260 261 262 263 264
{
    const DocumentTiming* timing = documentTiming();
    if (!timing)
        return 0;

265
    return monotonicTimeToIntegerMilliseconds(timing->domInteractive);
266 267
}

268
unsigned long long PerformanceTiming::domContentLoadedEventStart() const
269 270 271 272 273
{
    const DocumentTiming* timing = documentTiming();
    if (!timing)
        return 0;

274
    return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventStart);
275 276
}

277
unsigned long long PerformanceTiming::domContentLoadedEventEnd() const
278 279 280 281 282
{
    const DocumentTiming* timing = documentTiming();
    if (!timing)
        return 0;

283
    return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventEnd);
284 285
}

286
unsigned long long PerformanceTiming::domComplete() const
287 288 289 290 291
{
    const DocumentTiming* timing = documentTiming();
    if (!timing)
        return 0;

292
    return monotonicTimeToIntegerMilliseconds(timing->domComplete);
293 294
}

295
unsigned long long PerformanceTiming::loadEventStart() const
296
{
297 298
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
299 300
        return 0;

301
    return monotonicTimeToIntegerMilliseconds(timing->loadEventStart());
302 303
}

304
unsigned long long PerformanceTiming::loadEventEnd() const
305 306 307 308 309
{
    DocumentLoadTiming* timing = documentLoadTiming();
    if (!timing)
        return 0;

310
    return monotonicTimeToIntegerMilliseconds(timing->loadEventEnd());
311 312
}

313
DocumentLoader* PerformanceTiming::documentLoader() const
314 315 316 317
{
    if (!m_frame)
        return 0;

318
    return m_frame->loader().documentLoader();
319 320
}

321
const DocumentTiming* PerformanceTiming::documentTiming() const
322 323 324 325 326 327 328 329 330 331 332
{
    if (!m_frame)
        return 0;

    Document* document = m_frame->document();
    if (!document)
        return 0;

    return document->timing();
}

333
DocumentLoadTiming* PerformanceTiming::documentLoadTiming() const
334 335 336 337 338 339
{
    DocumentLoader* loader = documentLoader();
    if (!loader)
        return 0;

    return loader->timing();
340 341
}

342
ResourceLoadTiming* PerformanceTiming::resourceLoadTiming() const
343
{
344 345
    DocumentLoader* loader = documentLoader();
    if (!loader)
346 347
        return 0;

348 349 350
    return loader->response().resourceLoadTiming();
}

351
unsigned long long PerformanceTiming::resourceLoadTimeRelativeToAbsolute(int relativeMilliseconds) const
352
{
353
    ASSERT(relativeMilliseconds >= 0);
354 355
    ResourceLoadTiming* resourceTiming = resourceLoadTiming();
    ASSERT(resourceTiming);
356
    return monotonicTimeToIntegerMilliseconds(resourceTiming->convertResourceLoadTimeToMonotonicTime(relativeMilliseconds));
357 358 359 360 361 362 363
}

unsigned long long PerformanceTiming::monotonicTimeToIntegerMilliseconds(double monotonicSeconds) const
{
    ASSERT(monotonicSeconds >= 0);
    const DocumentLoadTiming* timing = documentLoadTiming();
    ASSERT(timing);
364
    return toIntegerMilliseconds(timing->monotonicTimeToPseudoWallTime(monotonicSeconds));
365 366
}

367 368 369
} // namespace WebCore

#endif // ENABLE(WEB_TIMING)