Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Adjust the bindings-exposed screen size in headless browser mode #13998

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/WebCore/page/ChromeClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ class ChromeClient {

virtual bool isInStableState() const { return true; }

virtual FloatSize screenSizeForHeadlessMode(const LocalFrame&, FloatSize defaultSize) const { return defaultSize; }

WEBCORE_EXPORT virtual ~ChromeClient();

protected:
Expand Down
30 changes: 10 additions & 20 deletions Source/WebCore/page/LocalFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,29 +1052,19 @@ FloatSize LocalFrame::screenSize() const
if (!m_overrideScreenSize.isEmpty())
return m_overrideScreenSize;

auto sizeForHeadlessMode = [&]() -> std::optional<IntSize> {
RefPtr document = this->document();
if (!document)
return std::nullopt;

RefPtr loader = document->loader();
if (!loader || !loader->isLoadingInHeadlessMode())
return std::nullopt;
auto defaultSize = screenRect(view()).size();
RefPtr document = this->document();
if (!document)
return defaultSize;

RefPtr window = this->window();
if (!window)
return std::nullopt;
RefPtr loader = document->loader();
if (!loader || !loader->isLoadingInHeadlessMode())
return defaultSize;

IntSize size { window->innerWidth(), window->innerHeight() };
if (auto* page = this->page())
size.scale(page->pageScaleFactor());
return size;
}();

if (sizeForHeadlessMode)
return *sizeForHeadlessMode;
if (auto* page = this->page())
return page->chrome().client().screenSizeForHeadlessMode(*this, defaultSize);

return screenRect(view()).size();
return defaultSize;
}

void LocalFrame::setOverrideScreenSize(FloatSize&& screenSize)
Expand Down
5 changes: 5 additions & 0 deletions Source/WebKit/WebProcess/WebCoreSupport/WebChromeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,11 @@ FloatSize WebChromeClient::overrideScreenSize() const

#endif

FloatSize WebChromeClient::screenSizeForHeadlessMode(const LocalFrame& frame, FloatSize defaultSize) const
{
return m_page.screenSizeForHeadlessMode(frame, defaultSize);
}

void WebChromeClient::dispatchDisabledAdaptationsDidChange(const OptionSet<DisabledAdaptations>& disabledAdaptations) const
{
m_page.disabledAdaptationsDidChange(disabledAdaptations);
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/WebProcess/WebCoreSupport/WebChromeClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ class WebChromeClient final : public WebCore::ChromeClient {

bool isInStableState() const final;

WebCore::FloatSize screenSizeForHeadlessMode(const WebCore::LocalFrame&, WebCore::FloatSize defaultSize) const final;

mutable bool m_cachedMainFrameHasHorizontalScrollbar { false };
mutable bool m_cachedMainFrameHasVerticalScrollbar { false };

Expand Down
9 changes: 9 additions & 0 deletions Source/WebKit/WebProcess/WebPage/WebPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,15 @@ void WebPage::viewportPropertiesDidChange(const ViewportArguments& viewportArgum
#endif
}

#if !PLATFORM(IOS_FAMILY)

FloatSize WebPage::screenSizeForHeadlessMode(const LocalFrame& frame, FloatSize defaultSize) const
{
return frame.view() ? FloatSize { frame.view()->unobscuredContentRectIncludingScrollbars().size() } : defaultSize;
}

#endif // !PLATFORM(IOS_FAMILY)

void WebPage::listenForLayoutMilestones(OptionSet<WebCore::LayoutMilestone> milestones)
{
if (!m_page)
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/WebProcess/WebPage/WebPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,8 @@ class WebPage : public API::ObjectImpl<API::Object::Type::BundlePage>, public IP
bool isInStableState() const { return m_isInStableState; }
#endif

WebCore::FloatSize screenSizeForHeadlessMode(const WebCore::LocalFrame&, WebCore::FloatSize defaultSize) const;

const Logger& logger() const;
const void* logIdentifier() const;

Expand Down
20 changes: 20 additions & 0 deletions Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#import "TextCheckingControllerProxy.h"
#import "UIKitSPI.h"
#import "UserData.h"
#import "UserInterfaceIdiom.h"
#import "ViewGestureGeometryCollector.h"
#import "VisibleContentRectUpdateInfo.h"
#import "WKAccessibilityWebPageObjectIOS.h"
Expand Down Expand Up @@ -5153,6 +5154,25 @@ static VisiblePositionRange constrainRangeToSelection(const VisiblePositionRange
scheduleEditorStateUpdateForStartOrEndContainerNodeIfNeeded(endContainer.get());
}

FloatSize WebPage::screenSizeForHeadlessMode(const LocalFrame&, FloatSize defaultSize) const
{
if (!currentUserInterfaceIdiomIsSmallScreen())
return m_viewportConfiguration.minimumLayoutSize();

static constexpr std::array fixedSizes {
FloatSize { 320, 568 },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did these values come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values represent device metrics from several iPhone models. While somewhat arbitrary, I did make sure to pick the most prevalent width values, by number of models with that width.

FloatSize { 375, 667 },
FloatSize { 414, 736 },
};

for (auto fixedSize : fixedSizes) {
if (defaultSize.width() <= fixedSize.width())
return fixedSize;
}

return std::get<std::tuple_size_v<decltype(fixedSizes)> - 1>(fixedSizes);
}

} // namespace WebKit

#undef WEBPAGE_RELEASE_LOG
Expand Down