summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/contacts/contact_map.cc
blob: f02aef4244ce009523a0be32b1e1ae544a0986a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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::Erase(const std::string& contact_id) {
  Map::iterator it = contacts_.find(contact_id);
  if (it == contacts_.end())
    return;

  delete it->second;
  contacts_.erase(it);
}

void ContactMap::Clear() {
  STLDeleteValues(&contacts_);
}

void ContactMap::Merge(scoped_ptr<ScopedVector<Contact> > updated_contacts,
                       DeletedContactPolicy policy) {
  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() && policy == DROP_DELETED_CONTACTS) {
      // 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();
}

}  // namespace contacts