chore: add OCR scaffolding to PDF Viewer (#38127)
This commit is contained in:
parent
c548f8f59e
commit
d95f9d2c63
10 changed files with 234 additions and 1 deletions
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
|
@ -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_
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -28,6 +28,10 @@
|
|||
"channel": "stable",
|
||||
"matches": ["<all_urls>"]
|
||||
},
|
||||
"pdfViewerPrivate": {
|
||||
"dependencies": ["permission:pdfViewerPrivate"],
|
||||
"contexts": ["blessed_extension"]
|
||||
},
|
||||
"resourcesPrivate": [{
|
||||
"dependencies": ["permission:resourcesPrivate"],
|
||||
"contexts": ["blessed_extension"]
|
||||
|
|
|
@ -6,6 +6,15 @@
|
|||
],
|
||||
"location": "component"
|
||||
},
|
||||
"pdfViewerPrivate": {
|
||||
"channel": "stable",
|
||||
"extension_types": [
|
||||
"extension"
|
||||
],
|
||||
"allowlist": [
|
||||
"CBCC42ABED43A4B58FE3810E62AFFA010EB0349F"
|
||||
]
|
||||
},
|
||||
"management": {
|
||||
"channel": "stable",
|
||||
"extension_types": [
|
||||
|
|
39
shell/common/extensions/api/pdf_viewer_private.idl
Normal file
39
shell/common/extensions/api/pdf_viewer_private.idl
Normal file
|
@ -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 <code>chrome.pdfViewerPrivate</code> 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);
|
||||
};
|
||||
};
|
|
@ -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<const APIPermissionInfo::InitInfo> GetPermissionInfos() {
|
||||
|
|
Loading…
Add table
Reference in a new issue