OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef ImageResourceContent_h | |
6 #define ImageResourceContent_h | |
7 | |
8 #include "core/CoreExport.h" | |
9 #include "core/fetch/ResourceStatus.h" | |
10 #include "platform/geometry/IntRect.h" | |
11 #include "platform/geometry/IntSizeHash.h" | |
12 #include "platform/geometry/LayoutSize.h" | |
13 #include "platform/graphics/Image.h" | |
14 #include "platform/graphics/ImageObserver.h" | |
15 #include "platform/graphics/ImageOrientation.h" | |
16 #include "platform/network/ResourceLoadPriority.h" | |
17 #include "platform/weborigin/KURL.h" | |
18 #include "wtf/HashCountedSet.h" | |
19 #include "wtf/HashMap.h" | |
20 #include <memory> | |
21 | |
22 namespace blink { | |
23 | |
24 class FetchRequest; | |
25 class ImageResourceInfo; | |
26 class ImageResourceObserver; | |
27 class ResourceError; | |
28 class ResourceFetcher; | |
29 class ResourceResponse; | |
30 class SecurityOrigin; | |
31 | |
32 // ImageResourceContent is a container that holds fetch result of | |
33 // an ImageResource in a decoded form. | |
34 // Classes that use the fetched images | |
35 // should hold onto this class and/or inherit ImageResourceObserver, | |
36 // instead of holding onto ImageResource or inheriting ResourceClient. | |
37 // https://docs.google.com/document/d/1O-fB83mrE0B_V8gzXNqHgmRLCvstTB4MMi3RnVLr8
bE/edit?usp=sharing | |
38 // TODO(hiroshige): Make ImageResourceContent ResourceClient and remove the | |
39 // word 'observer' from ImageResource. | |
40 // TODO(hiroshige): Rename local variables of type ImageResourceContent to | |
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); | |
46 | |
47 public: | |
48 static ImageResourceContent* create( | |
49 PassRefPtr<blink::Image> image = nullptr) { | |
50 return new ImageResourceContent(std::move(image)); | |
51 } | |
52 static ImageResourceContent* fetch(FetchRequest&, ResourceFetcher*); | |
53 | |
54 // Returns the nullImage() if the image is not available yet. | |
55 blink::Image* getImage(); | |
56 bool hasImage() const { return m_image.get(); } | |
57 | |
58 static std::pair<blink::Image*, float> brokenImage( | |
59 float deviceScaleFactor); // Returns an image and the image's resolution | |
60 // scale factor. | |
61 | |
62 bool usesImageContainerSize() const; | |
63 bool imageHasRelativeSize() const; | |
64 // The device pixel ratio we got from the server for this image, or 1.0. | |
65 float devicePixelRatioHeaderValue() const; | |
66 bool hasDevicePixelRatioHeaderValue() const; | |
67 | |
68 enum SizeType { | |
69 // Report the intrinsic size. | |
70 IntrinsicSize, | |
71 | |
72 // Report the intrinsic size corrected to account for image density. | |
73 IntrinsicCorrectedToDPR, | |
74 }; | |
75 | |
76 // This method takes a zoom multiplier that can be used to increase the | |
77 // natural size of the image by the zoom. | |
78 LayoutSize imageSize( | |
79 RespectImageOrientationEnum shouldRespectImageOrientation, | |
80 float multiplier, | |
81 SizeType = IntrinsicSize); | |
82 | |
83 void updateImageAnimationPolicy(); | |
84 | |
85 void addObserver(ImageResourceObserver*); | |
86 void removeObserver(ImageResourceObserver*); | |
87 | |
88 DECLARE_TRACE(); | |
89 | |
90 // Redirecting methods to Resource. | |
91 const KURL& url() const; | |
92 bool isAccessAllowed(SecurityOrigin*); | |
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; | |
100 | |
101 // For FrameSerializer. | |
102 bool hasCacheControlNoStoreHeader() const; | |
103 | |
104 void emulateLoadStartedForInspector(ResourceFetcher*, | |
105 const KURL&, | |
106 const AtomicString& initiatorName); | |
107 | |
108 void setNotRefetchableDataFromDiskCache() { | |
109 m_isRefetchableDataFromDiskCache = false; | |
110 } | |
111 | |
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 void doResetAnimation(); | |
124 PassRefPtr<const SharedBuffer> resourceBuffer() const; | |
125 bool shouldUpdateImageImmediately() const; | |
126 bool hasObservers() const { | |
127 return !m_observers.isEmpty() || !m_finishedObservers.isEmpty(); | |
128 } | |
129 bool isRefetchableDataFromDiskCache() const { | |
130 return m_isRefetchableDataFromDiskCache; | |
131 } | |
132 | |
133 private: | |
134 explicit ImageResourceContent(PassRefPtr<blink::Image> = nullptr); | |
135 | |
136 // ImageObserver | |
137 void decodedSizeChangedTo(const blink::Image*, size_t newSize) override; | |
138 bool shouldPauseAnimation(const blink::Image*) override; | |
139 void animationAdvanced(const blink::Image*) override; | |
140 void changedInRect(const blink::Image*, const IntRect&) override; | |
141 | |
142 PassRefPtr<Image> createImage(); | |
143 | |
144 // If not null, changeRect is the changed part of the image. | |
145 void notifyObservers(NotifyFinishOption, const IntRect* changeRect = nullptr); | |
146 void markObserverFinished(ImageResourceObserver*); | |
147 | |
148 Member<ImageResourceInfo> m_info; | |
149 | |
150 RefPtr<blink::Image> m_image; | |
151 | |
152 HashCountedSet<ImageResourceObserver*> m_observers; | |
153 HashCountedSet<ImageResourceObserver*> m_finishedObservers; | |
154 | |
155 Image::SizeAvailability m_sizeAvailable = Image::SizeUnavailable; | |
156 | |
157 // Indicates if this resource's encoded image data can be purged and refetched | |
158 // from disk cache to save memory usage. See crbug/664437. | |
159 bool m_isRefetchableDataFromDiskCache; | |
160 }; | |
161 | |
162 } // namespace blink | |
163 | |
164 #endif | |
OLD | NEW |