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

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

Issue 2290983003: CSSStyleSheetResource should cache decoded text instead of raw bytes (Closed)
Patch Set: add dcheck Created 4 years, 2 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 25 matching lines...) Expand all
36 #include "wtf/CurrentTime.h" 36 #include "wtf/CurrentTime.h"
37 37
38 namespace blink { 38 namespace blink {
39 39
40 CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request, 40 CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request,
41 ResourceFetcher* fetcher) { 41 ResourceFetcher* fetcher) {
42 DCHECK_EQ(request.resourceRequest().frameType(), 42 DCHECK_EQ(request.resourceRequest().frameType(),
43 WebURLRequest::FrameTypeNone); 43 WebURLRequest::FrameTypeNone);
44 request.mutableResourceRequest().setRequestContext( 44 request.mutableResourceRequest().setRequestContext(
45 WebURLRequest::RequestContextStyle); 45 WebURLRequest::RequestContextStyle);
46 return toCSSStyleSheetResource( 46 CSSStyleSheetResource* resource = toCSSStyleSheetResource(
47 fetcher->requestResource(request, CSSStyleSheetResourceFactory())); 47 fetcher->requestResource(request, CSSStyleSheetResourceFactory()));
48 if (resource && !request.integrityMetadata().isEmpty())
49 resource->setIntegrityMetadata(request.integrityMetadata());
Charlie Harrison 2016/10/05 15:24:48 It feels like setting the integrity metadata shoul
jww 2016/10/06 01:55:04 I wrote this code a while back, so my memory isn't
Charlie Harrison 2016/10/06 03:59:08 I think you're right about calculating the integri
kouhei (in TOK) 2016/10/06 12:02:36 Let me try this in separate CL. Added a TODO comme
50 return resource;
48 } 51 }
49 52
50 CSSStyleSheetResource* CSSStyleSheetResource::createForTest( 53 CSSStyleSheetResource* CSSStyleSheetResource::createForTest(
51 const ResourceRequest& request, 54 const ResourceRequest& request,
52 const String& charset) { 55 const String& charset) {
53 return new CSSStyleSheetResource(request, ResourceLoaderOptions(), charset); 56 return new CSSStyleSheetResource(request, ResourceLoaderOptions(), charset);
54 } 57 }
55 58
56 CSSStyleSheetResource::CSSStyleSheetResource( 59 CSSStyleSheetResource::CSSStyleSheetResource(
57 const ResourceRequest& resourceRequest, 60 const ResourceRequest& resourceRequest,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 static_cast<StyleSheetResourceClient*>(c)->didAppendFirstData(this); 95 static_cast<StyleSheetResourceClient*>(c)->didAppendFirstData(this);
93 96
94 // |c| might be removed in didAppendFirstData, so ensure it is still a client. 97 // |c| might be removed in didAppendFirstData, so ensure it is still a client.
95 if (hasClient(c) && !isLoading()) 98 if (hasClient(c) && !isLoading())
96 static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet( 99 static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet(
97 resourceRequest().url(), response().url(), encoding(), this); 100 resourceRequest().url(), response().url(), encoding(), this);
98 } 101 }
99 102
100 const String CSSStyleSheetResource::sheetText( 103 const String CSSStyleSheetResource::sheetText(
101 MIMETypeCheck mimeTypeCheck) const { 104 MIMETypeCheck mimeTypeCheck) const {
102 if (!data() || data()->isEmpty() || !canUseSheet(mimeTypeCheck)) 105 if (!canUseSheet(mimeTypeCheck))
103 return String(); 106 return String();
104 107
105 if (!m_decodedSheetText.isNull()) 108 // Use cached decoded sheet text when available
109 if (!m_decodedSheetText.isNull()) {
110 // We should have the decoded sheet text cached when the resource is fully l oaded.
111 DCHECK_EQ(getStatus(), Resource::Cached);
112
106 return m_decodedSheetText; 113 return m_decodedSheetText;
114 }
107 115
108 // Don't cache the decoded text, regenerating is cheap and it can use quite a 116 if (!data() || data()->isEmpty())
109 // bit of memory 117 return String();
118
110 return decodedText(); 119 return decodedText();
111 } 120 }
112 121
113 void CSSStyleSheetResource::appendData(const char* data, size_t length) { 122 void CSSStyleSheetResource::appendData(const char* data, size_t length) {
114 Resource::appendData(data, length); 123 Resource::appendData(data, length);
115 if (m_didNotifyFirstData) 124 if (m_didNotifyFirstData)
116 return; 125 return;
117 ResourceClientWalker<StyleSheetResourceClient> w(clients()); 126 ResourceClientWalker<StyleSheetResourceClient> w(clients());
118 while (StyleSheetResourceClient* c = w.next()) 127 while (StyleSheetResourceClient* c = w.next())
119 c->didAppendFirstData(this); 128 c->didAppendFirstData(this);
120 m_didNotifyFirstData = true; 129 m_didNotifyFirstData = true;
121 } 130 }
122 131
123 void CSSStyleSheetResource::checkNotify() { 132 void CSSStyleSheetResource::checkNotify() {
124 // Decode the data to find out the encoding and keep the sheet text around 133 // Decode the data to find out the encoding and cache the decoded sheet text.
125 // during checkNotify() 134 if (data()) {
126 if (data())
127 m_decodedSheetText = decodedText(); 135 m_decodedSheetText = decodedText();
136 }
128 137
129 ResourceClientWalker<StyleSheetResourceClient> w(clients()); 138 ResourceClientWalker<StyleSheetResourceClient> w(clients());
130 while (StyleSheetResourceClient* c = w.next()) { 139 while (StyleSheetResourceClient* c = w.next()) {
131 markClientFinished(c); 140 markClientFinished(c);
132 c->setCSSStyleSheet(resourceRequest().url(), response().url(), encoding(), 141 c->setCSSStyleSheet(resourceRequest().url(), response().url(), encoding(),
133 this); 142 this);
134 } 143 }
135 // Clear the decoded text as it is unlikely to be needed immediately again and 144
136 // is cheap to regenerate. 145 // Clear raw bytes as now we have the full decoded sheet text.
137 m_decodedSheetText = String(); 146 // We wait until all setCSSStyleSheet to run as SubresourceIntegrity checks re quire raw bytes.
Charlie Harrison 2016/10/05 15:24:48 Is it possible for setCSSStyleSheet to be run on a
jww 2016/10/05 15:32:49 nit: "until all" -> "for all"
kouhei (in TOK) 2016/10/06 12:02:36 Done.
kouhei (in TOK) 2016/10/06 12:02:36 Yes. From CSSStyleSheetResource::didAddClient(). H
147 clearData();
138 } 148 }
139 149
140 void CSSStyleSheetResource::destroyDecodedDataIfPossible() { 150 void CSSStyleSheetResource::destroyDecodedDataIfPossible() {
141 if (!m_parsedStyleSheetCache) 151 if (!m_parsedStyleSheetCache)
142 return; 152 return;
143 153
144 setParsedStyleSheetCache(nullptr); 154 setParsedStyleSheetCache(nullptr);
145 setDecodedSize(0); 155 setDecodedSize(0);
146 } 156 }
147 157
158 void CSSStyleSheetResource::destroyDecodedDataForFailedRevalidation() {
159 m_decodedSheetText = String();
160 destroyDecodedDataIfPossible();
161 }
162
148 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const { 163 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const {
149 if (errorOccurred()) 164 if (errorOccurred())
150 return false; 165 return false;
151 166
152 // This check exactly matches Firefox. Note that we grab the Content-Type 167 // This check exactly matches Firefox. Note that we grab the Content-Type
153 // header directly because we want to see what the value is BEFORE content 168 // header directly because we want to see what the value is BEFORE content
154 // sniffing. Firefox does this by setting a "type hint" on the channel. This 169 // sniffing. Firefox does this by setting a "type hint" on the channel. This
155 // implementation should be observationally equivalent. 170 // implementation should be observationally equivalent.
156 // 171 //
157 // This code defaults to allowing the stylesheet for non-HTTP protocols so 172 // This code defaults to allowing the stylesheet for non-HTTP protocols so
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // This stylesheet resource did conflict with another resource and was not 208 // This stylesheet resource did conflict with another resource and was not
194 // added to the cache. 209 // added to the cache.
195 setParsedStyleSheetCache(nullptr); 210 setParsedStyleSheetCache(nullptr);
196 return; 211 return;
197 } 212 }
198 setParsedStyleSheetCache(sheet); 213 setParsedStyleSheetCache(sheet);
199 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes()); 214 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes());
200 } 215 }
201 216
202 } // namespace blink 217 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698