OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/loader/LinkLoader.h" | 5 #include "core/loader/LinkLoader.h" |
6 | 6 |
7 #include "core/fetch/MemoryCache.h" | 7 #include "core/fetch/MemoryCache.h" |
8 #include "core/fetch/ResourceFetcher.h" | 8 #include "core/fetch/ResourceFetcher.h" |
9 #include "core/frame/Settings.h" | 9 #include "core/frame/Settings.h" |
10 #include "core/html/LinkRelAttribute.h" | 10 #include "core/html/LinkRelAttribute.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 mutable bool m_isHTTPS; | 76 mutable bool m_isHTTPS; |
77 mutable bool m_isCrossOrigin; | 77 mutable bool m_isCrossOrigin; |
78 }; | 78 }; |
79 | 79 |
80 TEST(LinkLoaderTest, Preload) { | 80 TEST(LinkLoaderTest, Preload) { |
81 struct TestCase { | 81 struct TestCase { |
82 const char* href; | 82 const char* href; |
83 const char* as; | 83 const char* as; |
84 const char* type; | 84 const char* type; |
85 const char* media; | 85 const char* media; |
| 86 const char* referrerPolicy; |
86 const ResourceLoadPriority priority; | 87 const ResourceLoadPriority priority; |
87 const WebURLRequest::RequestContext context; | 88 const WebURLRequest::RequestContext context; |
88 const bool linkLoaderShouldLoadValue; | 89 const bool linkLoaderShouldLoadValue; |
89 const bool expectingLoad; | 90 const bool expectingLoad; |
| 91 const ReferrerPolicy expectedReferrerPolicy; |
90 } cases[] = { | 92 } cases[] = { |
91 {"http://example.test/cat.jpg", "image", "", "", ResourceLoadPriorityLow, | 93 {"http://example.test/cat.jpg", "image", "", "", "", |
92 WebURLRequest::RequestContextImage, true, true}, | 94 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
93 {"http://example.test/cat.js", "script", "", "", ResourceLoadPriorityHigh, | 95 ReferrerPolicyDefault}, |
94 WebURLRequest::RequestContextScript, true, true}, | 96 {"http://example.test/cat.js", "script", "", "", "", |
95 {"http://example.test/cat.css", "style", "", "", | 97 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, |
| 98 true, ReferrerPolicyDefault}, |
| 99 {"http://example.test/cat.css", "style", "", "", "", |
96 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, | 100 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, |
97 true}, | 101 true, ReferrerPolicyDefault}, |
98 // TODO(yoav): It doesn't seem like the audio context is ever used. That | 102 // TODO(yoav): It doesn't seem like the audio context is ever used. That |
99 // should probably be fixed (or we can consolidate audio and video). | 103 // should probably be fixed (or we can consolidate audio and video). |
100 {"http://example.test/cat.wav", "media", "", "", ResourceLoadPriorityLow, | 104 {"http://example.test/cat.wav", "media", "", "", "", |
101 WebURLRequest::RequestContextVideo, true, true}, | 105 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
102 {"http://example.test/cat.mp4", "media", "", "", ResourceLoadPriorityLow, | 106 ReferrerPolicyDefault}, |
103 WebURLRequest::RequestContextVideo, true, true}, | 107 {"http://example.test/cat.mp4", "media", "", "", "", |
104 {"http://example.test/cat.vtt", "track", "", "", ResourceLoadPriorityLow, | 108 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
105 WebURLRequest::RequestContextTrack, true, true}, | 109 ReferrerPolicyDefault}, |
106 {"http://example.test/cat.woff", "font", "", "", | 110 {"http://example.test/cat.vtt", "track", "", "", "", |
| 111 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, |
| 112 ReferrerPolicyDefault}, |
| 113 {"http://example.test/cat.woff", "font", "", "", "", |
107 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, | 114 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, |
108 true}, | 115 true, ReferrerPolicyDefault}, |
109 // TODO(yoav): subresource should be *very* low priority (rather than | 116 // TODO(yoav): subresource should be *very* low priority (rather than |
110 // low). | 117 // low). |
111 {"http://example.test/cat.empty", "", "", "", ResourceLoadPriorityHigh, | 118 {"http://example.test/cat.empty", "", "", "", "", |
112 WebURLRequest::RequestContextSubresource, true, true}, | 119 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, |
113 {"http://example.test/cat.blob", "blabla", "", "", | 120 true, ReferrerPolicyDefault}, |
| 121 {"http://example.test/cat.blob", "blabla", "", "", "", |
114 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, | 122 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, |
115 false}, | 123 false, ReferrerPolicyDefault}, |
116 {"bla://example.test/cat.gif", "image", "", "", | 124 {"bla://example.test/cat.gif", "image", "", "", "", |
117 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, | 125 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, |
118 false, false}, | 126 false, false, ReferrerPolicyDefault}, |
119 // MIME type tests | 127 // MIME type tests |
120 {"http://example.test/cat.webp", "image", "image/webp", "", | 128 {"http://example.test/cat.webp", "image", "image/webp", "", "", |
121 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true}, | 129 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
122 {"http://example.test/cat.svg", "image", "image/svg+xml", "", | 130 ReferrerPolicyDefault}, |
123 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true}, | 131 {"http://example.test/cat.svg", "image", "image/svg+xml", "", "", |
124 {"http://example.test/cat.jxr", "image", "image/jxr", "", | 132 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
| 133 ReferrerPolicyDefault}, |
| 134 {"http://example.test/cat.jxr", "image", "image/jxr", "", "", |
125 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, | 135 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, |
126 false, false}, | 136 false, false, ReferrerPolicyDefault}, |
127 {"http://example.test/cat.js", "script", "text/javascript", "", | 137 {"http://example.test/cat.js", "script", "text/javascript", "", "", |
128 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, | 138 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, |
129 true}, | 139 true, ReferrerPolicyDefault}, |
130 {"http://example.test/cat.js", "script", "text/coffeescript", "", | 140 {"http://example.test/cat.js", "script", "text/coffeescript", "", "", |
131 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextScript, | 141 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextScript, |
132 false, false}, | 142 false, false, ReferrerPolicyDefault}, |
133 {"http://example.test/cat.css", "style", "text/css", "", | 143 {"http://example.test/cat.css", "style", "text/css", "", "", |
134 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, | 144 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, |
135 true}, | 145 true, ReferrerPolicyDefault}, |
136 {"http://example.test/cat.css", "style", "text/sass", "", | 146 {"http://example.test/cat.css", "style", "text/sass", "", "", |
137 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextStyle, | 147 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextStyle, |
138 false, false}, | 148 false, false, ReferrerPolicyDefault}, |
139 {"http://example.test/cat.wav", "media", "audio/wav", "", | 149 {"http://example.test/cat.wav", "media", "audio/wav", "", "", |
140 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true}, | 150 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
141 {"http://example.test/cat.wav", "media", "audio/mp57", "", | 151 ReferrerPolicyDefault}, |
| 152 {"http://example.test/cat.wav", "media", "audio/mp57", "", "", |
142 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, | 153 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, |
143 false, false}, | 154 false, false, ReferrerPolicyDefault}, |
144 {"http://example.test/cat.webm", "media", "video/webm", "", | 155 {"http://example.test/cat.webm", "media", "video/webm", "", "", |
145 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true}, | 156 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
146 {"http://example.test/cat.mp199", "media", "video/mp199", "", | 157 ReferrerPolicyDefault}, |
| 158 {"http://example.test/cat.mp199", "media", "video/mp199", "", "", |
147 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, | 159 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, |
148 false, false}, | 160 false, false, ReferrerPolicyDefault}, |
149 {"http://example.test/cat.vtt", "track", "text/vtt", "", | 161 {"http://example.test/cat.vtt", "track", "text/vtt", "", "", |
150 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true}, | 162 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, |
151 {"http://example.test/cat.vtt", "track", "text/subtitlething", "", | 163 ReferrerPolicyDefault}, |
| 164 {"http://example.test/cat.vtt", "track", "text/subtitlething", "", "", |
152 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextTrack, | 165 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextTrack, |
153 false, false}, | 166 false, false, ReferrerPolicyDefault}, |
154 {"http://example.test/cat.woff", "font", "font/woff2", "", | 167 {"http://example.test/cat.woff", "font", "font/woff2", "", "", |
155 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, | 168 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, |
156 true}, | 169 true, ReferrerPolicyDefault}, |
157 {"http://example.test/cat.woff", "font", "font/woff84", "", | 170 {"http://example.test/cat.woff", "font", "font/woff84", "", "", |
158 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextFont, false, | 171 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextFont, false, |
159 false}, | 172 false, ReferrerPolicyDefault}, |
160 {"http://example.test/cat.empty", "", "foo/bar", "", | 173 {"http://example.test/cat.empty", "", "foo/bar", "", "", |
161 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, | 174 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, |
162 true}, | 175 true, ReferrerPolicyDefault}, |
163 {"http://example.test/cat.blob", "blabla", "foo/bar", "", | 176 {"http://example.test/cat.blob", "blabla", "foo/bar", "", "", |
164 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, | 177 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, |
165 false}, | 178 false, ReferrerPolicyDefault}, |
166 // Media tests | 179 // Media tests |
167 {"http://example.test/cat.gif", "image", "image/gif", | 180 {"http://example.test/cat.gif", "image", "image/gif", |
168 "(max-width: 600px)", ResourceLoadPriorityLow, | 181 "(max-width: 600px)", "", ResourceLoadPriorityLow, |
169 WebURLRequest::RequestContextImage, true, true}, | 182 WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, |
170 {"http://example.test/cat.gif", "image", "image/gif", | 183 {"http://example.test/cat.gif", "image", "image/gif", |
171 "(max-width: 400px)", ResourceLoadPriorityUnresolved, | 184 "(max-width: 400px)", "", ResourceLoadPriorityUnresolved, |
172 WebURLRequest::RequestContextImage, true, false}, | 185 WebURLRequest::RequestContextImage, true, false, ReferrerPolicyDefault}, |
173 {"http://example.test/cat.gif", "image", "image/gif", | 186 {"http://example.test/cat.gif", "image", "image/gif", |
174 "(max-width: 600px)", ResourceLoadPriorityLow, | 187 "(max-width: 600px)", "", ResourceLoadPriorityLow, |
175 WebURLRequest::RequestContextImage, false, false}, | 188 WebURLRequest::RequestContextImage, false, false, ReferrerPolicyDefault}, |
| 189 // Referrer Policy |
| 190 {"http://example.test/cat.gif", "image", "image/gif", "", "origin", |
| 191 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
| 192 ReferrerPolicyOrigin}, |
| 193 {"http://example.test/cat.gif", "image", "image/gif", "", |
| 194 "origin-when-crossorigin", ResourceLoadPriorityLow, |
| 195 WebURLRequest::RequestContextImage, true, true, |
| 196 ReferrerPolicyOriginWhenCrossOrigin}, |
| 197 {"http://example.test/cat.gif", "image", "image/gif", "", "no-referrer", |
| 198 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
| 199 ReferrerPolicyNever}, |
176 }; | 200 }; |
177 | 201 |
178 // Test the cases with a single header | 202 // Test the cases with a single header |
179 for (const auto& testCase : cases) { | 203 for (const auto& testCase : cases) { |
180 std::unique_ptr<DummyPageHolder> dummyPageHolder = | 204 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
181 DummyPageHolder::create(IntSize(500, 500)); | 205 DummyPageHolder::create(IntSize(500, 500)); |
182 dummyPageHolder->frame().settings()->setScriptEnabled(true); | 206 dummyPageHolder->frame().settings()->setScriptEnabled(true); |
183 Persistent<MockLinkLoaderClient> loaderClient = | 207 Persistent<MockLinkLoaderClient> loaderClient = |
184 MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); | 208 MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); |
185 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 209 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
186 KURL hrefURL = KURL(KURL(), testCase.href); | 210 KURL hrefURL = KURL(KURL(), testCase.href); |
187 URLTestHelpers::registerMockedErrorURLLoad(hrefURL); | 211 URLTestHelpers::registerMockedErrorURLLoad(hrefURL); |
188 loader->loadLink(LinkRelAttribute("preload"), CrossOriginAttributeNotSet, | 212 loader->loadLink(LinkRelAttribute("preload"), CrossOriginAttributeNotSet, |
189 testCase.type, testCase.as, testCase.media, hrefURL, | 213 testCase.type, testCase.as, testCase.media, |
| 214 testCase.referrerPolicy, hrefURL, |
190 dummyPageHolder->document(), NetworkHintsMock()); | 215 dummyPageHolder->document(), NetworkHintsMock()); |
191 ASSERT_TRUE(dummyPageHolder->document().fetcher()); | 216 ASSERT_TRUE(dummyPageHolder->document().fetcher()); |
192 HeapListHashSet<Member<Resource>>* preloads = | 217 HeapListHashSet<Member<Resource>>* preloads = |
193 dummyPageHolder->document().fetcher()->preloads(); | 218 dummyPageHolder->document().fetcher()->preloads(); |
194 if (testCase.expectingLoad) { | 219 if (testCase.expectingLoad) { |
195 if (!preloads) { | 220 if (!preloads) { |
196 fprintf(stderr, "Unexpected result %s %s %s\n", testCase.href, | 221 fprintf(stderr, "Unexpected result %s %s %s\n", testCase.href, |
197 testCase.as, testCase.type); | 222 testCase.as, testCase.type); |
198 } | 223 } |
199 EXPECT_TRUE(preloads); | 224 EXPECT_TRUE(preloads); |
200 } else { | 225 } else { |
201 EXPECT_FALSE(preloads); | 226 EXPECT_FALSE(preloads); |
202 } | 227 } |
203 if (preloads) { | 228 if (preloads) { |
204 if (testCase.priority == ResourceLoadPriorityUnresolved) { | 229 if (testCase.priority == ResourceLoadPriorityUnresolved) { |
205 EXPECT_EQ(0u, preloads->size()); | 230 EXPECT_EQ(0u, preloads->size()); |
206 } else { | 231 } else { |
207 EXPECT_EQ(1u, preloads->size()); | 232 EXPECT_EQ(1u, preloads->size()); |
208 if (preloads->size() > 0) { | 233 if (preloads->size() > 0) { |
209 Resource* resource = preloads->begin().get()->get(); | 234 Resource* resource = preloads->begin().get()->get(); |
210 EXPECT_EQ(testCase.priority, resource->resourceRequest().priority()); | 235 EXPECT_EQ(testCase.priority, resource->resourceRequest().priority()); |
211 EXPECT_EQ(testCase.context, | 236 EXPECT_EQ(testCase.context, |
212 resource->resourceRequest().requestContext()); | 237 resource->resourceRequest().requestContext()); |
| 238 if (testCase.expectedReferrerPolicy != ReferrerPolicyDefault) { |
| 239 EXPECT_EQ(testCase.expectedReferrerPolicy, |
| 240 resource->resourceRequest().getReferrerPolicy()); |
| 241 } |
213 } | 242 } |
214 } | 243 } |
215 dummyPageHolder->document().fetcher()->clearPreloads(); | 244 dummyPageHolder->document().fetcher()->clearPreloads(); |
216 } | 245 } |
217 memoryCache()->evictResources(); | 246 memoryCache()->evictResources(); |
218 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); | 247 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
219 } | 248 } |
220 } | 249 } |
221 | 250 |
222 TEST(LinkLoaderTest, DNSPrefetch) { | 251 TEST(LinkLoaderTest, DNSPrefetch) { |
(...skipping 15 matching lines...) Expand all Loading... |
238 dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); | 267 dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); |
239 Persistent<MockLinkLoaderClient> loaderClient = | 268 Persistent<MockLinkLoaderClient> loaderClient = |
240 MockLinkLoaderClient::create(testCase.shouldLoad); | 269 MockLinkLoaderClient::create(testCase.shouldLoad); |
241 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 270 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
242 KURL hrefURL = | 271 KURL hrefURL = |
243 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), | 272 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), |
244 testCase.href); | 273 testCase.href); |
245 NetworkHintsMock networkHints; | 274 NetworkHintsMock networkHints; |
246 loader->loadLink(LinkRelAttribute("dns-prefetch"), | 275 loader->loadLink(LinkRelAttribute("dns-prefetch"), |
247 CrossOriginAttributeNotSet, String(), String(), String(), | 276 CrossOriginAttributeNotSet, String(), String(), String(), |
248 hrefURL, dummyPageHolder->document(), networkHints); | 277 String(), hrefURL, dummyPageHolder->document(), |
| 278 networkHints); |
249 EXPECT_FALSE(networkHints.didPreconnect()); | 279 EXPECT_FALSE(networkHints.didPreconnect()); |
250 EXPECT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); | 280 EXPECT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); |
251 } | 281 } |
252 } | 282 } |
253 | 283 |
254 TEST(LinkLoaderTest, Preconnect) { | 284 TEST(LinkLoaderTest, Preconnect) { |
255 struct { | 285 struct { |
256 const char* href; | 286 const char* href; |
257 CrossOriginAttributeValue crossOrigin; | 287 CrossOriginAttributeValue crossOrigin; |
258 const bool shouldLoad; | 288 const bool shouldLoad; |
(...skipping 11 matching lines...) Expand all Loading... |
270 std::unique_ptr<DummyPageHolder> dummyPageHolder = | 300 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
271 DummyPageHolder::create(IntSize(500, 500)); | 301 DummyPageHolder::create(IntSize(500, 500)); |
272 Persistent<MockLinkLoaderClient> loaderClient = | 302 Persistent<MockLinkLoaderClient> loaderClient = |
273 MockLinkLoaderClient::create(testCase.shouldLoad); | 303 MockLinkLoaderClient::create(testCase.shouldLoad); |
274 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 304 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
275 KURL hrefURL = | 305 KURL hrefURL = |
276 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), | 306 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), |
277 testCase.href); | 307 testCase.href); |
278 NetworkHintsMock networkHints; | 308 NetworkHintsMock networkHints; |
279 loader->loadLink(LinkRelAttribute("preconnect"), testCase.crossOrigin, | 309 loader->loadLink(LinkRelAttribute("preconnect"), testCase.crossOrigin, |
280 String(), String(), String(), hrefURL, | 310 String(), String(), String(), String(), hrefURL, |
281 dummyPageHolder->document(), networkHints); | 311 dummyPageHolder->document(), networkHints); |
282 EXPECT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); | 312 EXPECT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); |
283 EXPECT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); | 313 EXPECT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); |
284 EXPECT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); | 314 EXPECT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); |
285 } | 315 } |
286 } | 316 } |
287 | 317 |
288 } // namespace blink | 318 } // namespace blink |
OLD | NEW |