0

[PDF] Avoid sending unnecessary SetPluginCanSave messages

SetPluginCanSave is a renderer-to-browser message that PdfViewWebPlugin
sends to let the browser know that the plugin will handle the save.
PdfViewWebPlugin can send the message multiple times, even if the
browser already knows if the plugin will handle the save. Avoid
redundant calls.

Change-Id: Ice4dbc0d97fd70a48d7314adf7dc50f9a5a2dabf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6001192
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Andy Phan <andyphan@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1380560}
This commit is contained in:
Andy Phan
2024-11-08 20:07:37 +00:00
committed by Chromium LUCI CQ
parent c994200f2d
commit 1b98c1c3c1
3 changed files with 23 additions and 7 deletions

@ -1380,7 +1380,7 @@ void PdfViewWebPlugin::SelectionChanged(const gfx::Rect& left,
void PdfViewWebPlugin::EnteredEditMode() {
edit_mode_ = true;
pdf_host_->SetPluginCanSave(true);
SetPluginCanSave(true);
base::Value::Dict message;
message.Set("type", "setIsEditing");
@ -1690,17 +1690,19 @@ void PdfViewWebPlugin::HandleSaveMessage(const base::Value::Dict& message) {
#if BUILDFLAG(ENABLE_INK)
// In annotation mode, assume the user will make edits and prefer saving
// using the plugin data.
pdf_host_->SetPluginCanSave(true);
SetPluginCanSave(true);
SaveToBuffer(token);
return;
#else
NOTREACHED();
#endif // BUILDFLAG(ENABLE_INK)
case SaveRequestType::kOriginal:
pdf_host_->SetPluginCanSave(false);
case SaveRequestType::kOriginal: {
const bool can_save = plugin_can_save_ || edit_mode_;
SetPluginCanSave(false);
SaveToFile(token);
pdf_host_->SetPluginCanSave(edit_mode_);
SetPluginCanSave(can_save);
return;
}
case SaveRequestType::kEdited:
SaveToBuffer(token);
return;
@ -1909,6 +1911,15 @@ void PdfViewWebPlugin::SaveToFile(const std::string& token) {
pdf_host_->SaveUrlAs(GURL(url_), network::mojom::ReferrerPolicy::kDefault);
}
void PdfViewWebPlugin::SetPluginCanSave(bool can_save) {
if (plugin_can_save_ == can_save) {
return;
}
plugin_can_save_ = can_save;
pdf_host_->SetPluginCanSave(can_save);
}
void PdfViewWebPlugin::InvalidatePluginContainer() {
client_->Invalidate();
}

@ -556,6 +556,11 @@ class PdfViewWebPlugin final : public PDFiumEngineClient,
void SaveToBuffer(const std::string& token);
void SaveToFile(const std::string& token);
// Sets whether the plugin can and should handle the save by using `pdf_host_`
// to notify the browser. Prevents duplicate notifications to the browser if
// the state has not changed.
void SetPluginCanSave(bool can_save);
// Converts a scroll offset (which is relative to a UI direction-dependent
// scroll origin) to a scroll position (which is always relative to the
// top-left corner).
@ -702,6 +707,8 @@ class PdfViewWebPlugin final : public PDFiumEngineClient,
bool initialized_ = false;
bool plugin_can_save_ = false;
blink::WebString selected_text_;
std::unique_ptr<Client> const client_;

@ -2014,10 +2014,8 @@ TEST_F(PdfViewWebPluginSaveTest, OriginalInNonEditMode) {
{
InSequence pdf_host_sequence;
EXPECT_CALL(pdf_host_, SetPluginCanSave(false));
EXPECT_CALL(pdf_host_, SaveUrlAs(GURL(kPdfUrl),
network::mojom::ReferrerPolicy::kDefault));
EXPECT_CALL(pdf_host_, SetPluginCanSave(false));
}
ExpectUpdateTextInputState(blink::WebTextInputType::kWebTextInputTypeNone);