OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) | |
3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org> | |
4 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | |
5 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. | |
6 | |
7 This library is free software; you can redistribute it and/or | |
8 modify it under the terms of the GNU Library General Public | |
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 | |
26 #include "core/CoreExport.h" | |
27 #include "core/fetch/ImageResourceInfo.h" | |
28 #include "core/fetch/MultipartImageResourceParser.h" | |
29 #include "core/fetch/Resource.h" | |
30 #include "platform/Timer.h" | |
31 #include "platform/heap/Handle.h" | |
32 #include <memory> | |
33 | |
34 namespace blink { | |
35 | |
36 class FetchRequest; | |
37 class ImageResourceContent; | |
38 class ResourceClient; | |
39 class ResourceFetcher; | |
40 class SecurityOrigin; | |
41 | |
42 // ImageResource implements blink::Resource interface and image-specific logic | |
43 // for loading images. | |
44 // Image-related things (blink::Image and ImageResourceObserver) are handled by | |
45 // ImageResourceContent. | |
46 // Most users should use ImageResourceContent instead of ImageResource. | |
47 // https://docs.google.com/document/d/1O-fB83mrE0B_V8gzXNqHgmRLCvstTB4MMi3RnVLr8
bE/edit?usp=sharing | |
48 // | |
49 // As for the lifetimes of ImageResourceContent::m_image and m_data, see this | |
50 // document: | |
51 // https://docs.google.com/document/d/1v0yTAZ6wkqX2U_M6BNIGUJpM1s0TIw1VsqpxoL7ac
iY/edit?usp=sharing | |
52 class CORE_EXPORT ImageResource final | |
53 : public Resource, | |
54 public MultipartImageResourceParser::Client { | |
55 USING_GARBAGE_COLLECTED_MIXIN(ImageResource); | |
56 | |
57 public: | |
58 using ClientType = ResourceClient; | |
59 | |
60 // Use ImageResourceContent::fetch() unless ImageResource is required. | |
61 // TODO(hiroshige): Make fetch() private. | |
62 static ImageResource* fetch(FetchRequest&, ResourceFetcher*); | |
63 | |
64 // TODO(hiroshige): Make create() test-only by refactoring ImageDocument. | |
65 static ImageResource* create(const ResourceRequest&); | |
66 | |
67 ~ImageResource() override; | |
68 | |
69 ImageResourceContent* getContent(); | |
70 const ImageResourceContent* getContent() const; | |
71 | |
72 void reloadIfLoFiOrPlaceholderImage(ResourceFetcher*, | |
73 ReloadLoFiOrPlaceholderPolicy); | |
74 | |
75 void didAddClient(ResourceClient*) override; | |
76 | |
77 ResourcePriority priorityFromObservers() override; | |
78 | |
79 void allClientsAndObserversRemoved() override; | |
80 | |
81 PassRefPtr<const SharedBuffer> resourceBuffer() const override; | |
82 void appendData(const char*, size_t) override; | |
83 void error(const ResourceError&) override; | |
84 void responseReceived(const ResourceResponse&, | |
85 std::unique_ptr<WebDataConsumerHandle>) override; | |
86 void finish(double finishTime = 0.0) override; | |
87 | |
88 // For compatibility, images keep loading even if there are HTTP errors. | |
89 bool shouldIgnoreHTTPStatusCodeErrors() const override { return true; } | |
90 | |
91 bool isImage() const override { return true; } | |
92 | |
93 // MultipartImageResourceParser::Client | |
94 void onePartInMultipartReceived(const ResourceResponse&) final; | |
95 void multipartDataReceived(const char*, size_t) final; | |
96 | |
97 // Used by tests. | |
98 bool isPlaceholder() const { return m_isPlaceholder; } | |
99 | |
100 DECLARE_VIRTUAL_TRACE(); | |
101 | |
102 private: | |
103 enum class MultipartParsingState : uint8_t { | |
104 WaitingForFirstPart, | |
105 ParsingFirstPart, | |
106 FinishedParsingFirstPart, | |
107 }; | |
108 | |
109 class ImageResourceInfoImpl; | |
110 class ImageResourceFactory; | |
111 | |
112 ImageResource(const ResourceRequest&, | |
113 const ResourceLoaderOptions&, | |
114 ImageResourceContent*, | |
115 bool isPlaceholder); | |
116 | |
117 // Only for ImageResourceInfoImpl. | |
118 void decodeError(bool allDataReceived); | |
119 bool isAccessAllowed( | |
120 SecurityOrigin*, | |
121 ImageResourceInfo::DoesCurrentFrameHaveSingleSecurityOrigin) const; | |
122 | |
123 bool hasClientsOrObservers() const override; | |
124 | |
125 void updateImageAndClearBuffer(); | |
126 | |
127 void checkNotify() override; | |
128 | |
129 void destroyDecodedDataIfPossible() override; | |
130 void destroyDecodedDataForFailedRevalidation() override; | |
131 | |
132 void flushImageIfNeeded(TimerBase*); | |
133 | |
134 bool shouldReloadBrokenPlaceholder() const { | |
135 return m_isPlaceholder && willPaintBrokenImage(); | |
136 } | |
137 | |
138 bool willPaintBrokenImage() const; | |
139 | |
140 Member<ImageResourceContent> m_content; | |
141 | |
142 // TODO(hiroshige): move |m_devicePixelRatioHeaderValue| and | |
143 // |m_hasDevicePixelRatioHeaderValue| to ImageResourceContent and update | |
144 // it via ImageResourceContent::updateImage(). | |
145 float m_devicePixelRatioHeaderValue; | |
146 | |
147 Member<MultipartImageResourceParser> m_multipartParser; | |
148 MultipartParsingState m_multipartParsingState = | |
149 MultipartParsingState::WaitingForFirstPart; | |
150 bool m_hasDevicePixelRatioHeaderValue; | |
151 | |
152 // Indicates if the ImageResource is currently scheduling a reload, e.g. | |
153 // because reloadIfLoFi() was called. | |
154 bool m_isSchedulingReload; | |
155 | |
156 // Indicates if this ImageResource is either attempting to load a placeholder | |
157 // image, or is a (possibly broken) placeholder image. | |
158 bool m_isPlaceholder; | |
159 | |
160 Timer<ImageResource> m_flushTimer; | |
161 double m_lastFlushTime = 0.; | |
162 }; | |
163 | |
164 DEFINE_RESOURCE_TYPE_CASTS(Image); | |
165 | |
166 } // namespace blink | |
167 | |
168 #endif | |
OLD | NEW |