| 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/fetch/CSSStyleSheetResource.h" | 5 #include "core/fetch/CSSStyleSheetResource.h" |
| 6 | 6 |
| 7 #include "core/css/CSSCrossfadeValue.h" | 7 #include "core/css/CSSCrossfadeValue.h" |
| 8 #include "core/css/CSSImageValue.h" | 8 #include "core/css/CSSImageValue.h" |
| 9 #include "core/css/CSSPrimitiveValue.h" | 9 #include "core/css/CSSPrimitiveValue.h" |
| 10 #include "core/css/CSSProperty.h" | 10 #include "core/css/CSSProperty.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 { | 63 { |
| 64 { | 64 { |
| 65 // We need this scope to remove all references. | 65 // We need this scope to remove all references. |
| 66 KURL imageURL(KURL(), "https://localhost/image"); | 66 KURL imageURL(KURL(), "https://localhost/image"); |
| 67 KURL cssURL(KURL(), "https://localhost/style.css"); | 67 KURL cssURL(KURL(), "https://localhost/style.css"); |
| 68 | 68 |
| 69 // We need to disable loading because we manually give a response to | 69 // We need to disable loading because we manually give a response to |
| 70 // the image resource. | 70 // the image resource. |
| 71 document()->fetcher()->setAutoLoadImages(false); | 71 document()->fetcher()->setAutoLoadImages(false); |
| 72 | 72 |
| 73 RawPtr<CSSStyleSheetResource> cssResource = CSSStyleSheetResource::creat
eForTest(ResourceRequest(cssURL), "utf-8"); | 73 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTes
t(ResourceRequest(cssURL), "utf-8"); |
| 74 memoryCache()->add(cssResource.get()); | 74 memoryCache()->add(cssResource); |
| 75 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n
ullAtom, String()), nullptr); | 75 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n
ullAtom, String()), nullptr); |
| 76 cssResource->finish(); | 76 cssResource->finish(); |
| 77 | 77 |
| 78 RawPtr<StyleSheetContents> contents = StyleSheetContents::create(CSSPars
erContext(HTMLStandardMode, nullptr)); | 78 StyleSheetContents* contents = StyleSheetContents::create(CSSParserConte
xt(HTMLStandardMode, nullptr)); |
| 79 RawPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(contents, document()
); | 79 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); |
| 80 EXPECT_TRUE(sheet); | 80 EXPECT_TRUE(sheet); |
| 81 RawPtr<CSSCrossfadeValue> crossfade = CSSCrossfadeValue::create( | 81 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create( |
| 82 CSSImageValue::create(String("image"), imageURL), | 82 CSSImageValue::create(String("image"), imageURL), |
| 83 CSSImageValue::create(String("image"), imageURL), | 83 CSSImageValue::create(String("image"), imageURL), |
| 84 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number))
; | 84 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number))
; |
| 85 Vector<OwnPtr<CSSParserSelector>> selectors; | 85 Vector<OwnPtr<CSSParserSelector>> selectors; |
| 86 selectors.append(adoptPtr(new CSSParserSelector())); | 86 selectors.append(adoptPtr(new CSSParserSelector())); |
| 87 selectors[0]->setMatch(CSSSelector::Id); | 87 selectors[0]->setMatch(CSSSelector::Id); |
| 88 selectors[0]->setValue("foo"); | 88 selectors[0]->setValue("foo"); |
| 89 CSSProperty property(CSSPropertyBackground, crossfade); | 89 CSSProperty property(CSSPropertyBackground, crossfade); |
| 90 contents->parserAppendRule( | 90 contents->parserAppendRule( |
| 91 StyleRule::create(CSSSelectorList::adoptSelectorVector(selectors), I
mmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); | 91 StyleRule::create(CSSSelectorList::adoptSelectorVector(selectors), I
mmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); |
| 92 | 92 |
| 93 crossfade->loadSubimages(document()); | 93 crossfade->loadSubimages(document()); |
| 94 RawPtr<Resource> imageResource = memoryCache()->resourceForURL(imageURL,
MemoryCache::defaultCacheIdentifier()); | 94 Resource* imageResource = memoryCache()->resourceForURL(imageURL, Memory
Cache::defaultCacheIdentifier()); |
| 95 ASSERT_TRUE(imageResource); | 95 ASSERT_TRUE(imageResource); |
| 96 ResourceResponse imageResponse; | 96 ResourceResponse imageResponse; |
| 97 imageResponse.setURL(imageURL); | 97 imageResponse.setURL(imageURL); |
| 98 imageResponse.setHTTPHeaderField("cache-control", "no-store"); | 98 imageResponse.setHTTPHeaderField("cache-control", "no-store"); |
| 99 imageResource->responseReceived(imageResponse, nullptr); | 99 imageResource->responseReceived(imageResponse, nullptr); |
| 100 | 100 |
| 101 contents->checkLoaded(); | 101 contents->checkLoaded(); |
| 102 cssResource->saveParsedStyleSheet(contents); | 102 cssResource->saveParsedStyleSheet(contents); |
| 103 | 103 |
| 104 memoryCache()->update(cssResource.get(), cssResource->size(), cssResourc
e->size(), false); | 104 memoryCache()->update(cssResource, cssResource->size(), cssResource->siz
e(), false); |
| 105 memoryCache()->update(imageResource.get(), imageResource->size(), imageR
esource->size(), false); | 105 memoryCache()->update(imageResource, imageResource->size(), imageResourc
e->size(), false); |
| 106 if (!memoryCache()->isInSameLRUListForTest(cssResource.get(), imageResou
rce.get())) { | 106 if (!memoryCache()->isInSameLRUListForTest(cssResource, imageResource))
{ |
| 107 // We assume that the LRU list is determined by |size / accessCount|
. | 107 // We assume that the LRU list is determined by |size / accessCount|
. |
| 108 for (size_t i = 0; i < cssResource->size() + 1; ++i) | 108 for (size_t i = 0; i < cssResource->size() + 1; ++i) |
| 109 memoryCache()->update(cssResource.get(), cssResource->size(), cs
sResource->size(), true); | 109 memoryCache()->update(cssResource, cssResource->size(), cssResou
rce->size(), true); |
| 110 for (size_t i = 0; i < imageResource->size() + 1; ++i) | 110 for (size_t i = 0; i < imageResource->size() + 1; ++i) |
| 111 memoryCache()->update(imageResource.get(), imageResource->size()
, imageResource->size(), true); | 111 memoryCache()->update(imageResource, imageResource->size(), imag
eResource->size(), true); |
| 112 } | 112 } |
| 113 ASSERT_TRUE(memoryCache()->isInSameLRUListForTest(cssResource.get(), ima
geResource.get())); | 113 ASSERT_TRUE(memoryCache()->isInSameLRUListForTest(cssResource, imageReso
urce)); |
| 114 } | 114 } |
| 115 Heap::collectAllGarbage(); | 115 Heap::collectAllGarbage(); |
| 116 // This operation should not lead to crash! | 116 // This operation should not lead to crash! |
| 117 memoryCache()->pruneAll(); | 117 memoryCache()->pruneAll(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached) | 120 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached) |
| 121 { | 121 { |
| 122 const char url[] = "https://localhost/style.css"; | 122 const char url[] = "https://localhost/style.css"; |
| 123 KURL imageURL(KURL(), url); | 123 KURL imageURL(KURL(), url); |
| 124 KURL cssURL(KURL(), url); | 124 KURL cssURL(KURL(), url); |
| 125 | 125 |
| 126 // Emulate using <img> to do async stylesheet preloads. | 126 // Emulate using <img> to do async stylesheet preloads. |
| 127 | 127 |
| 128 RawPtr<Resource> imageResource = ImageResource::create(ResourceRequest(image
URL), nullptr); | 128 Resource* imageResource = ImageResource::create(ResourceRequest(imageURL), n
ullptr); |
| 129 ASSERT_TRUE(imageResource); | 129 ASSERT_TRUE(imageResource); |
| 130 memoryCache()->add(imageResource.get()); | 130 memoryCache()->add(imageResource); |
| 131 ASSERT_TRUE(memoryCache()->contains(imageResource.get())); | 131 ASSERT_TRUE(memoryCache()->contains(imageResource)); |
| 132 | 132 |
| 133 RawPtr<CSSStyleSheetResource> cssResource = CSSStyleSheetResource::createFor
Test(ResourceRequest(cssURL), "utf-8"); | 133 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTest(Re
sourceRequest(cssURL), "utf-8"); |
| 134 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, nullA
tom, String()), nullptr); | 134 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, nullA
tom, String()), nullptr); |
| 135 cssResource->finish(); | 135 cssResource->finish(); |
| 136 | 136 |
| 137 RawPtr<StyleSheetContents> contents = StyleSheetContents::create(CSSParserCo
ntext(HTMLStandardMode, nullptr)); | 137 StyleSheetContents* contents = StyleSheetContents::create(CSSParserContext(H
TMLStandardMode, nullptr)); |
| 138 RawPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(contents, document()); | 138 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); |
| 139 EXPECT_TRUE(sheet); | 139 EXPECT_TRUE(sheet); |
| 140 | 140 |
| 141 contents->checkLoaded(); | 141 contents->checkLoaded(); |
| 142 cssResource->saveParsedStyleSheet(contents); | 142 cssResource->saveParsedStyleSheet(contents); |
| 143 | 143 |
| 144 // Verify that the cache will have a mapping for |imageResource| at |url|. | 144 // Verify that the cache will have a mapping for |imageResource| at |url|. |
| 145 // The underlying |contents| for the stylesheet resource must have a | 145 // The underlying |contents| for the stylesheet resource must have a |
| 146 // matching cache status. | 146 // matching cache status. |
| 147 ASSERT_TRUE(memoryCache()->contains(imageResource.get())); | 147 ASSERT_TRUE(memoryCache()->contains(imageResource)); |
| 148 ASSERT_FALSE(memoryCache()->contains(cssResource.get())); | 148 ASSERT_FALSE(memoryCache()->contains(cssResource)); |
| 149 ASSERT_FALSE(contents->isInMemoryCache()); | 149 ASSERT_FALSE(contents->isInMemoryCache()); |
| 150 } | 150 } |
| 151 | 151 |
| 152 } // namespace | 152 } // namespace |
| 153 } // namespace blink | 153 } // namespace blink |
| OLD | NEW |