0
|
1 |
/*
|
|
2 |
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
* 1. Redistributions of source code must retain the above copyright
|
|
8 |
* notice, this list of conditions and the following disclaimer.
|
|
9 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer in the
|
|
11 |
* documentation and/or other materials provided with the distribution.
|
|
12 |
*
|
|
13 |
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
|
14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
|
17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
19 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
20 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
21 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
22 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
23 |
*/
|
|
24 |
|
|
25 |
#include "config.h"
|
|
26 |
#include "ImageDocument.h"
|
|
27 |
|
|
28 |
#include "CSSStyleDeclaration.h"
|
|
29 |
#include "CachedImage.h"
|
|
30 |
#include "DocumentLoader.h"
|
|
31 |
#include "Element.h"
|
|
32 |
#include "EventListener.h"
|
|
33 |
#include "EventNames.h"
|
|
34 |
#include "Frame.h"
|
|
35 |
#include "FrameLoader.h"
|
|
36 |
#include "FrameView.h"
|
|
37 |
#include "HTMLImageElement.h"
|
|
38 |
#include "HTMLNames.h"
|
|
39 |
#include "LocalizedStrings.h"
|
|
40 |
#include "MouseEvent.h"
|
|
41 |
#include "NotImplemented.h"
|
|
42 |
#include "Page.h"
|
|
43 |
#include "SegmentedString.h"
|
|
44 |
#include "Settings.h"
|
|
45 |
#include "Text.h"
|
|
46 |
#include "XMLTokenizer.h"
|
|
47 |
|
|
48 |
using std::min;
|
|
49 |
|
|
50 |
namespace WebCore {
|
|
51 |
|
|
52 |
using namespace HTMLNames;
|
|
53 |
|
|
54 |
class ImageEventListener : public EventListener {
|
|
55 |
public:
|
|
56 |
static PassRefPtr<ImageEventListener> create(ImageDocument* document) { return adoptRef(new ImageEventListener(document)); }
|
|
57 |
static const ImageEventListener* cast(const EventListener* listener)
|
|
58 |
{
|
|
59 |
return listener->type() == ImageEventListenerType
|
|
60 |
? static_cast<const ImageEventListener*>(listener)
|
|
61 |
: 0;
|
|
62 |
}
|
|
63 |
|
|
64 |
virtual bool operator==(const EventListener& other);
|
|
65 |
|
|
66 |
private:
|
|
67 |
ImageEventListener(ImageDocument* document)
|
|
68 |
: EventListener(ImageEventListenerType)
|
|
69 |
, m_doc(document)
|
|
70 |
{
|
|
71 |
}
|
|
72 |
|
|
73 |
virtual void handleEvent(ScriptExecutionContext*, Event*);
|
|
74 |
|
|
75 |
ImageDocument* m_doc;
|
|
76 |
};
|
|
77 |
|
|
78 |
class ImageTokenizer : public Tokenizer {
|
|
79 |
public:
|
|
80 |
ImageTokenizer(ImageDocument* doc) : m_doc(doc) {}
|
|
81 |
|
|
82 |
virtual void write(const SegmentedString&, bool appendData);
|
|
83 |
virtual void finish();
|
|
84 |
virtual bool isWaitingForScripts() const;
|
|
85 |
|
|
86 |
virtual bool wantsRawData() const { return true; }
|
|
87 |
virtual bool writeRawData(const char* data, int len);
|
|
88 |
|
|
89 |
private:
|
|
90 |
ImageDocument* m_doc;
|
|
91 |
};
|
|
92 |
|
|
93 |
class ImageDocumentElement : public HTMLImageElement {
|
|
94 |
public:
|
|
95 |
ImageDocumentElement(ImageDocument* doc)
|
|
96 |
: HTMLImageElement(imgTag, doc)
|
|
97 |
, m_imageDocument(doc)
|
|
98 |
{
|
|
99 |
}
|
|
100 |
|
|
101 |
virtual ~ImageDocumentElement();
|
|
102 |
virtual void willMoveToNewOwnerDocument();
|
|
103 |
|
|
104 |
private:
|
|
105 |
ImageDocument* m_imageDocument;
|
|
106 |
};
|
|
107 |
|
|
108 |
// --------
|
|
109 |
|
|
110 |
void ImageTokenizer::write(const SegmentedString&, bool)
|
|
111 |
{
|
|
112 |
// <https://bugs.webkit.org/show_bug.cgi?id=25397>: JS code can always call document.write, we need to handle it.
|
|
113 |
notImplemented();
|
|
114 |
}
|
|
115 |
|
|
116 |
bool ImageTokenizer::writeRawData(const char*, int)
|
|
117 |
{
|
|
118 |
CachedImage* cachedImage = m_doc->cachedImage();
|
|
119 |
cachedImage->data(m_doc->frame()->loader()->documentLoader()->mainResourceData(), false);
|
|
120 |
|
|
121 |
m_doc->imageChanged();
|
|
122 |
|
|
123 |
return false;
|
|
124 |
}
|
|
125 |
|
|
126 |
void ImageTokenizer::finish()
|
|
127 |
{
|
|
128 |
if (!m_parserStopped && m_doc->imageElement()) {
|
|
129 |
CachedImage* cachedImage = m_doc->cachedImage();
|
|
130 |
RefPtr<SharedBuffer> data = m_doc->frame()->loader()->documentLoader()->mainResourceData();
|
|
131 |
|
|
132 |
// If this is a multipart image, make a copy of the current part, since the resource data
|
|
133 |
// will be overwritten by the next part.
|
|
134 |
if (m_doc->frame()->loader()->documentLoader()->isLoadingMultipartContent())
|
|
135 |
data = data->copy();
|
|
136 |
|
|
137 |
cachedImage->data(data.release(), true);
|
|
138 |
cachedImage->finish();
|
|
139 |
|
|
140 |
cachedImage->setResponse(m_doc->frame()->loader()->documentLoader()->response());
|
|
141 |
|
|
142 |
IntSize size = cachedImage->imageSize(m_doc->frame()->pageZoomFactor());
|
|
143 |
if (size.width()) {
|
|
144 |
// Compute the title, we use the decoded filename of the resource, falling
|
|
145 |
// back on the (decoded) hostname if there is no path.
|
|
146 |
String fileName = decodeURLEscapeSequences(m_doc->url().lastPathComponent());
|
|
147 |
if (fileName.isEmpty())
|
|
148 |
fileName = m_doc->url().host();
|
|
149 |
m_doc->setTitle(imageTitle(fileName, size));
|
|
150 |
}
|
|
151 |
|
|
152 |
m_doc->imageChanged();
|
|
153 |
}
|
|
154 |
|
|
155 |
m_doc->finishedParsing();
|
|
156 |
}
|
|
157 |
|
|
158 |
bool ImageTokenizer::isWaitingForScripts() const
|
|
159 |
{
|
|
160 |
// An image document is never waiting for scripts
|
|
161 |
return false;
|
|
162 |
}
|
|
163 |
|
|
164 |
// --------
|
|
165 |
|
|
166 |
ImageDocument::ImageDocument(Frame* frame)
|
|
167 |
: HTMLDocument(frame)
|
|
168 |
, m_imageElement(0)
|
|
169 |
, m_imageSizeIsKnown(false)
|
|
170 |
, m_didShrinkImage(false)
|
|
171 |
, m_shouldShrinkImage(shouldShrinkToFit())
|
|
172 |
{
|
|
173 |
setParseMode(Compat);
|
|
174 |
}
|
|
175 |
|
|
176 |
Tokenizer* ImageDocument::createTokenizer()
|
|
177 |
{
|
|
178 |
return new ImageTokenizer(this);
|
|
179 |
}
|
|
180 |
|
|
181 |
void ImageDocument::createDocumentStructure()
|
|
182 |
{
|
|
183 |
ExceptionCode ec;
|
|
184 |
|
|
185 |
RefPtr<Element> rootElement = Document::createElement(htmlTag, false);
|
|
186 |
appendChild(rootElement, ec);
|
|
187 |
|
|
188 |
RefPtr<Element> body = Document::createElement(bodyTag, false);
|
|
189 |
body->setAttribute(styleAttr, "margin: 0px;");
|
|
190 |
|
|
191 |
rootElement->appendChild(body, ec);
|
|
192 |
|
|
193 |
RefPtr<ImageDocumentElement> imageElement = new ImageDocumentElement(this);
|
|
194 |
|
|
195 |
imageElement->setAttribute(styleAttr, "-webkit-user-select: none");
|
|
196 |
imageElement->setLoadManually(true);
|
|
197 |
imageElement->setSrc(url().string());
|
|
198 |
|
|
199 |
body->appendChild(imageElement, ec);
|
|
200 |
|
|
201 |
if (shouldShrinkToFit()) {
|
|
202 |
// Add event listeners
|
|
203 |
RefPtr<EventListener> listener = ImageEventListener::create(this);
|
|
204 |
if (DOMWindow* domWindow = this->domWindow())
|
|
205 |
domWindow->addEventListener("resize", listener, false);
|
|
206 |
imageElement->addEventListener("click", listener.release(), false);
|
|
207 |
}
|
|
208 |
|
|
209 |
m_imageElement = imageElement.get();
|
|
210 |
}
|
|
211 |
|
|
212 |
float ImageDocument::scale() const
|
|
213 |
{
|
|
214 |
if (!m_imageElement)
|
|
215 |
return 1.0f;
|
|
216 |
|
|
217 |
IntSize imageSize = m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor());
|
|
218 |
IntSize windowSize = IntSize(frame()->view()->width(), frame()->view()->height());
|
|
219 |
|
|
220 |
float widthScale = (float)windowSize.width() / imageSize.width();
|
|
221 |
float heightScale = (float)windowSize.height() / imageSize.height();
|
|
222 |
|
|
223 |
return min(widthScale, heightScale);
|
|
224 |
}
|
|
225 |
|
|
226 |
void ImageDocument::resizeImageToFit()
|
|
227 |
{
|
|
228 |
if (!m_imageElement)
|
|
229 |
return;
|
|
230 |
|
|
231 |
IntSize imageSize = m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor());
|
|
232 |
|
|
233 |
float scale = this->scale();
|
|
234 |
m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale));
|
|
235 |
m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale));
|
|
236 |
|
|
237 |
ExceptionCode ec;
|
|
238 |
m_imageElement->style()->setProperty("cursor", "-webkit-zoom-in", ec);
|
|
239 |
}
|
|
240 |
|
|
241 |
void ImageDocument::imageClicked(int x, int y)
|
|
242 |
{
|
|
243 |
if (!m_imageSizeIsKnown || imageFitsInWindow())
|
|
244 |
return;
|
|
245 |
|
|
246 |
m_shouldShrinkImage = !m_shouldShrinkImage;
|
|
247 |
|
|
248 |
if (m_shouldShrinkImage)
|
|
249 |
windowSizeChanged();
|
|
250 |
else {
|
|
251 |
restoreImageSize();
|
|
252 |
|
|
253 |
updateLayout();
|
|
254 |
|
|
255 |
float scale = this->scale();
|
|
256 |
|
|
257 |
int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width() / 2);
|
|
258 |
int scrollY = static_cast<int>(y / scale - (float)frame()->view()->height() / 2);
|
|
259 |
|
|
260 |
frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY));
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
void ImageDocument::imageChanged()
|
|
265 |
{
|
|
266 |
ASSERT(m_imageElement);
|
|
267 |
|
|
268 |
if (m_imageSizeIsKnown)
|
|
269 |
return;
|
|
270 |
|
|
271 |
if (m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor()).isEmpty())
|
|
272 |
return;
|
|
273 |
|
|
274 |
m_imageSizeIsKnown = true;
|
|
275 |
|
|
276 |
if (shouldShrinkToFit()) {
|
|
277 |
// Force resizing of the image
|
|
278 |
windowSizeChanged();
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
void ImageDocument::restoreImageSize()
|
|
283 |
{
|
|
284 |
if (!m_imageElement || !m_imageSizeIsKnown)
|
|
285 |
return;
|
|
286 |
|
|
287 |
m_imageElement->setWidth(m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor()).width());
|
|
288 |
m_imageElement->setHeight(m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor()).height());
|
|
289 |
|
|
290 |
ExceptionCode ec;
|
|
291 |
if (imageFitsInWindow())
|
|
292 |
m_imageElement->style()->removeProperty("cursor", ec);
|
|
293 |
else
|
|
294 |
m_imageElement->style()->setProperty("cursor", "-webkit-zoom-out", ec);
|
|
295 |
|
|
296 |
m_didShrinkImage = false;
|
|
297 |
}
|
|
298 |
|
|
299 |
bool ImageDocument::imageFitsInWindow() const
|
|
300 |
{
|
|
301 |
if (!m_imageElement)
|
|
302 |
return true;
|
|
303 |
|
|
304 |
IntSize imageSize = m_imageElement->cachedImage()->imageSize(frame()->pageZoomFactor());
|
|
305 |
IntSize windowSize = IntSize(frame()->view()->width(), frame()->view()->height());
|
|
306 |
|
|
307 |
return imageSize.width() <= windowSize.width() && imageSize.height() <= windowSize.height();
|
|
308 |
}
|
|
309 |
|
|
310 |
void ImageDocument::windowSizeChanged()
|
|
311 |
{
|
|
312 |
if (!m_imageElement || !m_imageSizeIsKnown)
|
|
313 |
return;
|
|
314 |
|
|
315 |
bool fitsInWindow = imageFitsInWindow();
|
|
316 |
|
|
317 |
// If the image has been explicitly zoomed in, restore the cursor if the image fits
|
|
318 |
// and set it to a zoom out cursor if the image doesn't fit
|
|
319 |
if (!m_shouldShrinkImage) {
|
|
320 |
ExceptionCode ec;
|
|
321 |
|
|
322 |
if (fitsInWindow)
|
|
323 |
m_imageElement->style()->removeProperty("cursor", ec);
|
|
324 |
else
|
|
325 |
m_imageElement->style()->setProperty("cursor", "-webkit-zoom-out", ec);
|
|
326 |
return;
|
|
327 |
}
|
|
328 |
|
|
329 |
if (m_didShrinkImage) {
|
|
330 |
// If the window has been resized so that the image fits, restore the image size
|
|
331 |
// otherwise update the restored image size.
|
|
332 |
if (fitsInWindow)
|
|
333 |
restoreImageSize();
|
|
334 |
else
|
|
335 |
resizeImageToFit();
|
|
336 |
} else {
|
|
337 |
// If the image isn't resized but needs to be, then resize it.
|
|
338 |
if (!fitsInWindow) {
|
|
339 |
resizeImageToFit();
|
|
340 |
m_didShrinkImage = true;
|
|
341 |
}
|
|
342 |
}
|
|
343 |
}
|
|
344 |
|
|
345 |
CachedImage* ImageDocument::cachedImage()
|
|
346 |
{
|
|
347 |
if (!m_imageElement)
|
|
348 |
createDocumentStructure();
|
|
349 |
|
|
350 |
return m_imageElement->cachedImage();
|
|
351 |
}
|
|
352 |
|
|
353 |
bool ImageDocument::shouldShrinkToFit() const
|
|
354 |
{
|
|
355 |
return frame()->page()->settings()->shrinksStandaloneImagesToFit() &&
|
|
356 |
frame()->page()->mainFrame() == frame();
|
|
357 |
}
|
|
358 |
|
|
359 |
// --------
|
|
360 |
|
|
361 |
void ImageEventListener::handleEvent(ScriptExecutionContext*, Event* event)
|
|
362 |
{
|
|
363 |
if (event->type() == eventNames().resizeEvent)
|
|
364 |
m_doc->windowSizeChanged();
|
|
365 |
else if (event->type() == eventNames().clickEvent) {
|
|
366 |
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
|
|
367 |
m_doc->imageClicked(mouseEvent->x(), mouseEvent->y());
|
|
368 |
}
|
|
369 |
}
|
|
370 |
|
|
371 |
bool ImageEventListener::operator==(const EventListener& listener)
|
|
372 |
{
|
|
373 |
if (const ImageEventListener* imageEventListener = ImageEventListener::cast(&listener))
|
|
374 |
return m_doc == imageEventListener->m_doc;
|
|
375 |
return false;
|
|
376 |
}
|
|
377 |
|
|
378 |
// --------
|
|
379 |
|
|
380 |
ImageDocumentElement::~ImageDocumentElement()
|
|
381 |
{
|
|
382 |
if (m_imageDocument)
|
|
383 |
m_imageDocument->disconnectImageElement();
|
|
384 |
}
|
|
385 |
|
|
386 |
void ImageDocumentElement::willMoveToNewOwnerDocument()
|
|
387 |
{
|
|
388 |
if (m_imageDocument) {
|
|
389 |
m_imageDocument->disconnectImageElement();
|
|
390 |
m_imageDocument = 0;
|
|
391 |
}
|
|
392 |
HTMLImageElement::willMoveToNewOwnerDocument();
|
|
393 |
}
|
|
394 |
|
|
395 |
}
|