IconController.cpp [plain text]
#include "config.h"
#include "IconController.h"
#include "Document.h"
#include "DocumentLoader.h"
#include "ElementChildIterator.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "HTMLHeadElement.h"
#include "HTMLLinkElement.h"
#include "IconDatabase.h"
#include "IconLoader.h"
#include "Logging.h"
#include "MainFrame.h"
#include "Page.h"
#include "Settings.h"
#include <wtf/text/CString.h>
namespace WebCore {
IconController::IconController(Frame& frame)
: m_frame(frame)
{
}
IconController::~IconController()
{
}
static URL iconFromLinkElements(Frame& frame)
{
URL result;
auto* document = frame.document();
if (!document)
return result;
auto* head = document->head();
if (!head)
return result;
for (auto& linkElement : childrenOfType<HTMLLinkElement>(*head)) {
if (!(linkElement.iconType() & Favicon))
continue;
if (linkElement.href().isEmpty())
continue;
result = linkElement.href();
if (!linkElement.type().isEmpty())
break;
}
return result;
}
URL IconController::url()
{
if (!m_frame.isMainFrame())
return URL();
auto icon = iconFromLinkElements(m_frame);
if (!icon.isEmpty())
return icon;
icon = m_frame.document()->completeURL(ASCIILiteral("/favicon.ico"));
if (icon.protocolIsInHTTPFamily()) {
icon.setUser(String());
icon.setPass(String());
return icon;
}
return URL();
}
void IconController::commitToDatabase(const URL& icon)
{
LOG(IconDatabase, "Committing iconURL %s to database for pageURLs %s and %s", icon.string().ascii().data(), m_frame.document()->url().string().ascii().data(), m_frame.loader().initialRequest().url().string().ascii().data());
iconDatabase().setIconURLForPageURL(icon.string(), m_frame.document()->url().string());
iconDatabase().setIconURLForPageURL(icon.string(), m_frame.loader().initialRequest().url().string());
}
void IconController::startLoader()
{
if (!m_frame.isMainFrame())
return;
if (!iconDatabase().isEnabled())
return;
ASSERT(!m_frame.tree().parent());
if (!documentCanHaveIcon(m_frame.document()->url()))
return;
URL iconURL = url();
if (iconURL.isEmpty())
return;
if (!m_frame.settings().loadsImagesAutomatically() && !m_frame.settings().loadsSiteIconsIgnoringImageLoadingSetting())
return;
if (m_frame.loader().loadType() == FrameLoadType::Reload && m_frame.loader().loadType() == FrameLoadType::ReloadFromOrigin) {
continueLoadWithDecision(IconLoadYes);
return;
}
if (iconDatabase().supportsAsynchronousMode()) {
if (m_frame.page() && m_frame.page()->usesEphemeralSession())
return;
m_frame.loader().documentLoader()->getIconLoadDecisionForIconURL(iconURL.string());
commitToDatabase(iconURL);
return;
}
IconLoadDecision decision = iconDatabase().synchronousLoadDecisionForIconURL(iconURL.string(), m_frame.loader().documentLoader());
if (decision == IconLoadUnknown) {
LOG(IconDatabase, "IconController %p might load icon %s later", this, iconURL.string().utf8().data());
m_waitingForLoadDecision = true;
m_frame.loader().client().registerForIconNotification();
commitToDatabase(iconURL);
return;
}
continueLoadWithDecision(decision);
}
void IconController::stopLoader()
{
if (m_iconLoader)
m_iconLoader->stopLoading();
}
void IconController::loadDecisionReceived(IconLoadDecision iconLoadDecision)
{
if (!m_waitingForLoadDecision)
return;
LOG(IconDatabase, "IconController %p was told a load decision is available for its icon", this);
continueLoadWithDecision(iconLoadDecision);
m_waitingForLoadDecision = false;
}
void IconController::continueLoadWithDecision(IconLoadDecision iconLoadDecision)
{
ASSERT(iconLoadDecision != IconLoadUnknown);
if (iconLoadDecision == IconLoadNo) {
URL iconURL = url();
if (iconURL.isEmpty())
return;
LOG(IconDatabase, "IconController::startLoader() - Told not to load this icon, committing iconURL %s to database for pageURL mapping", iconURL.string().utf8().data());
commitToDatabase(iconURL);
if (iconDatabase().supportsAsynchronousMode()) {
m_frame.loader().documentLoader()->getIconDataForIconURL(iconURL.string());
return;
}
if (!iconDatabase().synchronousIconDataKnownForIconURL(iconURL.string())) {
LOG(IconDatabase, "Told not to load icon %s but icon data is not yet available - registering for notification and requesting load from disk", iconURL.string().ascii().data());
m_frame.loader().client().registerForIconNotification();
iconDatabase().synchronousIconForPageURL(m_frame.document()->url().string(), IntSize(0, 0));
iconDatabase().synchronousIconForPageURL(m_frame.loader().initialRequest().url().string(), IntSize(0, 0));
} else
m_frame.loader().client().dispatchDidReceiveIcon();
return;
}
if (!m_iconLoader)
m_iconLoader = std::make_unique<IconLoader>(m_frame);
m_iconLoader->startLoading();
}
}