Skip to content

Commit

Permalink
chore: add OCR scaffolding to PDF Viewer (#38127)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored and pull[bot] committed Sep 21, 2023
1 parent 7c4128f commit 3691014
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 1 deletion.
2 changes: 2 additions & 0 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions shell/browser/extensions/api/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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_
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 10 additions & 0 deletions shell/common/extensions/api/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand All @@ -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"

Expand Down
4 changes: 4 additions & 0 deletions shell/common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"channel": "stable",
"matches": ["<all_urls>"]
},
"pdfViewerPrivate": {
"dependencies": ["permission:pdfViewerPrivate"],
"contexts": ["blessed_extension"]
},
"resourcesPrivate": [{
"dependencies": ["permission:resourcesPrivate"],
"contexts": ["blessed_extension"]
Expand Down
9 changes: 9 additions & 0 deletions shell/common/extensions/api/_permission_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
],
"location": "component"
},
"pdfViewerPrivate": {
"channel": "stable",
"extension_types": [
"extension"
],
"allowlist": [
"CBCC42ABED43A4B58FE3810E62AFFA010EB0349F"
]
},
"management": {
"channel": "stable",
"extension_types": [
Expand Down
39 changes: 39 additions & 0 deletions shell/common/extensions/api/pdf_viewer_private.idl
Original file line number Diff line number Diff line change
@@ -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);
};
};
3 changes: 3 additions & 0 deletions shell/common/extensions/electron_extensions_api_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3691014

Please sign in to comment.