Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(617)

Unified Diff: sync/internal_api/public/base/model_type_state_map.cc

Issue 10837214: Refactor ModelTypePayloadMap and ObjectIdPayloadMap to StateMaps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review changes Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sync/internal_api/public/base/model_type_state_map.cc
diff --git a/sync/internal_api/public/base/model_type_state_map.cc b/sync/internal_api/public/base/model_type_state_map.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5669379bda7927a5bf96017e5757d0c40aeec440
--- /dev/null
+++ b/sync/internal_api/public/base/model_type_state_map.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2012 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 "sync/internal_api/public/base/model_type_state_map.h"
+
+#include <vector>
+
+#include "base/json/json_writer.h"
+#include "base/json/string_escape.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/values.h"
+
+namespace syncer {
+
+ModelTypeStateMap ModelTypeSetToStateMap(ModelTypeSet types,
+ const std::string& payload) {
+ ModelTypeStateMap type_state_map;
+ for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) {
+ // TODO(dcheng): Do we need to set ack_handle?
+ type_state_map[it.Get()].payload = payload;
+ }
+ return type_state_map;
+}
+
+ModelTypeSet ModelTypeStateMapToSet(
+ const ModelTypeStateMap& type_state_map) {
+ ModelTypeSet types;
+ for (ModelTypeStateMap::const_iterator it = type_state_map.begin();
+ it != type_state_map.end(); ++it) {
+ types.Put(it->first);
+ }
+ return types;
+}
+
+std::string ModelTypeStateMapToString(const ModelTypeStateMap& type_state_map) {
+ scoped_ptr<DictionaryValue> value(ModelTypeStateMapToValue(type_state_map));
+ std::string json;
+ base::JSONWriter::Write(value.get(), &json);
+ return json;
+}
+
+DictionaryValue* ModelTypeStateMapToValue(
+ const ModelTypeStateMap& type_state_map) {
+ DictionaryValue* value = new DictionaryValue();
+ for (ModelTypeStateMap::const_iterator it = type_state_map.begin();
+ it != type_state_map.end(); ++it) {
+ std::string marker;
akalin 2012/08/22 23:24:27 rename marker to printable_payload
dcheng 2012/08/22 23:47:11 Done.
+ base::JsonDoubleQuote(it->second.payload, false /* add quotes */, &marker);
akalin 2012/08/22 23:24:27 add quotes -> add_quotes
dcheng 2012/08/22 23:47:11 Done. Since I had to break this up for 80 chars, I
+ value->SetString(ModelTypeToString(it->first), marker);
+ }
+ return value;
+}
+
+void CoalesceStates(ModelTypeStateMap* original,
+ const ModelTypeStateMap& update) {
+ // TODO(dcheng): Where is this called? Do we need to add more clever logic for
+ // handling ack_handle? We probably want to always use the "latest"
+ // ack_handle, which might imply always using the one in update?
+ for (ModelTypeStateMap::const_iterator i = update.begin();
+ i != update.end(); ++i) {
+ if (original->count(i->first) == 0) {
+ // If this datatype isn't already in our map, add it with
+ // whatever payload it has.
+ (*original)[i->first] = i->second;
+ } else if (i->second.payload.length() > 0) {
+ // If this datatype is already in our map, we only overwrite the
+ // payload if the new one is non-empty.
+ (*original)[i->first].payload = i->second.payload;
+ }
+ }
+}
+
+} // namespace syncer

Powered by Google App Engine
This is Rietveld 408576698