2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
#include "MediaDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsRect.h"
|
|
|
|
#include "nsHTMLDocument.h"
|
|
|
|
#include "nsIImageDocument.h"
|
|
|
|
#include "nsIImageLoadingContent.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsIDOMHTMLImageElement.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsIDOMMouseEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "imgIRequest.h"
|
|
|
|
#include "imgILoader.h"
|
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "nsStubImageDecoderObserver.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsStyleContext.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsStyleSet.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2011-10-29 13:03:55 -07:00
|
|
|
#include "nsIDOMHTMLElement.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2009-02-16 03:27:22 -08:00
|
|
|
#include "nsURILoader.h"
|
2008-02-07 11:58:05 -08:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIMarkupDocumentViewer.h"
|
2009-10-14 19:20:50 -07:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
2009-11-19 20:39:11 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2010-01-11 13:45:04 -08:00
|
|
|
#include "nsIScrollableFrame.h"
|
2011-08-11 06:29:50 -07:00
|
|
|
#include "nsContentUtils.h"
|
2010-05-05 11:18:05 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-04-30 06:12:05 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define AUTOMATIC_IMAGE_RESIZING_PREF "browser.enable_automatic_image_resizing"
|
2009-07-28 20:02:55 -07:00
|
|
|
#define CLICK_IMAGE_RESIZING_PREF "browser.enable_click_image_resizing"
|
2009-09-09 13:04:05 -07:00
|
|
|
//XXX A hack needed for Firefox's site specific zoom.
|
|
|
|
#define SITE_SPECIFIC_ZOOM "browser.zoom.siteSpecific"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class ImageDocument;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
class ImageListener : public MediaDocumentStreamListener
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageListener(ImageDocument* aDocument);
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~ImageListener();
|
|
|
|
|
2010-09-12 08:22:26 -07:00
|
|
|
/* nsIRequestObserver */
|
|
|
|
NS_IMETHOD OnStartRequest(nsIRequest* request, nsISupports *ctxt);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
class ImageDocument : public MediaDocument
|
|
|
|
, public nsIImageDocument
|
|
|
|
, public nsStubImageDecoderObserver
|
|
|
|
, public nsIDOMEventListener
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument();
|
|
|
|
virtual ~ImageDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-20 15:55:06 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
virtual nsresult Init();
|
|
|
|
|
|
|
|
virtual nsresult StartDocumentLoad(const char* aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIStreamListener** aDocListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aReset = true,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIContentSink* aSink = nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject);
|
|
|
|
virtual void Destroy();
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual void OnPageShow(bool aPersisted,
|
2009-09-01 06:15:33 -07:00
|
|
|
nsIDOMEventTarget* aDispatchStartTarget);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_NSIIMAGEDOCUMENT
|
|
|
|
|
|
|
|
// imgIDecoderObserver (override nsStubImageDecoderObserver)
|
|
|
|
NS_IMETHOD OnStartContainer(imgIRequest* aRequest, imgIContainer* aImage);
|
2012-08-08 12:42:37 -07:00
|
|
|
NS_IMETHOD OnStopContainer(imgIRequest* aRequest, imgIContainer* aImage);
|
2010-09-12 08:22:26 -07:00
|
|
|
NS_IMETHOD OnStopDecode(imgIRequest *aRequest, nsresult aStatus, const PRUnichar *aStatusArg);
|
2012-08-08 12:42:37 -07:00
|
|
|
NS_IMETHOD OnDiscard(imgIRequest *aRequest);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ImageDocument, MediaDocument)
|
2008-12-03 02:30:26 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
friend class ImageListener;
|
2009-11-19 20:39:11 -08:00
|
|
|
|
|
|
|
void DefaultCheckOverflowing() { CheckOverflowing(mResizeImageByDefault); }
|
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
virtual nsXPCClassInfo* GetClassInfo();
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual nsresult CreateSyntheticDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult CheckOverflowing(bool changeState);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void UpdateTitleAndCharset();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult ScrollImageTo(int32_t aX, int32_t aY, bool restoreImage);
|
2008-04-21 21:41:38 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
float GetRatio() {
|
2010-02-22 11:41:19 -08:00
|
|
|
return NS_MIN((float)mVisibleWidth / mImageWidth,
|
2007-03-22 10:30:00 -07:00
|
|
|
(float)mVisibleHeight / mImageHeight);
|
|
|
|
}
|
|
|
|
|
2009-09-01 06:15:33 -07:00
|
|
|
void ResetZoomLevel();
|
2008-02-07 11:58:05 -08:00
|
|
|
float GetZoomLevel();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIContent> mImageContent;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mVisibleWidth;
|
|
|
|
int32_t mVisibleHeight;
|
|
|
|
int32_t mImageWidth;
|
|
|
|
int32_t mImageHeight;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mResizeImageByDefault;
|
|
|
|
bool mClickResizingEnabled;
|
|
|
|
bool mImageIsOverflowing;
|
2007-03-22 10:30:00 -07:00
|
|
|
// mImageIsResized is true if the image is currently resized
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mImageIsResized;
|
2007-03-22 10:30:00 -07:00
|
|
|
// mShouldResize is true if the image should be resized when it doesn't fit
|
|
|
|
// mImageIsResized cannot be true when this is false, but mImageIsResized
|
|
|
|
// can be false when this is true
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mShouldResize;
|
|
|
|
bool mFirstResize;
|
2008-08-01 13:58:59 -07:00
|
|
|
// mObservingImageLoader is true while the observer is set.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mObservingImageLoader;
|
2009-09-01 06:15:33 -07:00
|
|
|
|
|
|
|
float mOriginalZoomLevel;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageListener::ImageListener(ImageDocument* aDocument)
|
|
|
|
: MediaDocumentStreamListener(aDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageListener::~ImageListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument *imgDoc = static_cast<ImageDocument*>(mDocument.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
if (!channel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> domWindow =
|
|
|
|
do_QueryInterface(imgDoc->GetScriptGlobalObject());
|
|
|
|
NS_ENSURE_TRUE(domWindow, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Do a ShouldProcess check to see whether to keep loading the image.
|
|
|
|
nsCOMPtr<nsIURI> channelURI;
|
|
|
|
channel->GetURI(getter_AddRefs(channelURI));
|
|
|
|
|
|
|
|
nsCAutoString mimeType;
|
|
|
|
channel->GetContentType(mimeType);
|
2007-08-07 18:16:09 -07:00
|
|
|
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
|
|
|
if (secMan) {
|
|
|
|
secMan->GetChannelPrincipal(channel, getter_AddRefs(channelPrincipal));
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t decision = nsIContentPolicy::ACCEPT;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_CheckContentProcessPolicy(nsIContentPolicy::TYPE_IMAGE,
|
|
|
|
channelURI,
|
2007-08-07 18:16:09 -07:00
|
|
|
channelPrincipal,
|
2007-03-22 10:30:00 -07:00
|
|
|
domWindow->GetFrameElementInternal(),
|
|
|
|
mimeType,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
&decision,
|
2007-08-07 18:16:09 -07:00
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
secMan);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(decision)) {
|
|
|
|
request->Cancel(NS_ERROR_CONTENT_BLOCKED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(imgDoc->mImageContent);
|
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
imageLoader->AddObserver(imgDoc);
|
2011-10-17 07:59:28 -07:00
|
|
|
imgDoc->mObservingImageLoader = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
imageLoader->LoadImageWithChannel(channel, getter_AddRefs(mNextStream));
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
return MediaDocumentStreamListener::OnStartRequest(request, ctxt);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::ImageDocument()
|
|
|
|
: mOriginalZoomLevel(1.0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// NOTE! nsDocument::operator new() zeroes out all members, so don't
|
|
|
|
// bother initializing members to 0.
|
|
|
|
}
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::~ImageDocument()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ImageDocument)
|
2008-12-03 02:30:26 -08:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ImageDocument, MediaDocument)
|
2008-12-03 02:30:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mImageContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ImageDocument, MediaDocument)
|
2008-12-03 02:30:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mImageContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ImageDocument, MediaDocument)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ImageDocument, MediaDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
DOMCI_NODE_DATA(ImageDocument, ImageDocument)
|
2010-01-12 05:08:43 -08:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(ImageDocument)
|
|
|
|
NS_HTML_DOCUMENT_INTERFACE_TABLE_BEGIN(ImageDocument)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(ImageDocument, nsIImageDocument)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(ImageDocument, imgIDecoderObserver)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(ImageDocument, imgIContainerObserver)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(ImageDocument, nsIDOMEventListener)
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_END
|
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_TO_MAP_SEGUE
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ImageDocument)
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MediaDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::Init()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-05-26 01:06:31 -07:00
|
|
|
nsresult rv = MediaDocument::Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
mResizeImageByDefault = Preferences::GetBool(AUTOMATIC_IMAGE_RESIZING_PREF);
|
|
|
|
mClickResizingEnabled = Preferences::GetBool(CLICK_IMAGE_RESIZING_PREF);
|
2007-03-22 10:30:00 -07:00
|
|
|
mShouldResize = mResizeImageByDefault;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFirstResize = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::StartDocumentLoad(const char* aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIStreamListener** aDocListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aReset,
|
2011-05-26 01:06:31 -07:00
|
|
|
nsIContentSink* aSink)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv =
|
2011-05-26 01:06:31 -07:00
|
|
|
MediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer,
|
|
|
|
aDocListener, aReset, aSink);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-09-09 13:04:05 -07:00
|
|
|
mOriginalZoomLevel =
|
2011-09-28 23:19:26 -07:00
|
|
|
Preferences::GetBool(SITE_SPECIFIC_ZOOM, false) ? 1.0 : GetZoomLevel();
|
2009-09-01 06:15:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(aDocListener, "null aDocListener");
|
|
|
|
*aDocListener = new ImageListener(this);
|
|
|
|
NS_ADDREF(*aDocListener);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::Destroy()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mImageContent) {
|
|
|
|
// Remove our event listener from the image content.
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mImageContent);
|
2011-10-17 07:59:28 -07:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("click"), this, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Break reference cycle with mImageContent, if we have one
|
2008-08-01 13:58:59 -07:00
|
|
|
if (mObservingImageLoader) {
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageContent);
|
|
|
|
if (imageLoader) {
|
2011-02-20 06:40:45 -08:00
|
|
|
// Push a null JSContext on the stack so that code that
|
|
|
|
// nsImageLoadingContent doesn't think it's being called by JS. See
|
|
|
|
// Bug 631241
|
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.PushNull();
|
2008-08-01 13:58:59 -07:00
|
|
|
imageLoader->RemoveObserver(this);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mImageContent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
MediaDocument::Destroy();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// If the script global object is changing, we need to unhook our event
|
|
|
|
// listeners on the window.
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
if (mScriptGlobalObject &&
|
|
|
|
aScriptGlobalObject != mScriptGlobalObject) {
|
|
|
|
target = do_QueryInterface(mScriptGlobalObject);
|
2011-10-17 07:59:28 -07:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this,
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the script global object on the superclass before doing
|
|
|
|
// anything that might require it....
|
2011-12-15 07:10:36 -08:00
|
|
|
MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aScriptGlobalObject) {
|
2010-04-30 06:12:05 -07:00
|
|
|
if (!GetRootElement()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Create synthetic document
|
2008-09-23 21:37:56 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
CreateSyntheticDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
|
|
|
|
|
|
|
|
target = do_QueryInterface(mImageContent);
|
2011-10-17 07:59:28 -07:00
|
|
|
target->AddEventListener(NS_LITERAL_STRING("click"), this, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
target = do_QueryInterface(aScriptGlobalObject);
|
2011-10-17 07:59:28 -07:00
|
|
|
target->AddEventListener(NS_LITERAL_STRING("resize"), this, false);
|
|
|
|
target->AddEventListener(NS_LITERAL_STRING("keypress"), this, false);
|
2012-01-07 18:43:48 -08:00
|
|
|
|
2012-07-27 06:35:09 -07:00
|
|
|
if (!nsContentUtils::IsChildOfSameType(this) &&
|
|
|
|
GetReadyStateEnum() != nsIDocument::READYSTATE_COMPLETE) {
|
2012-01-07 18:43:48 -08:00
|
|
|
LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelImageDocument.css"));
|
2012-04-10 16:03:41 -07:00
|
|
|
LinkStylesheet(NS_LITERAL_STRING("chrome://global/skin/TopLevelImageDocument.css"));
|
2012-01-07 18:43:48 -08:00
|
|
|
}
|
2012-07-27 06:35:09 -07:00
|
|
|
BecomeInteractive();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-01 06:15:33 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
ImageDocument::OnPageShow(bool aPersisted,
|
2011-05-26 01:06:31 -07:00
|
|
|
nsIDOMEventTarget* aDispatchStartTarget)
|
2009-09-01 06:15:33 -07:00
|
|
|
{
|
|
|
|
if (aPersisted) {
|
2009-09-09 13:04:05 -07:00
|
|
|
mOriginalZoomLevel =
|
2011-09-28 23:19:26 -07:00
|
|
|
Preferences::GetBool(SITE_SPECIFIC_ZOOM, false) ? 1.0 : GetZoomLevel();
|
2009-09-01 06:15:33 -07:00
|
|
|
}
|
2011-05-26 01:06:31 -07:00
|
|
|
MediaDocument::OnPageShow(aPersisted, aDispatchStartTarget);
|
2009-09-01 06:15:33 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
ImageDocument::GetImageResizingEnabled(bool* aImageResizingEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aImageResizingEnabled = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
ImageDocument::GetImageIsOverflowing(bool* aImageIsOverflowing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aImageIsOverflowing = mImageIsOverflowing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
ImageDocument::GetImageIsResized(bool* aImageIsResized)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aImageIsResized = mImageIsResized;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::GetImageRequest(imgIRequest** aImageRequest)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageContent);
|
|
|
|
if (imageLoader) {
|
|
|
|
return imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
aImageRequest);
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aImageRequest = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::ShrinkToFit()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-10-14 21:03:33 -07:00
|
|
|
if (!mImageContent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-10-14 19:20:50 -07:00
|
|
|
if (GetZoomLevel() != mOriginalZoomLevel && mImageIsResized &&
|
|
|
|
!nsContentUtils::IsChildOfSameType(this)) {
|
2008-02-07 11:58:05 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-05 06:24:30 -07:00
|
|
|
// Keep image content alive while changing the attributes.
|
|
|
|
nsCOMPtr<nsIContent> imageContent = mImageContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLImageElement> image = do_QueryInterface(mImageContent);
|
2010-02-22 11:41:19 -08:00
|
|
|
image->SetWidth(NS_MAX(1, NSToCoordFloor(GetRatio() * mImageWidth)));
|
|
|
|
image->SetHeight(NS_MAX(1, NSToCoordFloor(GetRatio() * mImageHeight)));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-21 21:41:38 -07:00
|
|
|
// The view might have been scrolled when zooming in, scroll back to the
|
|
|
|
// origin now that we're showing a shrunk-to-window version.
|
2011-10-17 07:59:28 -07:00
|
|
|
(void) ScrollImageTo(0, 0, false);
|
2008-04-21 21:41:38 -07:00
|
|
|
|
2007-08-05 06:24:30 -07:00
|
|
|
imageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_LITERAL_STRING("cursor: -moz-zoom-in"), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mImageIsResized = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UpdateTitleAndCharset();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
ImageDocument::RestoreImageTo(int32_t aX, int32_t aY)
|
2008-04-21 21:41:38 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return ScrollImageTo(aX, aY, true);
|
2008-04-21 21:41:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
ImageDocument::ScrollImageTo(int32_t aX, int32_t aY, bool restoreImage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
float ratio = GetRatio();
|
|
|
|
|
2008-04-21 21:41:38 -07:00
|
|
|
if (restoreImage) {
|
|
|
|
RestoreImage();
|
|
|
|
FlushPendingNotifications(Flush_Layout);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-25 06:59:57 -07:00
|
|
|
nsIPresShell *shell = GetShell();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!shell)
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-01-11 13:45:04 -08:00
|
|
|
nsIScrollableFrame* sf = shell->GetRootScrollFrameAsScrollable();
|
|
|
|
if (!sf)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2010-01-11 13:45:04 -08:00
|
|
|
nsRect portRect = sf->GetScrollPortRect();
|
|
|
|
sf->ScrollTo(nsPoint(nsPresContext::CSSPixelsToAppUnits(aX/ratio) - portRect.width/2,
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(aY/ratio) - portRect.height/2),
|
|
|
|
nsIScrollableFrame::INSTANT);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::RestoreImage()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-10-14 21:03:33 -07:00
|
|
|
if (!mImageContent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-08-05 06:24:30 -07:00
|
|
|
// Keep image content alive while changing the attributes.
|
|
|
|
nsCOMPtr<nsIContent> imageContent = mImageContent;
|
2011-10-17 07:59:28 -07:00
|
|
|
imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::width, true);
|
|
|
|
imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::height, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mImageIsOverflowing) {
|
2007-08-05 06:24:30 -07:00
|
|
|
imageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_LITERAL_STRING("cursor: -moz-zoom-out"), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::style, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mImageIsResized = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
UpdateTitleAndCharset();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::ToggleImageSize()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mShouldResize = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mImageIsResized) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mShouldResize = false;
|
2009-09-01 06:15:33 -07:00
|
|
|
ResetZoomLevel();
|
2007-03-22 10:30:00 -07:00
|
|
|
RestoreImage();
|
|
|
|
}
|
|
|
|
else if (mImageIsOverflowing) {
|
2009-09-01 06:15:33 -07:00
|
|
|
ResetZoomLevel();
|
2007-03-22 10:30:00 -07:00
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::OnStartContainer(imgIRequest* aRequest, imgIContainer* aImage)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aImage->GetWidth(&mImageWidth);
|
|
|
|
aImage->GetHeight(&mImageHeight);
|
2009-11-19 20:39:11 -08:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2011-05-26 01:06:31 -07:00
|
|
|
NS_NewRunnableMethod(this, &ImageDocument::DefaultCheckOverflowing);
|
2009-11-19 20:39:11 -08:00
|
|
|
nsContentUtils::AddScriptRunner(runnable);
|
2007-03-22 10:30:00 -07:00
|
|
|
UpdateTitleAndCharset();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:42:37 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageDocument::OnStopContainer(imgIRequest* aRequest, imgIContainer* aImage)
|
|
|
|
{
|
|
|
|
if (mImageContent) {
|
|
|
|
// Update the background-color of the image only after the
|
|
|
|
// image has been decoded to prevent flashes of just the
|
|
|
|
// background-color.
|
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
|
|
|
|
NS_LITERAL_STRING("decoded"), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-12 08:22:26 -07:00
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::OnStopDecode(imgIRequest *aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar *aStatusArg)
|
2010-09-12 08:22:26 -07:00
|
|
|
{
|
|
|
|
UpdateTitleAndCharset();
|
|
|
|
|
|
|
|
// mImageContent can be null if the document is already destroyed
|
|
|
|
if (NS_FAILED(aStatus) && mStringBundle && mImageContent) {
|
|
|
|
nsCAutoString src;
|
|
|
|
mDocumentURI->GetSpec(src);
|
|
|
|
NS_ConvertUTF8toUTF16 srcString(src);
|
|
|
|
const PRUnichar* formatString[] = { srcString.get() };
|
|
|
|
nsXPIDLString errorMsg;
|
|
|
|
NS_NAMED_LITERAL_STRING(str, "InvalidImage");
|
|
|
|
mStringBundle->FormatStringFromName(str.get(), formatString, 1,
|
|
|
|
getter_Copies(errorMsg));
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, errorMsg, false);
|
2010-09-12 08:22:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:42:37 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageDocument::OnDiscard(imgIRequest *aRequest)
|
|
|
|
{
|
|
|
|
// mImageContent can be null if the document is already destroyed
|
|
|
|
if (mImageContent) {
|
|
|
|
// Remove any decoded-related styling when the image is unloaded.
|
|
|
|
mImageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::_class,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::HandleEvent(nsIDOMEvent* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
|
|
|
if (eventType.EqualsLiteral("resize")) {
|
2011-10-17 07:59:28 -07:00
|
|
|
CheckOverflowing(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-07-28 20:02:55 -07:00
|
|
|
else if (eventType.EqualsLiteral("click") && mClickResizingEnabled) {
|
2009-09-01 06:15:33 -07:00
|
|
|
ResetZoomLevel();
|
2011-10-17 07:59:28 -07:00
|
|
|
mShouldResize = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mImageIsResized) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t x = 0, y = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMMouseEvent> event(do_QueryInterface(aEvent));
|
|
|
|
if (event) {
|
|
|
|
event->GetClientX(&x);
|
|
|
|
event->GetClientY(&y);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t left = 0, top = 0;
|
2011-10-29 13:03:55 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> htmlElement =
|
|
|
|
do_QueryInterface(mImageContent);
|
|
|
|
htmlElement->GetOffsetLeft(&left);
|
|
|
|
htmlElement->GetOffsetTop(&top);
|
2007-03-22 10:30:00 -07:00
|
|
|
x -= left;
|
|
|
|
y -= top;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mShouldResize = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
RestoreImageTo(x, y);
|
|
|
|
}
|
|
|
|
else if (mImageIsOverflowing) {
|
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::CreateSyntheticDocument()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Synthesize an html document that refers to the image
|
2011-05-26 01:06:31 -07:00
|
|
|
nsresult rv = MediaDocument::CreateSyntheticDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-06 21:04:04 -07:00
|
|
|
// We must declare the image as a block element. If we stay as
|
|
|
|
// an inline element, our parent LineBox will be inline too and
|
|
|
|
// ignore the available height during reflow.
|
|
|
|
// This is bad during printing, it means tall image frames won't know
|
|
|
|
// the size of the paper and cannot break into continuations along
|
|
|
|
// multiple pages.
|
2011-08-10 21:23:00 -07:00
|
|
|
Element* head = GetHeadElement();
|
2011-11-10 15:08:07 -08:00
|
|
|
NS_ENSURE_TRUE(head, NS_ERROR_FAILURE);
|
2011-07-06 21:04:04 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINodeInfo> nodeInfo;
|
2011-11-10 15:08:07 -08:00
|
|
|
if (nsContentUtils::IsChildOfSameType(this)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::style, nullptr,
|
2011-11-10 15:08:07 -08:00
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
|
|
|
NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsRefPtr<nsGenericHTMLElement> styleContent = NS_NewHTMLStyleElement(nodeInfo.forget());
|
|
|
|
NS_ENSURE_TRUE(styleContent, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
styleContent->SetTextContent(NS_LITERAL_STRING("img { display: block; }"));
|
|
|
|
head->AppendChildTo(styleContent, false);
|
2011-07-06 21:04:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the image element
|
2011-08-10 21:23:00 -07:00
|
|
|
Element* body = GetBodyElement();
|
|
|
|
if (!body) {
|
|
|
|
NS_WARNING("no body on image document!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::img, nullptr,
|
2011-06-14 00:56:49 -07:00
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
2008-09-25 15:46:52 -07:00
|
|
|
NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
mImageContent = NS_NewHTMLImageElement(nodeInfo.forget());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mImageContent) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageContent);
|
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCAutoString src;
|
|
|
|
mDocumentURI->GetSpec(src);
|
|
|
|
|
2011-01-31 20:50:01 -08:00
|
|
|
// Push a null JSContext on the stack so that code that runs within
|
|
|
|
// the below code doesn't think it's being called by JS. See bug
|
|
|
|
// 604262.
|
|
|
|
nsCxPusher pusher;
|
|
|
|
pusher.PushNull();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF8toUTF16 srcString(src);
|
|
|
|
// Make sure not to start the image load from here...
|
2011-10-17 07:59:28 -07:00
|
|
|
imageLoader->SetLoadingEnabled(false);
|
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::src, srcString, false);
|
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, srcString, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
body->AppendChildTo(mImageContent, false);
|
|
|
|
imageLoader->SetLoadingEnabled(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
ImageDocument::CheckOverflowing(bool changeState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-26 13:20:30 -07:00
|
|
|
/* Create a scope so that the style context gets destroyed before we might
|
2008-01-09 01:38:28 -08:00
|
|
|
* call RebuildStyleData. Also, holding onto pointers to the
|
2012-03-07 22:11:45 -08:00
|
|
|
* presentation through style resolution is potentially dangerous.
|
2007-07-26 13:20:30 -07:00
|
|
|
*/
|
|
|
|
{
|
2010-06-25 06:59:57 -07:00
|
|
|
nsIPresShell *shell = GetShell();
|
2007-07-26 13:20:30 -07:00
|
|
|
if (!shell) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-26 13:20:30 -07:00
|
|
|
nsPresContext *context = shell->GetPresContext();
|
|
|
|
nsRect visibleArea = context->GetVisibleArea();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-14 19:20:50 -07:00
|
|
|
mVisibleWidth = nsPresContext::AppUnitsToIntCSSPixels(visibleArea.width);
|
|
|
|
mVisibleHeight = nsPresContext::AppUnitsToIntCSSPixels(visibleArea.height);
|
2007-07-26 13:20:30 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool imageWasOverflowing = mImageIsOverflowing;
|
2007-03-22 10:30:00 -07:00
|
|
|
mImageIsOverflowing =
|
|
|
|
mImageWidth > mVisibleWidth || mImageHeight > mVisibleHeight;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool windowBecameBigEnough = imageWasOverflowing && !mImageIsOverflowing;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (changeState || mShouldResize || mFirstResize ||
|
|
|
|
windowBecameBigEnough) {
|
|
|
|
if (mImageIsOverflowing && (changeState || mShouldResize)) {
|
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
else if (mImageIsResized || mFirstResize || windowBecameBigEnough) {
|
|
|
|
RestoreImage();
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mFirstResize = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::UpdateTitleAndCharset()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCAutoString typeStr;
|
|
|
|
nsCOMPtr<imgIRequest> imageRequest;
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageContent);
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(imageRequest));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageRequest) {
|
|
|
|
nsXPIDLCString mimeType;
|
|
|
|
imageRequest->GetMimeType(getter_Copies(mimeType));
|
|
|
|
ToUpperCase(mimeType);
|
|
|
|
nsXPIDLCString::const_iterator start, end;
|
|
|
|
mimeType.BeginReading(start);
|
|
|
|
mimeType.EndReading(end);
|
|
|
|
nsXPIDLCString::const_iterator iter = end;
|
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("IMAGE/"), start, iter) &&
|
|
|
|
iter != end) {
|
|
|
|
// strip out "X-" if any
|
|
|
|
if (*iter == 'X') {
|
|
|
|
++iter;
|
|
|
|
if (iter != end && *iter == '-') {
|
|
|
|
++iter;
|
|
|
|
if (iter == end) {
|
|
|
|
// looks like "IMAGE/X-" is the type?? Bail out of here.
|
|
|
|
mimeType.BeginReading(iter);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typeStr = Substring(iter, end);
|
|
|
|
} else {
|
|
|
|
typeStr = mimeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLString status;
|
|
|
|
if (mImageIsResized) {
|
|
|
|
nsAutoString ratioStr;
|
|
|
|
ratioStr.AppendInt(NSToCoordFloor(GetRatio() * 100));
|
|
|
|
|
|
|
|
const PRUnichar* formatString[1] = { ratioStr.get() };
|
|
|
|
mStringBundle->FormatStringFromName(NS_LITERAL_STRING("ScaledImage").get(),
|
|
|
|
formatString, 1,
|
|
|
|
getter_Copies(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* const formatNames[4] =
|
|
|
|
{
|
|
|
|
"ImageTitleWithNeitherDimensionsNorFile",
|
|
|
|
"ImageTitleWithoutDimensions",
|
|
|
|
"ImageTitleWithDimensions",
|
|
|
|
"ImageTitleWithDimensionsAndFile",
|
|
|
|
};
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
MediaDocument::UpdateTitleAndCharset(typeStr, formatNames,
|
|
|
|
mImageWidth, mImageHeight, status);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:58:05 -08:00
|
|
|
void
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::ResetZoomLevel()
|
2008-02-07 11:58:05 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocumentContainer);
|
|
|
|
if (docShell) {
|
2009-10-14 19:20:50 -07:00
|
|
|
if (nsContentUtils::IsChildOfSameType(this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:58:05 -08:00
|
|
|
nsCOMPtr<nsIContentViewer> cv;
|
|
|
|
docShell->GetContentViewer(getter_AddRefs(cv));
|
|
|
|
nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(cv);
|
|
|
|
if (mdv) {
|
2009-09-01 06:15:33 -07:00
|
|
|
mdv->SetFullZoom(mOriginalZoomLevel);
|
2008-02-07 11:58:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2011-05-26 01:06:31 -07:00
|
|
|
ImageDocument::GetZoomLevel()
|
2008-02-07 11:58:05 -08:00
|
|
|
{
|
2009-09-01 06:15:33 -07:00
|
|
|
float zoomLevel = mOriginalZoomLevel;
|
2008-02-07 11:58:05 -08:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocumentContainer);
|
|
|
|
if (docShell) {
|
|
|
|
nsCOMPtr<nsIContentViewer> cv;
|
|
|
|
docShell->GetContentViewer(getter_AddRefs(cv));
|
|
|
|
nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(cv);
|
|
|
|
if (mdv) {
|
|
|
|
mdv->GetFullZoom(&zoomLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zoomLevel;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
DOMCI_DATA(ImageDocument, mozilla::dom::ImageDocument)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
NS_NewImageDocument(nsIDocument** aResult)
|
|
|
|
{
|
2011-05-26 01:06:31 -07:00
|
|
|
mozilla::dom::ImageDocument* doc = new mozilla::dom::ImageDocument();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(doc);
|
|
|
|
|
2011-05-26 01:06:31 -07:00
|
|
|
nsresult rv = doc->Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = doc;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|