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

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

Issue 2258743002: Reserve Vector's capacity manually in CachedMetadata (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 3 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, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. 6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "public/platform/WebScheduler.h" 42 #include "public/platform/WebScheduler.h"
43 #include "public/platform/WebSecurityOrigin.h" 43 #include "public/platform/WebSecurityOrigin.h"
44 #include "wtf/CurrentTime.h" 44 #include "wtf/CurrentTime.h"
45 #include "wtf/MathExtras.h" 45 #include "wtf/MathExtras.h"
46 #include "wtf/StdLibExtras.h" 46 #include "wtf/StdLibExtras.h"
47 #include "wtf/Vector.h" 47 #include "wtf/Vector.h"
48 #include "wtf/text/CString.h" 48 #include "wtf/text/CString.h"
49 #include "wtf/text/StringBuilder.h" 49 #include "wtf/text/StringBuilder.h"
50 #include <algorithm> 50 #include <algorithm>
51 #include <memory> 51 #include <memory>
52 #include <stdint.h>
52 53
53 namespace blink { 54 namespace blink {
54 55
55 // These response headers are not copied from a revalidated response to the 56 // These response headers are not copied from a revalidated response to the
56 // cached response headers. For compatibility, this list is based on Chromium's 57 // cached response headers. For compatibility, this list is based on Chromium's
57 // net/http/http_response_headers.cc. 58 // net/http/http_response_headers.cc.
58 const char* const headersToIgnoreAfterRevalidation[] = { 59 const char* const headersToIgnoreAfterRevalidation[] = {
59 "allow", 60 "allow",
60 "connection", 61 "connection",
61 "etag", 62 "etag",
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 96 }
96 97
97 class Resource::CachedMetadataHandlerImpl : public CachedMetadataHandler { 98 class Resource::CachedMetadataHandlerImpl : public CachedMetadataHandler {
98 public: 99 public:
99 static Resource::CachedMetadataHandlerImpl* create(Resource* resource) 100 static Resource::CachedMetadataHandlerImpl* create(Resource* resource)
100 { 101 {
101 return new CachedMetadataHandlerImpl(resource); 102 return new CachedMetadataHandlerImpl(resource);
102 } 103 }
103 ~CachedMetadataHandlerImpl() override {} 104 ~CachedMetadataHandlerImpl() override {}
104 DECLARE_VIRTUAL_TRACE(); 105 DECLARE_VIRTUAL_TRACE();
105 void setCachedMetadata(unsigned, const char*, size_t, CacheType) override; 106 void setCachedMetadata(uint32_t, const char*, size_t, CacheType) override;
106 void clearCachedMetadata(CacheType) override; 107 void clearCachedMetadata(CacheType) override;
107 PassRefPtr<CachedMetadata> cachedMetadata(unsigned) const override; 108 PassRefPtr<CachedMetadata> cachedMetadata(uint32_t) const override;
108 String encoding() const override; 109 String encoding() const override;
109 // Sets the serialized metadata retrieved from the platform's cache. 110 // Sets the serialized metadata retrieved from the platform's cache.
110 void setSerializedCachedMetadata(const char*, size_t); 111 void setSerializedCachedMetadata(const char*, size_t);
111 112
112 protected: 113 protected:
113 explicit CachedMetadataHandlerImpl(Resource*); 114 explicit CachedMetadataHandlerImpl(Resource*);
114 virtual void sendToPlatform(); 115 virtual void sendToPlatform();
115 const ResourceResponse& response() const { return m_resource->response(); } 116 const ResourceResponse& response() const { return m_resource->response(); }
116 117
117 RefPtr<CachedMetadata> m_cachedMetadata; 118 RefPtr<CachedMetadata> m_cachedMetadata;
118 119
119 private: 120 private:
120 Member<Resource> m_resource; 121 Member<Resource> m_resource;
121 }; 122 };
122 123
123 Resource::CachedMetadataHandlerImpl::CachedMetadataHandlerImpl(Resource* resourc e) 124 Resource::CachedMetadataHandlerImpl::CachedMetadataHandlerImpl(Resource* resourc e)
124 : m_resource(resource) 125 : m_resource(resource)
125 { 126 {
126 } 127 }
127 128
128 DEFINE_TRACE(Resource::CachedMetadataHandlerImpl) 129 DEFINE_TRACE(Resource::CachedMetadataHandlerImpl)
129 { 130 {
130 visitor->trace(m_resource); 131 visitor->trace(m_resource);
131 CachedMetadataHandler::trace(visitor); 132 CachedMetadataHandler::trace(visitor);
132 } 133 }
133 134
134 void Resource::CachedMetadataHandlerImpl::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size, CachedMetadataHandler::CacheType cacheType) 135 void Resource::CachedMetadataHandlerImpl::setCachedMetadata(uint32_t dataTypeID, const char* data, size_t size, CachedMetadataHandler::CacheType cacheType)
135 { 136 {
136 // Currently, only one type of cached metadata per resource is supported. 137 // Currently, only one type of cached metadata per resource is supported.
137 // If the need arises for multiple types of metadata per resource this could 138 // If the need arises for multiple types of metadata per resource this could
138 // be enhanced to store types of metadata in a map. 139 // be enhanced to store types of metadata in a map.
139 ASSERT(!m_cachedMetadata); 140 ASSERT(!m_cachedMetadata);
140 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size); 141 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
141 if (cacheType == CachedMetadataHandler::SendToPlatform) 142 if (cacheType == CachedMetadataHandler::SendToPlatform)
142 sendToPlatform(); 143 sendToPlatform();
143 } 144 }
144 145
145 void Resource::CachedMetadataHandlerImpl::clearCachedMetadata(CachedMetadataHand ler::CacheType cacheType) 146 void Resource::CachedMetadataHandlerImpl::clearCachedMetadata(CachedMetadataHand ler::CacheType cacheType)
146 { 147 {
147 m_cachedMetadata.clear(); 148 m_cachedMetadata.clear();
148 if (cacheType == CachedMetadataHandler::SendToPlatform) 149 if (cacheType == CachedMetadataHandler::SendToPlatform)
149 sendToPlatform(); 150 sendToPlatform();
150 } 151 }
151 152
152 PassRefPtr<CachedMetadata> Resource::CachedMetadataHandlerImpl::cachedMetadata(u nsigned dataTypeID) const 153 PassRefPtr<CachedMetadata> Resource::CachedMetadataHandlerImpl::cachedMetadata(u int32_t dataTypeID) const
153 { 154 {
154 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID) 155 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
155 return nullptr; 156 return nullptr;
156 return m_cachedMetadata.get(); 157 return m_cachedMetadata.get();
157 } 158 }
158 159
159 String Resource::CachedMetadataHandlerImpl::encoding() const 160 String Resource::CachedMetadataHandlerImpl::encoding() const
160 { 161 {
161 return m_resource->encoding(); 162 return m_resource->encoding();
162 } 163 }
163 164
164 void Resource::CachedMetadataHandlerImpl::setSerializedCachedMetadata(const char * data, size_t size) 165 void Resource::CachedMetadataHandlerImpl::setSerializedCachedMetadata(const char * data, size_t size)
165 { 166 {
166 // We only expect to receive cached metadata from the platform once. 167 // We only expect to receive cached metadata from the platform once.
167 // If this triggers, it indicates an efficiency problem which is most 168 // If this triggers, it indicates an efficiency problem which is most
168 // likely unexpected in code designed to improve performance. 169 // likely unexpected in code designed to improve performance.
169 ASSERT(!m_cachedMetadata); 170 ASSERT(!m_cachedMetadata);
170 m_cachedMetadata = CachedMetadata::deserialize(data, size); 171 m_cachedMetadata = CachedMetadata::createFromSerializedData(data, size);
171 } 172 }
172 173
173 void Resource::CachedMetadataHandlerImpl::sendToPlatform() 174 void Resource::CachedMetadataHandlerImpl::sendToPlatform()
174 { 175 {
175 if (m_cachedMetadata) { 176 if (m_cachedMetadata) {
176 const Vector<char>& serializedData = m_cachedMetadata->serialize(); 177 const Vector<char>& serializedData = m_cachedMetadata->serializedData();
177 Platform::current()->cacheMetadata(response().url(), response().response Time(), serializedData.data(), serializedData.size()); 178 Platform::current()->cacheMetadata(response().url(), response().response Time(), serializedData.data(), serializedData.size());
178 } else { 179 } else {
179 Platform::current()->cacheMetadata(response().url(), response().response Time(), nullptr, 0); 180 Platform::current()->cacheMetadata(response().url(), response().response Time(), nullptr, 0);
180 } 181 }
181 } 182 }
182 183
183 class Resource::ServiceWorkerResponseCachedMetadataHandler : public Resource::Ca chedMetadataHandlerImpl { 184 class Resource::ServiceWorkerResponseCachedMetadataHandler : public Resource::Ca chedMetadataHandlerImpl {
184 public: 185 public:
185 static Resource::CachedMetadataHandlerImpl* create(Resource* resource, Secur ityOrigin* securityOrigin) 186 static Resource::CachedMetadataHandlerImpl* create(Resource* resource, Secur ityOrigin* securityOrigin)
186 { 187 {
(...skipping 25 matching lines...) Expand all
212 void Resource::ServiceWorkerResponseCachedMetadataHandler::sendToPlatform() 213 void Resource::ServiceWorkerResponseCachedMetadataHandler::sendToPlatform()
213 { 214 {
214 // We don't support sending the metadata to the platform when the response 215 // We don't support sending the metadata to the platform when the response
215 // was directly fetched via a ServiceWorker 216 // was directly fetched via a ServiceWorker
216 // (eg: FetchEvent.respondWith(fetch(FetchEvent.request))) to prevent an 217 // (eg: FetchEvent.respondWith(fetch(FetchEvent.request))) to prevent an
217 // attacker's Service Worker from poisoning the metadata cache of HTTPCache. 218 // attacker's Service Worker from poisoning the metadata cache of HTTPCache.
218 if (response().cacheStorageCacheName().isNull()) 219 if (response().cacheStorageCacheName().isNull())
219 return; 220 return;
220 221
221 if (m_cachedMetadata) { 222 if (m_cachedMetadata) {
222 const Vector<char>& serializedData = m_cachedMetadata->serialize(); 223 const Vector<char>& serializedData = m_cachedMetadata->serializedData();
223 Platform::current()->cacheMetadataInCacheStorage(response().url(), respo nse().responseTime(), serializedData.data(), serializedData.size(), WebSecurityO rigin(m_securityOrigin), response().cacheStorageCacheName()); 224 Platform::current()->cacheMetadataInCacheStorage(response().url(), respo nse().responseTime(), serializedData.data(), serializedData.size(), WebSecurityO rigin(m_securityOrigin), response().cacheStorageCacheName());
224 } else { 225 } else {
225 Platform::current()->cacheMetadataInCacheStorage(response().url(), respo nse().responseTime(), nullptr, 0, WebSecurityOrigin(m_securityOrigin), response( ).cacheStorageCacheName()); 226 Platform::current()->cacheMetadataInCacheStorage(response().url(), respo nse().responseTime(), nullptr, 0, WebSecurityOrigin(m_securityOrigin), response( ).cacheStorageCacheName());
226 } 227 }
227 } 228 }
228 229
229 class Resource::ResourceCallback final : public GarbageCollectedFinalized<Resour ceCallback> { 230 class Resource::ResourceCallback final : public GarbageCollectedFinalized<Resour ceCallback> {
230 public: 231 public:
231 static ResourceCallback& callbackHandler(); 232 static ResourceCallback& callbackHandler();
232 DECLARE_TRACE(); 233 DECLARE_TRACE();
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 case Resource::TextTrack: 1066 case Resource::TextTrack:
1066 case Resource::Media: 1067 case Resource::Media:
1067 case Resource::Manifest: 1068 case Resource::Manifest:
1068 return false; 1069 return false;
1069 } 1070 }
1070 ASSERT_NOT_REACHED(); 1071 ASSERT_NOT_REACHED();
1071 return false; 1072 return false;
1072 } 1073 }
1073 1074
1074 } // namespace blink 1075 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698