Bug 1669774 part 2: Rename some APIs and variables for accuracy (s/page/sheet/) in nsPrintJob, nsPagePrintTimer, and nsDocumentViewer. r=TYLin

Differential Revision: https://phabricator.services.mozilla.com/D92791
This commit is contained in:
Daniel Holbert 2020-10-07 21:00:26 +00:00
parent fd2a60b29e
commit 3e52de68f4
6 changed files with 90 additions and 78 deletions

View File

@ -408,8 +408,7 @@ class nsDocumentViewer final : public nsIContentViewer,
nsresult PrintPreviewScrollToPageForOldUI(int16_t aType, int32_t aPageNum);
std::tuple<const nsIFrame*, int32_t> GetCurrentSheetFrameAndPageNumber()
const;
std::tuple<const nsIFrame*, int32_t> GetCurrentSheetFrameAndNumber() const;
protected:
// Returns the current viewmanager. Might be null.
@ -3207,7 +3206,7 @@ nsresult nsDocumentViewer::PrintPreviewScrollToPageForOldUI(int16_t aType,
}
// in PP mPrtPreview->mPrintObject->mSeqFrame is null
auto [seqFrame, pageCount] = mPrintJob->GetSeqFrameAndCountPages();
auto [seqFrame, sheetCount] = mPrintJob->GetSeqFrameAndCountSheets();
if (!seqFrame) {
return NS_ERROR_FAILURE;
}
@ -3222,7 +3221,7 @@ nsresult nsDocumentViewer::PrintPreviewScrollToPageForOldUI(int16_t aType,
// If it is "End" then just do a "goto" to the last page
if (aType == nsIWebBrowserPrint::PRINTPREVIEW_END) {
aType = nsIWebBrowserPrint::PRINTPREVIEW_GOTO_PAGENUM;
aPageNum = pageCount;
aPageNum = sheetCount;
}
// Now, locate the current page we are on and
@ -3258,7 +3257,7 @@ nsresult nsDocumentViewer::PrintPreviewScrollToPageForOldUI(int16_t aType,
return NS_OK;
}
} else { // If we get here we are doing "GoTo"
if (aPageNum < 0 || aPageNum > pageCount) {
if (aPageNum < 0 || aPageNum > sheetCount) {
return NS_OK;
}
}
@ -3319,8 +3318,8 @@ nsDocumentViewer::PrintPreviewScrollToPage(int16_t aType, int32_t aPageNum) {
mPrintJob->GetPrintPreviewPresShell()->GetRootScrollFrameAsScrollable();
if (!sf) return NS_OK;
auto [seqFrame, pageCount] = mPrintJob->GetSeqFrameAndCountPages();
Unused << pageCount;
auto [seqFrame, sheetCount] = mPrintJob->GetSeqFrameAndCountSheets();
Unused << sheetCount;
if (!seqFrame) {
return NS_ERROR_FAILURE;
}
@ -3338,9 +3337,8 @@ nsDocumentViewer::PrintPreviewScrollToPage(int16_t aType, int32_t aPageNum) {
break;
case nsIWebBrowserPrint::PRINTPREVIEW_PREV_PAGE:
case nsIWebBrowserPrint::PRINTPREVIEW_NEXT_PAGE: {
auto [currentFrame, currentPageNumber] =
GetCurrentSheetFrameAndPageNumber();
Unused << currentPageNumber;
auto [currentFrame, currentSheetNumber] = GetCurrentSheetFrameAndNumber();
Unused << currentSheetNumber;
if (!currentFrame) {
return NS_OK;
}
@ -3359,7 +3357,7 @@ nsDocumentViewer::PrintPreviewScrollToPage(int16_t aType, int32_t aPageNum) {
break;
}
case nsIWebBrowserPrint::PRINTPREVIEW_GOTO_PAGENUM: {
if (aPageNum < 0 || aPageNum > pageCount) {
if (aPageNum < 0 || aPageNum > sheetCount) {
return NS_ERROR_INVALID_ARG;
}
@ -3380,13 +3378,13 @@ nsDocumentViewer::PrintPreviewScrollToPage(int16_t aType, int32_t aPageNum) {
}
std::tuple<const nsIFrame*, int32_t>
nsDocumentViewer::GetCurrentSheetFrameAndPageNumber() const {
nsDocumentViewer::GetCurrentSheetFrameAndNumber() const {
MOZ_ASSERT(mPrintJob);
MOZ_ASSERT(GetIsPrintPreview() && !mPrintJob->GetIsCreatingPrintPreview());
// in PP mPrtPreview->mPrintObject->mSeqFrame is null
auto [seqFrame, pageCount] = mPrintJob->GetSeqFrameAndCountPages();
Unused << pageCount;
auto [seqFrame, sheetCount] = mPrintJob->GetSeqFrameAndCountSheets();
Unused << sheetCount;
if (!seqFrame) {
return {nullptr, 0};
}
@ -3394,7 +3392,7 @@ nsDocumentViewer::GetCurrentSheetFrameAndPageNumber() const {
nsIScrollableFrame* sf =
mPrintJob->GetPrintPreviewPresShell()->GetRootScrollFrameAsScrollable();
if (!sf) {
// No scrollable contents, returns 1 even if there are multiple pages.
// No scrollable contents, returns 1 even if there are multiple sheets.
return {seqFrame->PrincipalChildList().FirstChild(), 1};
}
@ -3402,17 +3400,17 @@ nsDocumentViewer::GetCurrentSheetFrameAndPageNumber() const {
float halfwayPoint =
currentScrollPosition.y + float(sf->GetScrollPortRect().height) / 2.0f;
float lastDistanceFromHalfwayPoint = std::numeric_limits<float>::max();
int32_t pageNumber = 0;
int32_t sheetNumber = 0;
const nsIFrame* currentSheet = nullptr;
float previewScale = seqFrame->GetPrintPreviewScale();
for (const nsIFrame* sheetFrame : seqFrame->PrincipalChildList()) {
nsRect sheetRect = sheetFrame->GetRect();
pageNumber++;
sheetNumber++;
currentSheet = sheetFrame;
float bottomOfSheet = sheetRect.YMost() * previewScale;
if (bottomOfSheet < halfwayPoint) {
// If the bottom of the page is not yet over the halfway point, iterate
// If the bottom of the sheet is not yet over the halfway point, iterate
// the next frame to see if the next frame is over the halfway point and
// compare the distance from the halfway point.
lastDistanceFromHalfwayPoint = halfwayPoint - bottomOfSheet;
@ -3421,28 +3419,31 @@ nsDocumentViewer::GetCurrentSheetFrameAndPageNumber() const {
float topOfSheet = sheetRect.Y() * previewScale;
if (topOfSheet <= halfwayPoint) {
// If the top of the page is not yet over the halfway point or on the
// point, it's the current page.
// If the top of the sheet is not yet over the halfway point or on the
// point, it's the current sheet.
break;
}
// Now the page rect is completely over the halfway point, compare the
// Now the sheet rect is completely over the halfway point, compare the
// distances from the halfway point.
if ((topOfSheet - halfwayPoint) >= lastDistanceFromHalfwayPoint) {
// If the previous page distance is less than or equal to the current page
// distance, choose the previous one as the current.
pageNumber--;
MOZ_ASSERT(pageNumber > 0);
// If the previous sheet distance is less than or equal to the current
// sheet distance, choose the previous one as the current.
sheetNumber--;
MOZ_ASSERT(sheetNumber > 0);
currentSheet = currentSheet->GetPrevInFlow();
MOZ_ASSERT(currentSheet);
}
break;
}
MOZ_ASSERT(pageNumber <= pageCount);
return {currentSheet, pageNumber};
MOZ_ASSERT(sheetNumber <= sheetCount);
return {currentSheet, sheetNumber};
}
// XXXdholbert As noted in nsIWebBrowserPrint.idl, this API (the IDL attr
// 'printPreviewCurrentPageNumber') is misnamed and needs s/Page/Sheet/. See
// bug 1669762.
NS_IMETHODIMP
nsDocumentViewer::GetPrintPreviewCurrentPageNumber(int32_t* aNumber) {
NS_ENSURE_ARG_POINTER(aNumber);
@ -3451,13 +3452,13 @@ nsDocumentViewer::GetPrintPreviewCurrentPageNumber(int32_t* aNumber) {
return NS_ERROR_FAILURE;
}
auto [currentFrame, currentPageNumber] = GetCurrentSheetFrameAndPageNumber();
auto [currentFrame, currentSheetNumber] = GetCurrentSheetFrameAndNumber();
Unused << currentFrame;
if (!currentPageNumber) {
if (!currentSheetNumber) {
return NS_ERROR_FAILURE;
}
*aNumber = currentPageNumber;
*aNumber = currentSheetNumber;
return NS_OK;
}
@ -3542,12 +3543,14 @@ nsDocumentViewer::GetRawNumPages(int32_t* aRawNumPages) {
return *aRawNumPages > 0 ? NS_OK : NS_ERROR_FAILURE;
}
// XXXdholbert As noted in nsIWebBrowserPrint.idl, this API (the IDL attr
// 'printPreviewNumPages') is misnamed and needs s/Page/Sheet/.
// See bug 1669762.
NS_IMETHODIMP
nsDocumentViewer::GetPrintPreviewNumPages(int32_t* aPrintPreviewNumPages) {
NS_ENSURE_ARG_POINTER(aPrintPreviewNumPages);
NS_ENSURE_TRUE(mPrintJob, NS_ERROR_FAILURE);
*aPrintPreviewNumPages = mPrintJob->GetPrintPreviewNumPages();
*aPrintPreviewNumPages = mPrintJob->GetPrintPreviewNumSheets();
return *aPrintPreviewNumPages > 0 ? NS_OK : NS_ERROR_FAILURE;
}

View File

@ -64,25 +64,25 @@ NS_IMETHODIMP
nsPagePrintTimer::Run() {
bool initNewTimer = true;
// Check to see if we are done
// inRange will be true if a page is actually printed
// inRange will be true if a sheet is actually printed
bool inRange;
bool donePrinting;
// donePrinting will be true if it completed successfully or
// if the printing was cancelled
donePrinting = !mPrintJob || mPrintJob->PrintPage(mPrintObj, inRange);
donePrinting = !mPrintJob || mPrintJob->PrintSheet(mPrintObj, inRange);
if (donePrinting) {
if (mWaitingForRemotePrint ||
// If we are not waiting for the remote printing, it is the time to
// end printing task by calling DonePrintingPages.
(!mPrintJob || mPrintJob->DonePrintingPages(mPrintObj, NS_OK))) {
// end printing task by calling DonePrintingSheets.
(!mPrintJob || mPrintJob->DonePrintingSheets(mPrintObj, NS_OK))) {
initNewTimer = false;
mDone = true;
}
}
// Note that the Stop() destroys this after the print job finishes
// (The nsPrintJob stops holding a reference when DonePrintingPages
// (The nsPrintJob stops holding a reference when DonePrintingSheets
// returns true.)
Stop();
if (initNewTimer) {
@ -108,7 +108,7 @@ nsPagePrintTimer::Notify(nsITimer* timer) {
}
// There are four things that call Notify with different values for timer:
// 1) the delay between pages (timer == mTimer)
// 1) the delay between sheets (timer == mTimer)
// 2) canvasPrintState done (timer == null)
// 3) the watch dog timer (timer == mWatchDogTimer)
// 4) the waiting for remote print "timer" (timer == mWaitingForRemotePrint)
@ -116,14 +116,14 @@ nsPagePrintTimer::Notify(nsITimer* timer) {
// Reset the counter since a mozPrintCallback has finished.
mWatchDogCount = 0;
} else if (timer == mTimer) {
// Reset the watchdog timer before the start of every page.
// Reset the watchdog timer before the start of every sheet.
mWatchDogCount = 0;
mTimer = nullptr;
} else if (timer == mWaitingForRemotePrint) {
mWaitingForRemotePrint = nullptr;
// If we are still waiting for the page delay timer, don't let the
// notification from the remote print job trigger the next page.
// If we are still waiting for the sheet delay timer, don't let the
// notification from the remote print job trigger the next sheet.
if (mTimer) {
return NS_OK;
}
@ -138,7 +138,7 @@ nsPagePrintTimer::Notify(nsITimer* timer) {
bool donePrePrint = true;
// Don't start to pre-print if we're waiting on the parent still.
if (mPrintJob && !mWaitingForRemotePrint) {
donePrePrint = mPrintJob->PrePrintPage();
donePrePrint = mPrintJob->PrePrintSheet();
}
if (donePrePrint && !mWaitingForRemotePrint) {
@ -168,7 +168,7 @@ void nsPagePrintTimer::RemotePrintFinished() {
// now clean up print or print the next webshell
if (mDone && mPrintJob) {
mDone = mPrintJob->DonePrintingPages(mPrintObj, NS_OK);
mDone = mPrintJob->DonePrintingSheets(mPrintObj, NS_OK);
}
mWaitingForRemotePrint->SetTarget(

View File

@ -20,6 +20,10 @@ class nsPrintJob;
//---------------------------------------------------
//-- Page Timer Class
//---------------------------------------------------
// Strictly speaking, this actually manages the timing of printing *sheets*
// (instances of "PrintedSheetFrame"), each of which may encompass multiple
// pages (nsPageFrames) of the document. The use of "Page" in the class name
// here is for historical / colloquial purposes.
class nsPagePrintTimer final : public mozilla::Runnable,
public nsITimerCallback {
public:

View File

@ -221,7 +221,7 @@ static bool IsParentAFrameSet(nsIDocShell* aParent) {
}
static std::tuple<nsPageSequenceFrame*, int32_t>
GetSeqFrameAndCountPagesInternal(const UniquePtr<nsPrintObject>& aPO) {
GetSeqFrameAndCountSheetsInternal(const UniquePtr<nsPrintObject>& aPO) {
if (!aPO) {
return {nullptr, 0};
}
@ -231,7 +231,7 @@ GetSeqFrameAndCountPagesInternal(const UniquePtr<nsPrintObject>& aPO) {
// Nightly/Aurora in case the other patch fixes this.
if (!aPO->mPresShell) {
MOZ_DIAGNOSTIC_ASSERT(
false, "GetSeqFrameAndCountPages needs a non-null pres shell");
false, "GetSeqFrameAndCountSheets needs a non-null pres shell");
return {nullptr, 0};
}
@ -240,7 +240,7 @@ GetSeqFrameAndCountPagesInternal(const UniquePtr<nsPrintObject>& aPO) {
return {nullptr, 0};
}
// count the total number of pages
// count the total number of sheets
return {seqFrame, seqFrame->PrincipalChildList().GetLength()};
}
@ -427,12 +427,12 @@ nsresult nsPrintJob::Cancel() {
//-----------------------------------------------------------------
std::tuple<nsPageSequenceFrame*, int32_t>
nsPrintJob::GetSeqFrameAndCountPages() {
nsPrintJob::GetSeqFrameAndCountSheets() {
MOZ_ASSERT(mPrtPreview);
// Guarantee that mPrintPreview->mPrintObject won't be deleted during a call
// of GetSeqFrameAndCountPagesInternal().
// of GetSeqFrameAndCountSheetsInternal().
RefPtr<nsPrintData> printDataForPrintPreview = mPrtPreview;
return GetSeqFrameAndCountPagesInternal(
return GetSeqFrameAndCountSheetsInternal(
printDataForPrintPreview->mPrintObject);
}
//---------------------------------------------------------------------------------
@ -871,23 +871,23 @@ int32_t nsPrintJob::GetRawNumPages() const {
if (NS_WARN_IF(!printData)) {
return 0;
}
auto [seqFrame, numPages] =
GetSeqFrameAndCountPagesInternal(printData->mPrintObject);
auto [seqFrame, numSheets] =
GetSeqFrameAndCountSheetsInternal(printData->mPrintObject);
Unused << numPages;
Unused << numSheets;
return seqFrame ? seqFrame->GetRawNumPages() : 0;
}
//----------------------------------------------------------------------------------
int32_t nsPrintJob::GetPrintPreviewNumPages() {
int32_t nsPrintJob::GetPrintPreviewNumSheets() {
RefPtr<nsPrintData> printData = mPrtPreview ? mPrtPreview : mPrt;
if (NS_WARN_IF(!printData)) {
return 0;
}
auto [seqFrame, numPages] =
GetSeqFrameAndCountPagesInternal(printData->mPrintObject);
auto [seqFrame, numSheets] =
GetSeqFrameAndCountSheetsInternal(printData->mPrintObject);
Unused << seqFrame;
return numPages;
return numSheets;
}
//----------------------------------------------------------------------------------
@ -1032,7 +1032,7 @@ nsresult nsPrintJob::DocumentReadyForPrinting() {
if (NS_FAILED(rv)) {
// The print job was canceled or there was a problem
// So remove all other documents from the print list
DonePrintingPages(nullptr, rv);
DonePrintingSheets(nullptr, rv);
}
return rv;
}
@ -1412,7 +1412,7 @@ nsresult nsPrintJob::SetupToPrintContent() {
}
// This will print the docshell document
// when it completes asynchronously in the DonePrintingPages method
// when it completes asynchronously in the DonePrintingSheets method
// it will check to see if there are more docshells to be printed and
// then PrintDocContent will be called again.
@ -2169,14 +2169,14 @@ void nsPrintJob::EllipseLongString(nsAString& aStr, const uint32_t aLen,
}
//-------------------------------------------------------
bool nsPrintJob::PrePrintPage() {
bool nsPrintJob::PrePrintSheet() {
NS_ASSERTION(mPageSeqFrame.IsAlive(), "mPageSeqFrame is not alive!");
NS_ASSERTION(mPrt, "mPrt is null!");
// Although these should NEVER be nullptr
// This is added insurance, to make sure we don't crash in optimized builds
if (!mPrt || !mPageSeqFrame.IsAlive()) {
return true; // means we are done preparing the page.
return true; // means we are done preparing the sheet.
}
// Guarantee that mPrt won't be deleted during a call of
@ -2188,13 +2188,13 @@ bool nsPrintJob::PrePrintPage() {
printData->mPrintSettings->GetIsCancelled(&isCancelled);
if (isCancelled) return true;
// Ask mPageSeqFrame if the page is ready to be printed.
// If the page doesn't get printed at all, the |done| will be |true|.
// Ask mPageSeqFrame if the sheet is ready to be printed.
// If the sheet doesn't get printed at all, the |done| will be |true|.
bool done = false;
nsPageSequenceFrame* pageSeqFrame = do_QueryFrame(mPageSeqFrame.GetFrame());
nsresult rv = pageSeqFrame->PrePrintNextSheet(mPagePrintTimer, &done);
if (NS_FAILED(rv)) {
// ??? ::PrintPage doesn't set |printData->mIsAborted = true| if
// ??? ::PrintSheet doesn't set |printData->mIsAborted = true| if
// rv != NS_ERROR_ABORT, but I don't really understand why this should be
// the right thing to do? Shouldn't |printData->mIsAborted| set to true
// all the time if something went wrong?
@ -2207,7 +2207,7 @@ bool nsPrintJob::PrePrintPage() {
return done;
}
bool nsPrintJob::PrintPage(nsPrintObject* aPO, bool& aInRange) {
bool nsPrintJob::PrintSheet(nsPrintObject* aPO, bool& aInRange) {
NS_ASSERTION(aPO, "aPO is null!");
NS_ASSERTION(mPageSeqFrame.IsAlive(), "mPageSeqFrame is not alive!");
NS_ASSERTION(mPrt, "mPrt is null!");
@ -2232,7 +2232,7 @@ bool nsPrintJob::PrintPage(nsPrintObject* aPO, bool& aInRange) {
RefPtr<nsPrintData> printData = mPrt;
PR_PL(("-----------------------------------\n"));
PR_PL(("------ In DV::PrintPage PO: %p (%s)\n", aPO,
PR_PL(("------ In DV::PrintSheet PO: %p (%s)\n", aPO,
gFrameTypesStr[aPO->mFrameType]));
// Check setting to see if someone request it be cancelled
@ -2263,7 +2263,7 @@ bool nsPrintJob::PrintPage(nsPrintObject* aPO, bool& aInRange) {
mPagePrintTimer->WaitForRemotePrint();
}
// Print the Page
// Print the sheet
// if a print job was cancelled externally, an EndPage or BeginPage may
// fail and the failure is passed back here.
// Returning true means we are done printing.
@ -2365,9 +2365,9 @@ bool nsPrintJob::IsWindowsInOurSubTree(nsPIDOMWindowOuter* window) const {
}
//-------------------------------------------------------
bool nsPrintJob::DonePrintingPages(nsPrintObject* aPO, nsresult aResult) {
bool nsPrintJob::DonePrintingSheets(nsPrintObject* aPO, nsresult aResult) {
// NS_ASSERTION(aPO, "Pointer is null!");
PR_PL(("****** In DV::DonePrintingPages PO: %p (%s)\n", aPO,
PR_PL(("****** In DV::DonePrintingSheets PO: %p (%s)\n", aPO,
aPO ? gFrameTypesStr[aPO->mFrameType] : ""));
// If there is a pageSeqFrame, make sure there are no more printCanvas active
@ -2388,7 +2388,7 @@ bool nsPrintJob::DonePrintingPages(nsPrintObject* aPO, nsresult aResult) {
bool didPrint = PrintDocContent(printData->mPrintObject, rv);
if (NS_SUCCEEDED(rv) && didPrint) {
PR_PL(
("****** In DV::DonePrintingPages PO: %p (%s) didPrint:%s (Not Done "
("****** In DV::DonePrintingSheets PO: %p (%s) didPrint:%s (Not Done "
"Printing)\n",
aPO, gFrameTypesStr[aPO->mFrameType], PRT_YESNO(didPrint)));
return false;
@ -2536,7 +2536,7 @@ nsresult nsPrintJob::FinishPrintPreview() {
if (mPrintPreviewCallback) {
mPrintPreviewCallback(PrintPreviewResultInfo(
GetPrintPreviewNumPages(), GetRawNumPages(),
GetPrintPreviewNumSheets(), GetRawNumPages(),
!mDisallowSelectionPrint && printData->mSelectionRoot));
mPrintPreviewCallback = nullptr;
}

View File

@ -127,7 +127,12 @@ class nsPrintJob final : public nsIObserver,
bool HasEverPrinted() const { return mHasEverPrinted; }
/// If the returned value is not greater than zero, an error occurred.
int32_t GetRawNumPages() const;
int32_t GetPrintPreviewNumPages();
// Returns the total number of PrintedSheetFrames (i.e. faces of a sheet of
// paper) for this print job. (This may be less than the raw number of pages,
// due to pages having been skipped in a page range or combined into a single
// sheet via pages-per-sheet.)
int32_t GetPrintPreviewNumSheets();
already_AddRefed<nsIPrintSettings> GetCurrentPrintSettings();
// The setters here also update the DocViewer
@ -136,11 +141,11 @@ class nsPrintJob final : public nsIObserver,
void SetIsPrintPreview(bool aIsPrintPreview);
bool GetIsCreatingPrintPreview() const { return mIsCreatingPrintPreview; }
std::tuple<nsPageSequenceFrame*, int32_t> GetSeqFrameAndCountPages();
std::tuple<nsPageSequenceFrame*, int32_t> GetSeqFrameAndCountSheets();
bool PrePrintPage();
bool PrintPage(nsPrintObject* aPOect, bool& aInRange);
bool DonePrintingPages(nsPrintObject* aPO, nsresult aResult);
bool PrePrintSheet();
bool PrintSheet(nsPrintObject* aPOect, bool& aInRange);
bool DonePrintingSheets(nsPrintObject* aPO, nsresult aResult);
nsresult CleanupOnFailure(nsresult aResult, bool aIsPrinting);
// If FinishPrintPreview() fails, caller may need to reset the state of the

View File

@ -34,7 +34,7 @@ interface nsIWebBrowserPrint : nsISupports
*
* XXXdholbert Consider renaming these? Strictly speaking, these deal with
* *sheets* (which are roughly the same as pages in the default configuration
* of one page per sheet).
* of one page per sheet). Fix in bug 1669762.
*/
const short PRINTPREVIEW_GOTO_PAGENUM = 0;
const short PRINTPREVIEW_PREV_PAGE = 1;
@ -77,7 +77,7 @@ interface nsIWebBrowserPrint : nsISupports
*
* XXXdholbert Consider renaming this? Strictly speaking, this is the number
* of *sheets* (which is the same as the number of pages in the default
* configuration of one page per sheet).
* configuration of one page per sheet). Fix in bug 1669762.
*/
readonly attribute long printPreviewNumPages;
@ -86,7 +86,7 @@ interface nsIWebBrowserPrint : nsISupports
*
* XXXdholbert Consider renaming this? (similar to printPreviewNumPages above)
* Strictly speaking, this is the number of the *sheet* which is currently in
* the print preview viewport.
* the print preview viewport. Fix in bug 1669762.
*/
readonly attribute long printPreviewCurrentPageNumber;