| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" | 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 void GoogleContactStore::TestAPI::NotifyAboutNetworkStateChange(bool online) { | 76 void GoogleContactStore::TestAPI::NotifyAboutNetworkStateChange(bool online) { |
| 77 net::NetworkChangeNotifier::ConnectionType type = | 77 net::NetworkChangeNotifier::ConnectionType type = |
| 78 online ? | 78 online ? |
| 79 net::NetworkChangeNotifier::CONNECTION_UNKNOWN : | 79 net::NetworkChangeNotifier::CONNECTION_UNKNOWN : |
| 80 net::NetworkChangeNotifier::CONNECTION_NONE; | 80 net::NetworkChangeNotifier::CONNECTION_NONE; |
| 81 store_->OnConnectionTypeChanged(type); | 81 store_->OnConnectionTypeChanged(type); |
| 82 } | 82 } |
| 83 | 83 |
| 84 GoogleContactStore::GoogleContactStore(Profile* profile) | 84 GoogleContactStore::GoogleContactStore(Profile* profile) |
| 85 : profile_(profile), | 85 : profile_(profile), |
| 86 contacts_deleter_(&contacts_), | |
| 87 db_(new ContactDatabase), | 86 db_(new ContactDatabase), |
| 88 update_delay_on_next_failure_( | 87 update_delay_on_next_failure_( |
| 89 base::TimeDelta::FromSeconds(kUpdateFailureInitialRetrySec)), | 88 base::TimeDelta::FromSeconds(kUpdateFailureInitialRetrySec)), |
| 90 is_online_(true), | 89 is_online_(true), |
| 91 should_update_when_online_(false), | 90 should_update_when_online_(false), |
| 92 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 94 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 93 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 95 is_online_ = !net::NetworkChangeNotifier::IsOffline(); | 94 is_online_ = !net::NetworkChangeNotifier::IsOffline(); |
| 96 } | 95 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 125 for (ContactMap::const_iterator it = contacts_.begin(); | 124 for (ContactMap::const_iterator it = contacts_.begin(); |
| 126 it != contacts_.end(); ++it) { | 125 it != contacts_.end(); ++it) { |
| 127 if (!it->second->deleted()) | 126 if (!it->second->deleted()) |
| 128 contacts_out->push_back(it->second); | 127 contacts_out->push_back(it->second); |
| 129 } | 128 } |
| 130 } | 129 } |
| 131 | 130 |
| 132 const Contact* GoogleContactStore::GetContactById( | 131 const Contact* GoogleContactStore::GetContactById( |
| 133 const std::string& contact_id) { | 132 const std::string& contact_id) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 135 ContactMap::const_iterator it = contacts_.find(contact_id); | 134 const Contact* contact = contacts_.Find(contact_id); |
| 136 return (it != contacts_.end() && !it->second->deleted()) ? it->second : NULL; | 135 return (contact && !contact->deleted()) ? contact : NULL; |
| 137 } | 136 } |
| 138 | 137 |
| 139 void GoogleContactStore::AddObserver(ContactStoreObserver* observer) { | 138 void GoogleContactStore::AddObserver(ContactStoreObserver* observer) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 141 DCHECK(observer); | 140 DCHECK(observer); |
| 142 observers_.AddObserver(observer); | 141 observers_.AddObserver(observer); |
| 143 } | 142 } |
| 144 | 143 |
| 145 void GoogleContactStore::RemoveObserver(ContactStoreObserver* observer) { | 144 void GoogleContactStore::RemoveObserver(ContactStoreObserver* observer) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 VLOG(1) << "Scheduling update of " << profile_->GetProfileName() | 233 VLOG(1) << "Scheduling update of " << profile_->GetProfileName() |
| 235 << " in " << delay.InSeconds() << " second(s)"; | 234 << " in " << delay.InSeconds() << " second(s)"; |
| 236 update_timer_.Start( | 235 update_timer_.Start( |
| 237 FROM_HERE, delay, this, &GoogleContactStore::UpdateContacts); | 236 FROM_HERE, delay, this, &GoogleContactStore::UpdateContacts); |
| 238 } | 237 } |
| 239 | 238 |
| 240 void GoogleContactStore::MergeContacts( | 239 void GoogleContactStore::MergeContacts( |
| 241 bool is_full_update, | 240 bool is_full_update, |
| 242 scoped_ptr<ScopedVector<Contact> > updated_contacts) { | 241 scoped_ptr<ScopedVector<Contact> > updated_contacts) { |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 244 if (is_full_update) { | 243 if (is_full_update) |
| 245 STLDeleteValues(&contacts_); | 244 contacts_.Clear(); |
| 246 contacts_.clear(); | 245 size_t num_updated_contacts = updated_contacts->size(); |
| 247 } | 246 contacts_.Merge(updated_contacts.Pass(), ContactMap::KEEP_DELETED_CONTACTS); |
| 248 | 247 |
| 249 for (ScopedVector<Contact>::iterator it = updated_contacts->begin(); | 248 if (is_full_update || num_updated_contacts > 0) |
| 250 it != updated_contacts->end(); ++it) { | 249 last_contact_update_time_ = contacts_.GetMaxUpdateTime(); |
| 251 Contact* contact = *it; | |
| 252 VLOG(1) << "Updating " << contact->contact_id(); | |
| 253 ContactMap::iterator map_it = contacts_.find(contact->contact_id()); | |
| 254 if (map_it == contacts_.end()) { | |
| 255 contacts_[contact->contact_id()] = contact; | |
| 256 } else { | |
| 257 delete map_it->second; | |
| 258 map_it->second = contact; | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 // Make sure that the Contact objects won't be destroyed when | |
| 263 // |updated_contacts| is destroyed. | |
| 264 size_t num_updated_contacts = updated_contacts->size(); | |
| 265 updated_contacts->weak_clear(); | |
| 266 | |
| 267 if (is_full_update || num_updated_contacts > 0) { | |
| 268 // Find the latest update time. | |
| 269 last_contact_update_time_ = base::Time(); | |
| 270 for (ContactMap::const_iterator it = contacts_.begin(); | |
| 271 it != contacts_.end(); ++it) { | |
| 272 const Contact* contact = it->second; | |
| 273 base::Time update_time = | |
| 274 base::Time::FromInternalValue(contact->update_time()); | |
| 275 | |
| 276 if (!update_time.is_null() && | |
| 277 (last_contact_update_time_.is_null() || | |
| 278 last_contact_update_time_ < update_time)) { | |
| 279 last_contact_update_time_ = update_time; | |
| 280 } | |
| 281 } | |
| 282 } | |
| 283 VLOG(1) << "Last contact update time is " | 250 VLOG(1) << "Last contact update time is " |
| 284 << (last_contact_update_time_.is_null() ? | 251 << (last_contact_update_time_.is_null() ? |
| 285 std::string("null") : | 252 std::string("null") : |
| 286 gdata::util::FormatTimeAsString(last_contact_update_time_)); | 253 gdata::util::FormatTimeAsString(last_contact_update_time_)); |
| 287 } | 254 } |
| 288 | 255 |
| 289 void GoogleContactStore::OnDownloadSuccess( | 256 void GoogleContactStore::OnDownloadSuccess( |
| 290 bool is_full_update, | 257 bool is_full_update, |
| 291 const base::Time& update_start_time, | 258 const base::Time& update_start_time, |
| 292 scoped_ptr<ScopedVector<Contact> > updated_contacts) { | 259 scoped_ptr<ScopedVector<Contact> > updated_contacts) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 return gdata::util::IsGDataAvailable(profile); | 370 return gdata::util::IsGDataAvailable(profile); |
| 404 } | 371 } |
| 405 | 372 |
| 406 ContactStore* GoogleContactStoreFactory::CreateContactStore(Profile* profile) { | 373 ContactStore* GoogleContactStoreFactory::CreateContactStore(Profile* profile) { |
| 407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 408 DCHECK(CanCreateContactStoreForProfile(profile)); | 375 DCHECK(CanCreateContactStoreForProfile(profile)); |
| 409 return new GoogleContactStore(profile); | 376 return new GoogleContactStore(profile); |
| 410 } | 377 } |
| 411 | 378 |
| 412 } // namespace contacts | 379 } // namespace contacts |
| OLD | NEW |