Index: chrome/browser/chromeos/contacts/google_contact_source.h |
diff --git a/chrome/browser/chromeos/contacts/google_contact_source.h b/chrome/browser/chromeos/contacts/google_contact_source.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2f00eef4e4aeaea8f583976585114c647114dea0 |
--- /dev/null |
+++ b/chrome/browser/chromeos/contacts/google_contact_source.h |
@@ -0,0 +1,133 @@ |
+// 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_SOURCE_H_ |
+#define CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_SOURCE_H_ |
+ |
+#include "chrome/browser/chromeos/contacts/contact_source.h" |
+ |
+#include <map> |
+#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/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 ContactDatabaseInterface; |
+ |
+// A collection of contacts from a Google account. |
+class GoogleContactSource : public ContactSource { |
+ public: |
+ class TestAPI { |
+ public: |
+ explicit TestAPI(GoogleContactSource* source); |
+ ~TestAPI(); |
+ |
+ bool update_scheduled() { return source_->update_timer_.IsRunning(); } |
+ |
+ // 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: |
+ GoogleContactSource* source_; // not owned |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestAPI); |
+ }; |
+ |
+ explicit GoogleContactSource(Profile* profile); |
+ virtual ~GoogleContactSource(); |
+ |
+ void Init(); |
+ |
+ // ContactSource implementation: |
+ virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; |
+ virtual const Contact* GetContactByProviderId( |
+ const std::string& provider_id) OVERRIDE; |
+ virtual void AddObserver(ContactSourceObserver* observer) OVERRIDE; |
+ virtual void RemoveObserver(ContactSourceObserver* observer) OVERRIDE; |
+ |
+ private: |
+ // Map from a contact's Google-assigned ID to the contact itself. |
+ typedef std::map<std::string, Contact*> ContactMap; |
+ |
+ // Asynchronously updates |contacts_|. |
+ void UpdateContacts(); |
+ |
+ // Starts |update_timer_| so UpdateContacts() will be run. |
+ void ScheduleUpdate(); |
+ |
+ // Moves |updated_contacts| into |contacts_| and updates |last_update_time_|. |
+ // Clears |updated_contacts| (so that the contacts won't be deleted). |
+ void MergeContacts(bool is_full_update, |
+ ScopedVector<Contact>* updated_contacts); |
+ |
+ // Handles a successful download, merging |updated_contacts| and saving the |
+ // updated contacts to |db_|. |
+ void OnDownloadSuccess(bool is_full_update, |
+ scoped_ptr<ScopedVector<Contact> > updated_contacts); |
+ |
+ // Handles a failed update. |
+ void OnDownloadFailure(); |
+ |
+ // Handles |db_|'s initialization. |
+ void OnDatabaseInitialized(bool success); |
+ |
+ // Handles contacts being loaded from |db_|. On success, we apply the loaded |
+ // contacts. No matter what, we call UpdateContacts(). |
+ void OnDatabaseContactsLoaded(bool success, |
+ scoped_ptr<ScopedVector<Contact> > contacts); |
+ |
+ // Handles contacts being saved to |db_|. Now that the contacts are no longer |
+ // being accessed by the database, we call ScheduleUpdate() to queue up the |
+ // next refresh. |
+ void OnDatabaseContactsSaved(bool success); |
+ |
+ Profile* profile_; // not owned |
+ |
+ ObserverList<ContactSourceObserver> 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 (within |
+ // Google). |
+ base::Time last_update_time_; |
+ |
+ // Used to save contacts to disk and restore them at startup. |
+ scoped_ptr<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<GoogleContactSource> update_timer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(GoogleContactSource); |
+}; |
+ |
+} // namespace contacts |
+ |
+#endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_SOURCE_H_ |