OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "chrome/browser/sync/notifier/sync_notifier_impl.h" | |
6 | |
7 #include "chrome/browser/sync/notifier/server_notifier_thread.h" | |
8 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" | |
9 #include "chrome/browser/sync/protocol/service_constants.h" | |
10 #include "chrome/browser/sync/sessions/session_state.h" | |
11 #include "chrome/browser/sync/syncable/model_type.h" | |
12 #include "chrome/browser/sync/sync_constants.h" | |
13 #include "jingle/notifier/listener/mediator_thread_impl.h" | |
14 #include "jingle/notifier/listener/notification_constants.h" | |
15 | |
akalin
2011/03/11 22:27:44
Add a TODO(akalin) to split this class into two im
Agrawal
2011/03/11 22:54:27
Done.
| |
16 using notifier::TalkMediator; | |
17 using notifier::TalkMediatorImpl; | |
18 namespace sync_notifier { | |
19 | |
20 SyncNotifierImpl::SyncNotifierImpl( | |
21 const notifier::NotifierOptions& notifier_options) | |
22 : notifier_options_(notifier_options), | |
23 server_notifier_thread_(NULL) { } | |
24 | |
25 SyncNotifierImpl::~SyncNotifierImpl() { | |
26 // TODO(akalin): NULL the other member variables defensively, too. | |
akalin
2011/03/11 22:27:44
this TODO belongs in syncapi, where we null out sy
Agrawal
2011/03/11 22:54:27
Done.
| |
27 scoped_ptr<TalkMediator> talk_mediator(talk_mediator_.release()); | |
28 | |
29 // Shutdown the xmpp buzz connection. | |
30 if (talk_mediator.get()) { | |
31 VLOG(1) << "P2P: Mediator logout started."; | |
32 talk_mediator->Logout(); | |
33 VLOG(1) << "P2P: Mediator logout completed."; | |
34 talk_mediator.reset(); | |
35 | |
36 // |server_notifier_thread_| is owned by |talk_mediator|. We NULL | |
37 // it out here so as to not have a dangling pointer. | |
38 server_notifier_thread_= NULL; | |
39 VLOG(1) << "P2P: Mediator destroyed."; | |
40 } | |
41 } | |
42 | |
43 void SyncNotifierImpl::AddObserver(SyncNotifierObserver* observer) { | |
44 observer_list_.AddObserver(observer); | |
45 } | |
46 | |
47 void SyncNotifierImpl::RemoveObserver(SyncNotifierObserver* observer) { | |
48 observer_list_.RemoveObserver(observer); | |
49 } | |
50 | |
51 void SyncNotifierImpl::OnNotificationStateChange(bool notifications_enabled) { | |
52 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
53 OnNotificationStateChange(notifications_enabled)); | |
54 | |
55 // If using p2p notifications, generate a notification for all enabled types. | |
56 // Used only for tests. | |
57 if ((notifier_options_.notification_method != | |
58 notifier::NOTIFICATION_SERVER) && notifications_enabled) { | |
59 browser_sync::sessions::TypePayloadMap model_types_with_payloads = | |
60 browser_sync::sessions::MakeTypePayloadMapFromBitSet( | |
61 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | |
62 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
63 OnIncomingNotification(model_types_with_payloads)); | |
64 } | |
65 } | |
66 | |
67 void SyncNotifierImpl::OnIncomingNotification( | |
68 const IncomingNotificationData& notification_data) { | |
69 browser_sync::sessions::TypePayloadMap model_types_with_payloads; | |
70 | |
71 // Check if the service url is a sync URL. An empty service URL is | |
72 // treated as a legacy sync notification. If we're listening to | |
73 // server-issued notifications, no need to check the service_url. | |
74 if (notifier_options_.notification_method == | |
75 notifier::NOTIFICATION_SERVER) { | |
76 VLOG(1) << "Sync received server notification from " << | |
77 notification_data.service_url << ": " << | |
78 notification_data.service_specific_data; | |
79 syncable::ModelTypeBitSet model_types; | |
80 const std::string& model_type_list = notification_data.service_url; | |
81 const std::string& notification_payload = | |
82 notification_data.service_specific_data; | |
83 | |
84 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) { | |
85 LOG(DFATAL) << "Could not extract model types from server data."; | |
86 model_types.set(); | |
87 } | |
88 | |
89 model_types_with_payloads = | |
90 browser_sync::sessions::MakeTypePayloadMapFromBitSet(model_types, | |
91 notification_payload); | |
92 } else if (notification_data.service_url.empty() || | |
93 (notification_data.service_url == | |
94 browser_sync::kSyncLegacyServiceUrl) || | |
95 (notification_data.service_url == | |
96 browser_sync::kSyncServiceUrl)) { | |
97 VLOG(1) << "Sync received P2P notification."; | |
98 | |
99 // Catch for sync integration tests (uses p2p). Just set all enabled | |
100 // datatypes. | |
101 model_types_with_payloads = | |
102 browser_sync::sessions::MakeTypePayloadMapFromBitSet( | |
103 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | |
104 } else { | |
105 LOG(WARNING) << "Notification fron unexpected source: " | |
106 << notification_data.service_url; | |
107 } | |
108 | |
109 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
110 OnIncomingNotification(model_types_with_payloads)); | |
111 } | |
112 | |
113 void SyncNotifierImpl::WriteState(const std::string& state) { | |
114 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
115 StoreState(state)); | |
116 } | |
117 | |
118 void SyncNotifierImpl::UpdateCredentials( | |
119 const std::string& email, const std::string& token) { | |
120 // Reset talk_mediator_ before creating ServerNotifierThread/MediatorThread, | |
121 // to avoid any problems with having two of those threads at the same time. | |
122 talk_mediator_.reset(); | |
123 | |
124 if (notifier_options_.notification_method == | |
125 notifier::NOTIFICATION_SERVER) { | |
126 | |
127 // |talk_mediator_| takes ownership of |sync_notifier_thread_| | |
128 // but it is guaranteed that |sync_notifier_thread_| is destroyed only | |
129 // when |talk_mediator_| is (see the comments in talk_mediator.h). | |
130 server_notifier_thread_ = new sync_notifier::ServerNotifierThread( | |
131 notifier_options_, state_, this); | |
132 talk_mediator_.reset( | |
133 new TalkMediatorImpl(server_notifier_thread_, | |
134 notifier_options_.invalidate_xmpp_login, | |
135 notifier_options_.allow_insecure_connection)); | |
136 | |
137 // Since we may be initialized more than once, make sure that any | |
138 // newly created server notifier thread has the latest enabled types. | |
139 server_notifier_thread_->UpdateEnabledTypes(enabled_types_); | |
140 } else { | |
141 notifier::MediatorThread* mediator_thread = | |
142 new notifier::MediatorThreadImpl(notifier_options_); | |
143 talk_mediator_.reset( | |
144 new TalkMediatorImpl(mediator_thread, | |
145 notifier_options_.invalidate_xmpp_login, | |
146 notifier_options_.allow_insecure_connection)); | |
147 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl); | |
148 server_notifier_thread_ = NULL; | |
149 } | |
150 talk_mediator_->SetDelegate(this); | |
151 talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); | |
152 talk_mediator_->Login(); | |
153 } | |
154 | |
155 void SyncNotifierImpl::SetState(const std::string& state) { | |
156 state_ = state; | |
157 } | |
158 | |
159 void SyncNotifierImpl::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { | |
160 enabled_types_ = types; | |
161 if (server_notifier_thread_ != NULL) { | |
162 server_notifier_thread_->UpdateEnabledTypes(types); | |
163 } | |
164 } | |
165 | |
166 void SyncNotifierImpl::SendNotification() { | |
167 // Do nothing if we are using server based notifications. | |
168 if (notifier_options_.notification_method == | |
169 notifier::NOTIFICATION_SERVER) { | |
170 return; | |
171 } | |
172 | |
173 if (!talk_mediator_.get()) { | |
174 NOTREACHED() << "Can not send notification: talk_mediator_ is NULL"; | |
akalin
2011/03/11 22:27:44
Can not -> cannot
Agrawal
2011/03/11 22:54:27
Done.
| |
175 return; | |
176 } | |
177 | |
178 VLOG(1) << "Sending XMPP notification..."; | |
179 OutgoingNotificationData notification_data; | |
180 notification_data.service_id = browser_sync::kSyncServiceId; | |
181 notification_data.service_url = browser_sync::kSyncServiceUrl; | |
182 notification_data.send_content = true; | |
183 notification_data.priority = browser_sync::kSyncPriority; | |
184 notification_data.write_to_cache_only = true; | |
185 notification_data.service_specific_data = | |
186 browser_sync::kSyncServiceSpecificData; | |
187 notification_data.require_subscription = true; | |
188 bool success = talk_mediator_->SendNotification(notification_data); | |
189 if (success) { | |
190 VLOG(1) << "Sent XMPP notification"; | |
191 } else { | |
192 VLOG(1) << "Could not send XMPP notification"; | |
193 } | |
194 } | |
195 } // namespace sync_notifier | |
OLD | NEW |