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/observer_list.h" | |
10 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "sync/notifier/sync_notifier_helper.h" |
13 #include "sync/notifier/sync_notifier_observer.h" | 13 #include "sync/notifier/sync_notifier_observer.h" |
14 | 14 |
15 using content::BrowserThread; | 15 using content::BrowserThread; |
16 | 16 |
17 namespace browser_sync { | 17 namespace browser_sync { |
18 | 18 |
19 class ChromeSyncNotificationBridge::Core | 19 class ChromeSyncNotificationBridge::Core |
20 : public base::RefCountedThreadSafe<Core> { | 20 : public base::RefCountedThreadSafe<Core> { |
21 public: | 21 public: |
22 // Created on UI thread. | 22 // Created on UI thread. |
23 explicit Core( | 23 explicit Core( |
24 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); | 24 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); |
25 | 25 |
26 // 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. |
27 | 27 |
28 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | 28 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); |
29 void AddObserver(syncer::SyncNotifierObserver* observer); | 29 void UpdateRegisteredIds(syncer::SyncNotifierObserver* handler, |
30 void RemoveObserver(syncer::SyncNotifierObserver* observer); | 30 const syncer::ObjectIdSet& ids); |
31 | 31 |
32 void EmitNotification( | 32 void EmitNotification( |
33 const syncer::ModelTypePayloadMap& payload_map, | 33 const syncer::ModelTypePayloadMap& payload_map, |
34 syncer::IncomingNotificationSource notification_source); | 34 syncer::IncomingNotificationSource notification_source); |
35 | 35 |
36 private: | 36 private: |
37 friend class base::RefCountedThreadSafe<Core>; | 37 friend class base::RefCountedThreadSafe<Core>; |
38 | 38 |
39 // Destroyed on the UI thread or on |sync_task_runner_|. | 39 // Destroyed on the UI thread or on |sync_task_runner_|. |
40 ~Core(); | 40 ~Core(); |
41 | 41 |
42 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | 42 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; |
43 | 43 |
44 // Used only on |sync_task_runner_|. | 44 // Used only on |sync_task_runner_|. |
45 syncer::ModelTypeSet enabled_types_; | 45 syncer::ModelTypeSet enabled_types_; |
46 ObserverList<syncer::SyncNotifierObserver> observers_; | 46 syncer::SyncNotifierHelper helper_; |
47 }; | 47 }; |
48 | 48 |
49 ChromeSyncNotificationBridge::Core::Core( | 49 ChromeSyncNotificationBridge::Core::Core( |
50 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 50 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
51 : sync_task_runner_(sync_task_runner) { | 51 : sync_task_runner_(sync_task_runner) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
53 DCHECK(sync_task_runner_.get()); | 53 DCHECK(sync_task_runner_.get()); |
54 } | 54 } |
55 | 55 |
56 ChromeSyncNotificationBridge::Core::~Core() { | 56 ChromeSyncNotificationBridge::Core::~Core() { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
58 sync_task_runner_->RunsTasksOnCurrentThread()); | 58 sync_task_runner_->RunsTasksOnCurrentThread()); |
59 } | 59 } |
60 | 60 |
61 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( | 61 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( |
62 syncer::ModelTypeSet types) { | 62 syncer::ModelTypeSet types) { |
63 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 63 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
64 enabled_types_ = types; | 64 enabled_types_ = types; |
65 } | 65 } |
66 | 66 |
67 void ChromeSyncNotificationBridge::Core::AddObserver( | 67 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( |
68 syncer::SyncNotifierObserver* observer) { | 68 syncer::SyncNotifierObserver* handler, |
| 69 const syncer::ObjectIdSet& ids) { |
69 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 70 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
70 observers_.AddObserver(observer); | 71 helper_.UpdateRegisteredIds(handler, ids); |
71 } | |
72 | |
73 void ChromeSyncNotificationBridge::Core::RemoveObserver( | |
74 syncer::SyncNotifierObserver* observer) { | |
75 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
76 observers_.RemoveObserver(observer); | |
77 } | 72 } |
78 | 73 |
79 void ChromeSyncNotificationBridge::Core::EmitNotification( | 74 void ChromeSyncNotificationBridge::Core::EmitNotification( |
80 const syncer::ModelTypePayloadMap& payload_map, | 75 const syncer::ModelTypePayloadMap& payload_map, |
81 syncer::IncomingNotificationSource notification_source) { | 76 syncer::IncomingNotificationSource notification_source) { |
82 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 77 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
83 const syncer::ModelTypePayloadMap& effective_payload_map = | 78 const syncer::ModelTypePayloadMap& effective_payload_map = |
84 payload_map.empty() ? | 79 payload_map.empty() ? |
85 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : | 80 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : |
86 payload_map; | 81 payload_map; |
87 | 82 |
88 FOR_EACH_OBSERVER( | 83 helper_.DispatchInvalidationsToHandlers( |
89 syncer::SyncNotifierObserver, observers_, | 84 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), |
90 OnIncomingNotification(effective_payload_map, notification_source)); | 85 notification_source); |
91 } | 86 } |
92 | 87 |
93 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 88 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
94 const Profile* profile, | 89 const Profile* profile, |
95 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 90 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
96 : sync_task_runner_(sync_task_runner), | 91 : sync_task_runner_(sync_task_runner), |
97 core_(new Core(sync_task_runner_)) { | 92 core_(new Core(sync_task_runner_)) { |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
99 DCHECK(profile); | 94 DCHECK(profile); |
100 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 95 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
101 content::Source<Profile>(profile)); | 96 content::Source<Profile>(profile)); |
102 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 97 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
103 content::Source<Profile>(profile)); | 98 content::Source<Profile>(profile)); |
104 } | 99 } |
105 | 100 |
106 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 101 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
107 | 102 |
108 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | 103 void ChromeSyncNotificationBridge::UpdateEnabledTypes( |
109 syncer::ModelTypeSet types) { | 104 syncer::ModelTypeSet types) { |
110 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
111 core_->UpdateEnabledTypes(types); | 106 core_->UpdateEnabledTypes(types); |
112 } | 107 } |
113 | 108 |
114 void ChromeSyncNotificationBridge::AddObserver( | 109 void ChromeSyncNotificationBridge::UpdateRegisteredIds( |
115 syncer::SyncNotifierObserver* observer) { | 110 syncer::SyncNotifierObserver* handler, |
| 111 const syncer::ObjectIdSet& ids) { |
116 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 112 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
117 core_->AddObserver(observer); | 113 core_->UpdateRegisteredIds(handler, ids); |
118 } | |
119 | |
120 void ChromeSyncNotificationBridge::RemoveObserver( | |
121 syncer::SyncNotifierObserver* observer) { | |
122 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
123 core_->RemoveObserver(observer); | |
124 } | 114 } |
125 | 115 |
126 void ChromeSyncNotificationBridge::Observe( | 116 void ChromeSyncNotificationBridge::Observe( |
127 int type, | 117 int type, |
128 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
129 const content::NotificationDetails& details) { | 119 const content::NotificationDetails& details) { |
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
131 | 121 |
132 syncer::IncomingNotificationSource notification_source; | 122 syncer::IncomingNotificationSource notification_source; |
133 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { | 123 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { |
134 notification_source = syncer::LOCAL_NOTIFICATION; | 124 notification_source = syncer::LOCAL_NOTIFICATION; |
135 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 125 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
136 notification_source = syncer::REMOTE_NOTIFICATION; | 126 notification_source = syncer::REMOTE_NOTIFICATION; |
137 } else { | 127 } else { |
138 NOTREACHED() << "Unexpected notification type: " << type; | 128 NOTREACHED() << "Unexpected notification type: " << type; |
139 return; | 129 return; |
140 } | 130 } |
141 | 131 |
142 content::Details<const syncer::ModelTypePayloadMap> | 132 content::Details<const syncer::ModelTypePayloadMap> |
143 payload_details(details); | 133 payload_details(details); |
144 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); | 134 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); |
145 sync_task_runner_->PostTask( | 135 sync_task_runner_->PostTask( |
146 FROM_HERE, | 136 FROM_HERE, |
147 base::Bind(&Core::EmitNotification, | 137 base::Bind(&Core::EmitNotification, |
148 core_, payload_map, notification_source)); | 138 core_, payload_map, notification_source)); |
149 } | 139 } |
150 | 140 |
151 } // namespace browser_sync | 141 } // namespace browser_sync |
OLD | NEW |