2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "ImageDocument.h"
28 #include "CachedImage.h"
29 #include "DocumentLoader.h"
30 #include "EventListener.h"
31 #include "EventNames.h"
33 #include "FrameLoaderClient.h"
34 #include "FrameView.h"
35 #include "HTMLHtmlElement.h"
36 #include "HTMLImageElement.h"
37 #include "HTMLNames.h"
38 #include "LocalizedStrings.h"
39 #include "MouseEvent.h"
40 #include "NotImplemented.h"
42 #include "RawDataDocumentParser.h"
49 using namespace HTMLNames;
51 class ImageEventListener : public EventListener {
53 static PassRefPtr<ImageEventListener> create(ImageDocument* document) { return adoptRef(new ImageEventListener(document)); }
54 static const ImageEventListener* cast(const EventListener* listener)
56 return listener->type() == ImageEventListenerType
57 ? static_cast<const ImageEventListener*>(listener)
61 virtual bool operator==(const EventListener& other);
64 ImageEventListener(ImageDocument* document)
65 : EventListener(ImageEventListenerType)
70 virtual void handleEvent(ScriptExecutionContext*, Event*);
75 class ImageDocumentParser : public RawDataDocumentParser {
77 static PassRefPtr<ImageDocumentParser> create(ImageDocument* document)
79 return adoptRef(new ImageDocumentParser(document));
82 ImageDocument* document() const
84 return static_cast<ImageDocument*>(RawDataDocumentParser::document());
88 ImageDocumentParser(ImageDocument* document)
89 : RawDataDocumentParser(document)
93 virtual void appendBytes(DocumentWriter*, const char*, size_t);
94 virtual void finish();
97 class ImageDocumentElement : public HTMLImageElement {
99 static PassRefPtr<ImageDocumentElement> create(ImageDocument*);
102 ImageDocumentElement(ImageDocument* document)
103 : HTMLImageElement(imgTag, document)
104 , m_imageDocument(document)
108 virtual ~ImageDocumentElement();
109 virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
111 ImageDocument* m_imageDocument;
114 inline PassRefPtr<ImageDocumentElement> ImageDocumentElement::create(ImageDocument* document)
116 return adoptRef(new ImageDocumentElement(document));
121 static float pageZoomFactor(const Document* document)
123 Frame* frame = document->frame();
124 return frame ? frame->pageZoomFactor() : 1;
127 void ImageDocumentParser::appendBytes(DocumentWriter*, const char*, size_t)
129 Frame* frame = document()->frame();
130 Settings* settings = frame->settings();
131 if (!frame->loader()->client()->allowImage(!settings || settings->areImagesEnabled(), document()->url()))
134 CachedImage* cachedImage = document()->cachedImage();
135 cachedImage->data(frame->loader()->documentLoader()->mainResourceData(), false);
137 document()->imageUpdated();
140 void ImageDocumentParser::finish()
142 if (!isStopped() && document()->imageElement()) {
143 CachedImage* cachedImage = document()->cachedImage();
144 RefPtr<SharedBuffer> data = document()->frame()->loader()->documentLoader()->mainResourceData();
146 // If this is a multipart image, make a copy of the current part, since the resource data
147 // will be overwritten by the next part.
148 if (document()->frame()->loader()->documentLoader()->isLoadingMultipartContent())
151 cachedImage->data(data.release(), true);
152 cachedImage->finish();
154 cachedImage->setResponse(document()->frame()->loader()->documentLoader()->response());
156 // Report the natural image size in the page title, regardless of zoom
158 IntSize size = cachedImage->imageSizeForRenderer(document()->imageElement()->renderer(), 1.0f);
160 // Compute the title, we use the decoded filename of the resource, falling
161 // back on the (decoded) hostname if there is no path.
162 String fileName = decodeURLEscapeSequences(document()->url().lastPathComponent());
163 if (fileName.isEmpty())
164 fileName = document()->url().host();
165 document()->setTitle(imageTitle(fileName, size));
168 document()->imageUpdated();
171 document()->finishedParsing();
176 ImageDocument::ImageDocument(Frame* frame, const KURL& url)
177 : HTMLDocument(frame, url)
179 , m_imageSizeIsKnown(false)
180 , m_didShrinkImage(false)
181 , m_shouldShrinkImage(shouldShrinkToFit())
183 setCompatibilityMode(QuirksMode);
184 lockCompatibilityMode();
187 PassRefPtr<DocumentParser> ImageDocument::createParser()
189 return ImageDocumentParser::create(this);
192 void ImageDocument::createDocumentStructure()
196 RefPtr<Element> rootElement = Document::createElement(htmlTag, false);
197 appendChild(rootElement, ec);
198 static_cast<HTMLHtmlElement*>(rootElement.get())->insertedByParser();
200 if (frame() && frame()->loader())
201 frame()->loader()->dispatchDocumentElementAvailable();
203 RefPtr<Element> body = Document::createElement(bodyTag, false);
204 body->setAttribute(styleAttr, "margin: 0px;");
206 rootElement->appendChild(body, ec);
208 RefPtr<ImageDocumentElement> imageElement = ImageDocumentElement::create(this);
210 imageElement->setAttribute(styleAttr, "-webkit-user-select: none");
211 imageElement->setLoadManually(true);
212 imageElement->setSrc(url().string());
214 body->appendChild(imageElement, ec);
216 if (shouldShrinkToFit()) {
217 // Add event listeners
218 RefPtr<EventListener> listener = ImageEventListener::create(this);
219 if (DOMWindow* domWindow = this->domWindow())
220 domWindow->addEventListener("resize", listener, false);
221 imageElement->addEventListener("click", listener.release(), false);
224 m_imageElement = imageElement.get();
227 float ImageDocument::scale() const
232 FrameView* view = frame()->view();
236 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
237 #if ENABLE(TIZEN_VIEWPORT_META_TAG)
238 // FIXME: view->width() and view->height() are frame's rect size. And it's calculated after layout.
239 // But ImageDocument's loading can be begun before frame's rect size is fixed.
240 // So, we use layoutWidth() here.
241 LayoutSize windowSize = LayoutSize(view->layoutWidth(), view->layoutHeight());
243 LayoutSize windowSize = LayoutSize(view->width(), view->height());
246 float widthScale = (float)windowSize.width() / imageSize.width();
247 float heightScale = (float)windowSize.height() / imageSize.height();
249 return min(widthScale, heightScale);
252 void ImageDocument::resizeImageToFit()
257 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
259 float scale = this->scale();
260 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale));
261 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale));
263 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-in", false);
266 void ImageDocument::imageClicked(int x, int y)
268 if (!m_imageSizeIsKnown || imageFitsInWindow())
271 m_shouldShrinkImage = !m_shouldShrinkImage;
273 if (m_shouldShrinkImage)
280 float scale = this->scale();
282 int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width() / 2);
283 int scrollY = static_cast<int>(y / scale - (float)frame()->view()->height() / 2);
285 frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY));
289 void ImageDocument::imageUpdated()
291 ASSERT(m_imageElement);
293 if (m_imageSizeIsKnown)
296 if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this)).isEmpty())
299 m_imageSizeIsKnown = true;
301 if (shouldShrinkToFit()) {
302 // Force resizing of the image
307 void ImageDocument::restoreImageSize()
309 if (!m_imageElement || !m_imageSizeIsKnown)
312 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
313 m_imageElement->setWidth(imageSize.width());
314 m_imageElement->setHeight(imageSize.height());
316 if (imageFitsInWindow())
317 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor);
319 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-out", false);
321 m_didShrinkImage = false;
324 bool ImageDocument::imageFitsInWindow() const
329 FrameView* view = frame()->view();
333 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
334 #if ENABLE(TIZEN_VIEWPORT_META_TAG)
335 // FIXME: view->width() and view->height() are frame's rect size. And it's calculated after layout.
336 // But ImageDocument's loading can be begun before frame's rect size is fixed.
337 // So, we use layoutWidth() here.
338 LayoutSize windowSize = LayoutSize(view->layoutWidth(), view->layoutHeight());
340 LayoutSize windowSize = LayoutSize(view->width(), view->height());
343 return imageSize.width() <= windowSize.width() && imageSize.height() <= windowSize.height();
346 void ImageDocument::windowSizeChanged()
348 if (!m_imageElement || !m_imageSizeIsKnown)
351 bool fitsInWindow = imageFitsInWindow();
353 // If the image has been explicitly zoomed in, restore the cursor if the image fits
354 // and set it to a zoom out cursor if the image doesn't fit
355 if (!m_shouldShrinkImage) {
357 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor);
359 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-out", false);
363 if (m_didShrinkImage) {
364 // If the window has been resized so that the image fits, restore the image size
365 // otherwise update the restored image size.
371 // If the image isn't resized but needs to be, then resize it.
374 m_didShrinkImage = true;
379 CachedImage* ImageDocument::cachedImage()
382 createDocumentStructure();
384 return m_imageElement->cachedImage();
387 bool ImageDocument::shouldShrinkToFit() const
389 return frame()->page()->settings()->shrinksStandaloneImagesToFit() &&
390 frame()->page()->mainFrame() == frame();
395 void ImageEventListener::handleEvent(ScriptExecutionContext*, Event* event)
397 if (event->type() == eventNames().resizeEvent)
398 m_doc->windowSizeChanged();
399 else if (event->type() == eventNames().clickEvent && event->isMouseEvent()) {
400 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
401 m_doc->imageClicked(mouseEvent->x(), mouseEvent->y());
405 bool ImageEventListener::operator==(const EventListener& listener)
407 if (const ImageEventListener* imageEventListener = ImageEventListener::cast(&listener))
408 return m_doc == imageEventListener->m_doc;
414 ImageDocumentElement::~ImageDocumentElement()
417 m_imageDocument->disconnectImageElement();
420 void ImageDocumentElement::didMoveToNewDocument(Document* oldDocument)
422 if (m_imageDocument) {
423 m_imageDocument->disconnectImageElement();
426 HTMLImageElement::didMoveToNewDocument(oldDocument);