Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: third_party/WebKit/Source/core/fetch/CSSStyleSheetResource.cpp

Issue 1807323002: [WeakMemoryCache 1a] Make Reference from Inspector to Resource weak, remove removedFromMemoryCache() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. 6 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
7 7
8 This library is free software; you can redistribute it and/or 8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public 9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either 10 License as published by the Free Software Foundation; either
(...skipping 10 matching lines...) Expand all
21 Boston, MA 02110-1301, USA. 21 Boston, MA 02110-1301, USA.
22 22
23 This class provides all functionality needed for loading images, style sheet s and html 23 This class provides all functionality needed for loading images, style sheet s and html
24 pages from the web. It has a memory cache for these objects. 24 pages from the web. It has a memory cache for these objects.
25 */ 25 */
26 26
27 #include "core/fetch/CSSStyleSheetResource.h" 27 #include "core/fetch/CSSStyleSheetResource.h"
28 28
29 #include "core/css/StyleSheetContents.h" 29 #include "core/css/StyleSheetContents.h"
30 #include "core/fetch/FetchRequest.h" 30 #include "core/fetch/FetchRequest.h"
31 #include "core/fetch/MemoryCache.h"
32 #include "core/fetch/ResourceClientOrObserverWalker.h" 31 #include "core/fetch/ResourceClientOrObserverWalker.h"
33 #include "core/fetch/ResourceFetcher.h" 32 #include "core/fetch/ResourceFetcher.h"
34 #include "core/fetch/StyleSheetResourceClient.h" 33 #include "core/fetch/StyleSheetResourceClient.h"
35 #include "platform/SharedBuffer.h" 34 #include "platform/SharedBuffer.h"
36 #include "wtf/CurrentTime.h" 35 #include "wtf/CurrentTime.h"
37 36
38 namespace blink { 37 namespace blink {
39 38
40 CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request, Resou rceFetcher* fetcher) 39 CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request, Resou rceFetcher* fetcher)
41 { 40 {
42 ASSERT(request.resourceRequest().frameType() == WebURLRequest::FrameTypeNone ); 41 ASSERT(request.resourceRequest().frameType() == WebURLRequest::FrameTypeNone );
43 request.mutableResourceRequest().setRequestContext(WebURLRequest::RequestCon textStyle); 42 request.mutableResourceRequest().setRequestContext(WebURLRequest::RequestCon textStyle);
44 return toCSSStyleSheetResource(fetcher->requestResource(request, CSSStyleShe etResourceFactory())); 43 return toCSSStyleSheetResource(fetcher->requestResource(request, CSSStyleShe etResourceFactory()));
45 } 44 }
46 45
47 CSSStyleSheetResource* CSSStyleSheetResource::createForTest(const ResourceReques t& request, const String& charset) 46 CSSStyleSheetResource* CSSStyleSheetResource::createForTest(const ResourceReques t& request, const String& charset)
48 { 47 {
49 return new CSSStyleSheetResource(request, ResourceLoaderOptions(), charset); 48 return new CSSStyleSheetResource(request, ResourceLoaderOptions(), charset);
50 } 49 }
51 50
52 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest& resourceRequ est, const ResourceLoaderOptions& options, const String& charset) 51 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest& resourceRequ est, const ResourceLoaderOptions& options, const String& charset)
53 : StyleSheetResource(resourceRequest, CSSStyleSheet, options, "text/css", ch arset) 52 : StyleSheetResource(resourceRequest, CSSStyleSheet, options, "text/css", ch arset)
54 { 53 {
55 } 54 }
56 55
57 CSSStyleSheetResource::~CSSStyleSheetResource() 56 CSSStyleSheetResource::~CSSStyleSheetResource()
58 { 57 {
59 } 58 }
60 59
61 void CSSStyleSheetResource::removedFromMemoryCache() 60 void CSSStyleSheetResource::willDestroyResourceInternal()
61 {
62 setParsedStyleSheetCache(nullptr);
63 }
64
65 void CSSStyleSheetResource::setParsedStyleSheetCache(StyleSheetContents* newShee t)
62 { 66 {
63 if (m_parsedStyleSheetCache) 67 if (m_parsedStyleSheetCache)
64 m_parsedStyleSheetCache->removedFromMemoryCache(); 68 m_parsedStyleSheetCache->setReferencedFromResource(false);
65 m_parsedStyleSheetCache.clear(); 69 m_parsedStyleSheetCache = newSheet;
66 Resource::removedFromMemoryCache(); 70 if (m_parsedStyleSheetCache)
71 m_parsedStyleSheetCache->setReferencedFromResource(true);
67 } 72 }
68 73
69 DEFINE_TRACE(CSSStyleSheetResource) 74 DEFINE_TRACE(CSSStyleSheetResource)
70 { 75 {
71 visitor->trace(m_parsedStyleSheetCache); 76 visitor->trace(m_parsedStyleSheetCache);
72 StyleSheetResource::trace(visitor); 77 StyleSheetResource::trace(visitor);
73 } 78 }
74 79
75 void CSSStyleSheetResource::didAddClient(ResourceClient* c) 80 void CSSStyleSheetResource::didAddClient(ResourceClient* c)
76 { 81 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 bool CSSStyleSheetResource::isSafeToUnlock() const 119 bool CSSStyleSheetResource::isSafeToUnlock() const
115 { 120 {
116 return m_data->hasOneRef(); 121 return m_data->hasOneRef();
117 } 122 }
118 123
119 void CSSStyleSheetResource::destroyDecodedDataIfPossible() 124 void CSSStyleSheetResource::destroyDecodedDataIfPossible()
120 { 125 {
121 if (!m_parsedStyleSheetCache) 126 if (!m_parsedStyleSheetCache)
122 return; 127 return;
123 128
124 m_parsedStyleSheetCache->removedFromMemoryCache(); 129 setParsedStyleSheetCache(nullptr);
125 m_parsedStyleSheetCache.clear();
126
127 setDecodedSize(0); 130 setDecodedSize(0);
128 } 131 }
129 132
130 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const 133 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const
131 { 134 {
132 if (errorOccurred()) 135 if (errorOccurred())
133 return false; 136 return false;
134 137
135 // This check exactly matches Firefox. Note that we grab the Content-Type 138 // This check exactly matches Firefox. Note that we grab the Content-Type
136 // header directly because we want to see what the value is BEFORE content 139 // header directly because we want to see what the value is BEFORE content
137 // sniffing. Firefox does this by setting a "type hint" on the channel. 140 // sniffing. Firefox does this by setting a "type hint" on the channel.
138 // This implementation should be observationally equivalent. 141 // This implementation should be observationally equivalent.
139 // 142 //
140 // This code defaults to allowing the stylesheet for non-HTTP protocols so 143 // This code defaults to allowing the stylesheet for non-HTTP protocols so
141 // folks can use standards mode for local HTML documents. 144 // folks can use standards mode for local HTML documents.
142 if (mimeTypeCheck == MIMETypeCheck::Lax) 145 if (mimeTypeCheck == MIMETypeCheck::Lax)
143 return true; 146 return true;
144 AtomicString contentType = httpContentType(); 147 AtomicString contentType = httpContentType();
145 return contentType.isEmpty() || equalIgnoringCase(contentType, "text/css") | | equalIgnoringCase(contentType, "application/x-unknown-content-type"); 148 return contentType.isEmpty() || equalIgnoringCase(contentType, "text/css") | | equalIgnoringCase(contentType, "application/x-unknown-content-type");
146 } 149 }
147 150
148 StyleSheetContents* CSSStyleSheetResource::restoreParsedStyleSheet(const CSSPars erContext& context) 151 StyleSheetContents* CSSStyleSheetResource::restoreParsedStyleSheet(const CSSPars erContext& context)
149 { 152 {
150 if (!m_parsedStyleSheetCache) 153 if (!m_parsedStyleSheetCache)
151 return nullptr; 154 return nullptr;
152 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) { 155 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) {
153 m_parsedStyleSheetCache->removedFromMemoryCache(); 156 setParsedStyleSheetCache(nullptr);
154 m_parsedStyleSheetCache.clear();
155 return nullptr; 157 return nullptr;
156 } 158 }
157 159
158 ASSERT(m_parsedStyleSheetCache->isCacheable()); 160 ASSERT(m_parsedStyleSheetCache->isCacheable());
159 ASSERT(m_parsedStyleSheetCache->isInMemoryCache()); 161 ASSERT(m_parsedStyleSheetCache->isReferencedFromResource());
160 162
161 // Contexts must be identical so we know we would get the same exact result if we parsed again. 163 // Contexts must be identical so we know we would get the same exact result if we parsed again.
162 if (m_parsedStyleSheetCache->parserContext() != context) 164 if (m_parsedStyleSheetCache->parserContext() != context)
163 return nullptr; 165 return nullptr;
164 166
165 didAccessDecodedData(); 167 didAccessDecodedData();
166 168
167 return m_parsedStyleSheetCache; 169 return m_parsedStyleSheetCache;
168 } 170 }
169 171
170 void CSSStyleSheetResource::saveParsedStyleSheet(StyleSheetContents* sheet) 172 void CSSStyleSheetResource::saveParsedStyleSheet(StyleSheetContents* sheet)
171 { 173 {
172 ASSERT(sheet && sheet->isCacheable()); 174 ASSERT(sheet && sheet->isCacheable());
173 175
174 if (m_parsedStyleSheetCache) 176 setParsedStyleSheetCache(sheet);
175 m_parsedStyleSheetCache->removedFromMemoryCache();
176 m_parsedStyleSheetCache = sheet;
177
178 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes()); 177 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes());
179
180 // Check if this stylesheet resource didn't conflict with
181 // another resource and has indeed been added to the cache.
182 if (memoryCache()->contains(this))
183 m_parsedStyleSheetCache->addedToMemoryCache();
184 } 178 }
185 179
186 } // namespace blink 180 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698