Chromium Code Reviews| Index: chrome/browser/chromeos/contacts/google_contact_store.h |
| diff --git a/chrome/browser/chromeos/contacts/google_contact_store.h b/chrome/browser/chromeos/contacts/google_contact_store.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bf583d6ace4df175c3bba5ecf111bc76be449fb9 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/contacts/google_contact_store.h |
| @@ -0,0 +1,164 @@ |
| +// 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. |
| + |
| +#ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ |
| +#define CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ |
| + |
| +#include "chrome/browser/chromeos/contacts/contact_store.h" |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/observer_list.h" |
| +#include "base/stl_util.h" |
| +#include "base/time.h" |
| +#include "base/timer.h" |
| + |
| +class Profile; |
| + |
| +namespace gdata { |
| +class GDataContactsServiceInterface; |
| +} |
| + |
| +namespace contacts { |
| + |
| +class Contact; |
| +class ContactDatabaseInterface; |
| +class UpdateMetadata; |
| + |
| +// A collection of contacts from a Google account. |
| +class GoogleContactStore : public ContactStore, |
| + public base::SupportsWeakPtr<GoogleContactStore> { |
|
satorux1
2012/08/02 22:21:25
Per the chromium-dev thread on WeakPtr, SupportsWe
Daniel Erat
2012/08/02 23:23:18
Done.
|
| + public: |
| + class TestAPI { |
| + public: |
| + explicit TestAPI(GoogleContactStore* store); |
| + ~TestAPI(); |
| + |
| + bool update_scheduled() { return store_->update_timer_.IsRunning(); } |
| + |
| + void set_current_time(const base::Time& time) { |
| + store_->current_time_for_testing_ = time; |
| + } |
| + |
| + // Takes ownership of |db|. Must be called before Init(). |
| + void SetDatabase(ContactDatabaseInterface* db); |
| + |
| + // Takes ownership of |service|. Must be called before Init(). |
| + void SetGDataService(gdata::GDataContactsServiceInterface* service); |
| + |
| + // Triggers an update, similar to what happens when the update timer fires. |
| + void DoUpdate(); |
| + |
| + private: |
| + GoogleContactStore* store_; // not owned |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestAPI); |
| + }; |
| + |
| + explicit GoogleContactStore(Profile* profile); |
| + virtual ~GoogleContactStore(); |
| + |
| + void Init(); |
| + |
| + // ContactStore implementation: |
| + virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; |
| + virtual const Contact* GetContactByProviderId( |
| + const std::string& provider_id) OVERRIDE; |
| + virtual void AddObserver(ContactStoreObserver* observer) OVERRIDE; |
| + virtual void RemoveObserver(ContactStoreObserver* observer) OVERRIDE; |
| + |
| + private: |
| + // Map from a contact's Google-assigned ID to the contact itself. |
| + typedef std::map<std::string, Contact*> ContactMap; |
| + |
| + // Returns the current time. Uses |current_time_for_testing_| instead if it's |
| + // set. |
| + base::Time GetCurrentTime() const; |
| + |
| + // Destroys |db_| if non-NULL and resets the pointer. |
| + // The underlying data is preserved on-disk. |
| + void DestroyDatabase(); |
| + |
| + // Asynchronously downloads updated contacts and merges them into |contacts_|. |
| + void UpdateContacts(); |
| + |
| + // Starts |update_timer_| so UpdateContacts() will be run. |
| + void ScheduleUpdate(bool last_update_was_successful); |
| + |
| + // Moves |updated_contacts| into |contacts_| and updates |
| + // |last_contact_update_time_|. |
| + void MergeContacts(bool is_full_update, |
| + scoped_ptr<ScopedVector<Contact> > updated_contacts); |
| + |
| + // Handles a successful download, merging |updated_contacts| and saving the |
| + // updated contacts to |db_|. |
| + void OnDownloadSuccess(bool is_full_update, |
| + const base::Time& update_start_time, |
| + scoped_ptr<ScopedVector<Contact> > updated_contacts); |
| + |
| + // Handles a failed update. A new update is scheduled. |
| + void OnDownloadFailure(); |
| + |
| + // Handles |db_|'s initialization. On success, we start loading the contacts |
| + // from the database; otherwise, we throw out the database and schedule an |
| + // update. |
| + void OnDatabaseInitialized(bool success); |
| + |
| + // Handles contacts being loaded from |db_|. On success, we merge the loaded |
| + // contacts. No matter what, we call UpdateContacts(). |
| + void OnDatabaseContactsLoaded(bool success, |
| + scoped_ptr<ScopedVector<Contact> > contacts, |
| + scoped_ptr<UpdateMetadata> metadata); |
| + |
| + // Handles contacts being saved to |db_|. Now that the contacts are no longer |
| + // being accessed by the database, we schedule an update. |
| + void OnDatabaseContactsSaved(bool success); |
| + |
| + Profile* profile_; // not owned |
| + |
| + ObserverList<ContactStoreObserver> observers_; |
| + |
| + // Owns the pointed-to Contact values. |
| + ContactMap contacts_; |
| + |
| + // Deletes values in |contacts_|. |
| + STLValueDeleter<ContactMap> contacts_deleter_; |
| + |
| + // Most-recent time that an entry in |contacts_| has been updated (as reported |
| + // by Google). |
| + base::Time last_contact_update_time_; |
| + |
| + // Used to save contacts to disk and load them at startup. Owns the object. |
| + ContactDatabaseInterface* db_; |
| + |
| + // If non-NULL, used in place of the real GData service to download contacts. |
| + scoped_ptr<gdata::GDataContactsServiceInterface> gdata_service_for_testing_; |
| + |
| + // Used to schedule calls to UpdateContacts(). |
| + base::OneShotTimer<GoogleContactStore> update_timer_; |
| + |
| + // Time at which the last successful update was started. |
| + base::Time last_successful_update_start_time_; |
| + |
| + // Amount of time that we'll wait before retrying the next time that an update |
| + // fails. |
| + base::TimeDelta update_delay_on_next_failure_; |
| + |
| + // If non-null, used in place of base::Time::Now() when the current time is |
| + // needed. |
| + base::Time current_time_for_testing_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(GoogleContactStore); |
| +}; |
| + |
| +} // namespace contacts |
| + |
| +#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ |