OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" | 5 #include "chrome/browser/chromeos/contacts/google_contact_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 void GoogleContactStore::TestAPI::SetGDataService( | 68 void GoogleContactStore::TestAPI::SetGDataService( |
69 gdata::GDataContactsServiceInterface* service) { | 69 gdata::GDataContactsServiceInterface* service) { |
70 store_->gdata_service_for_testing_.reset(service); | 70 store_->gdata_service_for_testing_.reset(service); |
71 } | 71 } |
72 | 72 |
73 void GoogleContactStore::TestAPI::DoUpdate() { | 73 void GoogleContactStore::TestAPI::DoUpdate() { |
74 store_->UpdateContacts(); | 74 store_->UpdateContacts(); |
75 } | 75 } |
76 | 76 |
| 77 void GoogleContactStore::TestAPI::NotifyAboutNetworkStateChange(bool online) { |
| 78 net::NetworkChangeNotifier::ConnectionType type = |
| 79 online ? |
| 80 net::NetworkChangeNotifier::CONNECTION_UNKNOWN : |
| 81 net::NetworkChangeNotifier::CONNECTION_NONE; |
| 82 store_->OnConnectionTypeChanged(type); |
| 83 } |
| 84 |
77 GoogleContactStore::GoogleContactStore(Profile* profile) | 85 GoogleContactStore::GoogleContactStore(Profile* profile) |
78 : profile_(profile), | 86 : profile_(profile), |
79 contacts_deleter_(&contacts_), | 87 contacts_deleter_(&contacts_), |
80 db_(new ContactDatabase), | 88 db_(new ContactDatabase), |
81 update_delay_on_next_failure_( | 89 update_delay_on_next_failure_( |
82 base::TimeDelta::FromSeconds(kUpdateFailureInitialRetrySec)), | 90 base::TimeDelta::FromSeconds(kUpdateFailureInitialRetrySec)), |
| 91 is_online_(true), |
| 92 should_update_when_online_(false), |
83 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 93 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 95 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 96 is_online_ = !net::NetworkChangeNotifier::IsOffline(); |
85 } | 97 } |
86 | 98 |
87 GoogleContactStore::~GoogleContactStore() { | 99 GoogleContactStore::~GoogleContactStore() { |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
89 weak_ptr_factory_.InvalidateWeakPtrs(); | 101 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 102 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
90 DestroyDatabase(); | 103 DestroyDatabase(); |
91 } | 104 } |
92 | 105 |
93 void GoogleContactStore::Init() { | 106 void GoogleContactStore::Init() { |
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
95 FilePath db_path = profile_->GetPath().Append(kDatabaseDirectoryName); | 108 FilePath db_path = profile_->GetPath().Append(kDatabaseDirectoryName); |
96 VLOG(1) << "Initializing contact database \"" << db_path.value() << "\" for " | 109 VLOG(1) << "Initializing contact database \"" << db_path.value() << "\" for " |
97 << profile_->GetProfileName(); | 110 << profile_->GetProfileName(); |
98 db_->Init(db_path, | 111 db_->Init(db_path, |
99 base::Bind(&GoogleContactStore::OnDatabaseInitialized, | 112 base::Bind(&GoogleContactStore::OnDatabaseInitialized, |
(...skipping 22 matching lines...) Expand all Loading... |
122 DCHECK(observer); | 135 DCHECK(observer); |
123 observers_.AddObserver(observer); | 136 observers_.AddObserver(observer); |
124 } | 137 } |
125 | 138 |
126 void GoogleContactStore::RemoveObserver(ContactStoreObserver* observer) { | 139 void GoogleContactStore::RemoveObserver(ContactStoreObserver* observer) { |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
128 DCHECK(observer); | 141 DCHECK(observer); |
129 observers_.RemoveObserver(observer); | 142 observers_.RemoveObserver(observer); |
130 } | 143 } |
131 | 144 |
| 145 void GoogleContactStore::OnConnectionTypeChanged( |
| 146 net::NetworkChangeNotifier::ConnectionType type) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 148 bool was_online = is_online_; |
| 149 is_online_ = (type != net::NetworkChangeNotifier::CONNECTION_NONE); |
| 150 if (!was_online && is_online_ && should_update_when_online_) { |
| 151 should_update_when_online_ = false; |
| 152 UpdateContacts(); |
| 153 } |
| 154 } |
| 155 |
132 base::Time GoogleContactStore::GetCurrentTime() const { | 156 base::Time GoogleContactStore::GetCurrentTime() const { |
133 return !current_time_for_testing_.is_null() ? | 157 return !current_time_for_testing_.is_null() ? |
134 current_time_for_testing_ : | 158 current_time_for_testing_ : |
135 base::Time::Now(); | 159 base::Time::Now(); |
136 } | 160 } |
137 | 161 |
138 void GoogleContactStore::DestroyDatabase() { | 162 void GoogleContactStore::DestroyDatabase() { |
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
140 if (db_) { | 164 if (db_) { |
141 db_->DestroyOnUIThread(); | 165 db_->DestroyOnUIThread(); |
142 db_ = NULL; | 166 db_ = NULL; |
143 } | 167 } |
144 } | 168 } |
145 | 169 |
146 void GoogleContactStore::UpdateContacts() { | 170 void GoogleContactStore::UpdateContacts() { |
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 172 |
| 173 // If we're offline, defer the update. |
| 174 if (!is_online_) { |
| 175 VLOG(1) << "Deferring contact update due to offline state"; |
| 176 should_update_when_online_ = true; |
| 177 return; |
| 178 } |
| 179 |
148 base::Time min_update_time; | 180 base::Time min_update_time; |
149 base::TimeDelta time_since_last_update = | 181 base::TimeDelta time_since_last_update = |
150 last_successful_update_start_time_.is_null() ? | 182 last_successful_update_start_time_.is_null() ? |
151 base::TimeDelta() : | 183 base::TimeDelta() : |
152 GetCurrentTime() - last_successful_update_start_time_; | 184 GetCurrentTime() - last_successful_update_start_time_; |
153 | 185 |
154 if (!last_contact_update_time_.is_null() && | 186 if (!last_contact_update_time_.is_null() && |
155 time_since_last_update < | 187 time_since_last_update < |
156 base::TimeDelta::FromDays(kForceFullUpdateDays)) { | 188 base::TimeDelta::FromDays(kForceFullUpdateDays)) { |
157 // TODO(derat): I'm adding one millisecond to the last update time here as I | 189 // TODO(derat): I'm adding one millisecond to the last update time here as I |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 return gdata::util::IsGDataAvailable(profile); | 400 return gdata::util::IsGDataAvailable(profile); |
369 } | 401 } |
370 | 402 |
371 ContactStore* GoogleContactStoreFactory::CreateContactStore(Profile* profile) { | 403 ContactStore* GoogleContactStoreFactory::CreateContactStore(Profile* profile) { |
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
373 DCHECK(CanCreateContactStoreForProfile(profile)); | 405 DCHECK(CanCreateContactStoreForProfile(profile)); |
374 return new GoogleContactStore(profile); | 406 return new GoogleContactStore(profile); |
375 } | 407 } |
376 | 408 |
377 } // namespace contacts | 409 } // namespace contacts |
OLD | NEW |