[email protected] | 24c9ee5 | 2014-06-02 22:17:50 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4482877 | 2014-06-06 02:56:52 | [diff] [blame] | 5 | #include "components/invalidation/invalidation_notifier.h" |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 6 | |
[email protected] | 8a50f99 | 2012-08-09 01:11:32 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 0a80fed | 2011-03-24 22:31:48 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 9 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 0857ae0b | 2012-05-31 00:39:26 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 11 | #include "components/invalidation/invalidation_handler.h" |
[email protected] | 8a50f99 | 2012-08-09 01:11:32 | [diff] [blame] | 12 | #include "google/cacheinvalidation/include/invalidation-client-factory.h" |
[email protected] | 1f7eba7 | 2012-05-30 09:10:17 | [diff] [blame] | 13 | #include "jingle/notifier/listener/push_client.h" |
[email protected] | 942e202 | 2011-04-07 22:27:43 | [diff] [blame] | 14 | #include "net/url_request/url_request_context.h" |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 15 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 16 | namespace syncer { |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 17 | |
| 18 | InvalidationNotifier::InvalidationNotifier( |
[email protected] | 04a830a | 2014-01-04 02:48:51 | [diff] [blame] | 19 | scoped_ptr<SyncNetworkChannel> network_channel, |
[email protected] | d551123 | 2013-03-28 01:34:54 | [diff] [blame] | 20 | const std::string& invalidator_client_id, |
[email protected] | a7b1639 | 2013-11-26 22:46:26 | [diff] [blame] | 21 | const UnackedInvalidationsMap& saved_invalidations, |
[email protected] | 8cdb689 | 2012-10-03 05:54:40 | [diff] [blame] | 22 | const std::string& invalidation_bootstrap_data, |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 23 | const base::WeakPtr<InvalidationStateTracker>& invalidation_state_tracker, |
| 24 | scoped_refptr<base::SingleThreadTaskRunner> |
| 25 | invalidation_state_tracker_task_runner, |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 26 | const std::string& client_info) |
[email protected] | 0a80fed | 2011-03-24 22:31:48 | [diff] [blame] | 27 | : state_(STOPPED), |
[email protected] | a7b1639 | 2013-11-26 22:46:26 | [diff] [blame] | 28 | saved_invalidations_(saved_invalidations), |
[email protected] | 46e43ee | 2012-05-18 19:24:41 | [diff] [blame] | 29 | invalidation_state_tracker_(invalidation_state_tracker), |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 30 | invalidation_state_tracker_task_runner_( |
| 31 | invalidation_state_tracker_task_runner), |
[email protected] | 1f7eba7 | 2012-05-30 09:10:17 | [diff] [blame] | 32 | client_info_(client_info), |
[email protected] | d551123 | 2013-03-28 01:34:54 | [diff] [blame] | 33 | invalidator_client_id_(invalidator_client_id), |
[email protected] | 8cdb689 | 2012-10-03 05:54:40 | [diff] [blame] | 34 | invalidation_bootstrap_data_(invalidation_bootstrap_data), |
[email protected] | 04a830a | 2014-01-04 02:48:51 | [diff] [blame] | 35 | invalidation_listener_(network_channel.Pass()) { |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | InvalidationNotifier::~InvalidationNotifier() { |
[email protected] | 870bcb3 | 2012-06-11 22:27:54 | [diff] [blame] | 39 | DCHECK(CalledOnValidThread()); |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | a329cb8 | 2012-08-28 03:17:58 | [diff] [blame] | 42 | void InvalidationNotifier::RegisterHandler(InvalidationHandler* handler) { |
[email protected] | 85b25eb | 2012-08-10 19:32:08 | [diff] [blame] | 43 | DCHECK(CalledOnValidThread()); |
| 44 | registrar_.RegisterHandler(handler); |
| 45 | } |
| 46 | |
[email protected] | a329cb8 | 2012-08-28 03:17:58 | [diff] [blame] | 47 | void InvalidationNotifier::UpdateRegisteredIds(InvalidationHandler* handler, |
[email protected] | d914f02 | 2012-07-27 02:02:00 | [diff] [blame] | 48 | const ObjectIdSet& ids) { |
[email protected] | 870bcb3 | 2012-06-11 22:27:54 | [diff] [blame] | 49 | DCHECK(CalledOnValidThread()); |
[email protected] | 85b25eb | 2012-08-10 19:32:08 | [diff] [blame] | 50 | registrar_.UpdateRegisteredIds(handler, ids); |
[email protected] | 1f7893fa | 2012-09-06 03:06:13 | [diff] [blame] | 51 | invalidation_listener_.UpdateRegisteredIds(registrar_.GetAllRegisteredIds()); |
[email protected] | 85b25eb | 2012-08-10 19:32:08 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | a329cb8 | 2012-08-28 03:17:58 | [diff] [blame] | 54 | void InvalidationNotifier::UnregisterHandler(InvalidationHandler* handler) { |
[email protected] | 85b25eb | 2012-08-10 19:32:08 | [diff] [blame] | 55 | DCHECK(CalledOnValidThread()); |
| 56 | registrar_.UnregisterHandler(handler); |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 08a6f999 | 2012-09-07 19:19:16 | [diff] [blame] | 59 | InvalidatorState InvalidationNotifier::GetInvalidatorState() const { |
| 60 | DCHECK(CalledOnValidThread()); |
| 61 | return registrar_.GetInvalidatorState(); |
| 62 | } |
| 63 | |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 64 | void InvalidationNotifier::UpdateCredentials( |
| 65 | const std::string& email, const std::string& token) { |
[email protected] | 1f7eba7 | 2012-05-30 09:10:17 | [diff] [blame] | 66 | if (state_ == STOPPED) { |
[email protected] | 1f7893fa | 2012-09-06 03:06:13 | [diff] [blame] | 67 | invalidation_listener_.Start( |
[email protected] | 8a50f99 | 2012-08-09 01:11:32 | [diff] [blame] | 68 | base::Bind(&invalidation::CreateInvalidationClient), |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 69 | invalidator_client_id_, |
| 70 | client_info_, |
| 71 | invalidation_bootstrap_data_, |
[email protected] | a7b1639 | 2013-11-26 22:46:26 | [diff] [blame] | 72 | saved_invalidations_, |
[email protected] | 1f7eba7 | 2012-05-30 09:10:17 | [diff] [blame] | 73 | invalidation_state_tracker_, |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 74 | invalidation_state_tracker_task_runner_, |
[email protected] | 0857ae0b | 2012-05-31 00:39:26 | [diff] [blame] | 75 | this); |
[email protected] | 1f7eba7 | 2012-05-30 09:10:17 | [diff] [blame] | 76 | state_ = STARTED; |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 77 | } |
[email protected] | 1f7893fa | 2012-09-06 03:06:13 | [diff] [blame] | 78 | invalidation_listener_.UpdateCredentials(email, token); |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 81 | void InvalidationNotifier::RequestDetailedStatus( |
[email protected] | 40848a9 | 2014-03-24 22:41:02 | [diff] [blame] | 82 | base::Callback<void(const base::DictionaryValue&)> callback) const { |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 83 | DCHECK(CalledOnValidThread()); |
| 84 | invalidation_listener_.RequestDetailedStatus(callback); |
| 85 | } |
| 86 | |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 87 | void InvalidationNotifier::OnInvalidate( |
| 88 | const ObjectIdInvalidationMap& invalidation_map) { |
[email protected] | 870bcb3 | 2012-06-11 22:27:54 | [diff] [blame] | 89 | DCHECK(CalledOnValidThread()); |
[email protected] | 0d0b59d | 2013-02-02 00:24:47 | [diff] [blame] | 90 | registrar_.DispatchInvalidationsToHandlers(invalidation_map); |
[email protected] | 0a80fed | 2011-03-24 22:31:48 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 08a6f999 | 2012-09-07 19:19:16 | [diff] [blame] | 93 | void InvalidationNotifier::OnInvalidatorStateChange(InvalidatorState state) { |
[email protected] | 2d3d1d1 | 2012-06-18 20:50:28 | [diff] [blame] | 94 | DCHECK(CalledOnValidThread()); |
[email protected] | 08a6f999 | 2012-09-07 19:19:16 | [diff] [blame] | 95 | registrar_.UpdateInvalidatorState(state); |
[email protected] | 0a80fed | 2011-03-24 22:31:48 | [diff] [blame] | 96 | } |
[email protected] | 08dd831 | 2011-03-19 20:09:31 | [diff] [blame] | 97 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 98 | } // namespace syncer |