2 * Copyright (C) 2013 Google Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * 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.
27 #include "core/html/parser/HTMLResourcePreloader.h"
29 #include "core/dom/Document.h"
30 #include "core/fetch/FetchInitiatorInfo.h"
31 #include "core/fetch/ResourceFetcher.h"
32 #include "core/loader/DocumentLoader.h"
33 #include "public/platform/Platform.h"
37 inline HTMLResourcePreloader::HTMLResourcePreloader(Document
& document
)
38 : m_document(document
)
42 PassOwnPtrWillBeRawPtr
<HTMLResourcePreloader
> HTMLResourcePreloader::create(Document
& document
)
44 return adoptPtrWillBeNoop(new HTMLResourcePreloader(document
));
47 DEFINE_TRACE(HTMLResourcePreloader
)
49 visitor
->trace(m_document
);
52 static void preconnectHost(PreloadRequest
* request
, const NetworkHintsInterface
& networkHintsInterface
)
55 ASSERT(request
->isPreconnect());
56 KURL
host(request
->baseURL(), request
->resourceURL());
57 if (!host
.isValid() || !host
.protocolIsInHTTPFamily())
59 CrossOriginAttributeValue crossOrigin
= CrossOriginAttributeNotSet
;
60 if (request
->isCORS()) {
61 if (request
->isAllowCredentials())
62 crossOrigin
= CrossOriginAttributeUseCredentials
;
64 crossOrigin
= CrossOriginAttributeAnonymous
;
66 networkHintsInterface
.preconnectHost(host
, crossOrigin
);
69 void HTMLResourcePreloader::preload(PassOwnPtr
<PreloadRequest
> preload
, const NetworkHintsInterface
& networkHintsInterface
)
71 if (preload
->isPreconnect()) {
72 preconnectHost(preload
.get(), networkHintsInterface
);
75 // TODO(yoichio): Should preload if document is imported.
76 if (!m_document
->loader())
78 FetchRequest request
= preload
->resourceRequest(m_document
);
79 // TODO(dgozman): This check should go to HTMLPreloadScanner, but this requires
80 // making Document::completeURLWithOverride logic to be statically accessible.
81 if (request
.url().protocolIsData())
83 if (preload
->resourceType() == Resource::Script
|| preload
->resourceType() == Resource::CSSStyleSheet
|| preload
->resourceType() == Resource::ImportResource
)
84 request
.setCharset(preload
->charset().isEmpty() ? m_document
->charset().string() : preload
->charset());
85 request
.setForPreload(true);
86 Platform::current()->histogramCustomCounts("WebCore.PreloadDelayMs", static_cast<int>(1000 * (monotonicallyIncreasingTime() - preload
->discoveryTime())), 0, 2000, 20);
87 m_document
->loader()->startPreload(preload
->resourceType(), request
);