diff --git a/filenames.gni b/filenames.gni index ab3b7365c0e4..be6d8e7bcd6b 100644 --- a/filenames.gni +++ b/filenames.gni @@ -697,6 +697,8 @@ filenames = { "shell/browser/extensions/api/management/electron_management_api_delegate.h", "shell/browser/extensions/api/resources_private/resources_private_api.cc", "shell/browser/extensions/api/resources_private/resources_private_api.h", + "shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc", + "shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h", "shell/browser/extensions/api/runtime/electron_runtime_api_delegate.cc", "shell/browser/extensions/api/runtime/electron_runtime_api_delegate.h", "shell/browser/extensions/api/streams_private/streams_private_api.cc", diff --git a/shell/browser/extensions/api/BUILD.gn b/shell/browser/extensions/api/BUILD.gn index bb19064bed01..7c9b0c7348ac 100644 --- a/shell/browser/extensions/api/BUILD.gn +++ b/shell/browser/extensions/api/BUILD.gn @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//electron/buildflags/buildflags.gni") import("//extensions/buildflags/buildflags.gni") import("//tools/json_schema_compiler/json_schema_api.gni") @@ -14,6 +15,12 @@ function_registration("api_registration") { "//electron/shell/common/extensions/api/resources_private.idl", "//electron/shell/common/extensions/api/tabs.json", ] + + if (enable_pdf_viewer) { + sources += + [ "//electron/shell/common/extensions/api/pdf_viewer_private.idl" ] + } + impl_dir = "//electron/shell/browser/extensions/api" configs = [ "//build/config:precompiled_headers" ] bundle_name = "Electron" diff --git a/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc b/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc new file mode 100644 index 000000000000..a7dce3c231ec --- /dev/null +++ b/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc @@ -0,0 +1,90 @@ +// Copyright 2022 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h" + +#include + +#include "base/values.h" +#include "chrome/common/extensions/api/pdf_viewer_private.h" +#include "chrome/common/pref_names.h" +#include "components/prefs/pref_service.h" +#include "shell/browser/electron_browser_context.h" +#include "url/url_constants.h" + +namespace extensions { + +namespace { + +namespace IsAllowedLocalFileAccess = + api::pdf_viewer_private::IsAllowedLocalFileAccess; + +namespace SetPdfOcrPref = api::pdf_viewer_private::SetPdfOcrPref; + +// Check if the current URL is allowed based on a list of allowlisted domains. +bool IsUrlAllowedToEmbedLocalFiles( + const GURL& current_url, + const base::Value::List& allowlisted_domains) { + if (!current_url.is_valid() || !current_url.SchemeIs(url::kHttpsScheme)) { + return false; + } + + for (auto const& value : allowlisted_domains) { + const std::string* domain = value.GetIfString(); + if (!domain) { + continue; + } + + if (current_url.DomainIs(*domain)) { + return true; + } + } + return false; +} + +} // namespace + +PdfViewerPrivateIsAllowedLocalFileAccessFunction:: + PdfViewerPrivateIsAllowedLocalFileAccessFunction() = default; + +PdfViewerPrivateIsAllowedLocalFileAccessFunction:: + ~PdfViewerPrivateIsAllowedLocalFileAccessFunction() = default; + +ExtensionFunction::ResponseAction +PdfViewerPrivateIsAllowedLocalFileAccessFunction::Run() { + absl::optional params = + IsAllowedLocalFileAccess::Params::Create(args()); + EXTENSION_FUNCTION_VALIDATE(params); + + return RespondNow(WithArguments( + IsUrlAllowedToEmbedLocalFiles(GURL(params->url), base::Value::List()))); +} + +PdfViewerPrivateIsPdfOcrAlwaysActiveFunction:: + PdfViewerPrivateIsPdfOcrAlwaysActiveFunction() = default; + +PdfViewerPrivateIsPdfOcrAlwaysActiveFunction:: + ~PdfViewerPrivateIsPdfOcrAlwaysActiveFunction() = default; + +// TODO(codebytere): enable when https://crbug.com/1393069 works properly. +ExtensionFunction::ResponseAction +PdfViewerPrivateIsPdfOcrAlwaysActiveFunction::Run() { + return RespondNow(WithArguments(false)); +} + +PdfViewerPrivateSetPdfOcrPrefFunction::PdfViewerPrivateSetPdfOcrPrefFunction() = + default; + +PdfViewerPrivateSetPdfOcrPrefFunction:: + ~PdfViewerPrivateSetPdfOcrPrefFunction() = default; + +// TODO(codebytere): enable when https://crbug.com/1393069 works properly. +ExtensionFunction::ResponseAction PdfViewerPrivateSetPdfOcrPrefFunction::Run() { + absl::optional params = + SetPdfOcrPref::Params::Create(args()); + EXTENSION_FUNCTION_VALIDATE(params); + return RespondNow(WithArguments(false)); +} + +} // namespace extensions diff --git a/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h b/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h new file mode 100644 index 000000000000..f2e1401e7601 --- /dev/null +++ b/shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h @@ -0,0 +1,69 @@ +// Copyright 2022 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ELECTRON_SHELL_BROWSER_EXTENSIONS_API_PDF_VIEWER_PRIVATE_PDF_VIEWER_PRIVATE_API_H_ +#define ELECTRON_SHELL_BROWSER_EXTENSIONS_API_PDF_VIEWER_PRIVATE_PDF_VIEWER_PRIVATE_API_H_ + +#include "extensions/browser/extension_function.h" + +namespace extensions { + +class PdfViewerPrivateIsAllowedLocalFileAccessFunction + : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("pdfViewerPrivate.isAllowedLocalFileAccess", + PDFVIEWERPRIVATE_ISALLOWEDLOCALFILEACCESS) + + PdfViewerPrivateIsAllowedLocalFileAccessFunction(); + PdfViewerPrivateIsAllowedLocalFileAccessFunction( + const PdfViewerPrivateIsAllowedLocalFileAccessFunction&) = delete; + PdfViewerPrivateIsAllowedLocalFileAccessFunction& operator=( + const PdfViewerPrivateIsAllowedLocalFileAccessFunction&) = delete; + + protected: + ~PdfViewerPrivateIsAllowedLocalFileAccessFunction() override; + + // Override from ExtensionFunction: + ResponseAction Run() override; +}; + +class PdfViewerPrivateIsPdfOcrAlwaysActiveFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("pdfViewerPrivate.isPdfOcrAlwaysActive", + PDFVIEWERPRIVATE_ISPDFOCRALWAYSACTIVE) + + PdfViewerPrivateIsPdfOcrAlwaysActiveFunction(); + PdfViewerPrivateIsPdfOcrAlwaysActiveFunction( + const PdfViewerPrivateIsPdfOcrAlwaysActiveFunction&) = delete; + PdfViewerPrivateIsPdfOcrAlwaysActiveFunction& operator=( + const PdfViewerPrivateIsPdfOcrAlwaysActiveFunction&) = delete; + + protected: + ~PdfViewerPrivateIsPdfOcrAlwaysActiveFunction() override; + + // Override from ExtensionFunction: + ResponseAction Run() override; +}; + +class PdfViewerPrivateSetPdfOcrPrefFunction : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("pdfViewerPrivate.setPdfOcrPref", + PDFVIEWERPRIVATE_SETPDFOCRPREF) + + PdfViewerPrivateSetPdfOcrPrefFunction(); + PdfViewerPrivateSetPdfOcrPrefFunction( + const PdfViewerPrivateSetPdfOcrPrefFunction&) = delete; + PdfViewerPrivateSetPdfOcrPrefFunction& operator=( + const PdfViewerPrivateSetPdfOcrPrefFunction&) = delete; + + protected: + ~PdfViewerPrivateSetPdfOcrPrefFunction() override; + + // Override from ExtensionFunction: + ResponseAction Run() override; +}; + +} // namespace extensions + +#endif // ELECTRON_SHELL_BROWSER_EXTENSIONS_API_PDF_VIEWER_PRIVATE_PDF_VIEWER_PRIVATE_API_H_ diff --git a/shell/browser/extensions/api/resources_private/resources_private_api.cc b/shell/browser/extensions/api/resources_private/resources_private_api.cc index 101293316faf..d834dd993160 100644 --- a/shell/browser/extensions/api/resources_private/resources_private_api.cc +++ b/shell/browser/extensions/api/resources_private/resources_private_api.cc @@ -25,7 +25,7 @@ // To add a new component to this API, simply: // 1. Add your component to the Component enum in -// chrome/common/extensions/api/resources_private.idl +// shell/common/extensions/api/resources_private.idl // 2. Create an AddStringsForMyComponent(base::Value::Dict* dict) method. // 3. Tie in that method to the switch statement in Run() diff --git a/shell/common/extensions/api/BUILD.gn b/shell/common/extensions/api/BUILD.gn index 717ec0b29ee5..013edcdba2a2 100644 --- a/shell/common/extensions/api/BUILD.gn +++ b/shell/common/extensions/api/BUILD.gn @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//electron/buildflags/buildflags.gni") import("//extensions/buildflags/buildflags.gni") import("//tools/json_schema_compiler/json_features.gni") import("//tools/json_schema_compiler/json_schema_api.gni") @@ -41,6 +42,10 @@ generated_json_strings("generated_api_json_strings") { "tabs.json", ] + if (enable_pdf_viewer) { + sources += [ "pdf_viewer_private.idl" ] + } + configs = [ "//build/config:precompiled_headers" ] bundle_name = "Electron" schema_include_rules = "extensions/common/api:extensions::api::%(namespace)s" @@ -56,6 +61,11 @@ generated_types("generated_api_types") { "resources_private.idl", "tabs.json", ] + + if (enable_pdf_viewer) { + sources += [ "pdf_viewer_private.idl" ] + } + configs = [ "//build/config:precompiled_headers" ] schema_include_rules = "extensions/common/api:extensions::api::%(namespace)s" diff --git a/shell/common/extensions/api/_api_features.json b/shell/common/extensions/api/_api_features.json index 6a8d18fa8564..2ee99309a6fa 100644 --- a/shell/common/extensions/api/_api_features.json +++ b/shell/common/extensions/api/_api_features.json @@ -28,6 +28,10 @@ "channel": "stable", "matches": [""] }, + "pdfViewerPrivate": { + "dependencies": ["permission:pdfViewerPrivate"], + "contexts": ["blessed_extension"] + }, "resourcesPrivate": [{ "dependencies": ["permission:resourcesPrivate"], "contexts": ["blessed_extension"] diff --git a/shell/common/extensions/api/_permission_features.json b/shell/common/extensions/api/_permission_features.json index ecd8f074f6e4..01bf003ec902 100644 --- a/shell/common/extensions/api/_permission_features.json +++ b/shell/common/extensions/api/_permission_features.json @@ -6,6 +6,15 @@ ], "location": "component" }, + "pdfViewerPrivate": { + "channel": "stable", + "extension_types": [ + "extension" + ], + "allowlist": [ + "CBCC42ABED43A4B58FE3810E62AFFA010EB0349F" + ] + }, "management": { "channel": "stable", "extension_types": [ diff --git a/shell/common/extensions/api/pdf_viewer_private.idl b/shell/common/extensions/api/pdf_viewer_private.idl new file mode 100644 index 000000000000..67eaab116f44 --- /dev/null +++ b/shell/common/extensions/api/pdf_viewer_private.idl @@ -0,0 +1,39 @@ +// Copyright 2022 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Use the chrome.pdfViewerPrivate API for specific browser +// functionality that the PDF Viewer needs from outside the PDF plugin. This API +// is exclusively for the PDF Viewer. +namespace pdfViewerPrivate { + callback IsAllowedLocalFileAccessCallback = void(boolean result); + callback IsPdfOcrAlwaysActiveCallback = void(boolean result); + callback OnPdfOcrPrefSetCallback = void(boolean result); + + interface Functions { + // Determines if the given URL should be allowed to access local files from + // the PDF Viewer. |callback|: Called with true if URL should be allowed to + // access local files from the PDF Viewer, false otherwise. + [supportsPromises] static void isAllowedLocalFileAccess( + DOMString url, + IsAllowedLocalFileAccessCallback callback); + + // Determines if the preference for PDF OCR is set to run PDF OCR always. + // |callback|: Called with true if PDF OCR is set to be always active; + // false otherwise. + [supportsPromises] static void isPdfOcrAlwaysActive( + IsPdfOcrAlwaysActiveCallback callback); + + // Sets a pref value for PDF OCR. + // |value|: The new value of the pref. + // |callback|: The callback for whether the pref was set or not. + [supportsPromises] static void setPdfOcrPref( + boolean value, OnPdfOcrPrefSetCallback callback); + }; + + interface Events { + // Fired when a pref value for PDF OCR has changed. + // |value| The pref value that changed. + static void onPdfOcrPrefChanged(boolean value); + }; +}; diff --git a/shell/common/extensions/electron_extensions_api_provider.cc b/shell/common/extensions/electron_extensions_api_provider.cc index 18309c03de00..a5aea27f0d77 100644 --- a/shell/common/extensions/electron_extensions_api_provider.cc +++ b/shell/common/extensions/electron_extensions_api_provider.cc @@ -34,6 +34,9 @@ constexpr APIPermissionInfo::InitInfo permissions_to_register[] = { APIPermissionInfo::kFlagInternal}, {mojom::APIPermissionID::kResourcesPrivate, "resourcesPrivate", APIPermissionInfo::kFlagCannotBeOptional}, +#if BUILDFLAG(ENABLE_PDF_VIEWER) + {mojom::APIPermissionID::kPdfViewerPrivate, "pdfViewerPrivate"}, +#endif {mojom::APIPermissionID::kManagement, "management"}, }; base::span GetPermissionInfos() {