OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/sync/notifier/server_notifier_thread.h" | 5 #include "chrome/browser/sync/notifier/server_notifier_thread.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" | |
12 #include "chrome/browser/sync/notifier/chrome_invalidation_client.h" | 11 #include "chrome/browser/sync/notifier/chrome_invalidation_client.h" |
13 #include "chrome/browser/sync/notifier/chrome_system_resources.h" | |
14 #include "chrome/browser/sync/notifier/invalidation_util.h" | |
15 #include "chrome/browser/sync/syncable/model_type.h" | |
16 #include "chrome/common/net/notifier/listener/notification_defines.h" | 12 #include "chrome/common/net/notifier/listener/notification_defines.h" |
17 #include "google/cacheinvalidation/invalidation-client-impl.h" | |
18 #include "talk/xmpp/jid.h" | |
19 | 13 |
20 namespace sync_notifier { | 14 namespace sync_notifier { |
21 | 15 |
22 ServerNotifierThread::ServerNotifierThread() {} | 16 ServerNotifierThread::ServerNotifierThread() {} |
23 | 17 |
24 ServerNotifierThread::~ServerNotifierThread() {} | 18 ServerNotifierThread::~ServerNotifierThread() {} |
25 | 19 |
26 void ServerNotifierThread::ListenForUpdates() { | 20 void ServerNotifierThread::ListenForUpdates() { |
27 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 21 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
28 worker_message_loop()->PostTask( | 22 worker_message_loop()->PostTask( |
(...skipping 20 matching lines...) Expand all Loading... |
49 MediatorThreadImpl::Logout(); | 43 MediatorThreadImpl::Logout(); |
50 } | 44 } |
51 | 45 |
52 void ServerNotifierThread::SendNotification( | 46 void ServerNotifierThread::SendNotification( |
53 const OutgoingNotificationData& data) { | 47 const OutgoingNotificationData& data) { |
54 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 48 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
55 NOTREACHED() << "Shouldn't send notifications if " | 49 NOTREACHED() << "Shouldn't send notifications if " |
56 << "ServerNotifierThread is used"; | 50 << "ServerNotifierThread is used"; |
57 } | 51 } |
58 | 52 |
59 void ServerNotifierThread::Invalidate( | 53 void ServerNotifierThread::OnInvalidate(syncable::ModelType model_type) { |
60 const invalidation::Invalidation& invalidation, | |
61 invalidation::Closure* callback) { | |
62 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 54 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
63 CHECK(invalidation::IsCallbackRepeatable(callback)); | 55 LOG(INFO) << "OnInvalidate: " << syncable::ModelTypeToString(model_type); |
64 LOG(INFO) << "Invalidate: " << InvalidationToString(invalidation); | |
65 // TODO(akalin): Signal notification only for the invalidated types. | 56 // TODO(akalin): Signal notification only for the invalidated types. |
66 parent_message_loop_->PostTask( | 57 parent_message_loop_->PostTask( |
67 FROM_HERE, | 58 FROM_HERE, |
68 NewRunnableMethod( | 59 NewRunnableMethod( |
69 this, | 60 this, |
70 &ServerNotifierThread::SignalIncomingNotification)); | 61 &ServerNotifierThread::SignalIncomingNotification)); |
71 RunAndDeleteClosure(callback); | |
72 } | 62 } |
73 | 63 |
74 void ServerNotifierThread::InvalidateAll( | 64 void ServerNotifierThread::OnInvalidateAll() { |
75 invalidation::Closure* callback) { | |
76 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 65 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
77 CHECK(invalidation::IsCallbackRepeatable(callback)); | 66 LOG(INFO) << "OnInvalidateAll"; |
78 LOG(INFO) << "InvalidateAll"; | |
79 parent_message_loop_->PostTask( | 67 parent_message_loop_->PostTask( |
80 FROM_HERE, | 68 FROM_HERE, |
81 NewRunnableMethod( | 69 NewRunnableMethod( |
82 this, | 70 this, |
83 &ServerNotifierThread::SignalIncomingNotification)); | 71 &ServerNotifierThread::SignalIncomingNotification)); |
84 RunAndDeleteClosure(callback); | |
85 } | |
86 | |
87 void ServerNotifierThread::AllRegistrationsLost( | |
88 invalidation::Closure* callback) { | |
89 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | |
90 CHECK(invalidation::IsCallbackRepeatable(callback)); | |
91 LOG(INFO) << "AllRegistrationsLost; reregistering"; | |
92 RegisterTypes(); | |
93 RunAndDeleteClosure(callback); | |
94 } | |
95 | |
96 void ServerNotifierThread::RegistrationLost( | |
97 const invalidation::ObjectId& object_id, | |
98 invalidation::Closure* callback) { | |
99 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | |
100 CHECK(invalidation::IsCallbackRepeatable(callback)); | |
101 LOG(INFO) << "RegistrationLost; reregistering: " | |
102 << ObjectIdToString(object_id); | |
103 RegisterTypes(); | |
104 RunAndDeleteClosure(callback); | |
105 } | 72 } |
106 | 73 |
107 void ServerNotifierThread::StartInvalidationListener() { | 74 void ServerNotifierThread::StartInvalidationListener() { |
108 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 75 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
109 | 76 |
110 StopInvalidationListener(); | 77 StopInvalidationListener(); |
111 chrome_invalidation_client_.reset(new ChromeInvalidationClient()); | 78 chrome_invalidation_client_.reset(new ChromeInvalidationClient()); |
112 | 79 |
113 // TODO(akalin): Make cache_guid() part of the client ID. If we do | 80 // TODO(akalin): Make cache_guid() part of the client ID. If we do |
114 // so and we somehow propagate it up to the server somehow, we can | 81 // so and we somehow propagate it up to the server somehow, we can |
115 // make it so that we won't receive any notifications that were | 82 // make it so that we won't receive any notifications that were |
116 // generated from our own changes. | 83 // generated from our own changes. |
117 const std::string kClientId = "server_notifier_thread"; | 84 const std::string kClientId = "server_notifier_thread"; |
118 chrome_invalidation_client_->Start(kClientId, this, xmpp_client()); | 85 chrome_invalidation_client_->Start(kClientId, this, xmpp_client()); |
119 } | 86 } |
120 | 87 |
121 void ServerNotifierThread::RegisterTypesAndSignalSubscribed() { | 88 void ServerNotifierThread::RegisterTypesAndSignalSubscribed() { |
122 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 89 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
123 RegisterTypes(); | 90 chrome_invalidation_client_->RegisterTypes(); |
124 parent_message_loop_->PostTask( | 91 parent_message_loop_->PostTask( |
125 FROM_HERE, | 92 FROM_HERE, |
126 NewRunnableMethod( | 93 NewRunnableMethod( |
127 this, | 94 this, |
128 &ServerNotifierThread::SignalSubscribed)); | 95 &ServerNotifierThread::SignalSubscribed)); |
129 } | 96 } |
130 | 97 |
131 void ServerNotifierThread::RegisterTypes() { | |
132 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | |
133 | |
134 // TODO(akalin): Make this configurable instead of listening to | |
135 // notifications for all possible types. | |
136 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
137 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
138 syncable::ModelType model_type = syncable::ModelTypeFromInt(i); | |
139 std::string notification_type; | |
140 if (!syncable::RealModelTypeToNotificationType( | |
141 model_type, ¬ification_type)) { | |
142 LOG(ERROR) << "Could not get notification type for model type " | |
143 << syncable::ModelTypeToString(model_type); | |
144 continue; | |
145 } | |
146 invalidation::ObjectId object_id; | |
147 object_id.mutable_name()->set_string_value(notification_type); | |
148 object_id.set_source(invalidation::ObjectId::CHROME_SYNC); | |
149 chrome_invalidation_client_->Register( | |
150 object_id, | |
151 invalidation::NewPermanentCallback( | |
152 this, &ServerNotifierThread::RegisterCallback)); | |
153 } | |
154 } | |
155 | |
156 void ServerNotifierThread::RegisterCallback( | |
157 const invalidation::RegistrationUpdateResult& result) { | |
158 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | |
159 // TODO(akalin): Do something meaningful here. | |
160 LOG(INFO) << "Registered: " << RegistrationUpdateResultToString(result); | |
161 } | |
162 | |
163 void ServerNotifierThread::SignalSubscribed() { | 98 void ServerNotifierThread::SignalSubscribed() { |
164 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 99 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
165 if (delegate_) { | 100 if (delegate_) { |
166 delegate_->OnSubscriptionStateChange(true); | 101 delegate_->OnSubscriptionStateChange(true); |
167 } | 102 } |
168 } | 103 } |
169 | 104 |
170 void ServerNotifierThread::SignalIncomingNotification() { | 105 void ServerNotifierThread::SignalIncomingNotification() { |
171 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 106 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
172 if (delegate_) { | 107 if (delegate_) { |
173 // TODO(akalin): Fill this in with something meaningful. | 108 // TODO(akalin): Fill this in with something meaningful. |
174 IncomingNotificationData notification_data; | 109 IncomingNotificationData notification_data; |
175 delegate_->OnIncomingNotification(notification_data); | 110 delegate_->OnIncomingNotification(notification_data); |
176 } | 111 } |
177 } | 112 } |
178 | 113 |
179 void ServerNotifierThread::StopInvalidationListener() { | 114 void ServerNotifierThread::StopInvalidationListener() { |
180 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 115 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
181 | |
182 if (chrome_invalidation_client_.get()) { | |
183 // TODO(akalin): Need to do unregisters here? | |
184 chrome_invalidation_client_->Stop(); | |
185 } | |
186 chrome_invalidation_client_.reset(); | 116 chrome_invalidation_client_.reset(); |
187 } | 117 } |
188 | 118 |
189 } // namespace sync_notifier | 119 } // namespace sync_notifier |
OLD | NEW |