Commit 48855478 authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[Sync:USS] Fix a crash when getting debugging counters for Bookmarks

BookmarkModelTypeProcessor::GetStatusCountersForDebugging() falsely
assumed the initial sync is done by the time this method is called
which isn't necessarily the case.

The bookmark_tracker_ is only instantiated after initial sync is done.
This caused reported crashes.

This CL address this properly by not emitting counters in that case,
but run the callback directly.

Bug: 516866
Change-Id: I2a6d605154694d8ab0ddb2a1e7d1ea4c31ddb620
Reviewed-on: https://chromium-review.googlesource.com/c/1291410
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601577}
parent 683280ad
...@@ -481,12 +481,14 @@ void BookmarkModelTypeProcessor::AppendNodeAndChildrenForDebugging( ...@@ -481,12 +481,14 @@ void BookmarkModelTypeProcessor::AppendNodeAndChildrenForDebugging(
void BookmarkModelTypeProcessor::GetStatusCountersForDebugging( void BookmarkModelTypeProcessor::GetStatusCountersForDebugging(
StatusCountersCallback callback) { StatusCountersCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(bookmark_tracker_);
syncer::StatusCounters counters; syncer::StatusCounters counters;
counters.num_entries = bookmark_tracker_->TrackedBookmarksCountForDebugging(); if (bookmark_tracker_) {
counters.num_entries_and_tombstones = counters.num_entries =
counters.num_entries + bookmark_tracker_->TrackedBookmarksCountForDebugging();
bookmark_tracker_->TrackedUncommittedTombstonesCountForDebugging(); counters.num_entries_and_tombstones =
counters.num_entries +
bookmark_tracker_->TrackedUncommittedTombstonesCountForDebugging();
}
std::move(callback).Run(syncer::BOOKMARKS, counters); std::move(callback).Run(syncer::BOOKMARKS, counters);
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <string> #include <string>
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_model.h"
...@@ -139,14 +140,21 @@ class BookmarkModelTypeProcessorTest : public testing::Test { ...@@ -139,14 +140,21 @@ class BookmarkModelTypeProcessorTest : public testing::Test {
// TODO(crbug.com/516866): This class assumes model is loaded and sync has // TODO(crbug.com/516866): This class assumes model is loaded and sync has
// started before running tests. We should test other variations (i.e. model // started before running tests. We should test other variations (i.e. model
// isn't loaded yet and/or sync didn't start yet). // isn't loaded yet and/or sync didn't start yet).
processor_.SetFaviconService(&favicon_service_);
}
void SimulateModelReadyToSync() {
processor_.ModelReadyToSync( processor_.ModelReadyToSync(
/*metadata_str=*/std::string(), schedule_save_closure_.Get(), /*metadata_str=*/std::string(), schedule_save_closure_.Get(),
bookmark_model_.get()); bookmark_model_.get());
}
void SimulateOnSyncStarting() {
syncer::DataTypeActivationRequest request; syncer::DataTypeActivationRequest request;
request.cache_guid = kCacheGuid; request.cache_guid = kCacheGuid;
processor_.SetFaviconService(&favicon_service_);
processor_.OnSyncStarting(request, base::DoNothing()); processor_.OnSyncStarting(request, base::DoNothing());
} }
bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_.get(); } bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_.get(); }
BookmarkUndoService* bookmark_undo_service() { BookmarkUndoService* bookmark_undo_service() {
return &bookmark_undo_service_; return &bookmark_undo_service_;
...@@ -167,6 +175,8 @@ class BookmarkModelTypeProcessorTest : public testing::Test { ...@@ -167,6 +175,8 @@ class BookmarkModelTypeProcessorTest : public testing::Test {
}; };
TEST_F(BookmarkModelTypeProcessorTest, ShouldUpdateModelAfterRemoteCreation) { TEST_F(BookmarkModelTypeProcessorTest, ShouldUpdateModelAfterRemoteCreation) {
SimulateModelReadyToSync();
SimulateOnSyncStarting();
syncer::UniquePosition kRandomPosition = syncer::UniquePosition kRandomPosition =
syncer::UniquePosition::InitialPosition( syncer::UniquePosition::InitialPosition(
syncer::UniquePosition::RandomSuffix()); syncer::UniquePosition::RandomSuffix());
...@@ -209,6 +219,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldUpdateModelAfterRemoteUpdate) { ...@@ -209,6 +219,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldUpdateModelAfterRemoteUpdate) {
std::vector<BookmarkInfo> bookmarks = { std::vector<BookmarkInfo> bookmarks = {
{kNodeId, kTitle, kUrl, kBookmarkBarId, /*server_tag=*/std::string()}}; {kNodeId, kTitle, kUrl, kBookmarkBarId, /*server_tag=*/std::string()}};
SimulateModelReadyToSync();
SimulateOnSyncStarting();
InitWithSyncedBookmarks(bookmarks, processor()); InitWithSyncedBookmarks(bookmarks, processor());
// Make sure original bookmark exists. // Make sure original bookmark exists.
...@@ -248,6 +260,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ...@@ -248,6 +260,8 @@ TEST_F(BookmarkModelTypeProcessorTest,
std::vector<BookmarkInfo> bookmarks = { std::vector<BookmarkInfo> bookmarks = {
{kNodeId, kTitle, kUrl, kBookmarkBarId, /*server_tag=*/std::string()}}; {kNodeId, kTitle, kUrl, kBookmarkBarId, /*server_tag=*/std::string()}};
SimulateModelReadyToSync();
SimulateOnSyncStarting();
InitWithSyncedBookmarks(bookmarks, processor()); InitWithSyncedBookmarks(bookmarks, processor());
// Make sure original bookmark exists. // Make sure original bookmark exists.
...@@ -282,6 +296,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldDecodeSyncMetadata) { ...@@ -282,6 +296,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldDecodeSyncMetadata) {
/*parent=*/bookmark_bar_node, /*index=*/0, base::UTF8ToUTF16(kTitle), /*parent=*/bookmark_bar_node, /*index=*/0, base::UTF8ToUTF16(kTitle),
GURL(kUrl)); GURL(kUrl));
SimulateModelReadyToSync();
SimulateOnSyncStarting();
// TODO(crbug.com/516866): Remove this after initial sync done is properly set // TODO(crbug.com/516866): Remove this after initial sync done is properly set
// within the processor. // within the processor.
sync_pb::BookmarkModelMetadata model_metadata; sync_pb::BookmarkModelMetadata model_metadata;
...@@ -329,7 +345,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldDecodeEncodedSyncMetadata) { ...@@ -329,7 +345,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldDecodeEncodedSyncMetadata) {
{kNodeId1, kTitle1, kUrl1, kBookmarkBarId, /*server_tag=*/std::string()}, {kNodeId1, kTitle1, kUrl1, kBookmarkBarId, /*server_tag=*/std::string()},
{kNodeId2, kTitle2, kUrl2, kBookmarkBarId, {kNodeId2, kTitle2, kUrl2, kBookmarkBarId,
/*server_tag=*/std::string()}}; /*server_tag=*/std::string()}};
SimulateModelReadyToSync();
SimulateOnSyncStarting();
InitWithSyncedBookmarks(bookmarks, processor()); InitWithSyncedBookmarks(bookmarks, processor());
std::string metadata_str = processor()->EncodeSyncMetadata(); std::string metadata_str = processor()->EncodeSyncMetadata();
...@@ -397,6 +414,8 @@ TEST_F(BookmarkModelTypeProcessorTest, ...@@ -397,6 +414,8 @@ TEST_F(BookmarkModelTypeProcessorTest,
// key name. // key name.
TEST_F(BookmarkModelTypeProcessorTest, TEST_F(BookmarkModelTypeProcessorTest,
ShouldUpdateModelTypeStateUponHandlingRemoteUpdates) { ShouldUpdateModelTypeStateUponHandlingRemoteUpdates) {
SimulateModelReadyToSync();
SimulateOnSyncStarting();
// Initialize the process to make sure the tracker has been created. // Initialize the process to make sure the tracker has been created.
InitWithSyncedBookmarks({}, processor()); InitWithSyncedBookmarks({}, processor());
const SyncedBookmarkTracker* tracker = processor()->GetTrackerForTest(); const SyncedBookmarkTracker* tracker = processor()->GetTrackerForTest();
...@@ -423,12 +442,16 @@ TEST_F(BookmarkModelTypeProcessorTest, ...@@ -423,12 +442,16 @@ TEST_F(BookmarkModelTypeProcessorTest,
// Verifies that the processor doesn't crash if sync is stopped before receiving // Verifies that the processor doesn't crash if sync is stopped before receiving
// remote updates or tracking metadata. // remote updates or tracking metadata.
TEST_F(BookmarkModelTypeProcessorTest, ShouldStopBeforeReceivingRemoteUpdates) { TEST_F(BookmarkModelTypeProcessorTest, ShouldStopBeforeReceivingRemoteUpdates) {
SimulateModelReadyToSync();
SimulateOnSyncStarting();
ASSERT_THAT(processor()->GetTrackerForTest(), IsNull()); ASSERT_THAT(processor()->GetTrackerForTest(), IsNull());
processor()->OnSyncStopping(syncer::CLEAR_METADATA); processor()->OnSyncStopping(syncer::CLEAR_METADATA);
EXPECT_THAT(processor()->GetTrackerForTest(), IsNull()); EXPECT_THAT(processor()->GetTrackerForTest(), IsNull());
} }
TEST_F(BookmarkModelTypeProcessorTest, ShouldStopAfterReceivingRemoteUpdates) { TEST_F(BookmarkModelTypeProcessorTest, ShouldStopAfterReceivingRemoteUpdates) {
SimulateModelReadyToSync();
SimulateOnSyncStarting();
// Initialize the process to make sure the tracker has been created. // Initialize the process to make sure the tracker has been created.
InitWithSyncedBookmarks({}, processor()); InitWithSyncedBookmarks({}, processor());
ASSERT_THAT(processor()->GetTrackerForTest(), NotNull()); ASSERT_THAT(processor()->GetTrackerForTest(), NotNull());
...@@ -436,6 +459,22 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldStopAfterReceivingRemoteUpdates) { ...@@ -436,6 +459,22 @@ TEST_F(BookmarkModelTypeProcessorTest, ShouldStopAfterReceivingRemoteUpdates) {
EXPECT_THAT(processor()->GetTrackerForTest(), IsNull()); EXPECT_THAT(processor()->GetTrackerForTest(), IsNull());
} }
TEST_F(BookmarkModelTypeProcessorTest,
ShouldReportNoCountersWhenModelIsNotLoaded) {
SimulateOnSyncStarting();
ASSERT_THAT(processor()->GetTrackerForTest(), IsNull());
syncer::StatusCounters status_counters;
// Assign an arbitrary non-zero number to the |num_entries| to be able to
// check that actually a 0 has been written to it later.
status_counters.num_entries = 1000;
processor()->GetStatusCountersForDebugging(
base::BindLambdaForTesting([&](syncer::ModelType model_type,
const syncer::StatusCounters& counters) {
status_counters = counters;
}));
EXPECT_EQ(0u, status_counters.num_entries);
}
} // namespace } // namespace
} // namespace sync_bookmarks } // namespace sync_bookmarks
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment