Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ScreenAI] Adds an item for Pdf Ocr to the context menu.
An item with two submenu items is added to the context menu. This context menu item is visible to the user when they open the menu on the pdf viewer. A screenshot of this context menu can be found in the following link: https://screenshot.googleplex.com/9qntRNjXWkEmgkM.png. Also, when "Always" is selected, it will show a checked item with text, "Convert image to text", on the context menu as depicted in the following link: https://screenshot.googleplex.com/6VtUSeRGqx7p7XB.png. AX-Relnotes: n/a Bug: 1393069 Change-Id: I227c09478aa9df62f71d0309889b45eda5f1aacc Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4061959 Reviewed-by: Ramin Halavati <rhalavati@chromium.org> Reviewed-by: Tommy Nyquist <nyquist@chromium.org> Reviewed-by: Ted Choc <tedchoc@chromium.org> Commit-Queue: Kyungjun Lee <kyungjunlee@google.com> Cr-Commit-Position: refs/heads/main@{#1084543}
- Loading branch information
Kyungjun Lee
authored and
Chromium LUCI CQ
committed
Dec 16, 2022
1 parent
9fd281c
commit 0dcd7c3
Showing
21 changed files
with
409 additions
and
55 deletions.
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
1 change: 1 addition & 0 deletions
1
chrome/app/generated_resources_grd/IDS_CONTENT_CONTEXT_PDF_OCR_MENU_OPTION.png.sha1
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 @@ | ||
de02f6a2f36ad4994dbee7b7775b7dd264249365 |
1 change: 1 addition & 0 deletions
1
chrome/app/generated_resources_grd/IDS_CONTENT_CONTEXT_PDF_OCR_MENU_OPTION_ALWAYS.png.sha1
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 @@ | ||
12a00036bde11df37a6c08ef9d9a4a3f60e5aa7d |
1 change: 1 addition & 0 deletions
1
chrome/app/generated_resources_grd/IDS_CONTENT_CONTEXT_PDF_OCR_MENU_OPTION_ONCE.png.sha1
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 @@ | ||
6101413baa79ef7b1d10990bb533dd704b27003b |
1 change: 0 additions & 1 deletion
1
chrome/app/generated_resources_grd/IDS_CONTENT_CONTEXT_RUN_PDF_OCR.png.sha1
This file was deleted.
Oops, something went wrong.
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
107 changes: 107 additions & 0 deletions
107
chrome/browser/renderer_context_menu/pdf_ocr_menu_observer.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,107 @@ | ||
// 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 "chrome/browser/renderer_context_menu/pdf_ocr_menu_observer.h" | ||
|
||
#include "chrome/app/chrome_command_ids.h" | ||
#include "chrome/browser/accessibility/accessibility_state_utils.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "content/public/browser/browser_thread.h" | ||
#include "content/public/browser/context_menu_params.h" | ||
#include "ui/accessibility/accessibility_features.h" | ||
|
||
using content::BrowserThread; | ||
|
||
namespace { | ||
|
||
// Whether the PDF OCR menu item should be shown in the menu. It now depends on | ||
// whether a screen reader is running. | ||
bool ShouldShowPdfOcrMenuItem() { | ||
return accessibility_state_utils::IsScreenReaderEnabled() && | ||
features::IsPdfOcrEnabled(); | ||
} | ||
|
||
} // namespace | ||
|
||
PdfOcrMenuObserver::PdfOcrMenuObserver(RenderViewContextMenuProxy* proxy) | ||
: proxy_(proxy) {} | ||
|
||
PdfOcrMenuObserver::~PdfOcrMenuObserver() = default; | ||
|
||
void PdfOcrMenuObserver::InitMenu(const content::ContextMenuParams& params) { | ||
Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | ||
DCHECK(profile != nullptr); | ||
if (ShouldShowPdfOcrMenuItem()) { | ||
proxy_->AddPdfOcrMenuItem(profile->GetPrefs()->GetBoolean( | ||
prefs::kAccessibilityPdfOcrAlwaysActive)); | ||
} | ||
} | ||
|
||
bool PdfOcrMenuObserver::IsCommandIdSupported(int command_id) { | ||
return command_id == IDC_CONTENT_CONTEXT_PDF_OCR || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ALWAYS || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ONCE; | ||
} | ||
|
||
bool PdfOcrMenuObserver::IsCommandIdChecked(int command_id) { | ||
DCHECK(IsCommandIdSupported(command_id)); | ||
Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | ||
DCHECK(profile != nullptr); | ||
if (command_id == IDC_CONTENT_CONTEXT_PDF_OCR || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ALWAYS || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ONCE) { | ||
return profile->GetPrefs()->GetBoolean( | ||
prefs::kAccessibilityPdfOcrAlwaysActive); | ||
} | ||
return false; | ||
} | ||
|
||
bool PdfOcrMenuObserver::IsCommandIdEnabled(int command_id) { | ||
DCHECK(IsCommandIdSupported(command_id)); | ||
if (command_id == IDC_CONTENT_CONTEXT_PDF_OCR || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ALWAYS || | ||
command_id == IDC_CONTENT_CONTEXT_PDF_OCR_ONCE) { | ||
return ShouldShowPdfOcrMenuItem(); | ||
} | ||
return false; | ||
} | ||
|
||
void PdfOcrMenuObserver::ExecuteCommand(int command_id) { | ||
DCHECK(IsCommandIdSupported(command_id)); | ||
Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | ||
DCHECK(profile != nullptr); | ||
bool is_always_active = | ||
profile->GetPrefs()->GetBoolean(prefs::kAccessibilityPdfOcrAlwaysActive); | ||
switch (command_id) { | ||
case IDC_CONTENT_CONTEXT_PDF_OCR: | ||
// If the user has selected to make PDF OCR always active, we directly | ||
// update the profile and change it to the original menu item when the | ||
// user disables this item. | ||
DCHECK(is_always_active); | ||
profile->GetPrefs()->SetBoolean(prefs::kAccessibilityPdfOcrAlwaysActive, | ||
false); | ||
// TODO(crbug.com/1393069): Stop the PDF OCR if running. | ||
NOTIMPLEMENTED() << "Need to stop PDF OCR accordingly"; | ||
break; | ||
case IDC_CONTENT_CONTEXT_PDF_OCR_ALWAYS: | ||
// When a user choose "Always" to run the PDF OCR, we save this | ||
// preference and change this item to a check item in the context menu. | ||
if (!is_always_active) { | ||
profile->GetPrefs()->SetBoolean(prefs::kAccessibilityPdfOcrAlwaysActive, | ||
true); | ||
// TODO(crbug.com/1393069): Start the PDF OCR if true is set. | ||
NOTIMPLEMENTED() << "Need to start PDF OCR accordingly"; | ||
} | ||
break; | ||
case IDC_CONTENT_CONTEXT_PDF_OCR_ONCE: | ||
// TODO(crbug.com/1393069): Run PDF OCR once to convert image to text. | ||
NOTIMPLEMENTED() << "Need to run PDF OCR once to convert image to text"; | ||
break; | ||
default: | ||
NOTREACHED(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
chrome/browser/renderer_context_menu/pdf_ocr_menu_observer.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,41 @@ | ||
// 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 CHROME_BROWSER_RENDERER_CONTEXT_MENU_PDF_OCR_MENU_OBSERVER_H_ | ||
#define CHROME_BROWSER_RENDERER_CONTEXT_MENU_PDF_OCR_MENU_OBSERVER_H_ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "components/prefs/pref_member.h" | ||
#include "components/renderer_context_menu/render_view_context_menu_observer.h" | ||
|
||
class RenderViewContextMenuProxy; | ||
|
||
// An observer that listens to events from the RenderViewContextMenu class and | ||
// shows the PDF OCR menu if a screen reader is enabled. | ||
class PdfOcrMenuObserver : public RenderViewContextMenuObserver { | ||
public: | ||
explicit PdfOcrMenuObserver(RenderViewContextMenuProxy* proxy); | ||
|
||
PdfOcrMenuObserver(const PdfOcrMenuObserver&) = delete; | ||
PdfOcrMenuObserver& operator=(const PdfOcrMenuObserver&) = delete; | ||
|
||
~PdfOcrMenuObserver() override; | ||
|
||
// RenderViewContextMenuObserver implementation. | ||
void InitMenu(const content::ContextMenuParams& params) override; | ||
bool IsCommandIdSupported(int command_id) override; | ||
bool IsCommandIdChecked(int command_id) override; | ||
bool IsCommandIdEnabled(int command_id) override; | ||
void ExecuteCommand(int command_id) override; | ||
|
||
private: | ||
// The interface to add a context-menu item and update it. This class uses | ||
// this interface to avoid accessing context-menu items directly. | ||
raw_ptr<RenderViewContextMenuProxy> proxy_; | ||
}; | ||
|
||
#endif // CHROME_BROWSER_RENDERER_CONTEXT_MENU_PDF_OCR_MENU_OBSERVER_H_ |
Oops, something went wrong.