Skip to content

Commit

Permalink
Migrate PdfViewPluginBase::HandleSaveMessage()
Browse files Browse the repository at this point in the history
Migrates HandleSaveMessage() from PdfViewPluginBase to PdfViewWebPlugin.

Bug: 1323307
Change-Id: Idbece51e91645ce11746b3f934140a08bc8e4104
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3662129
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
Auto-Submit: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1007947}
  • Loading branch information
kmoon-work authored and Chromium LUCI CQ committed May 26, 2022
1 parent 5c7327e commit 8c5aba7
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 145 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/resources/pdf/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type NamedDestinationMessageData = {

/**
* Enumeration of save message request types. Must match `SaveRequestType` in
* pdf/pdf_view_plugin_base.h.
* pdf/pdf_view_web_plugin.h.
*/
export enum SaveRequestType {
ANNOTATION,
Expand Down
1 change: 1 addition & 0 deletions pdf/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ if (enable_pdf) {

deps = [
":accessibility",
":buildflags",
":content_restriction",
":internal",
":ppapi_migration",
Expand Down
95 changes: 1 addition & 94 deletions pdf/pdf_view_plugin_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ PdfViewPluginBase::~PdfViewPluginBase() = default;
void PdfViewPluginBase::InitializeBase(std::unique_ptr<PDFiumEngine> engine,
base::StringPiece src_url,
base::StringPiece original_url,
bool full_frame,
bool has_edits) {
bool full_frame) {
full_frame_ = full_frame;

DCHECK(engine);
Expand All @@ -169,14 +168,6 @@ void PdfViewPluginBase::InitializeBase(std::unique_ptr<PDFiumEngine> engine,
last_progress_sent_ = 0;
LoadUrl(src_url, base::BindOnce(&PdfViewPluginBase::DidOpen, GetWeakPtr()));
url_ = std::string(original_url);

// Not all edits go through the PDF plugin's form filler. The plugin instance
// can be restarted by exiting annotation mode on ChromeOS, which can set the
// document to an edited state.
edit_mode_ = has_edits;
#if !BUILDFLAG(ENABLE_INK)
DCHECK(!edit_mode_);
#endif // !BUILDFLAG(ENABLE_INK)
}

void PdfViewPluginBase::ProposeDocumentLayout(const DocumentLayout& layout) {
Expand Down Expand Up @@ -446,15 +437,6 @@ void PdfViewPluginBase::SelectionChanged(const gfx::Rect& left,
PrepareAndSetAccessibilityViewportInfo();
}

void PdfViewPluginBase::EnteredEditMode() {
edit_mode_ = true;
SetPluginCanSave(true);

base::Value::Dict message;
message.Set("type", "setIsEditing");
SendMessage(std::move(message));
}

void PdfViewPluginBase::DocumentFocusChanged(bool document_has_focus) {
base::Value::Dict message;
message.Set("type", "documentFocusChanged");
Expand Down Expand Up @@ -514,7 +496,6 @@ void PdfViewPluginBase::HandleMessage(const base::Value::Dict& message) {
{"rotateClockwise", &PdfViewPluginBase::HandleRotateClockwiseMessage},
{"rotateCounterclockwise",
&PdfViewPluginBase::HandleRotateCounterclockwiseMessage},
{"save", &PdfViewPluginBase::HandleSaveMessage},
{"saveAttachment", &PdfViewPluginBase::HandleSaveAttachmentMessage},
{"selectAll", &PdfViewPluginBase::HandleSelectAllMessage},
{"setPresentationMode",
Expand All @@ -528,46 +509,6 @@ void PdfViewPluginBase::HandleMessage(const base::Value::Dict& message) {
(this->*handler)(message);
}

void PdfViewPluginBase::SaveToBuffer(const std::string& token) {
engine()->KillFormFocus();

base::Value::Dict message;
message.Set("type", "saveData");
message.Set("token", token);
message.Set("fileName", GetFileNameForSaveFromUrl(url_));

// Expose `edit_mode_` state for integration testing.
message.Set("editModeForTesting", edit_mode_);

base::Value data_to_save;
if (edit_mode_) {
base::Value::BlobStorage data = engine()->GetSaveData();
if (IsSaveDataSizeValid(data.size()))
data_to_save = base::Value(std::move(data));
} else {
#if BUILDFLAG(ENABLE_INK)
uint32_t length = engine()->GetLoadedByteSize();
if (IsSaveDataSizeValid(length)) {
base::Value::BlobStorage data(length);
if (engine()->ReadLoadedBytes(length, data.data()))
data_to_save = base::Value(std::move(data));
}
#else
NOTREACHED();
#endif // BUILDFLAG(ENABLE_INK)
}

message.Set("dataToSave", std::move(data_to_save));
SendMessage(std::move(message));
}

void PdfViewPluginBase::ConsumeSaveToken(const std::string& token) {
base::Value::Dict message;
message.Set("type", "consumeSaveToken");
message.Set("token", token);
SendMessage(std::move(message));
}

void PdfViewPluginBase::SendLoadingProgress(double percentage) {
DCHECK(percentage == -1 || (percentage >= 0 && percentage <= 100));
last_progress_sent_ = percentage;
Expand Down Expand Up @@ -1088,34 +1029,6 @@ void PdfViewPluginBase::HandleRotateCounterclockwiseMessage(
engine()->RotateCounterclockwise();
}

void PdfViewPluginBase::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));

switch (static_cast<SaveRequestType>(request_type)) {
case SaveRequestType::kAnnotation:
#if BUILDFLAG(ENABLE_INK)
// In annotation mode, assume the user will make edits and prefer saving
// using the plugin data.
SetPluginCanSave(true);
SaveToBuffer(token);
#else
NOTREACHED();
#endif // BUILDFLAG(ENABLE_INK)
break;
case SaveRequestType::kOriginal:
SetPluginCanSave(false);
SaveToFile(token);
SetPluginCanSave(edit_mode_);
break;
case SaveRequestType::kEdited:
SaveToBuffer(token);
break;
}
}

void PdfViewPluginBase::HandleSaveAttachmentMessage(
const base::Value::Dict& message) {
const int index = message.FindInt("attachmentIndex").value();
Expand Down Expand Up @@ -1280,12 +1193,6 @@ void PdfViewPluginBase::HandleViewportMessage(
UpdateScroll(GetScrollPositionFromOffset(scroll_offset));
}

void PdfViewPluginBase::SaveToFile(const std::string& token) {
engine()->KillFormFocus();
ConsumeSaveToken(token);
SaveAs();
}

void PdfViewPluginBase::DoPaint(const std::vector<gfx::Rect>& paint_rects,
std::vector<PaintReadyRect>& ready,
std::vector<gfx::Rect>& pending) {
Expand Down
30 changes: 1 addition & 29 deletions pdf/pdf_view_plugin_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ class PdfViewPluginBase : public PDFEngine::Client,
kFailed,
};

// Must match `SaveRequestType` in chrome/browser/resources/pdf/constants.ts.
enum class SaveRequestType {
kAnnotation = 0,
kOriginal = 1,
kEdited = 2,
};

PdfViewPluginBase(const PdfViewPluginBase& other) = delete;
PdfViewPluginBase& operator=(const PdfViewPluginBase& other) = delete;

Expand Down Expand Up @@ -124,7 +117,6 @@ class PdfViewPluginBase : public PDFEngine::Client,
void FormFieldFocusChange(PDFEngine::FocusFieldType type) override;
void SetIsSelecting(bool is_selecting) override;
void SelectionChanged(const gfx::Rect& left, const gfx::Rect& right) override;
void EnteredEditMode() override;
void DocumentFocusChanged(bool document_has_focus) override;
void SetLinkUnderCursor(const std::string& link_under_cursor) override;

Expand Down Expand Up @@ -184,8 +176,7 @@ class PdfViewPluginBase : public PDFEngine::Client,
void InitializeBase(std::unique_ptr<PDFiumEngine> engine,
base::StringPiece src_url,
base::StringPiece original_url,
bool full_frame,
bool has_edits);
bool full_frame);

// Creates a new `PDFiumEngine`.
virtual std::unique_ptr<PDFiumEngine> CreateEngine(
Expand Down Expand Up @@ -222,14 +213,6 @@ class PdfViewPluginBase : public PDFEngine::Client,
// non-blocking.
virtual void SendMessage(base::Value::Dict message) = 0;

// Invokes the "SaveAs" dialog.
virtual void SaveAs() = 0;

void SaveToBuffer(const std::string& token);

// Consumes a token for saving the document.
void ConsumeSaveToken(const std::string& token);

// Sends the loading progress, where `percentage` represents the progress, or
// -1 for loading error.
void SendLoadingProgress(double percentage);
Expand Down Expand Up @@ -322,10 +305,6 @@ class PdfViewPluginBase : public PDFEngine::Client,
// set by `chrome_pdf::ContentRestriction` enum values.
virtual void SetContentRestrictions(int content_restrictions) = 0;

// Informs the embedder whether the plugin can handle save commands
// internally.
virtual void SetPluginCanSave(bool can_save) = 0;

// Sends start/stop loading notifications to the plugin's render frame.
virtual void DidStartLoading() = 0;
virtual void DidStopLoading() = 0;
Expand Down Expand Up @@ -404,17 +383,13 @@ class PdfViewPluginBase : public PDFEngine::Client,
void HandleRotateClockwiseMessage(const base::Value::Dict& /*message*/);
void HandleRotateCounterclockwiseMessage(
const base::Value::Dict& /*message*/);
void HandleSaveMessage(const base::Value::Dict& message);
void HandleSaveAttachmentMessage(const base::Value::Dict& message);
void HandleSelectAllMessage(const base::Value::Dict& /*message*/);
void HandleSetPresentationModeMessage(const base::Value::Dict& message);
void HandleSetTwoUpViewMessage(const base::Value::Dict& message);
void HandleStopScrollingMessage(const base::Value::Dict& /*message*/);
void HandleViewportMessage(const base::Value::Dict& message);

// Saves the document to a file.
void SaveToFile(const std::string& token);

// Paints the given invalid area of the plugin to the given graphics device.
// PaintManager::Client::OnPaint() should be its only caller.
void DoPaint(const std::vector<gfx::Rect>& paint_rects,
Expand Down Expand Up @@ -560,9 +535,6 @@ class PdfViewPluginBase : public PDFEngine::Client,
// feature once, which helps prevent the infobar from going up more than once.
bool notified_browser_about_unsupported_feature_ = false;

// Whether the document is in edit mode.
bool edit_mode_ = false;

// Assigned a value only between `PrintBegin()` and `PrintEnd()` calls.
absl::optional<blink::WebPrintParams> print_params_;

Expand Down
4 changes: 0 additions & 4 deletions pdf/pdf_view_plugin_base_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ class FakePdfViewPluginBase : public PdfViewPluginBase {
sent_messages_.push_back(base::Value(std::move(message)));
}

MOCK_METHOD(void, SaveAs, (), (override));

MOCK_METHOD(void, SetFormTextFieldInFocus, (bool), (override));

MOCK_METHOD(void,
Expand All @@ -144,8 +142,6 @@ class FakePdfViewPluginBase : public PdfViewPluginBase {

MOCK_METHOD(void, SetContentRestrictions, (int), (override));

MOCK_METHOD(void, SetPluginCanSave, (bool), (override));

MOCK_METHOD(void, DidStartLoading, (), (override));
MOCK_METHOD(void, DidStopLoading, (), (override));

Expand Down
Loading

0 comments on commit 8c5aba7

Please sign in to comment.