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