2020-10-01 00:14:02 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/widget/IconLoader.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgLoader.h"
|
2021-02-12 03:32:01 +03:00
|
|
|
#include "imgRequestProxy.h"
|
2020-10-01 00:14:02 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIContent.h"
|
2021-02-12 03:32:06 +03:00
|
|
|
#include "nsIContentPolicy.h"
|
2020-10-01 00:14:02 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
namespace mozilla::widget {
|
|
|
|
|
2021-02-12 03:32:02 +03:00
|
|
|
NS_IMPL_ISUPPORTS(IconLoader, imgINotificationObserver)
|
2020-10-01 00:14:02 +03:00
|
|
|
|
2021-02-12 03:32:06 +03:00
|
|
|
IconLoader::IconLoader(Listener* aListener) : mListener(aListener) {}
|
2020-10-01 00:14:02 +03:00
|
|
|
|
2020-10-01 00:14:00 +03:00
|
|
|
IconLoader::~IconLoader() { Destroy(); }
|
|
|
|
|
|
|
|
void IconLoader::Destroy() {
|
2020-10-01 00:14:02 +03:00
|
|
|
if (mIconRequest) {
|
|
|
|
mIconRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
}
|
2021-02-12 03:32:04 +03:00
|
|
|
mListener = nullptr;
|
2020-10-01 00:14:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 03:32:01 +03:00
|
|
|
nsresult IconLoader::LoadIcon(nsIURI* aIconURI, nsINode* aNode,
|
|
|
|
bool aIsInternalIcon) {
|
2020-10-01 00:14:02 +03:00
|
|
|
if (mIconRequest) {
|
|
|
|
// Another icon request is already in flight. Kill it.
|
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:32:01 +03:00
|
|
|
if (!aNode) {
|
2020-10-01 00:14:02 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:32:01 +03:00
|
|
|
RefPtr<mozilla::dom::Document> document = aNode->OwnerDoc();
|
2020-10-01 00:14:02 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
|
|
|
|
if (!loadGroup) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<imgLoader> loader = nsContentUtils::GetImgLoaderForDocument(document);
|
|
|
|
if (!loader) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (aIsInternalIcon) {
|
|
|
|
rv = loader->LoadImage(
|
|
|
|
aIconURI, nullptr, nullptr, nullptr, 0, loadGroup, this, nullptr,
|
2021-02-12 03:32:06 +03:00
|
|
|
nullptr, nsIRequest::LOAD_NORMAL, nullptr,
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_IMAGE, u""_ns,
|
2020-10-01 00:14:02 +03:00
|
|
|
/* aUseUrgentStartForChannel */ false, /* aLinkPreload */ false,
|
|
|
|
getter_AddRefs(mIconRequest));
|
|
|
|
} else {
|
2021-02-12 03:32:06 +03:00
|
|
|
// TODO: nsIContentPolicy::TYPE_INTERNAL_IMAGE may not be the correct
|
|
|
|
// policy. See bug 1691868 for more details.
|
2020-10-01 00:14:02 +03:00
|
|
|
rv = loader->LoadImage(
|
2021-02-12 03:32:01 +03:00
|
|
|
aIconURI, nullptr, nullptr, aNode->NodePrincipal(), 0, loadGroup, this,
|
2021-02-12 03:32:06 +03:00
|
|
|
aNode, document, nsIRequest::LOAD_NORMAL, nullptr,
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_IMAGE, u""_ns,
|
2021-02-12 03:32:01 +03:00
|
|
|
/* aUseUrgentStartForChannel */ false,
|
2020-10-01 00:14:02 +03:00
|
|
|
/* aLinkPreload */ false, getter_AddRefs(mIconRequest));
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// imgINotificationObserver
|
|
|
|
//
|
|
|
|
|
|
|
|
void IconLoader::Notify(imgIRequest* aRequest, int32_t aType,
|
|
|
|
const nsIntRect* aData) {
|
|
|
|
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
|
|
|
|
// Make sure the image loaded successfully.
|
|
|
|
uint32_t status = imgIRequest::STATUS_ERROR;
|
|
|
|
if (NS_FAILED(aRequest->GetImageStatus(&status)) ||
|
|
|
|
(status & imgIRequest::STATUS_ERROR)) {
|
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
aRequest->GetImage(getter_AddRefs(image));
|
|
|
|
MOZ_ASSERT(image);
|
|
|
|
|
|
|
|
// Ask the image to decode at its intrinsic size.
|
|
|
|
int32_t width = 0, height = 0;
|
|
|
|
image->GetWidth(&width);
|
|
|
|
image->GetHeight(&height);
|
|
|
|
image->RequestDecodeForSize(nsIntSize(width, height),
|
|
|
|
imgIContainer::FLAG_HIGH_QUALITY_SCALING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType == imgINotificationObserver::FRAME_COMPLETE) {
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
aRequest->GetImage(getter_AddRefs(image));
|
|
|
|
MOZ_ASSERT(image);
|
|
|
|
|
2021-02-12 03:32:04 +03:00
|
|
|
if (mListener) {
|
2021-02-12 03:32:06 +03:00
|
|
|
mListener->OnComplete(image);
|
2021-02-12 03:32:01 +03:00
|
|
|
}
|
2020-10-01 00:14:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType == imgINotificationObserver::DECODE_COMPLETE) {
|
|
|
|
if (mIconRequest && mIconRequest == aRequest) {
|
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 11:53:08 +03:00
|
|
|
} // namespace mozilla::widget
|