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/sync/glue/chrome_sync_notification_bridge.h" | 5 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
12 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "sync/notifier/sync_notifier_helper.h" |
14 #include "sync/notifier/sync_notifier_observer.h" | 13 #include "sync/notifier/sync_notifier_observer.h" |
15 #include "sync/notifier/sync_notifier_registrar.h" | |
16 | 14 |
17 using content::BrowserThread; | 15 using content::BrowserThread; |
18 | 16 |
19 namespace browser_sync { | 17 namespace browser_sync { |
20 | 18 |
21 class ChromeSyncNotificationBridge::Core | 19 class ChromeSyncNotificationBridge::Core |
22 : public base::RefCountedThreadSafe<Core> { | 20 : public base::RefCountedThreadSafe<Core> { |
23 public: | 21 public: |
24 // Created on UI thread. | 22 // Created on UI thread. |
25 explicit Core( | 23 explicit Core( |
26 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); | 24 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); |
27 | 25 |
28 // All member functions below must be called on the sync task runner. | 26 // All member functions below must be called on the sync task runner. |
29 | 27 |
30 void InitializeOnSyncThread(); | |
31 void CleanupOnSyncThread(); | |
32 | |
33 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | 28 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); |
34 void RegisterHandler(syncer::SyncNotifierObserver* handler); | |
35 void UpdateRegisteredIds(syncer::SyncNotifierObserver* handler, | 29 void UpdateRegisteredIds(syncer::SyncNotifierObserver* handler, |
36 const syncer::ObjectIdSet& ids); | 30 const syncer::ObjectIdSet& ids); |
37 void UnregisterHandler(syncer::SyncNotifierObserver* handler); | |
38 | 31 |
39 void EmitNotification( | 32 void EmitNotification( |
40 const syncer::ModelTypePayloadMap& payload_map, | 33 const syncer::ModelTypePayloadMap& payload_map, |
41 syncer::IncomingNotificationSource notification_source); | 34 syncer::IncomingNotificationSource notification_source); |
42 | 35 |
43 private: | 36 private: |
44 friend class base::RefCountedThreadSafe<Core>; | 37 friend class base::RefCountedThreadSafe<Core>; |
45 | 38 |
46 // Destroyed on the UI thread or on |sync_task_runner_|. | 39 // Destroyed on the UI thread or on |sync_task_runner_|. |
47 ~Core(); | 40 ~Core(); |
48 | 41 |
49 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | 42 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; |
50 | 43 |
51 // Used only on |sync_task_runner_|. | 44 // Used only on |sync_task_runner_|. |
52 syncer::ModelTypeSet enabled_types_; | 45 syncer::ModelTypeSet enabled_types_; |
53 scoped_ptr<syncer::SyncNotifierRegistrar> notifier_registrar_; | 46 syncer::SyncNotifierHelper helper_; |
54 }; | 47 }; |
55 | 48 |
56 ChromeSyncNotificationBridge::Core::Core( | 49 ChromeSyncNotificationBridge::Core::Core( |
57 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 50 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
58 : sync_task_runner_(sync_task_runner) { | 51 : sync_task_runner_(sync_task_runner) { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
60 DCHECK(sync_task_runner_.get()); | 53 DCHECK(sync_task_runner_.get()); |
61 } | 54 } |
62 | 55 |
63 ChromeSyncNotificationBridge::Core::~Core() { | 56 ChromeSyncNotificationBridge::Core::~Core() { |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
65 sync_task_runner_->RunsTasksOnCurrentThread()); | 58 sync_task_runner_->RunsTasksOnCurrentThread()); |
66 DCHECK(!notifier_registrar_.get()); | |
67 } | |
68 | |
69 void ChromeSyncNotificationBridge::Core::InitializeOnSyncThread() { | |
70 notifier_registrar_.reset(new syncer::SyncNotifierRegistrar()); | |
71 } | |
72 | |
73 void ChromeSyncNotificationBridge::Core::CleanupOnSyncThread() { | |
74 notifier_registrar_.reset(); | |
75 } | 59 } |
76 | 60 |
77 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( | 61 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( |
78 syncer::ModelTypeSet types) { | 62 syncer::ModelTypeSet types) { |
79 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 63 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
80 enabled_types_ = types; | 64 enabled_types_ = types; |
81 } | 65 } |
82 | 66 |
83 void ChromeSyncNotificationBridge::Core::RegisterHandler( | |
84 syncer::SyncNotifierObserver* handler) { | |
85 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
86 notifier_registrar_->RegisterHandler(handler); | |
87 } | |
88 | |
89 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( | 67 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( |
90 syncer::SyncNotifierObserver* handler, | 68 syncer::SyncNotifierObserver* handler, |
91 const syncer::ObjectIdSet& ids) { | 69 const syncer::ObjectIdSet& ids) { |
92 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 70 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
93 notifier_registrar_->UpdateRegisteredIds(handler, ids); | 71 helper_.UpdateRegisteredIds(handler, ids); |
94 } | |
95 | |
96 void ChromeSyncNotificationBridge::Core::UnregisterHandler( | |
97 syncer::SyncNotifierObserver* handler) { | |
98 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
99 notifier_registrar_->UnregisterHandler(handler); | |
100 } | 72 } |
101 | 73 |
102 void ChromeSyncNotificationBridge::Core::EmitNotification( | 74 void ChromeSyncNotificationBridge::Core::EmitNotification( |
103 const syncer::ModelTypePayloadMap& payload_map, | 75 const syncer::ModelTypePayloadMap& payload_map, |
104 syncer::IncomingNotificationSource notification_source) { | 76 syncer::IncomingNotificationSource notification_source) { |
105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 77 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
106 const syncer::ModelTypePayloadMap& effective_payload_map = | 78 const syncer::ModelTypePayloadMap& effective_payload_map = |
107 payload_map.empty() ? | 79 payload_map.empty() ? |
108 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : | 80 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : |
109 payload_map; | 81 payload_map; |
110 | 82 |
111 notifier_registrar_->DispatchInvalidationsToHandlers( | 83 helper_.DispatchInvalidationsToHandlers( |
112 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), | 84 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), |
113 notification_source); | 85 notification_source); |
114 } | 86 } |
115 | 87 |
116 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 88 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
117 const Profile* profile, | 89 const Profile* profile, |
118 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 90 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
119 : sync_task_runner_(sync_task_runner), | 91 : sync_task_runner_(sync_task_runner), |
120 core_(new Core(sync_task_runner_)) { | 92 core_(new Core(sync_task_runner_)) { |
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
122 DCHECK(profile); | 94 DCHECK(profile); |
123 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 95 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
124 content::Source<Profile>(profile)); | 96 content::Source<Profile>(profile)); |
125 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 97 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
126 content::Source<Profile>(profile)); | 98 content::Source<Profile>(profile)); |
127 | |
128 if (!sync_task_runner_->PostTask( | |
129 FROM_HERE, base::Bind(&Core::InitializeOnSyncThread, core_))) { | |
130 NOTREACHED(); | |
131 } | |
132 } | 99 } |
133 | 100 |
134 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 101 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
135 | 102 |
136 void ChromeSyncNotificationBridge::StopForShutdown() { | |
137 if (!sync_task_runner_->PostTask( | |
138 FROM_HERE, base::Bind(&Core::CleanupOnSyncThread, core_))) { | |
139 NOTREACHED(); | |
140 } | |
141 } | |
142 | |
143 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | 103 void ChromeSyncNotificationBridge::UpdateEnabledTypes( |
144 syncer::ModelTypeSet types) { | 104 syncer::ModelTypeSet types) { |
145 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
146 core_->UpdateEnabledTypes(types); | 106 core_->UpdateEnabledTypes(types); |
147 } | 107 } |
148 | 108 |
149 void ChromeSyncNotificationBridge::RegisterHandler( | |
150 syncer::SyncNotifierObserver* handler) { | |
151 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
152 core_->RegisterHandler(handler); | |
153 } | |
154 | |
155 void ChromeSyncNotificationBridge::UpdateRegisteredIds( | 109 void ChromeSyncNotificationBridge::UpdateRegisteredIds( |
156 syncer::SyncNotifierObserver* handler, | 110 syncer::SyncNotifierObserver* handler, |
157 const syncer::ObjectIdSet& ids) { | 111 const syncer::ObjectIdSet& ids) { |
158 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 112 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
159 core_->UpdateRegisteredIds(handler, ids); | 113 core_->UpdateRegisteredIds(handler, ids); |
160 } | 114 } |
161 | 115 |
162 void ChromeSyncNotificationBridge::UnregisterHandler( | |
163 syncer::SyncNotifierObserver* handler) { | |
164 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
165 core_->UnregisterHandler(handler); | |
166 } | |
167 | |
168 void ChromeSyncNotificationBridge::Observe( | 116 void ChromeSyncNotificationBridge::Observe( |
169 int type, | 117 int type, |
170 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
171 const content::NotificationDetails& details) { | 119 const content::NotificationDetails& details) { |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
173 | 121 |
174 syncer::IncomingNotificationSource notification_source; | 122 syncer::IncomingNotificationSource notification_source; |
175 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { | 123 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { |
176 notification_source = syncer::LOCAL_NOTIFICATION; | 124 notification_source = syncer::LOCAL_NOTIFICATION; |
177 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 125 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
178 notification_source = syncer::REMOTE_NOTIFICATION; | 126 notification_source = syncer::REMOTE_NOTIFICATION; |
179 } else { | 127 } else { |
180 NOTREACHED() << "Unexpected notification type: " << type; | 128 NOTREACHED() << "Unexpected notification type: " << type; |
181 return; | 129 return; |
182 } | 130 } |
183 | 131 |
184 content::Details<const syncer::ModelTypePayloadMap> | 132 content::Details<const syncer::ModelTypePayloadMap> |
185 payload_details(details); | 133 payload_details(details); |
186 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); | 134 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); |
187 if (!sync_task_runner_->PostTask( | 135 sync_task_runner_->PostTask( |
188 FROM_HERE, | 136 FROM_HERE, |
189 base::Bind(&Core::EmitNotification, | 137 base::Bind(&Core::EmitNotification, |
190 core_, payload_map, notification_source))) { | 138 core_, payload_map, notification_source)); |
191 NOTREACHED(); | |
192 } | |
193 } | 139 } |
194 | 140 |
195 } // namespace browser_sync | 141 } // namespace browser_sync |
OLD | NEW |