// Regression test for crbug.com/645331, where a resource client gets callbacks // after the document is shutdown and we have no frame. TEST_F(CSSPreloadScannerTest, DoNotExpectValidDocument) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(dummyPageHolder->document()); KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); resource->setStatus(Resource::Pending); PreloadSuppressingCSSPreloaderResourceClient* resourceClient = new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); dummyPageHolder->document().shutdown(); const char* data = "@import url('http://127.0.0.1/preload.css');"; resource->appendData(data, strlen(data)); EXPECT_EQ(1u, resourceClient->m_preloads.size()); EXPECT_EQ("http://127.0.0.1/preload.css", resourceClient->m_preloads.first()->resourceURL()); }
// Regression test for crbug.com/645331, where a resource client gets callbacks // after the document is shutdown and we have no frame. TEST_F(CSSPreloadScannerTest, DoNotExpectValidDocument) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); MockHTMLResourcePreloader* preloader = new MockHTMLResourcePreloader(dummyPageHolder->document()); KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); resource->setStatus(Resource::Pending); PreloadRecordingCSSPreloaderResourceClient* resourceClient = new PreloadRecordingCSSPreloaderResourceClient(resource, preloader); dummyPageHolder->document().shutdown(); const char* data = "@import url('http://127.0.0.1/preload.css');"; resource->appendData(data, strlen(data)); // Do not expect to gather any preloads, as the document loader is invalid, // which means we can't notify WebLoadingBehaviorData of the preloads. EXPECT_EQ(0u, resourceClient->m_preloadUrls.size()); }
CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request, ResourceFetcher* fetcher) { DCHECK_EQ(request.resourceRequest().frameType(), WebURLRequest::FrameTypeNone); request.mutableResourceRequest().setRequestContext( WebURLRequest::RequestContextStyle); CSSStyleSheetResource* resource = toCSSStyleSheetResource( fetcher->requestResource(request, CSSStyleSheetResourceFactory())); // TODO(kouhei): Dedupe this logic w/ ScriptResource::fetch if (resource && !request.integrityMetadata().isEmpty()) resource->setIntegrityMetadata(request.integrityMetadata()); return resource; }
// Regression test for crbug.com/646869 where the client's data is cleared // before didAppendFirstData is called. TEST_F(CSSPreloadScannerTest, DontReadFromClearedData) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(dummyPageHolder->document()); KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); const char* data = "@import url('http://127.0.0.1/preload.css');"; resource->appendData(data, strlen(data)); ResourceError error(errorDomainBlinkInternal, 0, url.getString(), ""); resource->error(error); // Should not crash. PreloadSuppressingCSSPreloaderResourceClient* resourceClient = new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); EXPECT_EQ(0u, resourceClient->m_preloads.size()); }
TEST_F(CSSPreloadScannerTest, ScanFromResourceClient) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); MockHTMLResourcePreloader* preloader = new MockHTMLResourcePreloader(dummyPageHolder->document()); KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); resource->setStatus(Resource::Pending); PreloadRecordingCSSPreloaderResourceClient* resourceClient = new PreloadRecordingCSSPreloaderResourceClient(resource, preloader); const char* data = "@import url('http://127.0.0.1/preload.css');"; resource->appendData(data, strlen(data)); EXPECT_EQ(Resource::PreloadNotReferenced, resource->getPreloadResult()); EXPECT_EQ(1u, resourceClient->m_preloadUrls.size()); EXPECT_EQ("http://127.0.0.1/preload.css", resourceClient->m_preloadUrls.front()); }