OLD | NEW |
---|---|
1 /* | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) | 2 // Use of this source code is governed by a BSD-style license that can be |
3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org> | 3 // found in the LICENSE file. |
4 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | |
5 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. | |
6 | 4 |
7 This library is free software; you can redistribute it and/or | 5 #ifndef ImageResourceContent_h |
8 modify it under the terms of the GNU Library General Public | 6 #define ImageResourceContent_h |
9 License as published by the Free Software Foundation; either | |
10 version 2 of the License, or (at your option) any later version. | |
11 | |
12 This library is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 Library General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU Library General Public License | |
18 along with this library; see the file COPYING.LIB. If not, write to | |
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 Boston, MA 02110-1301, USA. | |
21 */ | |
22 | |
23 #ifndef ImageResource_h | |
24 #define ImageResource_h | |
25 | 7 |
26 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
27 #include "core/fetch/MultipartImageResourceParser.h" | 9 #include "core/fetch/ResourceStatus.h" |
28 #include "core/fetch/Resource.h" | |
29 #include "platform/geometry/IntRect.h" | 10 #include "platform/geometry/IntRect.h" |
30 #include "platform/geometry/IntSizeHash.h" | 11 #include "platform/geometry/IntSizeHash.h" |
31 #include "platform/geometry/LayoutSize.h" | 12 #include "platform/geometry/LayoutSize.h" |
32 #include "platform/graphics/Image.h" | 13 #include "platform/graphics/Image.h" |
33 #include "platform/graphics/ImageObserver.h" | 14 #include "platform/graphics/ImageObserver.h" |
34 #include "platform/graphics/ImageOrientation.h" | 15 #include "platform/graphics/ImageOrientation.h" |
16 #include "platform/network/ResourceLoadPriority.h" | |
17 #include "platform/weborigin/KURL.h" | |
18 #include "wtf/HashCountedSet.h" | |
35 #include "wtf/HashMap.h" | 19 #include "wtf/HashMap.h" |
36 #include <memory> | 20 #include <memory> |
37 | 21 |
38 namespace blink { | 22 namespace blink { |
39 | 23 |
40 class FetchRequest; | 24 class FetchRequest; |
25 class ImageResourceInfo; | |
41 class ImageResourceObserver; | 26 class ImageResourceObserver; |
42 class MemoryCache; | 27 class ResourceError; |
43 class ResourceClient; | |
44 class ResourceFetcher; | 28 class ResourceFetcher; |
29 class ResourceResponse; | |
45 class SecurityOrigin; | 30 class SecurityOrigin; |
46 | 31 |
47 // ImageResource class represents an image type resource. | 32 // ImageResourceContent is a container that holds fetch result of |
48 // | 33 // an ImageResource in a decoded form. |
49 // As for the lifetimes of m_image and m_data, see this document: | 34 // Classes that use the fetched images |
50 // https://docs.google.com/document/d/1v0yTAZ6wkqX2U_M6BNIGUJpM1s0TIw1VsqpxoL7ac iY/edit?usp=sharing | 35 // should hold onto this class and/or inherit ImageResourceObserver, |
51 class CORE_EXPORT ImageResource final | 36 // instead of holding onto ImageResource or inheriting ResourceClient. |
52 : public Resource, | 37 // https://docs.google.com/document/d/1O-fB83mrE0B_V8gzXNqHgmRLCvstTB4MMi3RnVLr8 bE/edit?usp=sharing |
53 public ImageObserver, | 38 // TODO(hiroshige): Make ImageResourceContent ResourceClient and remove the |
54 public MultipartImageResourceParser::Client { | 39 // word 'observer' from ImageResource. |
55 friend class MemoryCache; | 40 // TODO(hiroshige): Rename local variables of type ImageResourceContent to |
56 USING_GARBAGE_COLLECTED_MIXIN(ImageResource); | 41 // e.g. |imageContent|. Currently they have Resource-like names. |
42 class CORE_EXPORT ImageResourceContent final | |
43 : public GarbageCollectedFinalized<ImageResourceContent>, | |
44 public ImageObserver { | |
45 USING_GARBAGE_COLLECTED_MIXIN(ImageResourceContent); | |
57 | 46 |
58 public: | 47 public: |
59 using ClientType = ResourceClient; | 48 static ImageResourceContent* create( |
49 PassRefPtr<blink::Image> image = nullptr) { | |
50 return new ImageResourceContent(std::move(image)); | |
51 } | |
52 static ImageResourceContent* fetch(FetchRequest&, ResourceFetcher*); | |
60 | 53 |
61 static ImageResource* fetch(FetchRequest&, ResourceFetcher*); | 54 // Returns the nullImage() if the image is not available yet. |
62 | 55 blink::Image* getImage() const; |
kinuko
2016/12/09 10:42:24
ditto, avoid returning non-const ptr from const me
hiroshige
2016/12/11 19:17:02
I found ImageResource already have potentials for
| |
63 static ImageResource* create(blink::Image* image) { | |
64 return new ImageResource(image, ResourceLoaderOptions()); | |
65 } | |
66 | |
67 static ImageResource* create(const ResourceRequest& request) { | |
68 return new ImageResource(request, ResourceLoaderOptions(), false); | |
69 } | |
70 | |
71 ~ImageResource() override; | |
72 | |
73 blink::Image* | |
74 getImage(); // Returns the nullImage() if the image is not available yet. | |
75 bool hasImage() const { return m_image.get(); } | 56 bool hasImage() const { return m_image.get(); } |
76 | 57 |
77 static std::pair<blink::Image*, float> brokenImage( | 58 static std::pair<blink::Image*, float> brokenImage( |
78 float deviceScaleFactor); // Returns an image and the image's resolution | 59 float deviceScaleFactor); // Returns an image and the image's resolution |
79 // scale factor. | 60 // scale factor. |
80 bool willPaintBrokenImage() const; | |
81 | 61 |
82 bool usesImageContainerSize() const; | 62 bool usesImageContainerSize() const; |
83 bool imageHasRelativeSize() const; | 63 bool imageHasRelativeSize() const; |
84 // The device pixel ratio we got from the server for this image, or 1.0. | 64 // The device pixel ratio we got from the server for this image, or 1.0. |
85 float devicePixelRatioHeaderValue() const { | 65 float devicePixelRatioHeaderValue() const; |
86 return m_devicePixelRatioHeaderValue; | 66 bool hasDevicePixelRatioHeaderValue() const; |
87 } | |
88 bool hasDevicePixelRatioHeaderValue() const { | |
89 return m_hasDevicePixelRatioHeaderValue; | |
90 } | |
91 | 67 |
92 enum SizeType { | 68 enum SizeType { |
93 // Report the intrinsic size. | 69 // Report the intrinsic size. |
94 IntrinsicSize, | 70 IntrinsicSize, |
95 | 71 |
96 // Report the intrinsic size corrected to account for image density. | 72 // Report the intrinsic size corrected to account for image density. |
97 IntrinsicCorrectedToDPR, | 73 IntrinsicCorrectedToDPR, |
98 }; | 74 }; |
99 | 75 |
100 // This method takes a zoom multiplier that can be used to increase the | 76 // This method takes a zoom multiplier that can be used to increase the |
101 // natural size of the image by the zoom. | 77 // natural size of the image by the zoom. |
102 LayoutSize imageSize( | 78 LayoutSize imageSize( |
103 RespectImageOrientationEnum shouldRespectImageOrientation, | 79 RespectImageOrientationEnum shouldRespectImageOrientation, |
104 float multiplier, | 80 float multiplier, |
105 SizeType = IntrinsicSize); | 81 SizeType = IntrinsicSize); |
106 | 82 |
107 bool isAccessAllowed(SecurityOrigin*); | |
108 | |
109 void updateImageAnimationPolicy(); | 83 void updateImageAnimationPolicy(); |
110 | 84 |
111 enum class ReloadCachePolicy { | |
112 UseExistingPolicy = 0, // Don't modify the request's cache policy. | |
113 BypassCache, // Modify the request so that the reload bypasses the cache. | |
114 }; | |
115 | |
116 // If this ImageResource has the Lo-Fi response headers or is a placeholder, | |
117 // reload the full original image with the Lo-Fi state set to off and | |
118 // optionally bypassing the cache. | |
119 void reloadIfLoFiOrPlaceholder( | |
120 ResourceFetcher*, | |
121 ReloadCachePolicy = ReloadCachePolicy::BypassCache); | |
122 | |
123 void didAddClient(ResourceClient*) override; | |
124 | |
125 void addObserver(ImageResourceObserver*); | 85 void addObserver(ImageResourceObserver*); |
126 void removeObserver(ImageResourceObserver*); | 86 void removeObserver(ImageResourceObserver*); |
127 | 87 |
128 ResourcePriority priorityFromObservers() override; | 88 DECLARE_TRACE(); |
129 | 89 |
130 void allClientsAndObserversRemoved() override; | 90 // Redirecting methods to Resource. |
91 const KURL& url() const; | |
92 bool isAccessAllowed(SecurityOrigin*) const; | |
93 const ResourceResponse& response() const; | |
94 bool isLoaded() const; | |
95 bool isLoading() const; | |
96 bool errorOccurred() const; | |
97 bool loadFailedOrCanceled() const; | |
98 ResourceStatus getStatus() const; | |
99 const ResourceError& resourceError() const; | |
131 | 100 |
132 PassRefPtr<const SharedBuffer> resourceBuffer() const override; | 101 // For FrameSerializer. |
133 void appendData(const char*, size_t) override; | 102 bool hasCacheControlNoStoreHeader() const; |
134 void error(const ResourceError&) override; | |
135 void responseReceived(const ResourceResponse&, | |
136 std::unique_ptr<WebDataConsumerHandle>) override; | |
137 void finish(double finishTime = 0.0) override; | |
138 | 103 |
139 // For compatibility, images keep loading even if there are HTTP errors. | 104 void emulateLoadStartedForInspector(ResourceFetcher*, |
140 bool shouldIgnoreHTTPStatusCodeErrors() const override { return true; } | 105 const KURL&, |
141 | 106 const AtomicString& initiatorName); |
142 bool isImage() const override { return true; } | |
143 | |
144 // ImageObserver | |
145 void decodedSizeChangedTo(const blink::Image*, size_t newSize) override; | |
146 | |
147 bool shouldPauseAnimation(const blink::Image*) override; | |
148 void animationAdvanced(const blink::Image*) override; | |
149 void changedInRect(const blink::Image*, const IntRect&) override; | |
150 | |
151 // MultipartImageResourceParser::Client | |
152 void onePartInMultipartReceived(const ResourceResponse&) final; | |
153 void multipartDataReceived(const char*, size_t) final; | |
154 | |
155 // Used by tests. | |
156 bool isPlaceholder() const { return m_isPlaceholder; } | |
157 | |
158 bool shouldReloadBrokenPlaceholder() const { | |
159 return m_isPlaceholder && willPaintBrokenImage(); | |
160 } | |
161 | 107 |
162 void setNotRefetchableDataFromDiskCache() { | 108 void setNotRefetchableDataFromDiskCache() { |
163 m_isRefetchableDataFromDiskCache = false; | 109 m_isRefetchableDataFromDiskCache = false; |
164 } | 110 } |
165 | 111 |
166 DECLARE_VIRTUAL_TRACE(); | 112 // For ImageResource only. |
113 void setImageResourceInfo(ImageResourceInfo*); | |
114 enum ClearImageOption { ClearExistingImage, KeepExistingImage }; | |
115 void updateImage(PassRefPtr<SharedBuffer>, | |
116 ClearImageOption, | |
117 bool allDataReceived); | |
118 enum NotifyFinishOption { ShouldNotifyFinish, DoNotNotifyFinish }; | |
119 void clearImage(); | |
120 void clearImageAndNotifyObservers(NotifyFinishOption); | |
121 ResourcePriority priorityFromObservers() const; | |
122 void destroyDecodedData(); | |
123 bool hasObservers() const { | |
124 return !m_observers.isEmpty() || !m_finishedObservers.isEmpty(); | |
125 } | |
126 bool isSizeAvailable() const { | |
127 return m_sizeAvailable == Image::SizeAvailable; | |
128 } | |
129 bool isRefetchableDataFromDiskCache() const { | |
130 return m_isRefetchableDataFromDiskCache; | |
131 } | |
132 void doResetAnimation(); | |
167 | 133 |
168 private: | 134 private: |
169 explicit ImageResource(blink::Image*, const ResourceLoaderOptions&); | 135 ImageResourceContent(PassRefPtr<blink::Image> = nullptr); |
kinuko
2016/12/09 10:42:24
explicit
hiroshige
2016/12/11 19:17:02
Done.
| |
170 | 136 |
171 enum class MultipartParsingState : uint8_t { | 137 // ImageObserver |
172 WaitingForFirstPart, | 138 void decodedSizeChangedTo(const blink::Image*, size_t newSize) override; |
173 ParsingFirstPart, | 139 bool shouldPauseAnimation(const blink::Image*) override; |
174 FinishedParsingFirstPart, | 140 void animationAdvanced(const blink::Image*) override; |
175 }; | 141 void changedInRect(const blink::Image*, const IntRect&) override; |
176 | 142 |
177 class ImageResourceFactory; | 143 PassRefPtr<Image> createImage(); |
178 | 144 |
179 ImageResource(const ResourceRequest&, | |
180 const ResourceLoaderOptions&, | |
181 bool isPlaceholder); | |
182 | |
183 bool hasClientsOrObservers() const override { | |
184 return Resource::hasClientsOrObservers() || !m_observers.isEmpty() || | |
185 !m_finishedObservers.isEmpty(); | |
186 } | |
187 void clear(); | |
188 | |
189 void createImage(); | |
190 void updateImage(bool allDataReceived); | |
191 void updateImageAndClearBuffer(); | |
192 void clearImage(); | |
193 enum NotifyFinishOption { ShouldNotifyFinish, DoNotNotifyFinish }; | |
194 // If not null, changeRect is the changed part of the image. | 145 // If not null, changeRect is the changed part of the image. |
195 void notifyObservers(NotifyFinishOption, const IntRect* changeRect = nullptr); | 146 void notifyObservers(NotifyFinishOption, const IntRect* changeRect = nullptr); |
196 | |
197 void ensureImage(); | |
198 | |
199 void checkNotify() override; | |
200 void notifyObserversInternal(); | |
201 void markObserverFinished(ImageResourceObserver*); | 147 void markObserverFinished(ImageResourceObserver*); |
202 | 148 |
203 void doResetAnimation(); | 149 Member<ImageResourceInfo> m_info; |
204 | 150 |
205 void destroyDecodedDataIfPossible() override; | 151 RefPtr<blink::Image> m_image; |
206 void destroyDecodedDataForFailedRevalidation() override; | |
207 | 152 |
208 void flushImageIfNeeded(TimerBase*); | |
209 | |
210 float m_devicePixelRatioHeaderValue; | |
211 | |
212 Member<MultipartImageResourceParser> m_multipartParser; | |
213 RefPtr<blink::Image> m_image; | |
214 MultipartParsingState m_multipartParsingState = | |
215 MultipartParsingState::WaitingForFirstPart; | |
216 bool m_hasDevicePixelRatioHeaderValue; | |
217 HashCountedSet<ImageResourceObserver*> m_observers; | 153 HashCountedSet<ImageResourceObserver*> m_observers; |
218 HashCountedSet<ImageResourceObserver*> m_finishedObservers; | 154 HashCountedSet<ImageResourceObserver*> m_finishedObservers; |
219 | 155 |
220 // Indicates if the ImageResource is currently scheduling a reload, e.g. | |
221 // because reloadIfLoFi() was called. | |
222 bool m_isSchedulingReload; | |
223 | |
224 // Indicates if this ImageResource is either attempting to load a placeholder | |
225 // image, or is a (possibly broken) placeholder image. | |
226 bool m_isPlaceholder; | |
227 | |
228 Timer<ImageResource> m_flushTimer; | |
229 double m_lastFlushTime = 0.; | |
230 Image::SizeAvailability m_sizeAvailable = Image::SizeUnavailable; | 156 Image::SizeAvailability m_sizeAvailable = Image::SizeUnavailable; |
231 | 157 |
232 // Indicates if this resource's encoded image data can be purged and refetched | 158 // Indicates if this resource's encoded image data can be purged and refetched |
233 // from disk cache to save memory usage. See crbug/664437. | 159 // from disk cache to save memory usage. See crbug/664437. |
234 bool m_isRefetchableDataFromDiskCache; | 160 bool m_isRefetchableDataFromDiskCache; |
235 }; | 161 }; |
236 | 162 |
237 DEFINE_RESOURCE_TYPE_CASTS(Image); | |
238 | |
239 } // namespace blink | 163 } // namespace blink |
240 | 164 |
241 #endif | 165 #endif |
OLD | NEW |