1/*
2 * Copyright (C) 2012 Google, 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 GOOGLE 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.
24 */
25
26#pragma once
27
28#include "CachedResource.h"
29#include "Element.h"
30#include "ResourceLoadPriority.h"
31#include "ResourceLoaderOptions.h"
32#include "ResourceRequest.h"
33#include "SecurityOrigin.h"
34#include "ServiceWorkerIdentifier.h"
35#include <wtf/RefPtr.h>
36#include <wtf/text/AtomString.h>
37
38namespace WebCore {
39
40struct ContentRuleListResults;
41class Document;
42class FrameLoader;
43struct ServiceWorkerRegistrationData;
44enum class ReferrerPolicy : uint8_t;
45
46bool isRequestCrossOrigin(SecurityOrigin*, const URL& requestURL, const ResourceLoaderOptions&);
47
48class CachedResourceRequest {
49public:
50 CachedResourceRequest(ResourceRequest&&, const ResourceLoaderOptions&, Optional<ResourceLoadPriority> = WTF::nullopt, String&& charset = String());
51
52 ResourceRequest&& releaseResourceRequest() { return WTFMove(m_resourceRequest); }
53 const ResourceRequest& resourceRequest() const { return m_resourceRequest; }
54 ResourceRequest& resourceRequest() { return m_resourceRequest; }
55
56 const String& charset() const { return m_charset; }
57 void setCharset(const String& charset) { m_charset = charset; }
58
59 const ResourceLoaderOptions& options() const { return m_options; }
60 void setOptions(const ResourceLoaderOptions& options) { m_options = options; }
61
62 const Optional<ResourceLoadPriority>& priority() const { return m_priority; }
63 void setPriority(Optional<ResourceLoadPriority>&& priority) { m_priority = WTFMove(priority); }
64
65 void setInitiator(Element&);
66 void setInitiator(const AtomString& name);
67 const AtomString& initiatorName() const;
68
69 bool allowsCaching() const { return m_options.cachingPolicy == CachingPolicy::AllowCaching; }
70 void setCachingPolicy(CachingPolicy policy) { m_options.cachingPolicy = policy; }
71
72 // Whether this request should impact request counting and delay window.onload.
73 bool ignoreForRequestCount() const { return m_ignoreForRequestCount; }
74 void setIgnoreForRequestCount(bool ignoreForRequestCount) { m_ignoreForRequestCount = ignoreForRequestCount; }
75
76 void setDestinationIfNotSet(FetchOptions::Destination);
77
78 void deprecatedSetAsPotentiallyCrossOrigin(const String&, Document&); // Use WebCore::createPotentialAccessControlRequest() instead.
79
80 void updateForAccessControl(Document&);
81
82 void updateReferrerPolicy(ReferrerPolicy);
83 void updateReferrerOriginAndUserAgentHeaders(FrameLoader&);
84 void upgradeInsecureRequestIfNeeded(Document&);
85 void setAcceptHeaderIfNone(CachedResource::Type);
86 void updateAccordingCacheMode();
87 void updateAcceptEncodingHeader();
88
89 void removeFragmentIdentifierIfNeeded();
90#if ENABLE(CONTENT_EXTENSIONS)
91 void applyResults(ContentRuleListResults&&, Page*);
92#endif
93 void setDomainForCachePartition(Document&);
94 void setDomainForCachePartition(const String&);
95 bool isLinkPreload() const { return m_isLinkPreload; }
96 void setIsLinkPreload() { m_isLinkPreload = true; }
97
98 void setOrigin(Ref<SecurityOrigin>&& origin) { m_origin = WTFMove(origin); }
99 RefPtr<SecurityOrigin> releaseOrigin() { return WTFMove(m_origin); }
100 SecurityOrigin* origin() const { return m_origin.get(); }
101
102 String&& releaseFragmentIdentifier() { return WTFMove(m_fragmentIdentifier); }
103 void clearFragmentIdentifier() { m_fragmentIdentifier = { }; }
104
105 static String splitFragmentIdentifierFromRequestURL(ResourceRequest&);
106
107#if ENABLE(SERVICE_WORKER)
108 void setClientIdentifierIfNeeded(DocumentIdentifier);
109 void setSelectedServiceWorkerRegistrationIdentifierIfNeeded(ServiceWorkerRegistrationIdentifier);
110 void setNavigationServiceWorkerRegistrationData(const Optional<ServiceWorkerRegistrationData>&);
111#endif
112
113private:
114 ResourceRequest m_resourceRequest;
115 String m_charset;
116 ResourceLoaderOptions m_options;
117 Optional<ResourceLoadPriority> m_priority;
118 RefPtr<Element> m_initiatorElement;
119 AtomString m_initiatorName;
120 RefPtr<SecurityOrigin> m_origin;
121 String m_fragmentIdentifier;
122 bool m_isLinkPreload { false };
123 bool m_ignoreForRequestCount { false };
124};
125
126void upgradeInsecureResourceRequestIfNeeded(ResourceRequest&, Document&);
127
128} // namespace WebCore
129