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