OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/html/parser/CSSPreloadScanner.h" |
| 6 |
| 7 #include "core/fetch/FetchContext.h" |
| 8 #include "core/fetch/Resource.h" |
| 9 #include "core/fetch/ResourceFetcher.h" |
| 10 #include "core/frame/Settings.h" |
| 11 #include "core/html/parser/HTMLResourcePreloader.h" |
| 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include "platform/heap/Heap.h" |
| 14 #include "platform/network/ResourceRequest.h" |
| 15 #include "platform/weborigin/KURL.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include <memory> |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 class PreloadSuppressingCSSPreloaderResourceClient final : public CSSPreloaderRe
sourceClient { |
| 22 public: |
| 23 PreloadSuppressingCSSPreloaderResourceClient(Resource* resource, HTMLResourc
ePreloader* preloader) |
| 24 : CSSPreloaderResourceClient(resource, preloader) {} |
| 25 void fetchPreloads(PreloadRequestStream& preloads) override |
| 26 { |
| 27 PreloadRequestStream movedPreloads; |
| 28 movedPreloads.swap(preloads); |
| 29 for (PreloadRequestStream::iterator it = movedPreloads.begin(); it != mo
vedPreloads.end(); ++it) { |
| 30 m_preloads.append(std::move(*it)); |
| 31 } |
| 32 } |
| 33 |
| 34 PreloadRequestStream m_preloads; |
| 35 }; |
| 36 |
| 37 class CSSPreloadScannerTest : public ::testing::Test { |
| 38 }; |
| 39 |
| 40 TEST_F(CSSPreloadScannerTest, ScanFromResourceClient) |
| 41 { |
| 42 std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(I
ntSize(500, 500)); |
| 43 dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true)
; |
| 44 |
| 45 HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(dummyPageHo
lder->document()); |
| 46 |
| 47 KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); |
| 48 CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(Resou
rceRequest(url), "utf-8"); |
| 49 resource->setStatus(Resource::Pending); |
| 50 |
| 51 PreloadSuppressingCSSPreloaderResourceClient* resourceClient = new PreloadSu
ppressingCSSPreloaderResourceClient(resource, preloader); |
| 52 |
| 53 const char* data = "@import url('http://127.0.0.1/preload.css');"; |
| 54 resource->appendData(data, strlen(data)); |
| 55 |
| 56 EXPECT_EQ(1u, resourceClient->m_preloads.size()); |
| 57 EXPECT_EQ("http://127.0.0.1/preload.css", resourceClient->m_preloads.first()
->resourceURL()); |
| 58 } |
| 59 |
| 60 // Regression test for crbug.com/608310 where the client is destroyed but was |
| 61 // not removed from the resource's client list. |
| 62 TEST_F(CSSPreloadScannerTest, DestroyClientBeforeDataSent) |
| 63 { |
| 64 std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(I
ntSize(500, 500)); |
| 65 dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true)
; |
| 66 |
| 67 Persistent<HTMLResourcePreloader> preloader = HTMLResourcePreloader::create(
dummyPageHolder->document()); |
| 68 |
| 69 KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); |
| 70 Persistent<CSSStyleSheetResource> resource = CSSStyleSheetResource::createFo
rTest(ResourceRequest(url), "utf-8"); |
| 71 resource->setStatus(Resource::Pending); |
| 72 |
| 73 new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); |
| 74 |
| 75 // Destroys the resourceClient. |
| 76 ThreadHeap::collectAllGarbage(); |
| 77 |
| 78 const char* data = "@import url('http://127.0.0.1/preload.css');"; |
| 79 // Should not crash. |
| 80 resource->appendData(data, strlen(data)); |
| 81 } |
| 82 |
| 83 } // namespace blink |
OLD | NEW |