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

Migrate sidebar item pref for built-in items which do not specify built-in-item-type #14813

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

petemill
Copy link
Member

@petemill petemill commented Aug 24, 2022

Avoid crash by not trying to load them, but also migrate these items to include the item type key, bringing back the function to find built-in item type from url.

This crash was occuring on Nightly for some users who had modified sidebar preferences close to the time that it was initially released and not modified the preferences since.

Resolves brave/brave-browser#24965
Additionally, from #14800:
Resolves https://github.com/brave/internal/issues/899
Resolves https://github.com/brave/internal/issues/900
Resolves https://github.com/brave/internal/issues/901

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • 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, npm run lint, 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:

On any profile, open the Preferences file in a code editor, and replace the "brave.sidebar" section with the following:

    "sidebar": {
      "sidebar_items": [
        {
          "open_in_panel": true,
          "title": "Brave Together",
          "type": 0,
          "url": "https://together.brave.com/"
        },
        {
          "open_in_panel": false,
          "title": "Crypto Wallets",
          "type": 0,
          "url": "chrome://wallet/"
        },
        {
          "open_in_panel": true,
          "title": "Bookmarks",
          "type": 0,
          "url": "chrome://bookmarks/"
        },
        {
          "open_in_panel": true,
          "title": "History",
          "type": 0,
          "url": "chrome://history/"
        },
        {
          "open_in_panel": false,
          "title": "Settings - About Brave",
          "type": 1,
          "url": "chrome://settings/help"
        }
      ]
    },

@petemill petemill self-assigned this Aug 24, 2022
@petemill petemill force-pushed the fix-crash-sidebar-old-pref-format branch from 6e62806 to 44635a4 Compare August 24, 2022 21:32
Comment on lines 410 to 418
DCHECK(iter != default_items.end());
if (iter == default_items.end()) {
// This should not happen, since |LoadSidebarItems| ignores items which
// aren't included in |GetDefaultSidebarItems|.
LOG(ERROR) << "Bad built-in item loaded, cannot match to : "
<< static_cast<int>(added_item.type);
} else {
default_items.erase(iter);
}
Copy link
Member Author

@petemill petemill Aug 24, 2022

Choose a reason for hiding this comment

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

This is where the crash was, I would guess. It shouldn't get here anymore because of the fix earlier in the stack, but it's better to keep a check here.

@@ -421,6 +421,64 @@ TEST_F(SidebarServiceTest, MigratePrefSidebarBuiltInItemsNoneHidden) {
EXPECT_EQ(3, index);
}

// Verify service has migrated the previous pref format where built-in items
// had url stored and not built-in-item-type.
TEST_F(SidebarServiceTest, MigratePrefSidebarBuiltInItemsNoType) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I verified that this new test fails with the version of SidebarService previous to this PR, and passes with this PR.

@petemill petemill force-pushed the fix-crash-sidebar-old-pref-format branch 3 times, most recently from 44b7b7b to 4bc5b60 Compare August 24, 2022 21:45
Comment on lines +478 to 463
if (!built_in_type_value.has_value()) {
VLOG(1) << "built-in item did not have a type: "
<< item.DebugString();
continue;
Copy link
Member Author

Choose a reason for hiding this comment

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

These lines solve the issue since we should skip parsing the item if we have bad data. The previous version of this line was missing the continue. Plus it isn't immediately obvious that:

if (const auto value =
                item.FindIntKey(kSidebarItemBuiltInItemTypeKey)

is the same as:

        const auto value = item.FindIntKey(kSidebarItemBuiltInItemTypeKey);
        if (!value.has_value()) 

components/sidebar/sidebar_service.cc Outdated Show resolved Hide resolved
components/sidebar/sidebar_service_unittest.cc Outdated Show resolved Hide resolved
components/sidebar/sidebar_service_unittest.cc Outdated Show resolved Hide resolved
components/sidebar/sidebar_service_unittest.cc Outdated Show resolved Hide resolved
components/sidebar/sidebar_service.cc Show resolved Hide resolved
components/sidebar/sidebar_service.cc Outdated Show resolved Hide resolved
components/sidebar/sidebar_service.cc Show resolved Hide resolved
Copy link
Member

@simonhong simonhong left a comment

Choose a reason for hiding this comment

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

++

components/sidebar/sidebar_service.cc Outdated Show resolved Hide resolved
continue;
}
auto id =
static_cast<SidebarItem::BuiltInItemType>(*built_in_type_value);
auto iter = std::find_if(default_items_to_add.begin(),
Copy link
Member

Choose a reason for hiding this comment

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

base::ranges::find_if

components/sidebar/sidebar_service.cc Outdated Show resolved Hide resolved
@petemill petemill force-pushed the fix-crash-sidebar-old-pref-format branch from 4bc5b60 to 5f33f62 Compare August 25, 2022 06:19
@petemill
Copy link
Member Author

I have addressed all feedback and rebased on #14800 so that it is ready. We can even merge all together in this PR for most optimized CI and merge and easy uplift.

@petemill petemill dismissed cdesouza-chromium’s stale review August 25, 2022 06:21

addressed and rebased

Copy link
Collaborator

@cdesouza-chromium cdesouza-chromium left a comment

Choose a reason for hiding this comment

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

LGTM

…lt-in-item-type

Avoid crash by not trying to load them, but also migrate these items to include the item type key
@cdesouza-chromium cdesouza-chromium force-pushed the fix-crash-sidebar-old-pref-format branch from 5f33f62 to dd2c242 Compare August 25, 2022 10:34
@bridiver bridiver merged commit ba48f61 into master Aug 25, 2022
@bridiver bridiver deleted the fix-crash-sidebar-old-pref-format branch August 25, 2022 14:23
@github-actions github-actions bot added this to the 1.45.x - Nightly milestone Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Browser crashes at startup if sidebar pref has old format
4 participants