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

[CodeHealth][rewards] Use base::ScopedObservation pt.2 #25972

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

cdesouza-chromium
Copy link
Collaborator

This PR changes the use of manual raw_ptr for observations, to have then managed by base::ScopedObservation.

Resolves brave/brave-browser#41607

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:

@cdesouza-chromium cdesouza-chromium self-assigned this Oct 14, 2024
@cdesouza-chromium cdesouza-chromium requested a review from a team as a code owner October 14, 2024 11:58
Copy link
Contributor

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

Here's my review of the pull request:

Description

This PR refactors the BraveRewardsNativeWorker and RewardsTabHelper classes to use base::ScopedObservation instead of manually managing observer lifetime. It also removes the raw pointer to RewardsService and replaces it with observations. This change improves the code's safety and reduces the risk of use-after-free bugs.

Changes

Changes

  1. browser/brave_rewards/android/brave_rewards_native_worker.cc:

    • Replaced brave_rewards_service_ raw pointer with rewards_service_observation_ and rewards_notification_service_observation_.
    • Updated all method calls to use rewards_service_observation_.GetSource() instead of brave_rewards_service_.
    • Removed manual observer management in constructor and destructor.
    • Updated various method implementations to check IsObserving() instead of the raw pointer.
  2. browser/brave_rewards/android/brave_rewards_native_worker.h:

    • Removed brave_rewards_service_ raw pointer.
    • Added rewards_service_observation_ and rewards_notification_service_observation_ as member variables.
  3. browser/brave_rewards/rewards_tab_helper.cc:

    • Replaced rewards_service_ raw pointer with rewards_service_observation_.
    • Updated all method calls to use rewards_service_observation_.GetSource() instead of rewards_service_.
    • Removed manual observer management in constructor and destructor.
    • Updated various method implementations to check IsObserving() instead of the raw pointer.
  4. browser/brave_rewards/rewards_tab_helper.h:

    • Removed rewards_service_ raw pointer.
    • Added rewards_service_observation_ as a member variable.

Possible Issues

No major issues identified. The changes appear to be a well-executed refactoring that improves code safety.

Security Hotspots

No significant security hotspots identified. The use of base::ScopedObservation actually improves security by reducing the risk of use-after-free bugs associated with raw pointers.

Overall, this PR appears to be a positive change that improves code safety and maintainability without introducing new risks.

@cdesouza-chromium cdesouza-chromium force-pushed the use-scoped-observation-pt-2 branch 2 times, most recently from c9582c4 to caf7984 Compare October 14, 2024 13:06
This PR changes the use of manual `raw_ptr` for observations, to have
then managed by `base::ScopedObservation`.

Resolves brave/brave-browser#41607
@cdesouza-chromium cdesouza-chromium merged commit e6c7d49 into master Oct 15, 2024
17 checks passed
@cdesouza-chromium cdesouza-chromium deleted the use-scoped-observation-pt-2 branch October 15, 2024 15:03
@github-actions github-actions bot added this to the 1.73.x - Nightly milestone Oct 15, 2024
@brave-builds
Copy link
Collaborator

Released in v1.73.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use base::ScopedObservation where feasible to improve code safety
4 participants