Repository: electron/electron
Pull request: fix: add missed enum `SaveRequestType` to PdfViewerPrivate function

Backport of https://github.com/electron/electron/pull/48372

See that PR for details.

Notes: Fix download button does not work in PDF Viewer

Changed files:
filename: build/args/all.gn
patch: @@ -72,3 +72,6 @@ enterprise_cloud_content_analysis = false
 
 # We don't use anything from here, and it causes target collisions
 enable_linux_installer = false
+
+# Disable "Save to Drive" feature in PDF viewer
+enable_pdf_save_to_drive = false

filename: shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc
patch: @@ -16,6 +16,7 @@
 #include "components/pdf/common/constants.h"
 #include "components/prefs/pref_service.h"
 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
+#include "pdf/buildflags.h"
 #include "url/url_constants.h"
 
 namespace extensions {
@@ -25,6 +26,8 @@ namespace {
 namespace IsAllowedLocalFileAccess =
     api::pdf_viewer_private::IsAllowedLocalFileAccess;
 
+namespace SaveToDrive = api::pdf_viewer_private::SaveToDrive;
+
 namespace SetPdfPluginAttributes =
     api::pdf_viewer_private::SetPdfPluginAttributes;
 
@@ -107,6 +110,24 @@ PdfViewerPrivateIsAllowedLocalFileAccessFunction::Run() {
       IsUrlAllowedToEmbedLocalFiles(GURL(params->url), base::Value::List())));
 }
 
+PdfViewerPrivateSaveToDriveFunction::PdfViewerPrivateSaveToDriveFunction() =
+    default;
+
+PdfViewerPrivateSaveToDriveFunction::~PdfViewerPrivateSaveToDriveFunction() =
+    default;
+
+ExtensionFunction::ResponseAction PdfViewerPrivateSaveToDriveFunction::Run() {
+#if BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE)
+  std::optional<SaveToDrive::Params> params =
+      SaveToDrive::Params::Create(args());
+  EXTENSION_FUNCTION_VALIDATE(params);
+  // TODO(crbug.com/424208776): Start the save to drive flow.
+  return RespondNow(NoArguments());
+#else
+  return RespondNow(Error("Not supported"));
+#endif  // BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE)
+}
+
 PdfViewerPrivateSetPdfDocumentTitleFunction::
     PdfViewerPrivateSetPdfDocumentTitleFunction() = default;
 

filename: shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h
patch: @@ -46,6 +46,24 @@ class PdfViewerPrivateIsAllowedLocalFileAccessFunction
   ResponseAction Run() override;
 };
 
+class PdfViewerPrivateSaveToDriveFunction : public ExtensionFunction {
+ public:
+  DECLARE_EXTENSION_FUNCTION("pdfViewerPrivate.saveToDrive",
+                             PDFVIEWERPRIVATE_SAVETODRIVE)
+
+  PdfViewerPrivateSaveToDriveFunction();
+  PdfViewerPrivateSaveToDriveFunction(
+      const PdfViewerPrivateSaveToDriveFunction&) = delete;
+  PdfViewerPrivateSaveToDriveFunction& operator=(
+      const PdfViewerPrivateSaveToDriveFunction&) = delete;
+
+ protected:
+  ~PdfViewerPrivateSaveToDriveFunction() override;
+
+  // Override from ExtensionFunction:
+  ResponseAction Run() override;
+};
+
 class PdfViewerPrivateSetPdfDocumentTitleFunction : public ExtensionFunction {
  public:
   DECLARE_EXTENSION_FUNCTION("pdfViewerPrivate.setPdfDocumentTitle",

filename: shell/common/extensions/api/pdf_viewer_private.idl
patch: @@ -6,6 +6,15 @@
 // functionality that the PDF Viewer needs from outside the PDF plugin. This API
 // is exclusively for the PDF Viewer.
 namespace pdfViewerPrivate {
+  // Must match `SaveRequestType` in `pdf/mojom/pdf.mojom` and
+  // `tools/typescript/definitions/pdf_viewer_private.d.ts`.
+  enum SaveRequestType {
+    ANNOTATION,
+    ORIGINAL,
+    EDITED,
+    SEARCHIFIED
+  };
+
   // Nearly identical to mimeHandlerPrivate.StreamInfo, but without a mime type
   // nor a response header field. Those fields are unused by the PDF viewer.
   dictionary StreamInfo {
@@ -52,6 +61,13 @@ namespace pdfViewerPrivate {
         DOMString url,
         IsAllowedLocalFileAccessCallback callback);
 
+    // Sends a request to save the PDF to Google Drive if `saveRequestType` is
+    // set. Otherwise, if `saveRequestType` is not set, the default action is
+    // to cancel the existing upload.
+    static void saveToDrive(
+        optional SaveRequestType saveRequestType,
+        optional VoidCallback callback);
+
     // Sets the current tab title to `title` for a full-page PDF.
     static void setPdfDocumentTitle(
         DOMString title,