8ff4489db22cf761b77d08a289b38fa360007016
[WebKit-https.git] / Source / WebCore / loader / ResourceTimingInformation.cpp
1 /*
2  * Copyright (C) 2016 Akamai Technologies Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ResourceTimingInformation.h"
28
29 #if ENABLE(WEB_TIMING)
30
31 #include "CachedResource.h"
32 #include "DOMWindow.h"
33 #include "Document.h"
34 #include "Frame.h"
35 #include "HTMLFrameOwnerElement.h"
36 #include "LoadTiming.h"
37 #include "Performance.h"
38 #include "RuntimeEnabledFeatures.h"
39
40 namespace WebCore {
41
42 void ResourceTimingInformation::addResourceTiming(CachedResource* resource, Document& document, const LoadTiming& loadTiming)
43 {
44     ASSERT(RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled());
45     if (resource && resource->resourceRequest().url().protocolIsInHTTPFamily()
46         && ((!resource->errorOccurred() && !resource->wasCanceled()) || resource->response().httpStatusCode() == 304)) {
47         auto initiatorIt = m_initiatorMap.find(resource);
48         if (initiatorIt != m_initiatorMap.end() && initiatorIt->value.added == NotYetAdded) {
49             Document* initiatorDocument = &document;
50             if (resource->type() == CachedResource::MainResource)
51                 initiatorDocument = document.parentDocument();
52             ASSERT(initiatorDocument);
53             ASSERT(initiatorDocument->domWindow());
54             ASSERT(initiatorDocument->domWindow()->performance());
55             const InitiatorInfo& info = initiatorIt->value;
56             initiatorDocument->domWindow()->performance()->addResourceTiming(info.name, initiatorDocument, resource->resourceRequest().url(), resource->response(), loadTiming);
57             initiatorIt->value.added = Added;
58         }
59     }
60 }
61
62 void ResourceTimingInformation::storeResourceTimingInitiatorInformation(const CachedResourceHandle<CachedResource>& resource, const AtomicString& initiatorName, Frame* frame)
63 {
64     ASSERT(RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled());
65     ASSERT(resource.get());
66     if (resource->type() == CachedResource::MainResource) {
67         // <iframe>s should report the initial navigation requested by the parent document, but not subsequent navigations.
68         ASSERT(frame);
69         if (frame->ownerElement()) {
70             InitiatorInfo info = { frame->ownerElement()->localName(), monotonicallyIncreasingTime(), NotYetAdded };
71             m_initiatorMap.add(resource.get(), info);
72         }
73     } else {
74         InitiatorInfo info = { initiatorName, monotonicallyIncreasingTime(), NotYetAdded };
75         m_initiatorMap.add(resource.get(), info);
76     }
77 }
78
79 }
80
81 #endif // ENABLE(WEB_TIMING)