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/time/time.h" | |
21 #include "base/timer/timer.h" | |
22 #include "chrome/browser/chromeos/contacts/contact_map.h" | |
23 #include "net/base/network_change_notifier.h" | |
24 | |
25 class Profile; | |
26 | |
27 namespace net { | |
28 class URLRequestContextGetter; | |
29 } // namespace net | |
30 | |
31 namespace contacts { | |
32 | |
33 class Contact; | |
34 class ContactDatabaseInterface; | |
35 class GDataContactsServiceInterface; | |
36 class UpdateMetadata; | |
37 | |
38 // A collection of contacts from a Google account. | |
39 class GoogleContactStore | |
40 : public ContactStore, | |
41 public net::NetworkChangeNotifier::ConnectionTypeObserver { | |
42 public: | |
43 class TestAPI { | |
44 public: | |
45 explicit TestAPI(GoogleContactStore* store); | |
46 ~TestAPI(); | |
47 | |
48 bool update_scheduled() { return store_->update_timer_.IsRunning(); } | |
49 base::Time last_contact_update_time() const { | |
50 return store_->last_contact_update_time_; | |
51 } | |
52 void set_current_time(const base::Time& time) { | |
53 store_->current_time_for_testing_ = time; | |
54 } | |
55 | |
56 // Takes ownership of |db|. Must be called before Init(). | |
57 void SetDatabase(ContactDatabaseInterface* db); | |
58 | |
59 // Takes ownership of |service|. Must be called before Init(). The caller is | |
60 // responsible for calling |service|'s Initialize() method. | |
61 void SetGDataService(GDataContactsServiceInterface* service); | |
62 | |
63 // Triggers an update, similar to what happens when the update timer fires. | |
64 void DoUpdate(); | |
65 | |
66 // Notifies the store that the system has gone online or offline. | |
67 void NotifyAboutNetworkStateChange(bool online); | |
68 | |
69 // Returns pointers to all of the contacts in the store's |contacts_| | |
70 // member. | |
71 scoped_ptr<ContactPointers> GetLoadedContacts(); | |
72 | |
73 private: | |
74 GoogleContactStore* store_; // not owned | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(TestAPI); | |
77 }; | |
78 | |
79 GoogleContactStore( | |
80 net::URLRequestContextGetter* url_request_context_getter, | |
81 Profile* profile); | |
82 virtual ~GoogleContactStore(); | |
83 | |
84 // ContactStore implementation: | |
85 virtual void Init() OVERRIDE; | |
86 virtual void AppendContacts(ContactPointers* contacts_out) OVERRIDE; | |
87 virtual const Contact* GetContactById(const std::string& contact_id) OVERRIDE; | |
88 virtual void AddObserver(ContactStoreObserver* observer) OVERRIDE; | |
89 virtual void RemoveObserver(ContactStoreObserver* observer) OVERRIDE; | |
90 | |
91 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation: | |
92 virtual void OnConnectionTypeChanged( | |
93 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | |
94 | |
95 private: | |
96 // Returns the current time. Uses |current_time_for_testing_| instead if it's | |
97 // set. | |
98 base::Time GetCurrentTime() const; | |
99 | |
100 // Destroys |db_| if non-NULL and resets the pointer. | |
101 // The underlying data is preserved on-disk. | |
102 void DestroyDatabase(); | |
103 | |
104 // Asynchronously downloads updated contacts and merges them into |contacts_|. | |
105 void UpdateContacts(); | |
106 | |
107 // Starts |update_timer_| so UpdateContacts() will be run. | |
108 void ScheduleUpdate(bool last_update_was_successful); | |
109 | |
110 // Moves |updated_contacts| into |contacts_| and updates | |
111 // |last_contact_update_time_|. | |
112 void MergeContacts(bool is_full_update, | |
113 scoped_ptr<ScopedVector<Contact> > updated_contacts); | |
114 | |
115 // Handles a successful download, merging |updated_contacts| and saving the | |
116 // updated contacts to |db_|. | |
117 void OnDownloadSuccess(bool is_full_update, | |
118 const base::Time& update_start_time, | |
119 scoped_ptr<ScopedVector<Contact> > updated_contacts); | |
120 | |
121 // Handles a failed update. A new update is scheduled. | |
122 void OnDownloadFailure(); | |
123 | |
124 // Handles |db_|'s initialization. On success, we start loading the contacts | |
125 // from the database; otherwise, we throw out the database and schedule an | |
126 // update. | |
127 void OnDatabaseInitialized(bool success); | |
128 | |
129 // Handles contacts being loaded from |db_|. On success, we merge the loaded | |
130 // contacts. No matter what, we call UpdateContacts(). | |
131 void OnDatabaseContactsLoaded(bool success, | |
132 scoped_ptr<ScopedVector<Contact> > contacts, | |
133 scoped_ptr<UpdateMetadata> metadata); | |
134 | |
135 // Handles contacts being saved to |db_|. Now that the contacts are no longer | |
136 // being accessed by the database, we schedule an update. | |
137 void OnDatabaseContactsSaved(bool success); | |
138 | |
139 net::URLRequestContextGetter* url_request_context_getter_; // not owned | |
140 | |
141 Profile* profile_; // not owned | |
142 | |
143 ObserverList<ContactStoreObserver> observers_; | |
144 | |
145 // Owns the pointed-to Contact values. | |
146 ContactMap contacts_; | |
147 | |
148 // Most-recent time that an entry in |contacts_| has been updated (as reported | |
149 // by Google). | |
150 base::Time last_contact_update_time_; | |
151 | |
152 // Used to download contacts. | |
153 scoped_ptr<GDataContactsServiceInterface> gdata_service_; | |
154 | |
155 // Used to save contacts to disk and load them at startup. Owns the object. | |
156 ContactDatabaseInterface* db_; | |
157 | |
158 // Used to schedule calls to UpdateContacts(). | |
159 base::OneShotTimer<GoogleContactStore> update_timer_; | |
160 | |
161 // Time at which the last successful update was started. | |
162 base::Time last_successful_update_start_time_; | |
163 | |
164 // Amount of time that we'll wait before retrying the next time that an update | |
165 // fails. | |
166 base::TimeDelta update_delay_on_next_failure_; | |
167 | |
168 // Do we believe that it's likely that we'll be able to make network | |
169 // connections? | |
170 bool is_online_; | |
171 | |
172 // Should we call UpdateContacts() when |is_online_| becomes true? Set when | |
173 // UpdateContacts() is called while we're offline. | |
174 bool should_update_when_online_; | |
175 | |
176 // If non-null, used in place of base::Time::Now() when the current time is | |
177 // needed. | |
178 base::Time current_time_for_testing_; | |
179 | |
180 // Note: This should remain the last member so it'll be destroyed and | |
181 // invalidate its weak pointers before any other members are destroyed. | |
182 base::WeakPtrFactory<GoogleContactStore> weak_ptr_factory_; | |
183 | |
184 DISALLOW_COPY_AND_ASSIGN(GoogleContactStore); | |
185 }; | |
186 | |
187 // ContactStoreFactory implementation that returns GoogleContactStores. | |
188 class GoogleContactStoreFactory : public ContactStoreFactory { | |
189 public: | |
190 GoogleContactStoreFactory(); | |
191 virtual ~GoogleContactStoreFactory(); | |
192 | |
193 // ContactStoreFactory implementation: | |
194 virtual bool CanCreateContactStoreForProfile(Profile* profile) OVERRIDE; | |
195 virtual ContactStore* CreateContactStore(Profile* profile) OVERRIDE; | |
196 | |
197 private: | |
198 DISALLOW_COPY_AND_ASSIGN(GoogleContactStoreFactory); | |
199 }; | |
200 | |
201 } // namespace contacts | |
202 | |
203 #endif // CHROME_BROWSER_CHROMEOS_CONTACTS_GOOGLE_CONTACT_STORE_H_ | |
OLD | NEW |