Chromium Code Reviews| 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" | 14 #include "sync/notifier/sync_notifier_helper.h" |
| 13 #include "sync/notifier/sync_notifier_observer.h" | 15 #include "sync/notifier/sync_notifier_observer.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 DestroyOnSyncThread(); | |
|
tim (not reviewing)
2012/08/06 05:55:32
Is 'Destroy' the right name? This won't necessari
akalin
2012/08/07 07:25:19
Done, use Cleanup
| |
| 32 | |
| 28 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | 33 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); |
| 29 void UpdateRegisteredIds(syncer::SyncNotifierObserver* handler, | 34 void SetHandler(const std::string& handler_name, |
| 35 syncer::SyncNotifierObserver* handler); | |
| 36 void UpdateRegisteredIds(const std::string& handler_name, | |
| 30 const syncer::ObjectIdSet& ids); | 37 const syncer::ObjectIdSet& ids); |
| 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::SyncNotifierHelper> helper_; |
| 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), |
| 59 helper_(NULL) { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 DCHECK(sync_task_runner_.get()); | 61 DCHECK(sync_task_runner_.get()); |
| 54 } | 62 } |
| 55 | 63 |
| 56 ChromeSyncNotificationBridge::Core::~Core() { | 64 ChromeSyncNotificationBridge::Core::~Core() { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 58 sync_task_runner_->RunsTasksOnCurrentThread()); | 66 sync_task_runner_->RunsTasksOnCurrentThread()); |
| 67 DCHECK(!helper_.get()); | |
| 68 } | |
| 69 | |
| 70 void ChromeSyncNotificationBridge::Core::InitializeOnSyncThread() { | |
| 71 helper_.reset(new syncer::SyncNotifierHelper()); | |
| 72 } | |
| 73 | |
| 74 void ChromeSyncNotificationBridge::Core::DestroyOnSyncThread() { | |
| 75 helper_.reset(); | |
| 59 } | 76 } |
| 60 | 77 |
| 61 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( | 78 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( |
| 62 syncer::ModelTypeSet types) { | 79 syncer::ModelTypeSet types) { |
| 63 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 80 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 64 enabled_types_ = types; | 81 enabled_types_ = types; |
| 65 } | 82 } |
| 66 | 83 |
| 84 void ChromeSyncNotificationBridge::Core::SetHandler( | |
| 85 const std::string& handler_name, | |
| 86 syncer::SyncNotifierObserver* handler) { | |
| 87 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 88 helper_->SetHandler(handler_name, handler); | |
| 89 } | |
| 90 | |
| 67 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( | 91 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( |
| 68 syncer::SyncNotifierObserver* handler, | 92 const std::string& handler_name, |
| 69 const syncer::ObjectIdSet& ids) { | 93 const syncer::ObjectIdSet& ids) { |
| 70 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 94 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 71 helper_.UpdateRegisteredIds(handler, ids); | 95 helper_->UpdateRegisteredIds(handler_name, ids); |
| 72 } | 96 } |
| 73 | 97 |
| 74 void ChromeSyncNotificationBridge::Core::EmitNotification( | 98 void ChromeSyncNotificationBridge::Core::EmitNotification( |
| 75 const syncer::ModelTypePayloadMap& payload_map, | 99 const syncer::ModelTypePayloadMap& payload_map, |
| 76 syncer::IncomingNotificationSource notification_source) { | 100 syncer::IncomingNotificationSource notification_source) { |
| 77 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 101 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 78 const syncer::ModelTypePayloadMap& effective_payload_map = | 102 const syncer::ModelTypePayloadMap& effective_payload_map = |
| 79 payload_map.empty() ? | 103 payload_map.empty() ? |
| 80 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : | 104 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : |
| 81 payload_map; | 105 payload_map; |
| 82 | 106 |
| 83 helper_.DispatchInvalidationsToHandlers( | 107 helper_->DispatchInvalidationsToHandlers( |
| 84 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), | 108 ModelTypePayloadMapToObjectIdPayloadMap(effective_payload_map), |
| 85 notification_source); | 109 notification_source); |
| 86 } | 110 } |
| 87 | 111 |
| 88 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 112 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
| 89 const Profile* profile, | 113 const Profile* profile, |
| 90 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 114 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
| 91 : sync_task_runner_(sync_task_runner), | 115 : sync_task_runner_(sync_task_runner), |
| 92 core_(new Core(sync_task_runner_)) { | 116 core_(new Core(sync_task_runner_)) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 94 DCHECK(profile); | 118 DCHECK(profile); |
| 95 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 119 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
| 96 content::Source<Profile>(profile)); | 120 content::Source<Profile>(profile)); |
| 97 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 121 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 98 content::Source<Profile>(profile)); | 122 content::Source<Profile>(profile)); |
| 123 | |
| 124 if (!sync_task_runner_->PostTask( | |
| 125 FROM_HERE, base::Bind(&Core::InitializeOnSyncThread, core_))) { | |
| 126 NOTREACHED(); | |
| 127 } | |
| 99 } | 128 } |
| 100 | 129 |
| 101 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 130 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
| 102 | 131 |
| 132 void ChromeSyncNotificationBridge::StopForShutdown() { | |
| 133 if (!sync_task_runner_->PostTask( | |
| 134 FROM_HERE, base::Bind(&Core::DestroyOnSyncThread, core_))) { | |
| 135 NOTREACHED(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 103 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | 139 void ChromeSyncNotificationBridge::UpdateEnabledTypes( |
| 104 syncer::ModelTypeSet types) { | 140 syncer::ModelTypeSet types) { |
| 105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 141 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 106 core_->UpdateEnabledTypes(types); | 142 core_->UpdateEnabledTypes(types); |
| 107 } | 143 } |
| 108 | 144 |
| 145 void ChromeSyncNotificationBridge::SetHandler( | |
| 146 const std::string& handler_name, | |
| 147 syncer::SyncNotifierObserver* handler) { | |
| 148 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 149 core_->SetHandler(handler_name, handler); | |
| 150 } | |
| 151 | |
| 109 void ChromeSyncNotificationBridge::UpdateRegisteredIds( | 152 void ChromeSyncNotificationBridge::UpdateRegisteredIds( |
| 110 syncer::SyncNotifierObserver* handler, | 153 const std::string& handler_name, |
| 111 const syncer::ObjectIdSet& ids) { | 154 const syncer::ObjectIdSet& ids) { |
| 112 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 155 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 113 core_->UpdateRegisteredIds(handler, ids); | 156 core_->UpdateRegisteredIds(handler_name, ids); |
| 114 } | 157 } |
| 115 | 158 |
| 116 void ChromeSyncNotificationBridge::Observe( | 159 void ChromeSyncNotificationBridge::Observe( |
| 117 int type, | 160 int type, |
| 118 const content::NotificationSource& source, | 161 const content::NotificationSource& source, |
| 119 const content::NotificationDetails& details) { | 162 const content::NotificationDetails& details) { |
| 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 121 | 164 |
| 122 syncer::IncomingNotificationSource notification_source; | 165 syncer::IncomingNotificationSource notification_source; |
| 123 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { | 166 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { |
| 124 notification_source = syncer::LOCAL_NOTIFICATION; | 167 notification_source = syncer::LOCAL_NOTIFICATION; |
| 125 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 168 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
| 126 notification_source = syncer::REMOTE_NOTIFICATION; | 169 notification_source = syncer::REMOTE_NOTIFICATION; |
| 127 } else { | 170 } else { |
| 128 NOTREACHED() << "Unexpected notification type: " << type; | 171 NOTREACHED() << "Unexpected notification type: " << type; |
| 129 return; | 172 return; |
| 130 } | 173 } |
| 131 | 174 |
| 132 content::Details<const syncer::ModelTypePayloadMap> | 175 content::Details<const syncer::ModelTypePayloadMap> |
| 133 payload_details(details); | 176 payload_details(details); |
| 134 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); | 177 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); |
| 135 sync_task_runner_->PostTask( | 178 if (!sync_task_runner_->PostTask( |
| 136 FROM_HERE, | 179 FROM_HERE, |
| 137 base::Bind(&Core::EmitNotification, | 180 base::Bind(&Core::EmitNotification, |
| 138 core_, payload_map, notification_source)); | 181 core_, payload_map, notification_source))) { |
| 182 NOTREACHED(); | |
| 183 } | |
| 139 } | 184 } |
| 140 | 185 |
| 141 } // namespace browser_sync | 186 } // namespace browser_sync |
| OLD | NEW |