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