-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add OCR scaffolding to PDF Viewer (#38127)
- Loading branch information
1 parent
7c4128f
commit 3691014
Showing
10 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
69 changes: 69 additions & 0 deletions
69
shell/browser/extensions/api/pdf_viewer_private/pdf_viewer_private_api.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters