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