Chromium Code Reviews| Index: chrome/browser/sync/invalidations/invalidator_storage.cc | 
| diff --git a/chrome/browser/sync/invalidations/invalidator_storage.cc b/chrome/browser/sync/invalidations/invalidator_storage.cc | 
| index dd7dd9ee39461c1ab30458238460c56914000f61..1ee7352206330633df700167a1c0dc869238a69d 100644 | 
| --- a/chrome/browser/sync/invalidations/invalidator_storage.cc | 
| +++ b/chrome/browser/sync/invalidations/invalidator_storage.cc | 
| @@ -5,13 +5,18 @@ | 
| #include "chrome/browser/sync/invalidations/invalidator_storage.h" | 
| #include "base/base64.h" | 
| +#include "base/bind.h" | 
| +#include "base/callback.h" | 
| +#include "base/location.h" | 
| #include "base/logging.h" | 
| #include "base/metrics/histogram.h" | 
| #include "base/string_number_conversions.h" | 
| +#include "base/task_runner.h" | 
| #include "base/values.h" | 
| #include "chrome/browser/prefs/pref_service.h" | 
| #include "chrome/common/pref_names.h" | 
| #include "sync/internal_api/public/base/model_type.h" | 
| +#include "sync/notifier/invalidation_util.h" | 
| using syncer::InvalidationStateMap; | 
| @@ -22,35 +27,55 @@ namespace { | 
| const char kSourceKey[] = "source"; | 
| const char kNameKey[] = "name"; | 
| const char kMaxVersionKey[] = "max-version"; | 
| +const char kPayloadKey[] = "payload"; | 
| +const char kCurrentAckHandleKey[] = "current-ack"; | 
| +const char kExpectedAckHandleKey[] = "expected-ack"; | 
| bool ValueToObjectIdAndState(const DictionaryValue& value, | 
| invalidation::ObjectId* id, | 
| syncer::InvalidationState* state) { | 
| std::string source_str; | 
| - int source = 0; | 
| - std::string name; | 
| - std::string max_version_str; | 
| if (!value.GetString(kSourceKey, &source_str)) { | 
| DLOG(WARNING) << "Unable to deserialize source"; | 
| return false; | 
| } | 
| - if (!value.GetString(kNameKey, &name)) { | 
| - DLOG(WARNING) << "Unable to deserialize name"; | 
| - return false; | 
| - } | 
| - if (!value.GetString(kMaxVersionKey, &max_version_str)) { | 
| - DLOG(WARNING) << "Unable to deserialize max version"; | 
| - return false; | 
| - } | 
| + int source = 0; | 
| if (!base::StringToInt(source_str, &source)) { | 
| DLOG(WARNING) << "Invalid source: " << source_str; | 
| return false; | 
| } | 
| - if (!base::StringToInt64(max_version_str, &state->version)) { | 
| - DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | 
| + std::string name; | 
| + if (!value.GetString(kNameKey, &name)) { | 
| + DLOG(WARNING) << "Unable to deserialize name"; | 
| return false; | 
| } | 
| + // Fields that may not be set if we've only received an invalidation for an | 
| + // unknown version or for all types. | 
| + std::string max_version_str; | 
| + if (value.GetString(kMaxVersionKey, &max_version_str)) { | 
| 
 
akalin
2012/11/28 00:11:42
isn't kMaxVersionkey always present?  looks like i
 
dcheng
2012/11/30 01:42:54
Hm. Yeah, you're right. I was thinking from the wr
 
 | 
| + if (!base::StringToInt64(max_version_str, &state->version)) { | 
| + DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | 
| + return false; | 
| + } | 
| + } | 
| + value.GetString(kPayloadKey, &state->payload); | 
| *id = invalidation::ObjectId(source, name); | 
| 
 
akalin
2012/11/28 00:11:42
move this closer to where source and name are set?
 
dcheng
2012/11/30 01:42:54
I did this intentionally to avoid a state where th
 
 | 
| + // The ack handle fields won't be set if upgrading from previous versions of | 
| 
 
akalin
2012/11/28 00:11:42
clear state first?
 
dcheng
2012/11/30 01:42:54
While this could be less error prone, it's also re
 
akalin
2012/11/30 02:45:15
i guess it's okay since this is a private function
 
 | 
| + // Chrome. | 
| + const base::DictionaryValue* current_ack_handle_value = NULL; | 
| + if (value.GetDictionary(kCurrentAckHandleKey, ¤t_ack_handle_value)) { | 
| + state->current.ResetFromValue(*current_ack_handle_value); | 
| + } | 
| + // TODO(dcheng): Consider regenerating the latest ack handle value on Chrome | 
| 
 
akalin
2012/11/28 00:11:42
hmm i don't understand this comment.  what do you
 
dcheng
2012/11/30 01:42:54
One of my original ideas was to ignore the persist
 
 | 
| + // startup to allow for format changes? | 
| + const base::DictionaryValue* expected_ack_handle_value = NULL; | 
| + if (value.GetDictionary(kExpectedAckHandleKey, &expected_ack_handle_value)) { | 
| + state->expected.ResetFromValue(*expected_ack_handle_value); | 
| + } else { | 
| + // In this case, we should never have a valid current value set. | 
| + DCHECK(!state->current.IsValid()); | 
| + state->current = syncer::AckHandle::InvalidAckHandle(); | 
| + } | 
| return true; | 
| } | 
| @@ -61,6 +86,11 @@ DictionaryValue* ObjectIdAndStateToValue( | 
| value->SetString(kSourceKey, base::IntToString(id.source())); | 
| value->SetString(kNameKey, id.name()); | 
| value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); | 
| + value->SetString(kPayloadKey, state.payload); | 
| + if (state.current.IsValid()) | 
| + value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); | 
| + if (state.expected.IsValid()) | 
| + value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); | 
| return value; | 
| } | 
| @@ -98,8 +128,10 @@ InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { | 
| return state_map; | 
| } | 
| -void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, | 
| - int64 max_version) { | 
| +void InvalidatorStorage::SetMaxVersionAndPayload( | 
| + const invalidation::ObjectId& id, | 
| + int64 max_version, | 
| + const std::string& payload) { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| CHECK(pref_service_); | 
| InvalidationStateMap state_map = GetAllInvalidationStates(); | 
| @@ -109,6 +141,7 @@ void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, | 
| return; | 
| } | 
| state_map[id].version = max_version; | 
| + state_map[id].payload = payload; | 
| base::ListValue state_map_list; | 
| SerializeToList(state_map, &state_map_list); | 
| @@ -248,4 +281,46 @@ void InvalidatorStorage::Clear() { | 
| pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | 
| } | 
| +void InvalidatorStorage::GenerateAckHandles( | 
| + const syncer::ObjectIdSet& ids, | 
| + const scoped_refptr<base::TaskRunner>& task_runner, | 
| + const base::Callback<void(const syncer::AckHandleMap&)> callback) { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + CHECK(pref_service_); | 
| + InvalidationStateMap state_map = GetAllInvalidationStates(); | 
| + | 
| + syncer::AckHandleMap ack_handles; | 
| + for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | 
| + ++it) { | 
| + state_map[*it].expected = syncer::AckHandle::CreateUnique(); | 
| + ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); | 
| + } | 
| + | 
| + base::ListValue state_map_list; | 
| + SerializeToList(state_map, &state_map_list); | 
| + pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 
| + state_map_list); | 
| + | 
| + task_runner->PostTask(FROM_HERE, base::Bind(callback, ack_handles)); | 
| 
 
akalin
2012/11/28 00:11:42
do something with return value?  maybe just ignore
 
dcheng
2012/11/30 01:42:54
Done.
 
 | 
| +} | 
| + | 
| +void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, | 
| + const syncer::AckHandle& ack_handle) { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + CHECK(pref_service_); | 
| + InvalidationStateMap state_map = GetAllInvalidationStates(); | 
| + | 
| + InvalidationStateMap::iterator it = state_map.find(id); | 
| + // This could happen if the acknowledgement is delayed and Forget() has | 
| + // already been called. | 
| + if (it == state_map.end()) | 
| + return; | 
| + it->second.current = ack_handle; | 
| + | 
| + base::ListValue state_map_list; | 
| + SerializeToList(state_map, &state_map_list); | 
| + pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 
| + state_map_list); | 
| +} | 
| + | 
| } // namespace browser_sync |