OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/html/parser/CSSPreloadScanner.h" | 5 #include "core/html/parser/CSSPreloadScanner.h" |
6 | 6 |
7 #include "core/fetch/FetchContext.h" | 7 #include "core/fetch/FetchContext.h" |
8 #include "core/fetch/Resource.h" | 8 #include "core/fetch/Resource.h" |
9 #include "core/fetch/ResourceFetcher.h" | 9 #include "core/fetch/ResourceFetcher.h" |
10 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
11 #include "core/html/parser/HTMLResourcePreloader.h" | 11 #include "core/html/parser/HTMLResourcePreloader.h" |
12 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
13 #include "platform/heap/Heap.h" | 13 #include "platform/heap/Heap.h" |
| 14 #include "platform/network/ResourceError.h" |
14 #include "platform/network/ResourceRequest.h" | 15 #include "platform/network/ResourceRequest.h" |
15 #include "platform/weborigin/KURL.h" | 16 #include "platform/weborigin/KURL.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include <memory> | 18 #include <memory> |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 | 21 |
21 class PreloadSuppressingCSSPreloaderResourceClient final | 22 class PreloadSuppressingCSSPreloaderResourceClient final |
22 : public CSSPreloaderResourceClient { | 23 : public CSSPreloaderResourceClient { |
23 public: | 24 public: |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); | 81 new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); |
81 | 82 |
82 // Destroys the resourceClient. | 83 // Destroys the resourceClient. |
83 ThreadState::current()->collectAllGarbage(); | 84 ThreadState::current()->collectAllGarbage(); |
84 | 85 |
85 const char* data = "@import url('http://127.0.0.1/preload.css');"; | 86 const char* data = "@import url('http://127.0.0.1/preload.css');"; |
86 // Should not crash. | 87 // Should not crash. |
87 resource->appendData(data, strlen(data)); | 88 resource->appendData(data, strlen(data)); |
88 } | 89 } |
89 | 90 |
| 91 // Regression test for crbug.com/646869 where the client's data is cleared |
| 92 // before didAppendFirstData is called. |
| 93 TEST_F(CSSPreloadScannerTest, DontReadFromClearedData) { |
| 94 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
| 95 DummyPageHolder::create(IntSize(500, 500)); |
| 96 dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); |
| 97 |
| 98 HTMLResourcePreloader* preloader = |
| 99 HTMLResourcePreloader::create(dummyPageHolder->document()); |
| 100 |
| 101 KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); |
| 102 CSSStyleSheetResource* resource = |
| 103 CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); |
| 104 |
| 105 const char* data = "@import url('http://127.0.0.1/preload.css');"; |
| 106 resource->appendData(data, strlen(data)); |
| 107 ResourceError error(errorDomainBlinkInternal, 0, url.getString(), ""); |
| 108 resource->error(error); |
| 109 |
| 110 // Should not crash. |
| 111 PreloadSuppressingCSSPreloaderResourceClient* resourceClient = |
| 112 new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); |
| 113 |
| 114 EXPECT_EQ(0u, resourceClient->m_preloads.size()); |
| 115 } |
| 116 |
| 117 // Regression test for crbug.com/645331, where a resource client gets callbacks |
| 118 // after the document is shutdown and we have no frame. |
| 119 TEST_F(CSSPreloadScannerTest, DoNotExpectValidDocument) { |
| 120 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
| 121 DummyPageHolder::create(IntSize(500, 500)); |
| 122 dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true); |
| 123 |
| 124 HTMLResourcePreloader* preloader = |
| 125 HTMLResourcePreloader::create(dummyPageHolder->document()); |
| 126 |
| 127 KURL url(ParsedURLString, "http://127.0.0.1/foo.css"); |
| 128 CSSStyleSheetResource* resource = |
| 129 CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8"); |
| 130 resource->setStatus(Resource::Pending); |
| 131 |
| 132 PreloadSuppressingCSSPreloaderResourceClient* resourceClient = |
| 133 new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader); |
| 134 |
| 135 dummyPageHolder->document().shutdown(); |
| 136 |
| 137 const char* data = "@import url('http://127.0.0.1/preload.css');"; |
| 138 resource->appendData(data, strlen(data)); |
| 139 |
| 140 EXPECT_EQ(1u, resourceClient->m_preloads.size()); |
| 141 EXPECT_EQ("http://127.0.0.1/preload.css", |
| 142 resourceClient->m_preloads.first()->resourceURL()); |
| 143 } |
| 144 |
90 } // namespace blink | 145 } // namespace blink |
OLD | NEW |