Commit def052a1 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Avoid using mojo::WrapCallbackWithDefaultInvokeIfNotRun.

Since WrapCallbackWithDefaultInvokeIfNotRun() should be used sparingly,
replace it with code to handle a disconnect in arc::PrintSessionImpl.
Now, arc::PrintSessionImpl manages pending callbacks, and handles PDF
Flattener disconnects.

Bug: 997243
Change-Id: I8a0be44998ce1eec93f166f1776e0595d15e714b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1995941
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarJesse Schettler <jschettler@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731763}
parent f06be0f7
......@@ -28,7 +28,6 @@
#include "content/public/browser/web_contents.h"
#include "mojo/public/c/system/types.h"
#include "mojo/public/cpp/base/shared_memory_utils.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "net/base/filename_util.h"
#include "printing/page_range.h"
#include "printing/print_job_constants.h"
......@@ -265,13 +264,16 @@ void PrintSessionImpl::CreatePreviewDocument(
return;
}
int request_id = job_settings.FindIntKey(printing::kPreviewRequestID).value();
instance_->CreatePreviewDocument(
std::move(request),
base::BindOnce(&PrintSessionImpl::OnPreviewDocumentCreated,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
weak_ptr_factory_.GetWeakPtr(), request_id,
std::move(callback)));
}
void PrintSessionImpl::OnPreviewDocumentCreated(
int request_id,
CreatePreviewDocumentCallback callback,
mojo::ScopedHandle preview_document,
int64_t data_size) {
......@@ -286,10 +288,12 @@ void PrintSessionImpl::OnPreviewDocumentCreated(
base::BindOnce(&ReadPreviewDocument, std::move(preview_document),
static_cast<size_t>(data_size)),
base::BindOnce(&PrintSessionImpl::OnPreviewDocumentRead,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
weak_ptr_factory_.GetWeakPtr(), request_id,
std::move(callback)));
}
void PrintSessionImpl::OnPreviewDocumentRead(
int request_id,
CreatePreviewDocumentCallback callback,
base::ReadOnlySharedMemoryRegion preview_document_region) {
if (!preview_document_region.IsValid()) {
......@@ -300,11 +304,34 @@ void PrintSessionImpl::OnPreviewDocumentRead(
if (!pdf_flattener_.is_bound()) {
GetPrintingService()->BindPdfFlattener(
pdf_flattener_.BindNewPipeAndPassReceiver());
pdf_flattener_.set_disconnect_handler(
base::BindOnce(&PrintSessionImpl::OnPdfFlattenerDisconnected,
weak_ptr_factory_.GetWeakPtr()));
}
bool inserted =
callbacks_.emplace(std::make_pair(request_id, std::move(callback)))
.second;
DCHECK(inserted);
pdf_flattener_->FlattenPdf(
std::move(preview_document_region),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(callback), base::ReadOnlySharedMemoryRegion()));
base::BindOnce(&PrintSessionImpl::OnPdfFlattened,
weak_ptr_factory_.GetWeakPtr(), request_id));
}
void PrintSessionImpl::OnPdfFlattened(
int request_id,
base::ReadOnlySharedMemoryRegion flattened_document_region) {
auto it = callbacks_.find(request_id);
std::move(it->second).Run(std::move(flattened_document_region));
callbacks_.erase(it);
}
void PrintSessionImpl::OnPdfFlattenerDisconnected() {
for (auto& it : callbacks_)
std::move(it.second).Run(base::ReadOnlySharedMemoryRegion());
callbacks_.clear();
}
void PrintSessionImpl::Close() {
......
......@@ -7,7 +7,7 @@
#include <memory>
#include "base/macros.h"
#include "base/containers/flat_map.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
......@@ -44,6 +44,8 @@ class PrintSessionImpl : public mojom::PrintSessionHost,
std::unique_ptr<ash::ArcCustomTab> custom_tab,
mojom::PrintSessionInstancePtr instance);
PrintSessionImpl(const PrintSessionImpl&) = delete;
PrintSessionImpl& operator=(const PrintSessionImpl&) = delete;
~PrintSessionImpl() override;
// Called when print preview is closed.
......@@ -63,16 +65,24 @@ class PrintSessionImpl : public mojom::PrintSessionHost,
// Called once the preview document has been created by ARC. The preview
// document must be read and flattened before being returned by the
// PrintRenderer.
void OnPreviewDocumentCreated(CreatePreviewDocumentCallback callback,
void OnPreviewDocumentCreated(int request_id,
CreatePreviewDocumentCallback callback,
mojo::ScopedHandle preview_document,
int64_t data_size);
// Called once the preview document from ARC has been read. The preview
// document must be flattened before being returned by the PrintRenderer.
void OnPreviewDocumentRead(
int request_id,
CreatePreviewDocumentCallback callback,
base::ReadOnlySharedMemoryRegion preview_document_region);
void OnPdfFlattened(
int request_id,
base::ReadOnlySharedMemoryRegion flattened_document_region);
void OnPdfFlattenerDisconnected();
// Used to close the ARC Custom Tab used for printing. If the remote end
// closes the connection, the ARC Custom Tab and print preview will be closed.
// If printing has already started, this will not cancel any active print job.
......@@ -98,13 +108,14 @@ class PrintSessionImpl : public mojom::PrintSessionHost,
// Remote interface used to flatten a PDF (preview document).
mojo::Remote<printing::mojom::PdfFlattener> pdf_flattener_;
// In flight callbacks to |pdf_flattener_|, with their request IDs as the key.
base::flat_map<int, CreatePreviewDocumentCallback> callbacks_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<PrintSessionImpl> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(PrintSessionImpl);
};
} // namespace arc
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment