| 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_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 AddObserver(syncer::SyncNotifierObserver* observer); | 29 void AddObserver(syncer::SyncNotifierObserver* observer); |
| 29 void RemoveObserver(syncer::SyncNotifierObserver* observer); | 30 void RemoveObserver(syncer::SyncNotifierObserver* observer); |
| 30 | 31 |
| 31 void EmitNotification( | 32 void EmitNotification( |
| 32 syncer::ModelTypePayloadMap payload_map, | 33 const syncer::ModelTypePayloadMap& 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::ModelTypeSet enabled_types_; |
| 45 ObserverList<syncer::SyncNotifierObserver> observers_; | 46 ObserverList<syncer::SyncNotifierObserver> observers_; |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 ChromeSyncNotificationBridge::Core::Core( | 49 ChromeSyncNotificationBridge::Core::Core( |
| 49 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 50 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
| 50 : sync_task_runner_(sync_task_runner) { | 51 : sync_task_runner_(sync_task_runner) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 DCHECK(sync_task_runner_.get()); | 53 DCHECK(sync_task_runner_.get()); |
| 53 } | 54 } |
| 54 | 55 |
| 55 ChromeSyncNotificationBridge::Core::~Core() { | 56 ChromeSyncNotificationBridge::Core::~Core() { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 57 sync_task_runner_->RunsTasksOnCurrentThread()); | 58 sync_task_runner_->RunsTasksOnCurrentThread()); |
| 58 } | 59 } |
| 59 | 60 |
| 61 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( |
| 62 syncer::ModelTypeSet types) { |
| 63 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 64 enabled_types_ = types; |
| 65 } |
| 66 |
| 60 void ChromeSyncNotificationBridge::Core::AddObserver( | 67 void ChromeSyncNotificationBridge::Core::AddObserver( |
| 61 syncer::SyncNotifierObserver* observer) { | 68 syncer::SyncNotifierObserver* observer) { |
| 62 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 69 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 63 observers_.AddObserver(observer); | 70 observers_.AddObserver(observer); |
| 64 } | 71 } |
| 65 | 72 |
| 66 void ChromeSyncNotificationBridge::Core::RemoveObserver( | 73 void ChromeSyncNotificationBridge::Core::RemoveObserver( |
| 67 syncer::SyncNotifierObserver* observer) { | 74 syncer::SyncNotifierObserver* observer) { |
| 68 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 75 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 69 observers_.RemoveObserver(observer); | 76 observers_.RemoveObserver(observer); |
| 70 } | 77 } |
| 71 | 78 |
| 72 void ChromeSyncNotificationBridge::Core::EmitNotification( | 79 void ChromeSyncNotificationBridge::Core::EmitNotification( |
| 73 syncer::ModelTypePayloadMap payload_map, | 80 const syncer::ModelTypePayloadMap& payload_map, |
| 74 syncer::IncomingNotificationSource notification_source) { | 81 syncer::IncomingNotificationSource notification_source) { |
| 75 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 82 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 83 const syncer::ModelTypePayloadMap& effective_payload_map = |
| 84 payload_map.empty() ? |
| 85 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()) : |
| 86 payload_map; |
| 87 |
| 76 FOR_EACH_OBSERVER( | 88 FOR_EACH_OBSERVER( |
| 77 syncer::SyncNotifierObserver, observers_, | 89 syncer::SyncNotifierObserver, observers_, |
| 78 OnIncomingNotification(payload_map, notification_source)); | 90 OnIncomingNotification(effective_payload_map, notification_source)); |
| 79 } | 91 } |
| 80 | 92 |
| 81 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 93 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
| 82 const Profile* profile, | 94 const Profile* profile, |
| 83 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 95 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
| 84 : sync_task_runner_(sync_task_runner), | 96 : sync_task_runner_(sync_task_runner), |
| 85 core_(new Core(sync_task_runner_)) { | 97 core_(new Core(sync_task_runner_)) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 DCHECK(profile); | 99 DCHECK(profile); |
| 88 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 100 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
| 89 content::Source<Profile>(profile)); | 101 content::Source<Profile>(profile)); |
| 90 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 102 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 91 content::Source<Profile>(profile)); | 103 content::Source<Profile>(profile)); |
| 92 } | 104 } |
| 93 | 105 |
| 94 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 106 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
| 95 | 107 |
| 96 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | 108 void ChromeSyncNotificationBridge::UpdateEnabledTypes( |
| 97 const syncer::ModelTypeSet types) { | 109 syncer::ModelTypeSet types) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 110 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 99 enabled_types_ = types; | 111 core_->UpdateEnabledTypes(types); |
| 100 } | 112 } |
| 101 | 113 |
| 102 void ChromeSyncNotificationBridge::AddObserver( | 114 void ChromeSyncNotificationBridge::AddObserver( |
| 103 syncer::SyncNotifierObserver* observer) { | 115 syncer::SyncNotifierObserver* observer) { |
| 104 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 116 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 105 core_->AddObserver(observer); | 117 core_->AddObserver(observer); |
| 106 } | 118 } |
| 107 | 119 |
| 108 void ChromeSyncNotificationBridge::RemoveObserver( | 120 void ChromeSyncNotificationBridge::RemoveObserver( |
| 109 syncer::SyncNotifierObserver* observer) { | 121 syncer::SyncNotifierObserver* observer) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 122 notification_source = syncer::LOCAL_NOTIFICATION; | 134 notification_source = syncer::LOCAL_NOTIFICATION; |
| 123 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 135 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
| 124 notification_source = syncer::REMOTE_NOTIFICATION; | 136 notification_source = syncer::REMOTE_NOTIFICATION; |
| 125 } else { | 137 } else { |
| 126 NOTREACHED() << "Unexpected notification type: " << type; | 138 NOTREACHED() << "Unexpected notification type: " << type; |
| 127 return; | 139 return; |
| 128 } | 140 } |
| 129 | 141 |
| 130 content::Details<const syncer::ModelTypePayloadMap> | 142 content::Details<const syncer::ModelTypePayloadMap> |
| 131 payload_details(details); | 143 payload_details(details); |
| 132 syncer::ModelTypePayloadMap payload_map = *(payload_details.ptr()); | 144 const syncer::ModelTypePayloadMap& payload_map = *(payload_details.ptr()); |
| 133 | |
| 134 if (payload_map.empty()) { | |
| 135 // No model types to invalidate, invalidating all enabled types. | |
| 136 payload_map = | |
| 137 syncer::ModelTypePayloadMapFromEnumSet(enabled_types_, std::string()); | |
| 138 } | |
| 139 | |
| 140 sync_task_runner_->PostTask( | 145 sync_task_runner_->PostTask( |
| 141 FROM_HERE, | 146 FROM_HERE, |
| 142 base::Bind(&Core::EmitNotification, | 147 base::Bind(&Core::EmitNotification, |
| 143 core_, payload_map, notification_source)); | 148 core_, payload_map, notification_source)); |
| 144 } | 149 } |
| 145 | 150 |
| 146 } // namespace browser_sync | 151 } // namespace browser_sync |
| OLD | NEW |