diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-10 22:03:06 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-10 22:03:06 +0000 |
commit | 3b361fc4b7a0dbffabf98d7c89b814b3e15f703f (patch) | |
tree | 211b53e5797c59df497ae3f0f322deba7228f83b /chrome/browser/sync/sessions | |
parent | 02c2d5c58e90800e56e137968e5bb31a112d7c29 (diff) | |
download | chromium_src-3b361fc4b7a0dbffabf98d7c89b814b3e15f703f.zip chromium_src-3b361fc4b7a0dbffabf98d7c89b814b3e15f703f.tar.gz chromium_src-3b361fc4b7a0dbffabf98d7c89b814b3e15f703f.tar.bz2 |
[Sync] Add more conversions to Value for sync types
This is a prerequisite for exposing some functions to chrome://sync-internals.
Cleaned up some test code also.
BUG=69500
Review URL: http://codereview.chromium.org/6476016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74489 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/sessions')
-rw-r--r-- | chrome/browser/sync/sessions/session_state.cc | 92 | ||||
-rw-r--r-- | chrome/browser/sync/sessions/session_state.h | 26 | ||||
-rw-r--r-- | chrome/browser/sync/sessions/session_state_unittest.cc | 191 |
3 files changed, 306 insertions, 3 deletions
diff --git a/chrome/browser/sync/sessions/session_state.cc b/chrome/browser/sync/sessions/session_state.cc index 3c9887b..e52ebdc 100644 --- a/chrome/browser/sync/sessions/session_state.cc +++ b/chrome/browser/sync/sessions/session_state.cc @@ -7,6 +7,10 @@ #include <set> #include <vector> +#include "base/base64.h" +#include "base/values.h" +#include "chrome/browser/sync/protocol/proto_enum_conversions.h" + using std::set; using std::vector; @@ -37,6 +41,15 @@ TypePayloadMap MakeTypePayloadMapFromRoutingInfo( return types_with_payloads; } +DictionaryValue* TypePayloadMapToValue(const TypePayloadMap& type_payloads) { + DictionaryValue* value = new DictionaryValue(); + for (TypePayloadMap::const_iterator it = type_payloads.begin(); + it != type_payloads.end(); ++it) { + value->SetString(syncable::ModelTypeToString(it->first), it->second); + } + return value; +} + void CoalescePayloads(TypePayloadMap* original, const TypePayloadMap& update) { for (TypePayloadMap::const_iterator i = update.begin(); @@ -63,6 +76,14 @@ SyncSourceInfo::SyncSourceInfo( SyncSourceInfo::~SyncSourceInfo() {} +DictionaryValue* SyncSourceInfo::ToValue() const { + DictionaryValue* value = new DictionaryValue(); + value->SetString("updatesSource", + GetUpdatesSourceString(updates_source)); + value->Set("types", TypePayloadMapToValue(types)); + return value; +} + SyncerStatus::SyncerStatus() : invalid_store(false), syncer_stuck(false), @@ -73,19 +94,62 @@ SyncerStatus::SyncerStatus() num_tombstone_updates_downloaded_total(0) { } +DictionaryValue* SyncerStatus::ToValue() const { + DictionaryValue* value = new DictionaryValue(); + value->SetBoolean("invalidStore", invalid_store); + value->SetBoolean("syncerStuck", syncer_stuck); + value->SetBoolean("syncing", syncing); + value->SetInteger("numSuccessfulCommits", num_successful_commits); + value->SetInteger("numSuccessfulBookmarkCommits", + num_successful_bookmark_commits); + value->SetInteger("numUpdatesDownloadedTotal", + num_updates_downloaded_total); + value->SetInteger("numTombstoneUpdatesDownloadedTotal", + num_tombstone_updates_downloaded_total); + return value; +} + +DictionaryValue* DownloadProgressMarkersToValue( + const std::string + (&download_progress_markers)[syncable::MODEL_TYPE_COUNT]) { + DictionaryValue* value = new DictionaryValue(); + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + // TODO(akalin): Unpack the value into a protobuf. + std::string base64_marker; + bool encoded = + base::Base64Encode(download_progress_markers[i], &base64_marker); + DCHECK(encoded); + value->SetString( + syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)), + base64_marker); + } + return value; +} + ErrorCounters::ErrorCounters() : num_conflicting_commits(0), consecutive_transient_error_commits(0), consecutive_errors(0) { } +DictionaryValue* ErrorCounters::ToValue() const { + DictionaryValue* value = new DictionaryValue(); + value->SetInteger("numConflictingCommits", num_conflicting_commits); + value->SetInteger("consecutiveTransientErrorCommits", + consecutive_transient_error_commits); + value->SetInteger("consecutiveErrors", consecutive_errors); + return value; +} + SyncSessionSnapshot::SyncSessionSnapshot( const SyncerStatus& syncer_status, const ErrorCounters& errors, int64 num_server_changes_remaining, bool is_share_usable, const syncable::ModelTypeBitSet& initial_sync_ended, - std::string download_progress_markers[syncable::MODEL_TYPE_COUNT], + const std::string + (&download_progress_markers)[syncable::MODEL_TYPE_COUNT], bool more_to_sync, bool is_silenced, int64 unsynced_count, @@ -104,7 +168,8 @@ SyncSessionSnapshot::SyncSessionSnapshot( num_conflicting_updates(num_conflicting_updates), did_commit_items(did_commit_items), source(source) { - for (int i = 0; i < syncable::MODEL_TYPE_COUNT; ++i) { + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { const_cast<std::string&>(this->download_progress_markers[i]).assign( download_progress_markers[i]); } @@ -112,6 +177,29 @@ SyncSessionSnapshot::SyncSessionSnapshot( SyncSessionSnapshot::~SyncSessionSnapshot() {} +DictionaryValue* SyncSessionSnapshot::ToValue() const { + DictionaryValue* value = new DictionaryValue(); + value->Set("syncerStatus", syncer_status.ToValue()); + value->Set("errors", errors.ToValue()); + // We don't care too much if we lose precision here. + value->SetInteger("numServerChangesRemaining", + static_cast<int>(num_server_changes_remaining)); + value->SetBoolean("isShareUsable", is_share_usable); + value->Set("initialSyncEnded", + syncable::ModelTypeBitSetToValue(initial_sync_ended)); + value->Set("downloadProgressMarkers", + DownloadProgressMarkersToValue(download_progress_markers)); + value->SetBoolean("hasMoreToSync", has_more_to_sync); + value->SetBoolean("isSilenced", is_silenced); + // We don't care too much if we lose precision here, also. + value->SetInteger("unsyncedCount", + static_cast<int>(unsynced_count)); + value->SetInteger("numConflictingUpdates", num_conflicting_updates); + value->SetBoolean("didCommitItems", did_commit_items); + value->Set("source", source.ToValue()); + return value; +} + ConflictProgress::ConflictProgress(bool* dirty_flag) : dirty_(dirty_flag) {} ConflictProgress::~ConflictProgress() { diff --git a/chrome/browser/sync/sessions/session_state.h b/chrome/browser/sync/sessions/session_state.h index d1a6e17..69fbf7a 100644 --- a/chrome/browser/sync/sessions/session_state.h +++ b/chrome/browser/sync/sessions/session_state.h @@ -23,6 +23,8 @@ #include "chrome/browser/sync/syncable/model_type.h" #include "chrome/browser/sync/syncable/syncable.h" +class DictionaryValue; + namespace syncable { class DirectoryManager; } @@ -48,6 +50,9 @@ TypePayloadMap MakeTypePayloadMapFromRoutingInfo( const ModelSafeRoutingInfo& routes, const std::string& payload); +// Caller takes ownership of the returned dictionary. +DictionaryValue* TypePayloadMapToValue(const TypePayloadMap& type_payloads); + // Coalesce |update| into |original|, overwriting only when |update| has // a non-empty payload. void CoalescePayloads(TypePayloadMap* original, const TypePayloadMap& update); @@ -62,6 +67,9 @@ struct SyncSourceInfo { const TypePayloadMap& t); ~SyncSourceInfo(); + // Caller takes ownership of the returned dictionary. + DictionaryValue* ToValue() const; + sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source; TypePayloadMap types; }; @@ -70,6 +78,9 @@ struct SyncSourceInfo { struct SyncerStatus { SyncerStatus(); + // Caller takes ownership of the returned dictionary. + DictionaryValue* ToValue() const; + // True when we get such an INVALID_STORE error from the server. bool invalid_store; // True iff we're stuck. @@ -87,6 +98,10 @@ struct SyncerStatus { // Counters for various errors that can occur repeatedly during a sync session. struct ErrorCounters { ErrorCounters(); + + // Caller takes ownership of the returned dictionary. + DictionaryValue* ToValue() const; + int num_conflicting_commits; // Number of commits hitting transient errors since the last successful @@ -99,6 +114,11 @@ struct ErrorCounters { int consecutive_errors; }; +// Caller takes ownership of the returned dictionary. +DictionaryValue* DownloadProgressMarkersToValue( + const std::string + (&download_progress_markers)[syncable::MODEL_TYPE_COUNT]); + // An immutable snapshot of state from a SyncSession. Convenient to use as // part of notifications as it is inherently thread-safe. struct SyncSessionSnapshot { @@ -108,7 +128,8 @@ struct SyncSessionSnapshot { int64 num_server_changes_remaining, bool is_share_usable, const syncable::ModelTypeBitSet& initial_sync_ended, - std::string download_progress_markers[syncable::MODEL_TYPE_COUNT], + const std::string + (&download_progress_markers)[syncable::MODEL_TYPE_COUNT], bool more_to_sync, bool is_silenced, int64 unsynced_count, @@ -117,6 +138,9 @@ struct SyncSessionSnapshot { const SyncSourceInfo& source); ~SyncSessionSnapshot(); + // Caller takes ownership of the returned dictionary. + DictionaryValue* ToValue() const; + const SyncerStatus syncer_status; const ErrorCounters errors; const int64 num_server_changes_remaining; diff --git a/chrome/browser/sync/sessions/session_state_unittest.cc b/chrome/browser/sync/sessions/session_state_unittest.cc new file mode 100644 index 0000000..3ac832f --- /dev/null +++ b/chrome/browser/sync/sessions/session_state_unittest.cc @@ -0,0 +1,191 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/sessions/session_state.h" + +#include <string> + +#include "base/base64.h" +#include "base/scoped_ptr.h" +#include "base/values.h" +#include "chrome/test/values_test_util.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace browser_sync { +namespace sessions { +namespace { + +using test::ExpectBooleanValue; +using test::ExpectDictionaryValue; +using test::ExpectIntegerValue; +using test::ExpectListValue; +using test::ExpectStringValue; + +class SessionStateTest : public testing::Test {}; + +TEST_F(SessionStateTest, TypePayloadMapToValue) { + TypePayloadMap payloads; + payloads[syncable::BOOKMARKS] = "bookmarkpayload"; + payloads[syncable::APPS] = ""; + + scoped_ptr<DictionaryValue> value(TypePayloadMapToValue(payloads)); + EXPECT_EQ(2u, value->size()); + ExpectStringValue("bookmarkpayload", *value, "Bookmarks"); + ExpectStringValue("", *value, "Apps"); + EXPECT_FALSE(value->HasKey("Preferences")); +} + +TEST_F(SessionStateTest, SyncSourceInfoToValue) { + sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source = + sync_pb::GetUpdatesCallerInfo::PERIODIC; + TypePayloadMap types; + types[syncable::PREFERENCES] = "preferencespayload"; + types[syncable::EXTENSIONS] = ""; + scoped_ptr<DictionaryValue> expected_types_value( + TypePayloadMapToValue(types)); + + SyncSourceInfo source_info(updates_source, types); + + scoped_ptr<DictionaryValue> value(source_info.ToValue()); + EXPECT_EQ(2u, value->size()); + ExpectStringValue("PERIODIC", *value, "updatesSource"); + ExpectDictionaryValue(*expected_types_value, *value, "types"); +} + +TEST_F(SessionStateTest, SyncerStatusToValue) { + SyncerStatus status; + status.invalid_store = true; + status.syncer_stuck = false; + status.syncing = true; + status.num_successful_commits = 5; + status.num_successful_bookmark_commits = 10; + status.num_updates_downloaded_total = 100; + status.num_tombstone_updates_downloaded_total = 200; + + scoped_ptr<DictionaryValue> value(status.ToValue()); + EXPECT_EQ(7u, value->size()); + ExpectBooleanValue(status.invalid_store, *value, "invalidStore"); + ExpectBooleanValue(status.syncer_stuck, *value, "syncerStuck"); + ExpectBooleanValue(status.syncing, *value, "syncing"); + ExpectIntegerValue(status.num_successful_commits, + *value, "numSuccessfulCommits"); + ExpectIntegerValue(status.num_successful_bookmark_commits, + *value, "numSuccessfulBookmarkCommits"); + ExpectIntegerValue(status.num_updates_downloaded_total, + *value, "numUpdatesDownloadedTotal"); + ExpectIntegerValue(status.num_tombstone_updates_downloaded_total, + *value, "numTombstoneUpdatesDownloadedTotal"); +} + +TEST_F(SessionStateTest, ErrorCountersToValue) { + ErrorCounters counters; + counters.num_conflicting_commits = 1; + counters.consecutive_transient_error_commits = 5; + counters.consecutive_errors = 3; + + scoped_ptr<DictionaryValue> value(counters.ToValue()); + EXPECT_EQ(3u, value->size()); + ExpectIntegerValue(counters.num_conflicting_commits, + *value, "numConflictingCommits"); + ExpectIntegerValue(counters.consecutive_transient_error_commits, + *value, "consecutiveTransientErrorCommits"); + ExpectIntegerValue(counters.consecutive_errors, + *value, "consecutiveErrors"); +} + +TEST_F(SessionStateTest, DownloadProgressMarkersToValue) { + std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + std::string marker(i, i); + download_progress_markers[i] = marker; + } + + scoped_ptr<DictionaryValue> value( + DownloadProgressMarkersToValue(download_progress_markers)); + EXPECT_EQ(syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE, + static_cast<int>(value->size())); + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + syncable::ModelType model_type = syncable::ModelTypeFromInt(i); + std::string marker(i, i); + std::string expected_value; + EXPECT_TRUE(base::Base64Encode(marker, &expected_value)); + ExpectStringValue(expected_value, + *value, syncable::ModelTypeToString(model_type)); + } +} + +TEST_F(SessionStateTest, SyncSessionSnapshotToValue) { + SyncerStatus syncer_status; + syncer_status.num_successful_commits = 500; + scoped_ptr<DictionaryValue> expected_syncer_status_value( + syncer_status.ToValue()); + + ErrorCounters errors; + errors.num_conflicting_commits = 250; + scoped_ptr<DictionaryValue> expected_errors_value( + errors.ToValue()); + + const int kNumServerChangesRemaining = 105; + const bool kIsShareUsable = true; + + syncable::ModelTypeBitSet initial_sync_ended; + initial_sync_ended.set(syncable::BOOKMARKS); + initial_sync_ended.set(syncable::PREFERENCES); + scoped_ptr<ListValue> expected_initial_sync_ended_value( + syncable::ModelTypeBitSetToValue(initial_sync_ended)); + + std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; + download_progress_markers[syncable::BOOKMARKS] = "test"; + download_progress_markers[syncable::APPS] = "apps"; + scoped_ptr<DictionaryValue> expected_download_progress_markers_value( + DownloadProgressMarkersToValue(download_progress_markers)); + + const bool kHasMoreToSync = false; + const bool kIsSilenced = true; + const int kUnsyncedCount = 1053; + const int kNumConflictingUpdates = 1055; + const bool kDidCommitItems = true; + + SyncSourceInfo source; + scoped_ptr<DictionaryValue> expected_source_value(source.ToValue()); + + SyncSessionSnapshot snapshot(syncer_status, + errors, + kNumServerChangesRemaining, + kIsShareUsable, + initial_sync_ended, + download_progress_markers, + kHasMoreToSync, + kIsSilenced, + kUnsyncedCount, + kNumConflictingUpdates, + kDidCommitItems, + source); + scoped_ptr<DictionaryValue> value(snapshot.ToValue()); + EXPECT_EQ(12u, value->size()); + ExpectDictionaryValue(*expected_syncer_status_value, *value, + "syncerStatus"); + ExpectDictionaryValue(*expected_errors_value, *value, "errors"); + ExpectIntegerValue(kNumServerChangesRemaining, *value, + "numServerChangesRemaining"); + ExpectBooleanValue(kIsShareUsable, *value, "isShareUsable"); + ExpectListValue(*expected_initial_sync_ended_value, *value, + "initialSyncEnded"); + ExpectDictionaryValue(*expected_download_progress_markers_value, + *value, "downloadProgressMarkers"); + ExpectBooleanValue(kHasMoreToSync, *value, "hasMoreToSync"); + ExpectBooleanValue(kIsSilenced, *value, "isSilenced"); + ExpectIntegerValue(kUnsyncedCount, *value, "unsyncedCount"); + ExpectIntegerValue(kNumConflictingUpdates, *value, + "numConflictingUpdates"); + ExpectBooleanValue(kDidCommitItems, *value, + "didCommitItems"); + ExpectDictionaryValue(*expected_source_value, *value, "source"); +} + +} // namespace +} // namespace sessions +} // namespace browser_sync |