| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "components/password_manager/sync/browser/password_model_worker.h" | 5 #include "components/password_manager/sync/browser/password_model_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "components/password_manager/core/browser/password_store.h" | 10 #include "components/password_manager/core/browser/password_store.h" |
| 11 | 11 #include "components/sync/base/scoped_event_signal.h" |
| 12 using base::WaitableEvent; | |
| 13 | 12 |
| 14 namespace browser_sync { | 13 namespace browser_sync { |
| 15 | 14 |
| 15 namespace { |
| 16 |
| 17 void CallDoWorkAndSignalEvent(const syncer::WorkCallback& work, |
| 18 syncer::ScopedEventSignal scoped_event_signal, |
| 19 syncer::SyncerError* error_info) { |
| 20 *error_info = work.Run(); |
| 21 // The event in |scoped_event_signal| is signaled at the end of this scope. |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 16 PasswordModelWorker::PasswordModelWorker( | 26 PasswordModelWorker::PasswordModelWorker( |
| 17 const scoped_refptr<password_manager::PasswordStore>& password_store, | 27 const scoped_refptr<password_manager::PasswordStore>& password_store, |
| 18 syncer::WorkerLoopDestructionObserver* observer) | 28 syncer::WorkerLoopDestructionObserver* observer) |
| 19 : syncer::ModelSafeWorker(observer), password_store_(password_store) { | 29 : syncer::ModelSafeWorker(observer), password_store_(password_store) { |
| 20 DCHECK(password_store.get()); | 30 DCHECK(password_store.get()); |
| 21 } | 31 } |
| 22 | 32 |
| 23 void PasswordModelWorker::RegisterForLoopDestruction() { | 33 void PasswordModelWorker::RegisterForLoopDestruction() { |
| 24 base::AutoLock lock(password_store_lock_); | 34 base::AutoLock lock(password_store_lock_); |
| 25 password_store_->ScheduleTask(base::Bind( | 35 password_store_->ScheduleTask(base::Bind( |
| 26 &PasswordModelWorker::RegisterForPasswordLoopDestruction, this)); | 36 &PasswordModelWorker::RegisterForPasswordLoopDestruction, this)); |
| 27 } | 37 } |
| 28 | 38 |
| 29 syncer::SyncerError PasswordModelWorker::DoWorkAndWaitUntilDoneImpl( | 39 syncer::SyncerError PasswordModelWorker::DoWorkAndWaitUntilDoneImpl( |
| 30 const syncer::WorkCallback& work) { | 40 const syncer::WorkCallback& work) { |
| 31 syncer::SyncerError error = syncer::UNSET; | 41 syncer::SyncerError error = syncer::UNSET; |
| 32 | 42 |
| 43 // Signaled when the task is deleted, i.e. after it runs or when it is |
| 44 // abandoned. |
| 45 base::WaitableEvent work_done_or_abandoned( |
| 46 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 47 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 48 |
| 33 bool scheduled = false; | 49 bool scheduled = false; |
| 34 { | 50 { |
| 35 base::AutoLock lock(password_store_lock_); | 51 base::AutoLock lock(password_store_lock_); |
| 36 if (!password_store_.get()) | 52 if (!password_store_.get()) |
| 37 return syncer::CANNOT_DO_WORK; | 53 return syncer::CANNOT_DO_WORK; |
| 38 | 54 |
| 39 scheduled = password_store_->ScheduleTask( | 55 scheduled = password_store_->ScheduleTask(base::Bind( |
| 40 base::Bind(&PasswordModelWorker::CallDoWorkAndSignalTask, this, work, | 56 &CallDoWorkAndSignalEvent, work, |
| 41 work_done_or_stopped(), &error)); | 57 base::Passed(syncer::ScopedEventSignal(&work_done_or_abandoned)), |
| 58 &error)); |
| 42 } | 59 } |
| 43 | 60 |
| 44 if (scheduled) | 61 if (scheduled) |
| 45 work_done_or_stopped()->Wait(); | 62 work_done_or_abandoned.Wait(); |
| 46 else | 63 else |
| 47 error = syncer::CANNOT_DO_WORK; | 64 error = syncer::CANNOT_DO_WORK; |
| 48 return error; | 65 return error; |
| 49 } | 66 } |
| 50 | 67 |
| 51 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { | 68 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { |
| 52 return syncer::GROUP_PASSWORD; | 69 return syncer::GROUP_PASSWORD; |
| 53 } | 70 } |
| 54 | 71 |
| 55 PasswordModelWorker::~PasswordModelWorker() {} | 72 PasswordModelWorker::~PasswordModelWorker() {} |
| 56 | 73 |
| 57 void PasswordModelWorker::CallDoWorkAndSignalTask( | |
| 58 const syncer::WorkCallback& work, | |
| 59 WaitableEvent* done, | |
| 60 syncer::SyncerError* error) { | |
| 61 *error = work.Run(); | |
| 62 done->Signal(); | |
| 63 } | |
| 64 | |
| 65 void PasswordModelWorker::RegisterForPasswordLoopDestruction() { | 74 void PasswordModelWorker::RegisterForPasswordLoopDestruction() { |
| 66 SetWorkingLoopToCurrent(); | 75 SetWorkingLoopToCurrent(); |
| 67 } | 76 } |
| 68 | 77 |
| 69 void PasswordModelWorker::RequestStop() { | 78 void PasswordModelWorker::RequestStop() { |
| 70 ModelSafeWorker::RequestStop(); | 79 ModelSafeWorker::RequestStop(); |
| 71 | 80 |
| 72 base::AutoLock lock(password_store_lock_); | 81 base::AutoLock lock(password_store_lock_); |
| 73 password_store_ = NULL; | 82 password_store_ = NULL; |
| 74 } | 83 } |
| 75 | 84 |
| 76 } // namespace browser_sync | 85 } // namespace browser_sync |
| OLD | NEW |