OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_SOURCE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_SOURCE_H_ |
| 7 |
| 8 #include "chrome/browser/chromeos/contacts/contact_source.h" |
| 9 |
| 10 #include <map> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/observer_list.h" |
| 18 #include "base/stl_util.h" |
| 19 #include "base/time.h" |
| 20 #include "base/timer.h" |
| 21 |
| 22 class Profile; |
| 23 |
| 24 namespace gdata { |
| 25 class GDataContactsServiceInterface; |
| 26 } |
| 27 |
| 28 namespace contacts { |
| 29 |
| 30 class ContactDatabaseInterface; |
| 31 |
| 32 // A collection of contacts from a Google account. |
| 33 class GoogleContactSource : public ContactSource { |
| 34 public: |
| 35 class TestAPI { |
| 36 public: |
| 37 explicit TestAPI(GoogleContactSource* source); |
| 38 ~TestAPI(); |
| 39 |
| 40 bool update_scheduled() { return source_->update_timer_.IsRunning(); } |
| 41 |
| 42 // Takes ownership of |db|. Must be called before Init(). |
| 43 void SetDatabase(ContactDatabaseInterface* db); |
| 44 |
| 45 // Takes ownership of |service|. Must be called before Init(). |
| 46 void SetGDataService(gdata::GDataContactsServiceInterface* service); |
| 47 |
| 48 // Triggers an update, similar to what happens when the update timer fires. |
| 49 void DoUpdate(); |
| 50 |
| 51 private: |
| 52 GoogleContactSource* source_; // not owned |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestAPI); |
| 55 }; |
| 56 |
| 57 explicit GoogleContactSource(Profile* profile); |
| 58 virtual ~GoogleContactSource(); |
| 59 |
| 60 void Init(); |
| 61 |
| 62 // ContactSource implementation: |
| 63 virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; |
| 64 virtual const Contact* GetContactByProviderId( |
| 65 const std::string& provider_id) OVERRIDE; |
| 66 virtual void AddObserver(ContactSourceObserver* observer) OVERRIDE; |
| 67 virtual void RemoveObserver(ContactSourceObserver* observer) OVERRIDE; |
| 68 |
| 69 private: |
| 70 // Map from a contact's Google-assigned ID to the contact itself. |
| 71 typedef std::map<std::string, Contact*> ContactMap; |
| 72 |
| 73 // Asynchronously updates |contacts_|. |
| 74 void UpdateContacts(); |
| 75 |
| 76 // Starts |update_timer_| so UpdateContacts() will be run. |
| 77 void ScheduleUpdate(); |
| 78 |
| 79 // Moves |updated_contacts| into |contacts_| and updates |last_update_time_|. |
| 80 // Clears |updated_contacts| (so that the contacts won't be deleted). |
| 81 void MergeContacts(bool is_full_update, |
| 82 ScopedVector<Contact>* updated_contacts); |
| 83 |
| 84 // Handles a successful download, merging |updated_contacts| and saving the |
| 85 // updated contacts to |db_|. |
| 86 void OnDownloadSuccess(bool is_full_update, |
| 87 scoped_ptr<ScopedVector<Contact> > updated_contacts); |
| 88 |
| 89 // Handles a failed update. |
| 90 void OnDownloadFailure(); |
| 91 |
| 92 // Handles |db_|'s initialization. |
| 93 void OnDatabaseInitialized(bool success); |
| 94 |
| 95 // Handles contacts being loaded from |db_|. On success, we apply the loaded |
| 96 // contacts. No matter what, we call UpdateContacts(). |
| 97 void OnDatabaseContactsLoaded(bool success, |
| 98 scoped_ptr<ScopedVector<Contact> > contacts); |
| 99 |
| 100 // Handles contacts being saved to |db_|. Now that the contacts are no longer |
| 101 // being accessed by the database, we call ScheduleUpdate() to queue up the |
| 102 // next refresh. |
| 103 void OnDatabaseContactsSaved(bool success); |
| 104 |
| 105 Profile* profile_; // not owned |
| 106 |
| 107 ObserverList<ContactSourceObserver> observers_; |
| 108 |
| 109 // Owns the pointed-to Contact values. |
| 110 ContactMap contacts_; |
| 111 |
| 112 // Deletes values in |contacts_|. |
| 113 STLValueDeleter<ContactMap> contacts_deleter_; |
| 114 |
| 115 // Most-recent time that an entry in |contacts_| has been updated (within |
| 116 // Google). |
| 117 base::Time last_update_time_; |
| 118 |
| 119 // Used to save contacts to disk and restore them at startup. |
| 120 scoped_ptr<ContactDatabaseInterface> db_; |
| 121 |
| 122 // If non-NULL, used in place of the real GData service to download contacts. |
| 123 scoped_ptr<gdata::GDataContactsServiceInterface> gdata_service_for_testing_; |
| 124 |
| 125 // Used to schedule calls to UpdateContacts(). |
| 126 base::OneShotTimer<GoogleContactSource> update_timer_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(GoogleContactSource); |
| 129 }; |
| 130 |
| 131 } // namespace contacts |
| 132 |
| 133 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_SOURCE_H_ |
OLD | NEW |