Chromium Code Reviews| Index: chrome/browser/chromeos/contacts/contact_map.cc |
| diff --git a/chrome/browser/chromeos/contacts/contact_map.cc b/chrome/browser/chromeos/contacts/contact_map.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5821df3b2ed759ccd56335db35ac8444c096001b |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/contact_map.cc |
| @@ -0,0 +1,68 @@ |
| +// 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 "chrome/browser/chromeos/contacts/contact_map.h" |
| + |
| +#include "chrome/browser/chromeos/contacts/contact.pb.h" |
| + |
| +namespace contacts { |
| + |
| +ContactMap::ContactMap() : contacts_deleter_(&contacts_) {} |
| + |
| +ContactMap::~ContactMap() {} |
| + |
| +const Contact* ContactMap::Find(const std::string& contact_id) const { |
| + Map::const_iterator it = contacts_.find(contact_id); |
| + return (it != contacts_.end()) ? it->second : NULL; |
| +} |
| + |
| +void ContactMap::Clear() { |
| + STLDeleteValues(&contacts_); |
| + contacts_.clear(); |
| +} |
| + |
| +void ContactMap::Merge(scoped_ptr<ScopedVector<Contact> > updated_contacts, |
|
satorux1
2012/09/06 17:16:48
scoped_ptr of ScopedVector looks a bit tricky, but
Daniel Erat
2012/09/09 14:51:30
Yeah, I think that scoped_ptr makes the ownership
|
| + bool drop_deleted) { |
| + for (ScopedVector<Contact>::iterator it = updated_contacts->begin(); |
| + it != updated_contacts->end(); ++it) { |
| + Contact* contact = *it; |
| + Map::iterator map_it = contacts_.find(contact->contact_id()); |
| + |
| + if (contact->deleted() && drop_deleted) { |
| + // Also delete the previous version of the contact, if any. |
| + if (map_it != contacts_.end()) { |
| + delete map_it->second; |
| + contacts_.erase(map_it); |
| + } |
| + delete contact; |
| + } else { |
| + if (map_it != contacts_.end()) { |
| + delete map_it->second; |
| + map_it->second = contact; |
| + } else { |
| + contacts_[contact->contact_id()] = contact; |
| + } |
| + } |
| + } |
| + |
| + // Make sure that the Contact objects that we just saved to the map won't be |
| + // destroyed when |updated_contacts| is destroyed. |
| + updated_contacts->weak_clear(); |
| +} |
| + |
| +base::Time ContactMap::GetMaxUpdateTime() const { |
| + base::Time max_update_time; |
| + for (const_iterator it = begin(); it != end(); ++it) { |
| + const Contact* contact = it->second; |
| + const base::Time update_time = |
| + base::Time::FromInternalValue(contact->update_time()); |
| + if (!update_time.is_null() && |
| + (max_update_time.is_null() || max_update_time < update_time)) { |
| + max_update_time = update_time; |
| + } |
| + } |
| + return max_update_time; |
| +} |
| + |
| +} // namespace contacts |