Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds screenshot options to context menu and commands #27083

Open
wants to merge 49 commits into
base: master
Choose a base branch
from

Conversation

jonathansampson
Copy link
Contributor

@jonathansampson jonathansampson commented Dec 21, 2024

Resolves brave/brave-browser#25816
Security/Privacy Review: https://github.com/brave/reviews/issues/1831

screenshot-context-menu.mp4

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

  1. Right-click on a page
  2. Click Screenshot Tools from the context menu
  3. Select an option (i.e., Selection, Viewport, Full page) from the submenu

Once a screenshot has been taken, you should see a UI element (with a preview of your screenshot) informing you that the screenshot has been copied to your device's clipboard.

@jonathansampson jonathansampson added enhancement CI/skip-android Do not run CI builds for Android CI/skip-ios Do not run CI builds for iOS labels Dec 21, 2024
@jonathansampson jonathansampson self-assigned this Dec 21, 2024
@github-actions github-actions bot added the CI/run-upstream-tests Run upstream unit and browser tests on Linux and Windows (otherwise only on Linux) label Dec 21, 2024
@jonathansampson jonathansampson marked this pull request as ready for review December 23, 2024 04:48
@jonathansampson jonathansampson requested review from a team and bridiver as code owners December 23, 2024 04:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson marked this pull request as draft December 26, 2024 22:48
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch 2 times, most recently from 0e69a8a to 4b7451d Compare December 28, 2024 23:18
@jonathansampson jonathansampson marked this pull request as ready for review December 29, 2024 02:58
@jonathansampson
Copy link
Contributor Author

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

@bridiver
Copy link
Collaborator

bridiver commented Jan 2, 2025

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

Make sure you follow the latest updates using TabInterface and not WebContentsUserData

@jonathansampson
Copy link
Contributor Author

I've refactored this into a TabHelper pattern, with deferred attachment (until user requests a screenshot). Commits landing here in a bit.

Make sure you follow the latest updates using TabInterface and not WebContentsUserData

Looking into that now. Thanks!

@jonathansampson jonathansampson marked this pull request as draft January 2, 2025 21:17
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch 2 times, most recently from 22868e1 to fdd6104 Compare January 4, 2025 17:34
@github-actions github-actions bot added the CI/run-network-audit Run network-audit label Jan 4, 2025
Copy link
Contributor

@fallaciousreasoning fallaciousreasoning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % nits

@jonathansampson jonathansampson marked this pull request as ready for review January 7, 2025 14:04
Remove general "NotifyUser" method and use explicit methods. Remove redundancy in tab feature logic, now passing a callback to the fullscreen page method so that all methods call a single OnComplete when they finish.
By using a browser reference we can avoid the reliance on global methods to look up a window from a given webcontents instance.
Removed the helper "TakeScreenshot" method which originally existed to stand up and instance of the helper. Now that the helper is a proper tab feature, we can use it as such from context menu commands and elsewhere.
If there is nothing selected, we shouldn't attempt to construct an autocomplete match, run the autocomplete classifier, etc. Furthermore, this enables us to simplify tests involving the context menu due to reduced setup.
@jonathansampson jonathansampson force-pushed the sampson-brave-screenshots branch from f8c8d02 to 6d39b1e Compare January 15, 2025 17:02
After rebasing, there is now a failure in this PR unrelated to the contents or changes introduced by the PR itself. A fresh build from scratch will resolve this, but a component build will proceed if any change is made to the policy template file. As such, a typo has been found and fixed so as to 1) Improve the comments, and importantly 2) Fix the build 😀 See https://bravesoftware.slack.com/archives/C7VLGSR55/p1736928018175489?thread_ts=1736911378.517619&cid=C7VLGSR55 for more info.
Copy link
Contributor

[puLL-Merge] - brave/brave-core@27083

Description

This PR introduces a new screenshot feature for the Brave browser. It adds functionality to capture screenshots of selected regions, the viewport, or full pages, and copy them to the clipboard. The feature is implemented as a set of new commands in the browser's context menu and is controlled by a feature flag.

Changes

