0

[PDF] Refactor save handling

Perform the following refactoring:
1. In PdfViewWebPlugin::HandleSaveMessage(), cast the request type to
   SaveRequestType immediately. Remove unnecessary DCHECKs, as the
   method would crash from NOTREACHED() anyway.
2. In PdfViewWebPlugin::SaveToBuffer(), add a new parameter for the save
   request type, to validate that SaveToBuffer() is only called for
   annotation and edited save requests.

Change-Id: I92a1e7a004a6abea1f2858c0f5a5148f7499f48d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6013704
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Andy Phan <andyphan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1381554}
This commit is contained in:
Andy Phan
2024-11-12 01:54:14 +00:00
committed by Chromium LUCI CQ
parent 7e5edaad9c
commit 621a039fbc
2 changed files with 11 additions and 8 deletions

@ -1716,17 +1716,16 @@ void PdfViewWebPlugin::HandleSaveAttachmentMessage(
void PdfViewWebPlugin::HandleSaveMessage(const base::Value::Dict& message) {
const std::string& token = *message.FindString("token");
int request_type = message.FindInt("saveRequestType").value();
DCHECK_GE(request_type, static_cast<int>(SaveRequestType::kAnnotation));
DCHECK_LE(request_type, static_cast<int>(SaveRequestType::kEdited));
SaveRequestType request_type =
static_cast<SaveRequestType>(message.FindInt("saveRequestType").value());
switch (static_cast<SaveRequestType>(request_type)) {
switch (request_type) {
case SaveRequestType::kAnnotation:
#if BUILDFLAG(ENABLE_INK) || BUILDFLAG(ENABLE_PDF_INK2)
// In annotation mode, assume the user will make edits and prefer saving
// using the plugin data.
SetPluginCanSave(true);
SaveToBuffer(token);
SaveToBuffer(request_type, token);
return;
#else
NOTREACHED();
@ -1739,7 +1738,7 @@ void PdfViewWebPlugin::HandleSaveMessage(const base::Value::Dict& message) {
return;
}
case SaveRequestType::kEdited:
SaveToBuffer(token);
SaveToBuffer(request_type, token);
return;
}
NOTREACHED();
@ -1902,7 +1901,11 @@ void PdfViewWebPlugin::HandleViewportMessage(const base::Value::Dict& message) {
UpdateScroll(GetScrollPositionFromOffset(scroll_offset));
}
void PdfViewWebPlugin::SaveToBuffer(const std::string& token) {
void PdfViewWebPlugin::SaveToBuffer(SaveRequestType request_type,
const std::string& token) {
CHECK(request_type == SaveRequestType::kAnnotation ||
request_type == SaveRequestType::kEdited);
engine_->KillFormFocus();
base::Value::Dict message;

@ -553,7 +553,7 @@ class PdfViewWebPlugin final : public PDFiumEngineClient,
void HandleStopScrollingMessage(const base::Value::Dict& message);
void HandleViewportMessage(const base::Value::Dict& message);
void SaveToBuffer(const std::string& token);
void SaveToBuffer(SaveRequestType request_type, const std::string& token);
void SaveToFile(const std::string& token);
// Sets whether the plugin can and should handle the save by using `pdf_host_`