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_STORE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ |
| 7 |
| 8 #include "chrome/browser/chromeos/contacts/contact_store.h" |
| 9 |
| 10 #include <map> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/compiler_specific.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/scoped_vector.h" |
| 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/observer_list.h" |
| 20 #include "base/stl_util.h" |
| 21 #include "base/time.h" |
| 22 #include "base/timer.h" |
| 23 |
| 24 class Profile; |
| 25 |
| 26 namespace gdata { |
| 27 class GDataContactsServiceInterface; |
| 28 } |
| 29 |
| 30 namespace contacts { |
| 31 |
| 32 class Contact; |
| 33 class ContactDatabaseInterface; |
| 34 class UpdateMetadata; |
| 35 |
| 36 // A collection of contacts from a Google account. |
| 37 class GoogleContactStore : public ContactStore { |
| 38 public: |
| 39 class TestAPI { |
| 40 public: |
| 41 explicit TestAPI(GoogleContactStore* store); |
| 42 ~TestAPI(); |
| 43 |
| 44 bool update_scheduled() { return store_->update_timer_.IsRunning(); } |
| 45 |
| 46 void set_current_time(const base::Time& time) { |
| 47 store_->current_time_for_testing_ = time; |
| 48 } |
| 49 |
| 50 // Takes ownership of |db|. Must be called before Init(). |
| 51 void SetDatabase(ContactDatabaseInterface* db); |
| 52 |
| 53 // Takes ownership of |service|. Must be called before Init(). |
| 54 void SetGDataService(gdata::GDataContactsServiceInterface* service); |
| 55 |
| 56 // Triggers an update, similar to what happens when the update timer fires. |
| 57 void DoUpdate(); |
| 58 |
| 59 private: |
| 60 GoogleContactStore* store_; // not owned |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestAPI); |
| 63 }; |
| 64 |
| 65 explicit GoogleContactStore(Profile* profile); |
| 66 virtual ~GoogleContactStore(); |
| 67 |
| 68 void Init(); |
| 69 |
| 70 // ContactStore implementation: |
| 71 virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; |
| 72 virtual const Contact* GetContactByProviderId( |
| 73 const std::string& provider_id) OVERRIDE; |
| 74 virtual void AddObserver(ContactStoreObserver* observer) OVERRIDE; |
| 75 virtual void RemoveObserver(ContactStoreObserver* observer) OVERRIDE; |
| 76 |
| 77 private: |
| 78 // Map from a contact's Google-assigned ID to the contact itself. |
| 79 typedef std::map<std::string, Contact*> ContactMap; |
| 80 |
| 81 // Returns the current time. Uses |current_time_for_testing_| instead if it's |
| 82 // set. |
| 83 base::Time GetCurrentTime() const; |
| 84 |
| 85 // Destroys |db_| if non-NULL and resets the pointer. |
| 86 // The underlying data is preserved on-disk. |
| 87 void DestroyDatabase(); |
| 88 |
| 89 // Asynchronously downloads updated contacts and merges them into |contacts_|. |
| 90 void UpdateContacts(); |
| 91 |
| 92 // Starts |update_timer_| so UpdateContacts() will be run. |
| 93 void ScheduleUpdate(bool last_update_was_successful); |
| 94 |
| 95 // Moves |updated_contacts| into |contacts_| and updates |
| 96 // |last_contact_update_time_|. |
| 97 void MergeContacts(bool is_full_update, |
| 98 scoped_ptr<ScopedVector<Contact> > updated_contacts); |
| 99 |
| 100 // Handles a successful download, merging |updated_contacts| and saving the |
| 101 // updated contacts to |db_|. |
| 102 void OnDownloadSuccess(bool is_full_update, |
| 103 const base::Time& update_start_time, |
| 104 scoped_ptr<ScopedVector<Contact> > updated_contacts); |
| 105 |
| 106 // Handles a failed update. A new update is scheduled. |
| 107 void OnDownloadFailure(); |
| 108 |
| 109 // Handles |db_|'s initialization. On success, we start loading the contacts |
| 110 // from the database; otherwise, we throw out the database and schedule an |
| 111 // update. |
| 112 void OnDatabaseInitialized(bool success); |
| 113 |
| 114 // Handles contacts being loaded from |db_|. On success, we merge the loaded |
| 115 // contacts. No matter what, we call UpdateContacts(). |
| 116 void OnDatabaseContactsLoaded(bool success, |
| 117 scoped_ptr<ScopedVector<Contact> > contacts, |
| 118 scoped_ptr<UpdateMetadata> metadata); |
| 119 |
| 120 // Handles contacts being saved to |db_|. Now that the contacts are no longer |
| 121 // being accessed by the database, we schedule an update. |
| 122 void OnDatabaseContactsSaved(bool success); |
| 123 |
| 124 Profile* profile_; // not owned |
| 125 |
| 126 ObserverList<ContactStoreObserver> observers_; |
| 127 |
| 128 // Owns the pointed-to Contact values. |
| 129 ContactMap contacts_; |
| 130 |
| 131 // Deletes values in |contacts_|. |
| 132 STLValueDeleter<ContactMap> contacts_deleter_; |
| 133 |
| 134 // Most-recent time that an entry in |contacts_| has been updated (as reported |
| 135 // by Google). |
| 136 base::Time last_contact_update_time_; |
| 137 |
| 138 // Used to save contacts to disk and load them at startup. Owns the object. |
| 139 ContactDatabaseInterface* db_; |
| 140 |
| 141 // If non-NULL, used in place of the real GData service to download contacts. |
| 142 scoped_ptr<gdata::GDataContactsServiceInterface> gdata_service_for_testing_; |
| 143 |
| 144 // Used to schedule calls to UpdateContacts(). |
| 145 base::OneShotTimer<GoogleContactStore> update_timer_; |
| 146 |
| 147 // Time at which the last successful update was started. |
| 148 base::Time last_successful_update_start_time_; |
| 149 |
| 150 // Amount of time that we'll wait before retrying the next time that an update |
| 151 // fails. |
| 152 base::TimeDelta update_delay_on_next_failure_; |
| 153 |
| 154 // If non-null, used in place of base::Time::Now() when the current time is |
| 155 // needed. |
| 156 base::Time current_time_for_testing_; |
| 157 |
| 158 // Note: This should remain the last member so it'll be destroyed and |
| 159 // invalidate its weak pointers before any other members are destroyed. |
| 160 base::WeakPtrFactory<GoogleContactStore> weak_ptr_factory_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(GoogleContactStore); |
| 163 }; |
| 164 |
| 165 } // namespace contacts |
| 166 |
| 167 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ |
OLD | NEW |