Changes

  1. app/brave_command_ids.h:

    • Added new command IDs for screenshot tools.
  2. app/brave_generated_resources.grd and app/brave_screenshots_strings.grdp:

    • Added new string resources for screenshot menu items.
  3. browser/about_flags.cc:

    • Added a new flag for enabling the screenshot feature.
  4. browser/brave_screenshots/:

    • Added new files implementing the screenshot functionality:
      • features.cc/h: Defines the feature flag.
      • screenshots_tab_feature.cc/h: Implements the main screenshot functionality.
      • screenshots_utils.cc/h: Utility functions for screenshot operations.
      • strategies/: Various screenshot capture strategies (fullpage, selection, viewport).
  5. browser/ui/brave_browser_command_controller.cc:

    • Added support for new screenshot commands.
  6. browser/ui/browser_commands.cc/h:

    • Added a new TakeScreenshot function.
  7. chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc/h:

    • Modified to include the new screenshot menu items in the context menu.
  8. chromium_src/chrome/browser/ui/tabs/public/tab_features.h and chromium_src/chrome/browser/ui/tabs/tab_features.cc:

    • Added support for the new screenshot feature in tab features.
  9. test/BUILD.gn:

    • Added unit tests for the new screenshot feature.
sequenceDiagram
    participant User
    participant ContextMenu
    participant BraveScreenshotsTabFeature
    participant ScreenshotStrategy
    participant WebContents
    participant Clipboard

    User->>ContextMenu: Select screenshot option
    ContextMenu->>BraveScreenshotsTabFeature: StartScreenshot
    BraveScreenshotsTabFeature->>ScreenshotStrategy: Create strategy
    ScreenshotStrategy->>WebContents: Capture screenshot
    WebContents-->>ScreenshotStrategy: Return image
    ScreenshotStrategy-->>BraveScreenshotsTabFeature: OnCaptureComplete
    BraveScreenshotsTabFeature->>Clipboard: Copy image
    BraveScreenshotsTabFeature->>User: Display screenshot bubble
Loading

Possible Issues

  1. The feature is currently limited to desktop platforms (Windows, Mac, Linux).
  2. There might be performance concerns for capturing very large or complex pages.
  3. The feature may not work correctly in all scenarios, such as with certain web technologies or in specific browser states.

Security Hotspots

  1. fullpage_strategy.cc: The use of DevTools protocol for capturing full-page screenshots could potentially expose sensitive information if not properly secured.
  2. screenshots_utils.cc: Writing to the clipboard could potentially be a vector for data exfiltration if not properly handled.

]

deps = [
"//chrome/browser/image_editor:image_editor",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be just //chrome/browser/image_editor same as "//chrome/browser/ui and //ui/base/clipboard

~BraveScreenshotsTabFeature();

void StartScreenshot(Browser* browser, ScreenshotType type);
void OnCaptureComplete(const image_editor::ScreenshotCaptureResult& result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be public.

Comment on lines +19 to +24
// Some screenshots may need to be clipped to avoid the GPU limit.
// See https://crbug.com/1260828. When this happens, we may wish to notify the
// user that only a portion of their page could be captured.
void DisplayScreenshotClippedNotification(base::WeakPtr<Browser> browser) {
NOTIMPLEMENTED();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we create an GitHub issue as follow-up so we don't forget?

Comment on lines +35 to +37
if (strategy_) {
strategy_.reset();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be necessary, it will get destructed along with destructor.

Comment on lines +50 to +52
if (strategy_) {
strategy_.reset();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the new unique_ptr gets move into it in the next few lines, the old one will be destroyed so we don't need to explicitly reset it here.

Comment on lines +57 to +60
if (!strategy_) {
OnCaptureComplete({});
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't happen because there is no nullptr returned in CreateStrategy

Comment on lines +55 to +56
base::Value::Dict params; // No parameters needed
SendDevToolsCommand("Page.getLayoutMetrics", std::move(params), next_id_++);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be inlined in function call as base::Value::Dict()

content::ContextMenuParams params;
params.page_url = GURL("devtools://devtools");

auto context_menu = CreateContextMenu(GetWebContents(), params, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is is_pwa_browser true in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/run-network-audit Run network-audit CI/run-upstream-tests Run upstream unit and browser tests on Linux and Windows (otherwise only on Linux) CI/skip-android Do not run CI builds for Android CI/skip-ios Do not run CI builds for iOS enhancement puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include "Capture Screenshot" option in the context menu
4 participants