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