chore: add OCR scaffolding to PDF Viewer (#38127)

This commit is contained in:
Shelley Vohr 2023-05-02 10:27:32 +02:00 committed by GitHub
parent c548f8f59e
commit d95f9d2c63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 234 additions and 1 deletions

View file

@ -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"

View file

@ -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 <string>
#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<IsAllowedLocalFileAccess::Params> 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<SetPdfOcrPref::Params> params =
SetPdfOcrPref::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
return RespondNow(WithArguments(false));
}
} // namespace extensions

View file

@ -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_

View file

@ -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()