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" |
9 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/notification_service.h" | 12 #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 UpdateRegisteredIds(syncer::SyncNotifierObserver* handler, | 29 void AddObserver(syncer::SyncNotifierObserver* observer); |
30 const syncer::ObjectIdSet& ids); | 30 void RemoveObserver(syncer::SyncNotifierObserver* observer); |
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 syncer::SyncNotifierHelper helper_; | 46 ObserverList<syncer::SyncNotifierObserver> observers_; |
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::UpdateRegisteredIds( | 67 void ChromeSyncNotificationBridge::Core::AddObserver( |
68 syncer::SyncNotifierObserver* handler, | 68 syncer::SyncNotifierObserver* observer) { |
69 const syncer::ObjectIdSet& ids) { | |
70 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 69 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
71 helper_.UpdateRegisteredIds(handler, ids); | 70 observers_.AddObserver(observer); |
| 71 } |
| 72 |
| 73 void ChromeSyncNotificationBridge::Core::RemoveObserver( |
| 74 syncer::SyncNotifierObserver* observer) { |
| 75 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 76 observers_.RemoveObserver(observer); |
72 } | 77 } |
73 | 78 |
74 void ChromeSyncNotificationBridge::Core::EmitNotification( | 79 void ChromeSyncNotificationBridge::Core::EmitNotification( |
75 const syncer::ModelTypePayloadMap& payload_map, | 80 const syncer::ModelTypePayloadMap& payload_map, |
76 syncer::IncomingNotificationSource notification_source) { | 81 syncer::IncomingNotificationSource notification_source) { |
77 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 82 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
78 const syncer::ModelTypePayloadMap& effective_payload_map = | 83 const syncer::ModelTypePayloadMap& effective_payload_map = |
79 payload_map.empty() ? | 84 payload_map.empty() ? |
80 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : | 85 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : |
81 payload_map; | 86 payload_map; |
82 | 87 |
83 helper_.DispatchInvalidationsToHandlers( | 88 FOR_EACH_OBSERVER( |
84 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), | 89 syncer::SyncNotifierObserver, observers_, |
85 notification_source); | 90 OnIncomingNotification(effective_payload_map, notification_source)); |
86 } | 91 } |
87 | 92 |
88 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 93 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
89 const Profile* profile, | 94 const Profile* profile, |
90 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 95 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
91 : sync_task_runner_(sync_task_runner), | 96 : sync_task_runner_(sync_task_runner), |
92 core_(new Core(sync_task_runner_)) { | 97 core_(new Core(sync_task_runner_)) { |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
94 DCHECK(profile); | 99 DCHECK(profile); |
95 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 100 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
96 content::Source<Profile>(profile)); | 101 content::Source<Profile>(profile)); |
97 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 102 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
98 content::Source<Profile>(profile)); | 103 content::Source<Profile>(profile)); |
99 } | 104 } |
100 | 105 |
101 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 106 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
102 | 107 |
103 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | 108 void ChromeSyncNotificationBridge::UpdateEnabledTypes( |
104 syncer::ModelTypeSet types) { | 109 syncer::ModelTypeSet types) { |
105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 110 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
106 core_->UpdateEnabledTypes(types); | 111 core_->UpdateEnabledTypes(types); |
107 } | 112 } |
108 | 113 |
109 void ChromeSyncNotificationBridge::UpdateRegisteredIds( | 114 void ChromeSyncNotificationBridge::AddObserver( |
110 syncer::SyncNotifierObserver* handler, | 115 syncer::SyncNotifierObserver* observer) { |
111 const syncer::ObjectIdSet& ids) { | |
112 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 116 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
113 core_->UpdateRegisteredIds(handler, ids); | 117 core_->AddObserver(observer); |
| 118 } |
| 119 |
| 120 void ChromeSyncNotificationBridge::RemoveObserver( |
| 121 syncer::SyncNotifierObserver* observer) { |
| 122 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 123 core_->RemoveObserver(observer); |
114 } | 124 } |
115 | 125 |
116 void ChromeSyncNotificationBridge::Observe( | 126 void ChromeSyncNotificationBridge::Observe( |
117 int type, | 127 int type, |
118 const content::NotificationSource& source, | 128 const content::NotificationSource& source, |
119 const content::NotificationDetails& details) { | 129 const content::NotificationDetails& details) { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
121 | 131 |
122 syncer::IncomingNotificationSource notification_source; | 132 syncer::IncomingNotificationSource notification_source; |
123 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { | 133 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { |
124 notification_source = syncer::LOCAL_NOTIFICATION; | 134 notification_source = syncer::LOCAL_NOTIFICATION; |
125 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 135 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
126 notification_source = syncer::REMOTE_NOTIFICATION; | 136 notification_source = syncer::REMOTE_NOTIFICATION; |
127 } else { | 137 } else { |
128 NOTREACHED() << "Unexpected notification type: " << type; | 138 NOTREACHED() << "Unexpected notification type: " << type; |
129 return; | 139 return; |
130 } | 140 } |
131 | 141 |
132 content::Details<const syncer::ModelTypePayloadMap> | 142 content::Details<const syncer::ModelTypePayloadMap> |
133 payload_details(details); | 143 payload_details(details); |
134 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); | 144 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); |
135 sync_task_runner_->PostTask( | 145 sync_task_runner_->PostTask( |
136 FROM_HERE, | 146 FROM_HERE, |
137 base::Bind(&Core::EmitNotification, | 147 base::Bind(&Core::EmitNotification, |
138 core_, payload_map, notification_source)); | 148 core_, payload_map, notification_source)); |
139 } | 149 } |
140 | 150 |
141 } // namespace browser_sync | 151 } // namespace browser_sync |
OLD | NEW |