| 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 <utility> |
| 8 #include "base/callback.h" | 8 |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "components/password_manager/core/browser/password_store.h" | 9 #include "components/password_manager/core/browser/password_store.h" |
| 11 #include "components/sync/base/scoped_event_signal.h" | |
| 12 | 10 |
| 13 namespace browser_sync { | 11 namespace browser_sync { |
| 14 | 12 |
| 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 | |
| 26 PasswordModelWorker::PasswordModelWorker( | 13 PasswordModelWorker::PasswordModelWorker( |
| 27 const scoped_refptr<password_manager::PasswordStore>& password_store) | 14 const scoped_refptr<password_manager::PasswordStore>& password_store) |
| 28 : password_store_(password_store) { | 15 : password_store_(password_store) { |
| 29 DCHECK(password_store.get()); | 16 DCHECK(password_store.get()); |
| 30 } | 17 } |
| 31 | 18 |
| 32 syncer::SyncerError PasswordModelWorker::DoWorkAndWaitUntilDoneImpl( | |
| 33 const syncer::WorkCallback& work) { | |
| 34 syncer::SyncerError error = syncer::UNSET; | |
| 35 | |
| 36 // Signaled when the task is deleted, i.e. after it runs or when it is | |
| 37 // abandoned. | |
| 38 base::WaitableEvent work_done_or_abandoned( | |
| 39 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 40 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 41 | |
| 42 bool scheduled = false; | |
| 43 { | |
| 44 base::AutoLock lock(password_store_lock_); | |
| 45 if (!password_store_.get()) | |
| 46 return syncer::CANNOT_DO_WORK; | |
| 47 | |
| 48 scheduled = password_store_->ScheduleTask(base::Bind( | |
| 49 &CallDoWorkAndSignalEvent, work, | |
| 50 base::Passed(syncer::ScopedEventSignal(&work_done_or_abandoned)), | |
| 51 &error)); | |
| 52 } | |
| 53 | |
| 54 if (scheduled) | |
| 55 work_done_or_abandoned.Wait(); | |
| 56 else | |
| 57 error = syncer::CANNOT_DO_WORK; | |
| 58 return error; | |
| 59 } | |
| 60 | |
| 61 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { | 19 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { |
| 62 return syncer::GROUP_PASSWORD; | 20 return syncer::GROUP_PASSWORD; |
| 63 } | 21 } |
| 64 | 22 |
| 65 bool PasswordModelWorker::IsOnModelThread() { | 23 bool PasswordModelWorker::IsOnModelThread() { |
| 66 // Ideally PasswordStore would expose a way to check whether this is the | 24 // Ideally PasswordStore would expose a way to check whether this is the |
| 67 // thread it does work on. Since it doesn't, just return true to bypass a | 25 // thread it does work on. Since it doesn't, just return true to bypass a |
| 68 // CHECK in the sync code. | 26 // CHECK in the sync code. |
| 69 return true; | 27 return true; |
| 70 } | 28 } |
| 71 | 29 |
| 72 PasswordModelWorker::~PasswordModelWorker() {} | 30 PasswordModelWorker::~PasswordModelWorker() {} |
| 73 | 31 |
| 32 void PasswordModelWorker::ScheduleWork(base::OnceClosure work) { |
| 33 base::AutoLock lock(password_store_lock_); |
| 34 if (password_store_) { |
| 35 password_store_->ScheduleTask( |
| 36 base::Bind([](base::OnceClosure work) { std::move(work).Run(); }, |
| 37 base::Passed(std::move(work)))); |
| 38 } |
| 39 } |
| 40 |
| 74 void PasswordModelWorker::RequestStop() { | 41 void PasswordModelWorker::RequestStop() { |
| 75 ModelSafeWorker::RequestStop(); | 42 ModelSafeWorker::RequestStop(); |
| 76 | 43 |
| 77 base::AutoLock lock(password_store_lock_); | 44 base::AutoLock lock(password_store_lock_); |
| 78 password_store_ = NULL; | 45 password_store_ = NULL; |
| 79 } | 46 } |
| 80 | 47 |
| 81 } // namespace browser_sync | 48 } // namespace browser_sync |
| OLD | NEW |