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 <utility> | 7 #include "base/bind.h" |
8 | 8 #include "base/callback.h" |
| 9 #include "base/synchronization/waitable_event.h" |
9 #include "components/password_manager/core/browser/password_store.h" | 10 #include "components/password_manager/core/browser/password_store.h" |
| 11 #include "components/sync/base/scoped_event_signal.h" |
10 | 12 |
11 namespace browser_sync { | 13 namespace browser_sync { |
12 | 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 |
13 PasswordModelWorker::PasswordModelWorker( | 26 PasswordModelWorker::PasswordModelWorker( |
14 const scoped_refptr<password_manager::PasswordStore>& password_store) | 27 const scoped_refptr<password_manager::PasswordStore>& password_store) |
15 : password_store_(password_store) { | 28 : password_store_(password_store) { |
16 DCHECK(password_store.get()); | 29 DCHECK(password_store.get()); |
17 } | 30 } |
18 | 31 |
| 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 |
19 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { | 61 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { |
20 return syncer::GROUP_PASSWORD; | 62 return syncer::GROUP_PASSWORD; |
21 } | 63 } |
22 | 64 |
23 bool PasswordModelWorker::IsOnModelThread() { | 65 bool PasswordModelWorker::IsOnModelThread() { |
24 // Ideally PasswordStore would expose a way to check whether this is the | 66 // Ideally PasswordStore would expose a way to check whether this is the |
25 // thread it does work on. Since it doesn't, just return true to bypass a | 67 // thread it does work on. Since it doesn't, just return true to bypass a |
26 // CHECK in the sync code. | 68 // CHECK in the sync code. |
27 return true; | 69 return true; |
28 } | 70 } |
29 | 71 |
30 PasswordModelWorker::~PasswordModelWorker() {} | 72 PasswordModelWorker::~PasswordModelWorker() {} |
31 | 73 |
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 | |
41 void PasswordModelWorker::RequestStop() { | 74 void PasswordModelWorker::RequestStop() { |
42 ModelSafeWorker::RequestStop(); | 75 ModelSafeWorker::RequestStop(); |
43 | 76 |
44 base::AutoLock lock(password_store_lock_); | 77 base::AutoLock lock(password_store_lock_); |
45 password_store_ = NULL; | 78 password_store_ = NULL; |
46 } | 79 } |
47 | 80 |
48 } // namespace browser_sync | 81 } // namespace browser_sync |
OLD | NEW |