2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2001-03-22 01:42:32 +00:00
|
|
|
|
2013-05-06 13:42:00 +00:00
|
|
|
#include "ImageDocument.h"
|
2018-11-07 12:58:18 +00:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2018-03-22 18:20:41 +00:00
|
|
|
#include "mozilla/ComputedStyle.h"
|
2018-03-20 04:16:05 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-04-20 04:49:29 +00:00
|
|
|
#include "mozilla/dom/Event.h"
|
2013-05-06 13:42:00 +00:00
|
|
|
#include "mozilla/dom/ImageDocumentBinding.h"
|
2016-03-04 23:02:08 +00:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2018-03-20 04:16:05 +00:00
|
|
|
#include "mozilla/dom/MouseEvent.h"
|
2019-03-29 15:12:47 +00:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-07-26 01:10:23 +00:00
|
|
|
#include "mozilla/StaticPrefs_privacy.h"
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "nsRect.h"
|
2003-04-02 03:27:20 +00:00
|
|
|
#include "nsIImageLoadingContent.h"
|
2001-10-30 10:14:06 +00:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2013-11-15 07:12:43 +00:00
|
|
|
#include "nsDocShell.h"
|
2019-01-02 13:05:23 +00:00
|
|
|
#include "DocumentInlines.h"
|
2013-03-15 20:45:40 +00:00
|
|
|
#include "nsDOMTokenList.h"
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "nsIDOMEventListener.h"
|
2013-11-19 22:24:59 +00:00
|
|
|
#include "nsIFrame.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2001-03-22 01:42:32 +00:00
|
|
|
#include "imgIRequest.h"
|
|
|
|
#include "imgILoader.h"
|
2001-04-23 23:46:50 +00:00
|
|
|
#include "imgIContainer.h"
|
2012-10-12 16:11:22 +00:00
|
|
|
#include "imgINotificationObserver.h"
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2004-07-18 19:40:44 +00:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2009-02-16 11:27:22 +00:00
|
|
|
#include "nsURILoader.h"
|
2008-02-07 19:58:05 +00:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIContentViewer.h"
|
2009-11-20 04:39:11 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2010-01-11 21:45:04 +00:00
|
|
|
#include "nsIScrollableFrame.h"
|
2011-08-11 13:29:50 +00:00
|
|
|
#include "nsContentUtils.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2010-04-30 13:12:05 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
#define AUTOMATIC_IMAGE_RESIZING_PREF "browser.enable_automatic_image_resizing"
|
2009-07-29 03:02:55 +00:00
|
|
|
#define CLICK_IMAGE_RESIZING_PREF "browser.enable_click_image_resizing"
|
2017-06-19 02:33:23 +00:00
|
|
|
|
2009-09-09 20:04:05 +00:00
|
|
|
// XXX A hack needed for Firefox's site specific zoom.
|
2017-06-19 02:33:23 +00:00
|
|
|
static bool IsSiteSpecific() {
|
2018-09-10 18:36:17 +00:00
|
|
|
return !mozilla::StaticPrefs::privacy_resistFingerprinting() &&
|
2017-06-19 02:33:23 +00:00
|
|
|
mozilla::Preferences::GetBool("browser.zoom.siteSpecific", false);
|
|
|
|
}
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2017-06-19 02:33:23 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
class ImageListener : public MediaDocumentStreamListener {
|
2003-02-18 00:18:44 +00:00
|
|
|
public:
|
2013-07-29 15:03:41 +00:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit ImageListener(ImageDocument* aDocument);
|
2003-02-18 00:18:44 +00:00
|
|
|
virtual ~ImageListener();
|
|
|
|
};
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
ImageListener::ImageListener(ImageDocument* aDocument)
|
|
|
|
: MediaDocumentStreamListener(aDocument) {}
|
2003-02-18 00:18:44 +00:00
|
|
|
|
|
|
|
ImageListener::~ImageListener() {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-27 23:41:04 +00:00
|
|
|
ImageListener::OnStartRequest(nsIRequest* request) {
|
2003-04-04 00:26:33 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
ImageDocument* imgDoc = static_cast<ImageDocument*>(mDocument.get());
|
2003-02-18 00:18:44 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
if (!channel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> domWindow = imgDoc->GetWindow();
|
2004-07-18 19:40:44 +00:00
|
|
|
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));
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString mimeType;
|
2004-07-18 19:40:44 +00:00
|
|
|
channel->GetContentType(mimeType);
|
2007-08-08 01:16:09 +00:00
|
|
|
|
2019-02-20 12:27:25 +00:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
|
2018-03-29 10:16:23 +00:00
|
|
|
// query the corresponding arguments for the channel loadinfo and pass
|
|
|
|
// it on to the temporary loadinfo used for content policy checks.
|
|
|
|
nsCOMPtr<nsINode> requestingNode = domWindow->GetFrameElementInternal();
|
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrincipal;
|
|
|
|
if (requestingNode) {
|
|
|
|
loadingPrincipal = requestingNode->NodePrincipal();
|
|
|
|
} else {
|
|
|
|
nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(
|
|
|
|
channel, getter_AddRefs(loadingPrincipal));
|
2007-08-08 01:16:09 +00:00
|
|
|
}
|
2013-07-29 15:03:41 +00:00
|
|
|
|
2018-03-29 10:16:23 +00:00
|
|
|
nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new net::LoadInfo(
|
2019-02-20 12:27:25 +00:00
|
|
|
loadingPrincipal, loadInfo->TriggeringPrincipal(), requestingNode,
|
|
|
|
nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK,
|
2018-03-29 10:16:23 +00:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_IMAGE);
|
Bug 1407056: Part 1 - Provide more consistent principal/origin URL to content policies. r=bz,ckerschb
We're currently fairly vague and inconsistent about the values we provide to
content policy implementations for requestOrigin and requestPrincipal. In some
cases they're the triggering principal, sometimes the loading principal,
sometimes the channel principal.
Our existing content policy implementations which require or expect a loading
principal currently retrieve it from the context node. Since no current
callers require the principal to be the loading principal, and some already
expect it to be the triggering principal (which there's currently no other way
to retrieve), I chose to pass the triggering principal whenever possible, but
use the loading principal to determine the origin URL.
As a follow-up, I'd like to change the nsIContentPolicy interface to
explicitly receive loading and triggering principals, or possibly just
LoadInfo instances, rather than poorly-defined request
origin/principal/context args. But since that may cause trouble for
comm-central, I'd rather not do it as part of this bug.
MozReview-Commit-ID: LqD9GxdzMte
--HG--
extra : rebase_source : 41ce439912ae7b895e0a3b0e660fa6ba571eb50f
2017-10-12 22:43:55 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t decision = nsIContentPolicy::ACCEPT;
|
2018-03-29 10:16:23 +00:00
|
|
|
nsresult rv = NS_CheckContentProcessPolicy(
|
|
|
|
channelURI, secCheckLoadInfo, mimeType, &decision,
|
2017-07-10 22:00:03 +00:00
|
|
|
nsContentUtils::GetContentPolicy());
|
2013-07-29 15:03:41 +00:00
|
|
|
|
2004-07-18 19:40:44 +00:00
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(decision)) {
|
|
|
|
request->Cancel(NS_ERROR_CONTENT_BLOCKED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-29 22:23:22 +00:00
|
|
|
if (!imgDoc->mObservingImageLoader) {
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(imgDoc->mImageContent);
|
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
2003-04-02 03:27:20 +00:00
|
|
|
|
2017-07-19 18:15:12 +00:00
|
|
|
imageLoader->AddNativeObserver(imgDoc);
|
2014-07-29 22:23:22 +00:00
|
|
|
imgDoc->mObservingImageLoader = true;
|
|
|
|
imageLoader->LoadImageWithChannel(channel, getter_AddRefs(mNextStream));
|
|
|
|
}
|
2003-02-18 00:18:44 +00:00
|
|
|
|
2019-02-27 23:41:04 +00:00
|
|
|
return MediaDocumentStreamListener::OnStartRequest(request);
|
2003-02-18 00:18:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 15:03:41 +00:00
|
|
|
NS_IMETHODIMP
|
2019-02-27 23:41:31 +00:00
|
|
|
ImageListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
|
2013-07-29 15:03:41 +00:00
|
|
|
ImageDocument* imgDoc = static_cast<ImageDocument*>(mDocument.get());
|
2018-12-26 02:32:55 +00:00
|
|
|
nsContentUtils::DispatchChromeEvent(imgDoc, ToSupports(imgDoc),
|
2013-07-29 15:03:41 +00:00
|
|
|
NS_LITERAL_STRING("ImageContentLoaded"),
|
2018-06-25 16:23:50 +00:00
|
|
|
CanBubble::eYes, Cancelable::eYes);
|
2019-02-27 23:41:31 +00:00
|
|
|
return MediaDocumentStreamListener::OnStopRequest(aRequest, aStatus);
|
2013-07-29 15:03:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
ImageDocument::ImageDocument()
|
2017-01-24 16:10:39 +00:00
|
|
|
: MediaDocument(),
|
|
|
|
mVisibleWidth(0.0),
|
|
|
|
mVisibleHeight(0.0),
|
|
|
|
mImageWidth(0),
|
|
|
|
mImageHeight(0),
|
|
|
|
mResizeImageByDefault(false),
|
|
|
|
mClickResizingEnabled(false),
|
|
|
|
mImageIsOverflowingHorizontally(false),
|
|
|
|
mImageIsOverflowingVertically(false),
|
|
|
|
mImageIsResized(false),
|
|
|
|
mShouldResize(false),
|
|
|
|
mFirstResize(false),
|
|
|
|
mObservingImageLoader(false),
|
2018-11-07 12:58:18 +00:00
|
|
|
mTitleUpdateInProgress(false),
|
|
|
|
mHasCustomTitle(false),
|
2017-01-24 16:10:39 +00:00
|
|
|
mOriginalZoomLevel(1.0)
|
2018-02-20 22:07:21 +00:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
,
|
|
|
|
mOriginalResolution(1.0)
|
|
|
|
#endif
|
2003-01-18 10:17:58 +00:00
|
|
|
{
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
ImageDocument::~ImageDocument() {}
|
2008-12-03 10:30:26 +00:00
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ImageDocument, MediaDocument, mImageContent)
|
2008-12-03 10:30:26 +00:00
|
|
|
|
2017-08-31 23:29:22 +00:00
|
|
|
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(ImageDocument, MediaDocument,
|
|
|
|
nsIImageDocument,
|
|
|
|
imgINotificationObserver,
|
|
|
|
nsIDOMEventListener)
|
2002-01-25 06:37:35 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
nsresult ImageDocument::Init() {
|
|
|
|
nsresult rv = MediaDocument::Init();
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-25 06:37:35 +00:00
|
|
|
|
2011-05-25 06:31:59 +00:00
|
|
|
mResizeImageByDefault = Preferences::GetBool(AUTOMATIC_IMAGE_RESIZING_PREF);
|
|
|
|
mClickResizingEnabled = Preferences::GetBool(CLICK_IMAGE_RESIZING_PREF);
|
2006-03-07 01:44:44 +00:00
|
|
|
mShouldResize = mResizeImageByDefault;
|
2011-10-17 14:59:28 +00:00
|
|
|
mFirstResize = true;
|
2002-01-25 06:37:35 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
JSObject* ImageDocument::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-11-28 23:24:53 +00:00
|
|
|
return ImageDocument_Binding::Wrap(aCx, this, aGivenProto);
|
2013-05-06 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
nsresult ImageDocument::StartDocumentLoad(const char* aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIStreamListener** aDocListener,
|
|
|
|
bool aReset, nsIContentSink* aSink) {
|
|
|
|
nsresult rv = MediaDocument::StartDocumentLoad(
|
|
|
|
aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink);
|
1998-12-11 02:47:25 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2000-01-27 02:15:19 +00:00
|
|
|
|
2017-06-19 02:33:23 +00:00
|
|
|
mOriginalZoomLevel = IsSiteSpecific() ? 1.0 : GetZoomLevel();
|
2018-02-20 22:07:21 +00:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
mOriginalResolution = GetResolution();
|
|
|
|
#endif
|
2009-09-01 13:15:33 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
NS_ASSERTION(aDocListener, "null aDocListener");
|
2003-04-04 00:26:33 +00:00
|
|
|
*aDocListener = new ImageListener(this);
|
|
|
|
NS_ADDREF(*aDocListener);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::Destroy() {
|
2005-05-20 01:31:26 +00:00
|
|
|
if (mImageContent) {
|
|
|
|
// Remove our event listener from the image content.
|
2018-10-01 21:38:01 +00:00
|
|
|
nsCOMPtr<EventTarget> target = mImageContent;
|
2013-11-19 22:24:59 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("load"), this, false);
|
2011-10-17 14:59:28 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("click"), this, false);
|
2005-05-20 01:31:26 +00:00
|
|
|
|
|
|
|
// Break reference cycle with mImageContent, if we have one
|
2008-08-01 20:58:59 +00:00
|
|
|
if (mObservingImageLoader) {
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(mImageContent);
|
|
|
|
if (imageLoader) {
|
2017-07-19 18:15:12 +00:00
|
|
|
imageLoader->RemoveNativeObserver(this);
|
2008-08-01 20:58:59 +00:00
|
|
|
}
|
2005-05-20 01:31:26 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mImageContent = nullptr;
|
2005-05-04 20:22:32 +00:00
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
MediaDocument::Destroy();
|
2005-05-04 20:22:32 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::SetScriptGlobalObject(
|
|
|
|
nsIScriptGlobalObject* aScriptGlobalObject) {
|
2005-05-04 20:22:32 +00:00
|
|
|
// If the script global object is changing, we need to unhook our event
|
|
|
|
// listeners on the window.
|
2013-04-06 00:44:15 +00:00
|
|
|
nsCOMPtr<EventTarget> target;
|
2005-05-04 20:22:32 +00:00
|
|
|
if (mScriptGlobalObject && aScriptGlobalObject != mScriptGlobalObject) {
|
|
|
|
target = do_QueryInterface(mScriptGlobalObject);
|
2011-10-17 14:59:28 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, false);
|
2005-05-04 20:22:32 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, false);
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
2003-03-20 02:03:13 +00:00
|
|
|
|
|
|
|
// Set the script global object on the superclass before doing
|
|
|
|
// anything that might require it....
|
2011-12-15 15:10:36 +00:00
|
|
|
MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
|
2003-03-26 07:41:30 +00:00
|
|
|
|
2003-03-20 02:03:13 +00:00
|
|
|
if (aScriptGlobalObject) {
|
2018-05-23 15:06:34 +00:00
|
|
|
if (!InitialSetupHasBeenDone()) {
|
|
|
|
MOZ_ASSERT(!GetRootElement(), "Where did the root element come from?");
|
2005-05-04 20:22:32 +00:00
|
|
|
// Create synthetic document
|
2008-09-24 04:37:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
CreateSyntheticDocument();
|
2005-05-04 20:22:32 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
|
|
|
|
|
2018-10-01 21:38:01 +00:00
|
|
|
target = mImageContent;
|
2013-11-19 22:24:59 +00:00
|
|
|
target->AddEventListener(NS_LITERAL_STRING("load"), this, false);
|
2011-10-17 14:59:28 +00:00
|
|
|
target->AddEventListener(NS_LITERAL_STRING("click"), this, false);
|
2003-03-20 02:03:13 +00:00
|
|
|
}
|
|
|
|
|
2006-03-07 01:44:44 +00:00
|
|
|
target = do_QueryInterface(aScriptGlobalObject);
|
2011-10-17 14:59:28 +00:00
|
|
|
target->AddEventListener(NS_LITERAL_STRING("resize"), this, false);
|
|
|
|
target->AddEventListener(NS_LITERAL_STRING("keypress"), this, false);
|
2012-01-08 02:43:48 +00:00
|
|
|
|
2018-05-23 15:06:34 +00:00
|
|
|
if (!InitialSetupHasBeenDone()) {
|
2017-06-08 09:52:46 +00:00
|
|
|
LinkStylesheet(
|
|
|
|
NS_LITERAL_STRING("resource://content-accessible/ImageDocument.css"));
|
2013-04-21 22:29:14 +00:00
|
|
|
if (!nsContentUtils::IsChildOfSameType(this)) {
|
2017-06-08 09:52:46 +00:00
|
|
|
LinkStylesheet(NS_LITERAL_STRING(
|
|
|
|
"resource://content-accessible/TopLevelImageDocument.css"));
|
2013-04-21 22:29:14 +00:00
|
|
|
LinkStylesheet(NS_LITERAL_STRING(
|
|
|
|
"chrome://global/skin/media/TopLevelImageDocument.css"));
|
|
|
|
}
|
2018-05-23 15:06:34 +00:00
|
|
|
InitialSetupDone();
|
2012-01-08 02:43:48 +00:00
|
|
|
}
|
2002-12-13 23:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
void ImageDocument::OnPageShow(bool aPersisted,
|
2018-08-16 03:06:49 +00:00
|
|
|
EventTarget* aDispatchStartTarget,
|
|
|
|
bool aOnlySystemGroup) {
|
2009-09-01 13:15:33 +00:00
|
|
|
if (aPersisted) {
|
2017-06-19 02:33:23 +00:00
|
|
|
mOriginalZoomLevel = IsSiteSpecific() ? 1.0 : GetZoomLevel();
|
2018-02-20 22:07:21 +00:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
mOriginalResolution = GetResolution();
|
|
|
|
#endif
|
2009-09-01 13:15:33 +00:00
|
|
|
}
|
2016-04-13 05:42:15 +00:00
|
|
|
RefPtr<ImageDocument> kungFuDeathGrip(this);
|
|
|
|
UpdateSizeFromLayout();
|
|
|
|
|
2018-08-16 03:06:49 +00:00
|
|
|
MediaDocument::OnPageShow(aPersisted, aDispatchStartTarget, aOnlySystemGroup);
|
2009-09-01 13:15:33 +00:00
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
ImageDocument::GetImageIsOverflowing(bool* aImageIsOverflowing) {
|
2013-05-06 13:42:00 +00:00
|
|
|
*aImageIsOverflowing = ImageIsOverflowing();
|
2003-01-18 10:17:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
ImageDocument::GetImageIsResized(bool* aImageIsResized) {
|
2013-05-06 13:42:00 +00:00
|
|
|
*aImageIsResized = ImageIsResized();
|
2003-01-18 10:17:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:42:00 +00:00
|
|
|
already_AddRefed<imgIRequest> ImageDocument::GetImageRequest(ErrorResult& aRv) {
|
2005-08-25 20:05:24 +00:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(mImageContent);
|
2013-05-06 13:42:00 +00:00
|
|
|
nsCOMPtr<imgIRequest> imageRequest;
|
2005-08-25 20:05:24 +00:00
|
|
|
if (imageLoader) {
|
2013-05-06 13:42:00 +00:00
|
|
|
aRv = imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(imageRequest));
|
2005-08-25 20:05:24 +00:00
|
|
|
}
|
2013-05-06 13:42:00 +00:00
|
|
|
return imageRequest.forget();
|
2005-08-25 20:05:24 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_IMETHODIMP
|
2013-05-06 13:42:00 +00:00
|
|
|
ImageDocument::GetImageRequest(imgIRequest** aImageRequest) {
|
|
|
|
ErrorResult rv;
|
2014-03-15 19:00:15 +00:00
|
|
|
*aImageRequest = GetImageRequest(rv).take();
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
2013-05-06 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::ShrinkToFit() {
|
2010-10-15 04:03:33 +00:00
|
|
|
if (!mImageContent) {
|
2013-05-06 13:42:00 +00:00
|
|
|
return;
|
2010-10-15 04:03:33 +00:00
|
|
|
}
|
2009-10-15 02:20:50 +00:00
|
|
|
if (GetZoomLevel() != mOriginalZoomLevel && mImageIsResized &&
|
|
|
|
!nsContentUtils::IsChildOfSameType(this)) {
|
2016-03-04 23:02:08 +00:00
|
|
|
// If we're zoomed, so that we don't maintain the invariant that
|
|
|
|
// mImageIsResized if and only if its displayed width/height fit in
|
|
|
|
// mVisibleWidth/mVisibleHeight, then we may need to switch to/from the
|
|
|
|
// overflowingVertical class here, because our viewport size may have
|
|
|
|
// changed and we don't plan to adjust the image size to compensate. Since
|
|
|
|
// mImageIsResized it has a "height" attribute set, and we can just get the
|
|
|
|
// displayed image height by getting .height on the HTMLImageElement.
|
2017-07-14 03:46:59 +00:00
|
|
|
//
|
|
|
|
// Hold strong ref, because Height() can run script.
|
2018-03-21 21:39:04 +00:00
|
|
|
RefPtr<HTMLImageElement> img = HTMLImageElement::FromNode(mImageContent);
|
2016-03-04 23:02:08 +00:00
|
|
|
uint32_t imageHeight = img->Height();
|
|
|
|
nsDOMTokenList* classList = img->ClassList();
|
|
|
|
ErrorResult ignored;
|
|
|
|
if (imageHeight > mVisibleHeight) {
|
|
|
|
classList->Add(NS_LITERAL_STRING("overflowingVertical"), ignored);
|
|
|
|
} else {
|
|
|
|
classList->Remove(NS_LITERAL_STRING("overflowingVertical"), ignored);
|
|
|
|
}
|
|
|
|
ignored.SuppressException();
|
2013-05-06 13:42:00 +00:00
|
|
|
return;
|
2008-02-07 19:58:05 +00:00
|
|
|
}
|
2018-02-20 22:07:21 +00:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
if (GetResolution() != mOriginalResolution && mImageIsResized) {
|
|
|
|
// Don't resize if resolution has changed, e.g., through pinch-zooming on
|
|
|
|
// Android.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-14 15:12:24 +00:00
|
|
|
|
2016-02-27 18:53:28 +00:00
|
|
|
// Keep image content alive while changing the attributes.
|
2018-03-21 21:39:04 +00:00
|
|
|
RefPtr<HTMLImageElement> image = HTMLImageElement::FromNode(mImageContent);
|
2018-02-14 15:12:24 +00:00
|
|
|
|
2018-02-20 22:07:21 +00:00
|
|
|
uint32_t newWidth = std::max(1, NSToCoordFloor(GetRatio() * mImageWidth));
|
|
|
|
uint32_t newHeight = std::max(1, NSToCoordFloor(GetRatio() * mImageHeight));
|
2018-02-14 15:12:24 +00:00
|
|
|
image->SetWidth(newWidth, IgnoreErrors());
|
|
|
|
image->SetHeight(newHeight, IgnoreErrors());
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2013-05-01 09:12:41 +00: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.
|
2013-05-06 13:42:00 +00:00
|
|
|
ScrollImageTo(0, 0, false);
|
2008-04-22 04:41:38 +00:00
|
|
|
|
2013-08-08 22:04:58 +00:00
|
|
|
if (!mImageContent) {
|
|
|
|
// ScrollImageTo flush destroyed our content.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-15 20:45:40 +00:00
|
|
|
SetModeClass(eShrinkToFit);
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mImageIsResized = true;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2006-03-07 01:44:44 +00:00
|
|
|
UpdateTitleAndCharset();
|
2013-05-06 13:42:00 +00:00
|
|
|
}
|
2003-04-21 22:45:28 +00:00
|
|
|
|
2013-05-06 13:42:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageDocument::DOMShrinkToFit() {
|
|
|
|
ShrinkToFit();
|
2003-01-18 10:17:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-09-09 22:40:40 +00:00
|
|
|
NS_IMETHODIMP
|
2013-05-06 13:42:00 +00:00
|
|
|
ImageDocument::DOMRestoreImageTo(int32_t aX, int32_t aY) {
|
|
|
|
RestoreImageTo(aX, aY);
|
|
|
|
return NS_OK;
|
2013-04-18 15:27:37 +00:00
|
|
|
}
|
2004-09-09 22:40:40 +00:00
|
|
|
|
2013-05-01 09:12:41 +00:00
|
|
|
void ImageDocument::ScrollImageTo(int32_t aX, int32_t aY, bool restoreImage) {
|
|
|
|
if (restoreImage) {
|
|
|
|
RestoreImage();
|
2017-01-05 07:31:56 +00:00
|
|
|
FlushPendingNotifications(FlushType::Layout);
|
2013-05-01 09:12:41 +00:00
|
|
|
}
|
2004-09-09 22:40:40 +00:00
|
|
|
|
2019-03-29 15:12:47 +00:00
|
|
|
RefPtr<PresShell> presShell = GetPresShell();
|
|
|
|
if (!presShell) {
|
2013-05-06 13:42:00 +00:00
|
|
|
return;
|
2014-07-29 22:23:22 +00:00
|
|
|
}
|
2004-09-09 22:40:40 +00:00
|
|
|
|
2019-03-29 15:12:47 +00:00
|
|
|
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
|
2014-07-29 22:23:22 +00:00
|
|
|
if (!sf) {
|
2013-05-06 13:42:00 +00:00
|
|
|
return;
|
2014-07-29 22:23:22 +00:00
|
|
|
}
|
2004-09-09 22:40:40 +00:00
|
|
|
|
2017-11-07 09:51:13 +00:00
|
|
|
float ratio = GetRatio();
|
|
|
|
// Don't try to scroll image if the document is not visible (mVisibleWidth or
|
|
|
|
// mVisibleHeight is zero).
|
|
|
|
if (ratio <= 0.0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-01 09:12:41 +00:00
|
|
|
nsRect portRect = sf->GetScrollPortRect();
|
2018-02-19 20:15:23 +00:00
|
|
|
sf->ScrollTo(
|
|
|
|
nsPoint(
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(aX / ratio) - portRect.width / 2,
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(aY / ratio) - portRect.height / 2),
|
2019-04-25 23:03:04 +00:00
|
|
|
ScrollMode::Instant);
|
2004-09-09 22:40:40 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::RestoreImage() {
|
2010-10-15 04:03:33 +00:00
|
|
|
if (!mImageContent) {
|
2013-05-06 13:42:00 +00:00
|
|
|
return;
|
2010-10-15 04:03:33 +00:00
|
|
|
}
|
2007-08-05 13:24:30 +00:00
|
|
|
// Keep image content alive while changing the attributes.
|
2016-03-03 17:55:54 +00:00
|
|
|
nsCOMPtr<Element> imageContent = mImageContent;
|
2011-10-17 14:59:28 +00:00
|
|
|
imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::width, true);
|
|
|
|
imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::height, true);
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2016-02-06 04:31:09 +00:00
|
|
|
if (ImageIsOverflowing()) {
|
2016-02-06 04:31:19 +00:00
|
|
|
if (!mImageIsOverflowingVertically) {
|
|
|
|
SetModeClass(eOverflowingHorizontalOnly);
|
|
|
|
} else {
|
|
|
|
SetModeClass(eOverflowingVertical);
|
|
|
|
}
|
2006-03-07 01:44:44 +00:00
|
|
|
} else {
|
2013-03-15 20:45:40 +00:00
|
|
|
SetModeClass(eNone);
|
2006-03-07 01:44:44 +00:00
|
|
|
}
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2013-04-18 15:27:37 +00:00
|
|
|
mImageIsResized = false;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2006-03-07 01:44:44 +00:00
|
|
|
UpdateTitleAndCharset();
|
2013-05-06 13:42:00 +00:00
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2013-05-06 13:42:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageDocument::DOMRestoreImage() {
|
|
|
|
RestoreImage();
|
2003-01-18 10:17:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::ToggleImageSize() {
|
2011-10-17 14:59:28 +00:00
|
|
|
mShouldResize = true;
|
2006-03-07 01:44:44 +00:00
|
|
|
if (mImageIsResized) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mShouldResize = false;
|
2009-09-01 13:15:33 +00:00
|
|
|
ResetZoomLevel();
|
2006-03-07 01:44:44 +00:00
|
|
|
RestoreImage();
|
2016-02-06 04:31:09 +00:00
|
|
|
} else if (ImageIsOverflowing()) {
|
2009-09-01 13:15:33 +00:00
|
|
|
ResetZoomLevel();
|
2006-03-07 01:44:44 +00:00
|
|
|
ShrinkToFit();
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
2013-05-06 13:42:00 +00:00
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2013-05-06 13:42:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageDocument::DOMToggleImageSize() {
|
|
|
|
ToggleImageSize();
|
2003-01-18 10:17:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:58:18 +00:00
|
|
|
void ImageDocument::NotifyPossibleTitleChange(bool aBoundTitleElement) {
|
|
|
|
if (!mHasCustomTitle && !mTitleUpdateInProgress) {
|
|
|
|
mHasCustomTitle = true;
|
|
|
|
}
|
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
Document::NotifyPossibleTitleChange(aBoundTitleElement);
|
2018-11-07 12:58:18 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_IMETHODIMP
|
2012-10-12 16:11:22 +00:00
|
|
|
ImageDocument::Notify(imgIRequest* aRequest, int32_t aType,
|
|
|
|
const nsIntRect* aData) {
|
2012-10-12 16:11:23 +00:00
|
|
|
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
|
2012-10-12 16:11:22 +00:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
aRequest->GetImage(getter_AddRefs(image));
|
2014-11-17 22:29:56 +00:00
|
|
|
return OnSizeAvailable(aRequest, image);
|
2012-10-12 16:11:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 19:16:45 +00:00
|
|
|
// Run this using a script runner because HAS_TRANSPARENCY notifications can
|
|
|
|
// come during painting and this will trigger invalidation.
|
|
|
|
if (aType == imgINotificationObserver::HAS_TRANSPARENCY) {
|
2014-08-10 18:01:40 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2017-06-12 19:34:10 +00:00
|
|
|
NewRunnableMethod("dom::ImageDocument::OnHasTransparency", this,
|
|
|
|
&ImageDocument::OnHasTransparency);
|
2014-08-10 18:01:40 +00:00
|
|
|
nsContentUtils::AddScriptRunner(runnable);
|
2012-10-12 16:11:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 16:11:23 +00:00
|
|
|
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
|
2012-10-12 16:11:22 +00:00
|
|
|
uint32_t reqStatus;
|
|
|
|
aRequest->GetImageStatus(&reqStatus);
|
|
|
|
nsresult status =
|
|
|
|
reqStatus & imgIRequest::STATUS_ERROR ? NS_ERROR_FAILURE : NS_OK;
|
2014-11-17 22:29:56 +00:00
|
|
|
return OnLoadComplete(aRequest, status);
|
2012-10-12 16:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-17 19:16:45 +00:00
|
|
|
void ImageDocument::OnHasTransparency() {
|
2014-08-10 18:01:40 +00:00
|
|
|
if (!mImageContent || nsContentUtils::IsChildOfSameType(this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-03 17:55:54 +00:00
|
|
|
nsDOMTokenList* classList = mImageContent->ClassList();
|
2014-08-10 18:01:40 +00:00
|
|
|
mozilla::ErrorResult rv;
|
2014-11-17 19:16:45 +00:00
|
|
|
classList->Add(NS_LITERAL_STRING("transparent"), rv);
|
2014-08-10 18:01:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 20:45:40 +00:00
|
|
|
void ImageDocument::SetModeClass(eModeClasses mode) {
|
2016-03-03 17:55:54 +00:00
|
|
|
nsDOMTokenList* classList = mImageContent->ClassList();
|
2016-03-04 23:02:08 +00:00
|
|
|
ErrorResult rv;
|
2013-03-15 20:45:40 +00:00
|
|
|
|
|
|
|
if (mode == eShrinkToFit) {
|
|
|
|
classList->Add(NS_LITERAL_STRING("shrinkToFit"), rv);
|
|
|
|
} else {
|
|
|
|
classList->Remove(NS_LITERAL_STRING("shrinkToFit"), rv);
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:31:19 +00:00
|
|
|
if (mode == eOverflowingVertical) {
|
|
|
|
classList->Add(NS_LITERAL_STRING("overflowingVertical"), rv);
|
2013-03-15 20:45:40 +00:00
|
|
|
} else {
|
2016-02-06 04:31:19 +00:00
|
|
|
classList->Remove(NS_LITERAL_STRING("overflowingVertical"), rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == eOverflowingHorizontalOnly) {
|
|
|
|
classList->Add(NS_LITERAL_STRING("overflowingHorizontalOnly"), rv);
|
|
|
|
} else {
|
|
|
|
classList->Remove(NS_LITERAL_STRING("overflowingHorizontalOnly"), rv);
|
2013-03-15 20:45:40 +00:00
|
|
|
}
|
2016-03-04 23:02:08 +00:00
|
|
|
|
|
|
|
rv.SuppressException();
|
2013-03-15 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
nsresult ImageDocument::OnSizeAvailable(imgIRequest* aRequest,
|
|
|
|
imgIContainer* aImage) {
|
2017-02-13 22:46:47 +00:00
|
|
|
int32_t oldWidth = mImageWidth;
|
|
|
|
int32_t oldHeight = mImageHeight;
|
|
|
|
|
2013-11-19 22:24:59 +00:00
|
|
|
// Styles have not yet been applied, so we don't know the final size. For now,
|
|
|
|
// default to the image's intrinsic size.
|
2003-01-18 10:17:58 +00:00
|
|
|
aImage->GetWidth(&mImageWidth);
|
|
|
|
aImage->GetHeight(&mImageHeight);
|
2013-11-19 22:24:59 +00:00
|
|
|
|
2017-02-13 22:46:47 +00:00
|
|
|
// Multipart images send size available for each part; ignore them if it
|
|
|
|
// doesn't change our size. (We may not even support changing size in
|
|
|
|
// multipart images in the future.)
|
|
|
|
if (oldWidth == mImageWidth && oldHeight == mImageHeight) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-20 04:39:11 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2017-06-12 19:34:10 +00:00
|
|
|
NewRunnableMethod("dom::ImageDocument::DefaultCheckOverflowing", this,
|
|
|
|
&ImageDocument::DefaultCheckOverflowing);
|
2009-11-20 04:39:11 +00:00
|
|
|
nsContentUtils::AddScriptRunner(runnable);
|
2004-02-08 20:58:38 +00:00
|
|
|
UpdateTitleAndCharset();
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
nsresult ImageDocument::OnLoadComplete(imgIRequest* aRequest,
|
|
|
|
nsresult aStatus) {
|
2010-09-12 15:22:26 +00:00
|
|
|
UpdateTitleAndCharset();
|
|
|
|
|
|
|
|
// mImageContent can be null if the document is already destroyed
|
2019-11-04 16:56:27 +00:00
|
|
|
if (NS_FAILED(aStatus) && mImageContent) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString src;
|
2010-09-12 15:22:26 +00:00
|
|
|
mDocumentURI->GetSpec(src);
|
2019-06-11 15:51:51 +00:00
|
|
|
AutoTArray<nsString, 1> formatString;
|
|
|
|
CopyUTF8toUTF16(src, *formatString.AppendElement());
|
2017-08-04 04:40:52 +00:00
|
|
|
nsAutoString errorMsg;
|
2019-11-04 16:56:27 +00:00
|
|
|
FormatStringFromName("InvalidImage", formatString, errorMsg);
|
2010-09-12 15:22:26 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, errorMsg, false);
|
2010-09-12 15:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_IMETHODIMP
|
2018-04-20 04:49:29 +00:00
|
|
|
ImageDocument::HandleEvent(Event* aEvent) {
|
2003-01-18 10:17:58 +00:00
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
2004-05-22 22:15:22 +00:00
|
|
|
if (eventType.EqualsLiteral("resize")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
CheckOverflowing(false);
|
2013-05-01 09:12:41 +00:00
|
|
|
} else if (eventType.EqualsLiteral("click") && mClickResizingEnabled) {
|
2009-09-01 13:15:33 +00:00
|
|
|
ResetZoomLevel();
|
2011-10-17 14:59:28 +00:00
|
|
|
mShouldResize = true;
|
2013-05-01 09:12:41 +00:00
|
|
|
if (mImageIsResized) {
|
|
|
|
int32_t x = 0, y = 0;
|
2018-04-20 04:49:30 +00:00
|
|
|
MouseEvent* event = aEvent->AsMouseEvent();
|
2013-05-01 09:12:41 +00:00
|
|
|
if (event) {
|
2017-07-14 03:45:50 +00:00
|
|
|
RefPtr<HTMLImageElement> img =
|
2018-03-21 21:39:04 +00:00
|
|
|
HTMLImageElement::FromNode(mImageContent);
|
2018-03-20 04:16:05 +00:00
|
|
|
x = event->ClientX() - img->OffsetLeft();
|
|
|
|
y = event->ClientY() - img->OffsetTop();
|
2013-04-18 15:27:37 +00:00
|
|
|
}
|
|
|
|
mShouldResize = false;
|
2013-05-01 09:12:41 +00:00
|
|
|
RestoreImageTo(x, y);
|
2016-02-06 04:31:09 +00:00
|
|
|
} else if (ImageIsOverflowing()) {
|
2013-05-01 09:12:41 +00:00
|
|
|
ShrinkToFit();
|
2004-09-09 22:40:40 +00:00
|
|
|
}
|
2013-11-19 22:24:59 +00:00
|
|
|
} else if (eventType.EqualsLiteral("load")) {
|
|
|
|
UpdateSizeFromLayout();
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:24:59 +00:00
|
|
|
void ImageDocument::UpdateSizeFromLayout() {
|
|
|
|
// Pull an updated size from the content frame to account for any size
|
|
|
|
// change due to CSS properties like |image-orientation|.
|
2016-03-03 17:55:54 +00:00
|
|
|
if (!mImageContent) {
|
2013-11-19 22:24:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-14 03:46:59 +00:00
|
|
|
// Need strong ref, because GetPrimaryFrame can run script.
|
|
|
|
nsCOMPtr<Element> imageContent = mImageContent;
|
|
|
|
nsIFrame* contentFrame = imageContent->GetPrimaryFrame(FlushType::Frames);
|
2013-11-19 22:24:59 +00:00
|
|
|
if (!contentFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntSize oldSize(mImageWidth, mImageHeight);
|
|
|
|
IntrinsicSize newSize = contentFrame->GetIntrinsicSize();
|
|
|
|
|
2019-04-25 23:48:14 +00:00
|
|
|
if (newSize.width) {
|
|
|
|
mImageWidth = nsPresContext::AppUnitsToFloatCSSPixels(*newSize.width);
|
2013-11-19 22:24:59 +00:00
|
|
|
}
|
2019-04-25 23:48:14 +00:00
|
|
|
if (newSize.height) {
|
|
|
|
mImageHeight = nsPresContext::AppUnitsToFloatCSSPixels(*newSize.height);
|
2013-11-19 22:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that our information about overflow is up-to-date if needed.
|
|
|
|
if (mImageWidth != oldSize.width || mImageHeight != oldSize.height) {
|
|
|
|
CheckOverflowing(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
nsresult ImageDocument::CreateSyntheticDocument() {
|
1998-07-27 17:50:58 +00:00
|
|
|
// Synthesize an html document that refers to the image
|
2011-05-26 08:06:31 +00:00
|
|
|
nsresult rv = MediaDocument::CreateSyntheticDocument();
|
2000-05-10 13:13:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-07 04:04:04 +00:00
|
|
|
// Add the image element
|
2019-08-06 13:43:01 +00:00
|
|
|
RefPtr<Element> body = GetBodyElement();
|
2011-08-11 04:23:00 +00:00
|
|
|
if (!body) {
|
|
|
|
NS_WARNING("no body on image document!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
|
2012-07-30 14:20:58 +00:00
|
|
|
nodeInfo = mNodeInfoManager->GetNodeInfo(
|
2018-01-30 04:10:53 +00:00
|
|
|
nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, nsINode::ELEMENT_NODE);
|
2000-05-10 13:13:39 +00:00
|
|
|
|
2010-07-23 09:49:57 +00:00
|
|
|
mImageContent = NS_NewHTMLImageElement(nodeInfo.forget());
|
2004-09-09 22:40:40 +00:00
|
|
|
if (!mImageContent) {
|
2004-06-06 02:38:32 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
2004-09-09 22:40:40 +00:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(mImageContent);
|
2003-04-02 03:27:20 +00:00
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString src;
|
2004-01-09 23:54:21 +00:00
|
|
|
mDocumentURI->GetSpec(src);
|
2000-04-16 11:19:26 +00:00
|
|
|
|
2006-02-03 14:18:39 +00:00
|
|
|
NS_ConvertUTF8toUTF16 srcString(src);
|
2003-04-02 03:27:20 +00:00
|
|
|
// Make sure not to start the image load from here...
|
2011-10-17 14:59:28 +00:00
|
|
|
imageLoader->SetLoadingEnabled(false);
|
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::src, srcString, false);
|
|
|
|
mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, srcString, false);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
body->AppendChildTo(mImageContent, false);
|
|
|
|
imageLoader->SetLoadingEnabled(true);
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
nsresult ImageDocument::CheckOverflowing(bool changeState) {
|
2018-03-23 13:49:21 +00:00
|
|
|
/* Create a scope so that the ComputedStyle gets destroyed before we might
|
2008-01-09 09:38:28 +00:00
|
|
|
* call RebuildStyleData. Also, holding onto pointers to the
|
2012-03-08 06:11:45 +00:00
|
|
|
* presentation through style resolution is potentially dangerous.
|
2007-07-26 20:20:30 +00:00
|
|
|
*/
|
|
|
|
{
|
2018-02-20 22:00:10 +00:00
|
|
|
nsPresContext* context = GetPresContext();
|
|
|
|
if (!context) {
|
2007-07-26 20:20:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2007-07-26 20:20:30 +00:00
|
|
|
nsRect visibleArea = context->GetVisibleArea();
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2018-02-19 20:15:23 +00:00
|
|
|
mVisibleWidth = nsPresContext::AppUnitsToFloatCSSPixels(visibleArea.width);
|
|
|
|
mVisibleHeight =
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(visibleArea.height);
|
2007-07-26 20:20:30 +00:00
|
|
|
}
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2016-02-06 04:31:09 +00:00
|
|
|
bool imageWasOverflowing = ImageIsOverflowing();
|
2016-02-06 04:31:19 +00:00
|
|
|
bool imageWasOverflowingVertically = mImageIsOverflowingVertically;
|
2016-02-06 04:31:09 +00:00
|
|
|
mImageIsOverflowingHorizontally = mImageWidth > mVisibleWidth;
|
|
|
|
mImageIsOverflowingVertically = mImageHeight > mVisibleHeight;
|
|
|
|
bool windowBecameBigEnough = imageWasOverflowing && !ImageIsOverflowing();
|
2016-02-06 04:31:19 +00:00
|
|
|
bool verticalOverflowChanged =
|
|
|
|
mImageIsOverflowingVertically != imageWasOverflowingVertically;
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2006-12-14 02:06:49 +00:00
|
|
|
if (changeState || mShouldResize || mFirstResize || windowBecameBigEnough ||
|
2016-02-06 04:31:19 +00:00
|
|
|
verticalOverflowChanged) {
|
2016-02-06 04:31:09 +00:00
|
|
|
if (ImageIsOverflowing() && (changeState || mShouldResize)) {
|
2005-04-05 18:59:22 +00:00
|
|
|
ShrinkToFit();
|
2006-12-14 02:06:49 +00:00
|
|
|
} else if (mImageIsResized || mFirstResize || windowBecameBigEnough) {
|
2005-04-05 18:59:22 +00:00
|
|
|
RestoreImage();
|
2016-02-06 04:31:19 +00:00
|
|
|
} else if (!mImageIsResized && verticalOverflowChanged) {
|
|
|
|
if (mImageIsOverflowingVertically) {
|
|
|
|
SetModeClass(eOverflowingVertical);
|
|
|
|
} else {
|
|
|
|
SetModeClass(eOverflowingHorizontalOnly);
|
|
|
|
}
|
2005-04-05 18:59:22 +00:00
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mFirstResize = false;
|
1998-07-27 17:50:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::UpdateTitleAndCharset() {
|
2018-11-07 12:58:18 +00:00
|
|
|
if (mHasCustomTitle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRestore<bool> restore(mTitleUpdateInProgress);
|
|
|
|
mTitleUpdateInProgress = true;
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString typeStr;
|
2003-04-13 00:40:26 +00:00
|
|
|
nsCOMPtr<imgIRequest> imageRequest;
|
2004-09-09 22:40:40 +00:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(mImageContent);
|
2003-04-13 00:40:26 +00:00
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(imageRequest));
|
|
|
|
}
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
if (imageRequest) {
|
2017-08-16 03:58:35 +00:00
|
|
|
nsCString mimeType;
|
2003-04-13 00:40:26 +00:00
|
|
|
imageRequest->GetMimeType(getter_Copies(mimeType));
|
|
|
|
ToUpperCase(mimeType);
|
2017-08-16 03:58:35 +00:00
|
|
|
nsCString::const_iterator start, end;
|
2003-04-13 00:40:26 +00:00
|
|
|
mimeType.BeginReading(start);
|
|
|
|
mimeType.EndReading(end);
|
2017-08-16 03:58:35 +00:00
|
|
|
nsCString::const_iterator iter = end;
|
2017-07-06 12:00:35 +00:00
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("IMAGE/"), start, iter) &&
|
2003-04-13 00:40:26 +00:00
|
|
|
iter != end) {
|
|
|
|
// strip out "X-" if any
|
|
|
|
if (*iter == 'X') {
|
|
|
|
++iter;
|
|
|
|
if (iter != end && *iter == '-') {
|
2002-02-06 21:05:52 +00:00
|
|
|
++iter;
|
2003-04-13 00:40:26 +00:00
|
|
|
if (iter == end) {
|
|
|
|
// looks like "IMAGE/X-" is the type?? Bail out of here.
|
|
|
|
mimeType.BeginReading(iter);
|
2002-02-06 21:05:52 +00:00
|
|
|
}
|
2003-04-13 00:40:26 +00:00
|
|
|
} else {
|
|
|
|
--iter;
|
2002-02-06 21:05:52 +00:00
|
|
|
}
|
2002-01-15 23:34:32 +00:00
|
|
|
}
|
2003-04-13 00:40:26 +00:00
|
|
|
typeStr = Substring(iter, end);
|
2002-01-15 23:34:32 +00:00
|
|
|
} else {
|
2003-04-13 00:40:26 +00:00
|
|
|
typeStr = mimeType;
|
2001-02-06 23:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-21 22:45:28 +00:00
|
|
|
|
2017-08-04 04:40:52 +00:00
|
|
|
nsAutoString status;
|
2013-05-01 09:12:41 +00:00
|
|
|
if (mImageIsResized) {
|
2019-06-11 15:51:51 +00:00
|
|
|
AutoTArray<nsString, 1> formatString;
|
|
|
|
formatString.AppendElement()->AppendInt(NSToCoordFloor(GetRatio() * 100));
|
2003-04-21 22:45:28 +00:00
|
|
|
|
2019-11-04 16:56:27 +00:00
|
|
|
FormatStringFromName("ScaledImage", formatString, status);
|
2003-04-21 22:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 12:00:35 +00:00
|
|
|
static const char* const formatNames[4] = {
|
2003-04-13 00:40:26 +00:00
|
|
|
"ImageTitleWithNeitherDimensionsNorFile",
|
|
|
|
"ImageTitleWithoutDimensions",
|
2013-09-04 16:52:25 +00:00
|
|
|
"ImageTitleWithDimensions2",
|
|
|
|
"ImageTitleWithDimensions2AndFile",
|
2003-04-13 00:40:26 +00:00
|
|
|
};
|
|
|
|
|
2014-10-30 05:08:00 +00:00
|
|
|
MediaDocument::UpdateTitleAndCharset(typeStr, mChannel, formatNames,
|
2011-05-26 08:06:31 +00:00
|
|
|
mImageWidth, mImageHeight, status);
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
void ImageDocument::ResetZoomLevel() {
|
2013-11-15 07:12:43 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(mDocumentContainer);
|
2008-02-07 19:58:05 +00:00
|
|
|
if (docShell) {
|
2009-10-15 02:20:50 +00:00
|
|
|
if (nsContentUtils::IsChildOfSameType(this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-07 19:58:05 +00:00
|
|
|
nsCOMPtr<nsIContentViewer> cv;
|
|
|
|
docShell->GetContentViewer(getter_AddRefs(cv));
|
2014-07-09 21:27:49 +00:00
|
|
|
if (cv) {
|
|
|
|
cv->SetFullZoom(mOriginalZoomLevel);
|
2008-02-07 19:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
float ImageDocument::GetZoomLevel() {
|
2009-09-01 13:15:33 +00:00
|
|
|
float zoomLevel = mOriginalZoomLevel;
|
2013-11-15 07:12:43 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(mDocumentContainer);
|
2008-02-07 19:58:05 +00:00
|
|
|
if (docShell) {
|
|
|
|
nsCOMPtr<nsIContentViewer> cv;
|
|
|
|
docShell->GetContentViewer(getter_AddRefs(cv));
|
2014-07-09 21:27:49 +00:00
|
|
|
if (cv) {
|
|
|
|
cv->GetFullZoom(&zoomLevel);
|
2008-02-07 19:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return zoomLevel;
|
|
|
|
}
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2018-02-20 22:07:21 +00:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
float ImageDocument::GetResolution() {
|
|
|
|
float resolution = mOriginalResolution;
|
2019-03-29 15:12:47 +00:00
|
|
|
RefPtr<PresShell> presShell = GetPresShell();
|
|
|
|
if (presShell) {
|
|
|
|
resolution = presShell->GetResolution();
|
2018-02-20 22:07:21 +00:00
|
|
|
}
|
|
|
|
return resolution;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
nsresult NS_NewImageDocument(mozilla::dom::Document** aResult) {
|
|
|
|
auto* doc = new mozilla::dom::ImageDocument();
|
2005-04-28 17:16:19 +00:00
|
|
|
NS_ADDREF(doc);
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2011-05-26 08:06:31 +00:00
|
|
|
nsresult rv = doc->Init();
|
2003-01-18 10:17:58 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-04-28 17:16:19 +00:00
|
|
|
NS_RELEASE(doc);
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 17:16:19 +00:00
|
|
|
*aResult = doc;
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2005-04-28 17:16:19 +00:00
|
|
|
return rv;
|
1999-07-07 07:31:24 +00:00
|
|
|
}
|