Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Side by Side Diff: chrome/browser/sync/notifier/sync_notifier_impl.cc

Issue 6621062: Refactor sync notifier out of sync api. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fix build. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/protocol/service_constants.h"
9 #include "chrome/browser/sync/sessions/session_state.h"
10 #include "chrome/browser/sync/syncable/model_type.h"
11 #include "chrome/browser/sync/sync_constants.h"
12 #include "jingle/notifier/listener/mediator_thread_impl.h"
13 #include "jingle/notifier/listener/notification_constants.h"
14
15 using notifier::TalkMediator;
16 using notifier::TalkMediatorImpl;
17 namespace sync_notifier {
18
19 void SyncNotifierImpl::InitializeTalkMediator(const std::string& state,
20 const notifier::NotifierOptions& notifier_options_) {
21 }
22
23 SyncNotifierImpl::SyncNotifierImpl(
24 notifier::NotifierOptions* notifier_options)
25 : notifier_options_(notifier_options) { }
26
27 SyncNotifierImpl::~SyncNotifierImpl() {
28 // We NULL out talk_mediator_ so that any pending tasks do not
29 // trigger further XMPP actions.
30 // TODO(akalin): NULL the other member variables defensively, too.
31 scoped_ptr<TalkMediator> talk_mediator(talk_mediator_.release());
32
33 // Shutdown the xmpp buzz connection.
34 if (talk_mediator.get()) {
35 VLOG(1) << "P2P: Mediator logout started.";
36 talk_mediator->Logout();
37 VLOG(1) << "P2P: Mediator logout completed.";
38 talk_mediator.reset();
39
40 // |server_notifier_thread_| is owned by |talk_mediator|. We NULL
41 // it out here so as to not have a dangling pointer.
42 server_notifier_thread_= NULL;
43 VLOG(1) << "P2P: Mediator destroyed.";
44 }
45
46 delete notifier_options_;
47 }
48
49 void SyncNotifierImpl::AddObserver(SyncNotifierObserver* observer) {
50 observer_list_.AddObserver(observer);
51 }
52
53 void SyncNotifierImpl::RemoveObserver(SyncNotifierObserver* observer) {
54 observer_list_.RemoveObserver(observer);
55 }
56
57 void SyncNotifierImpl::OnNotificationStateChange(bool notifications_enabled) {
58 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_,
59 OnNotificationStateChange(notifications_enabled));
60 }
61
62 void SyncNotifierImpl::OnIncomingNotification(
63 const IncomingNotificationData& notification_data) {
64 browser_sync::sessions::TypePayloadMap model_types_with_payloads;
65
66 // Check if the service url is a sync URL. An empty service URL is
67 // treated as a legacy sync notification. If we're listening to
68 // server-issued notifications, no need to check the service_url.
69 if (notifier_options_->notification_method ==
70 notifier::NOTIFICATION_SERVER) {
71 VLOG(1) << "Sync received server notification from " <<
72 notification_data.service_url << ": " <<
73 notification_data.service_specific_data;
74 syncable::ModelTypeBitSet model_types;
75 const std::string& model_type_list = notification_data.service_url;
76 const std::string& notification_payload =
77 notification_data.service_specific_data;
78
79 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) {
80 LOG(DFATAL) << "Could not extract model types from server data.";
81 model_types.set();
82 }
83
84 model_types_with_payloads =
85 browser_sync::sessions::MakeTypePayloadMapFromBitSet(model_types,
86 notification_payload);
87 } else if (notification_data.service_url.empty() ||
88 (notification_data.service_url ==
89 browser_sync::kSyncLegacyServiceUrl) ||
90 (notification_data.service_url ==
91 browser_sync::kSyncServiceUrl)) {
92 VLOG(1) << "Sync received P2P notification.";
93
94 // Catch for sync integration tests (uses p2p). Just set all enabled
95 // datatypes.
96 model_types_with_payloads =
97 browser_sync::sessions::MakeTypePayloadMapFromBitSet(
98 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string());
99 } else {
100 LOG(WARNING) << "Notification fron unexpected source: "
101 << notification_data.service_url;
102 }
103
104 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_,
105 OnIncomingNotification(model_types_with_payloads));
106 }
107
108 void SyncNotifierImpl::WriteState(const std::string& state) {
109 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_,
110 StoreCookie(state));
111 }
112
113 void SyncNotifierImpl::Login(
114 const std::string& email, const std::string& token,
115 const std::string& state) {
116 if (notifier_options_->notification_method ==
117 notifier::NOTIFICATION_SERVER) {
118
119 // |talk_mediator_| takes ownership of |sync_notifier_thread_|
120 // but it is guaranteed that |sync_notifier_thread_| is destroyed only
121 // when |talk_mediator_| is (see the comments in talk_mediator.h).
122 server_notifier_thread_ = new sync_notifier::ServerNotifierThread(
123 *notifier_options_, state, this);
124 talk_mediator_.reset(
125 new TalkMediatorImpl(server_notifier_thread_,
126 notifier_options_->invalidate_xmpp_login,
127 notifier_options_->allow_insecure_connection));
128
129 // Since we may be initialized more than once, make sure that any
130 // newly created server notifier thread has the latest enabled types.
131 server_notifier_thread_->UpdateEnabledTypes(enabled_types_);
132 } else {
133 notifier::MediatorThread* mediator_thread =
134 new notifier::MediatorThreadImpl(*notifier_options_);
135 talk_mediator_.reset(
136 new TalkMediatorImpl(mediator_thread,
137 notifier_options_->invalidate_xmpp_login,
138 notifier_options_->allow_insecure_connection));
139 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl);
140 server_notifier_thread_ = NULL;
141 }
142 talk_mediator()->SetDelegate(this);
143 talk_mediator()->SetAuthToken(email, token, SYNC_SERVICE_NAME);
144 talk_mediator()->Login();
145 }
146
147 void SyncNotifierImpl::UpdateEnabledTypes(const syncable::ModelTypeSet& types) {
148 enabled_types_ = types;
149 if (server_notifier_thread_ != NULL) {
150 server_notifier_thread_->UpdateEnabledTypes(types);
151 }
152 }
153
154 void SyncNotifierImpl::SendNotification(bool* notification_pending) {
155 if (!talk_mediator()) {
156 VLOG(1) << "Not sending notification: shutting down (talk_mediator_ is "
157 "NULL)";
158 return;
159 }
160
161 DCHECK_NE(notifier_options_->notification_method,
162 notifier::NOTIFICATION_SERVER);
163 VLOG(1) << "Sending XMPP notification...";
164 OutgoingNotificationData notification_data;
165 notification_data.service_id = browser_sync::kSyncServiceId;
166 notification_data.service_url = browser_sync::kSyncServiceUrl;
167 notification_data.send_content = true;
168 notification_data.priority = browser_sync::kSyncPriority;
169 notification_data.write_to_cache_only = true;
170 notification_data.service_specific_data =
171 browser_sync::kSyncServiceSpecificData;
172 notification_data.require_subscription = true;
173 bool success = talk_mediator()->SendNotification(notification_data);
174 if (success) {
175 *notification_pending = false;
176 VLOG(1) << "Sent XMPP notification";
177 } else {
178 VLOG(1) << "Could not send XMPP notification";
179 }
180 }
181 } // namespace sync_notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698