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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/sync/glue/password_model_worker.h" 6 7 #include "base/bind.h" 8 #include "base/memory/ref_counted.h" 9 #include "base/synchronization/waitable_event.h" 10 #include "components/password_manager/core/browser/password_store.h" 11 12 using base::WaitableEvent; 13 14 namespace browser_sync { 15 16 PasswordModelWorker::PasswordModelWorker( 17 const scoped_refptr<password_manager::PasswordStore>& password_store, 18 syncer::WorkerLoopDestructionObserver* observer) 19 : syncer::ModelSafeWorker(observer), password_store_(password_store) { 20 DCHECK(password_store.get()); 21 } 22 23 void PasswordModelWorker::RegisterForLoopDestruction() { 24 base::AutoLock lock(password_store_lock_); 25 password_store_->ScheduleTask( 26 base::Bind(&PasswordModelWorker::RegisterForPasswordLoopDestruction, 27 this)); 28 } 29 30 syncer::SyncerError PasswordModelWorker::DoWorkAndWaitUntilDoneImpl( 31 const syncer::WorkCallback& work) { 32 syncer::SyncerError error = syncer::UNSET; 33 34 bool scheduled = false; 35 { 36 base::AutoLock lock(password_store_lock_); 37 if (!password_store_.get()) 38 return syncer::CANNOT_DO_WORK; 39 40 scheduled = password_store_->ScheduleTask( 41 base::Bind(&PasswordModelWorker::CallDoWorkAndSignalTask, 42 this, work, work_done_or_stopped(), &error)); 43 } 44 45 if (scheduled) 46 work_done_or_stopped()->Wait(); 47 else 48 error = syncer::CANNOT_DO_WORK; 49 return error; 50 } 51 52 syncer::ModelSafeGroup PasswordModelWorker::GetModelSafeGroup() { 53 return syncer::GROUP_PASSWORD; 54 } 55 56 PasswordModelWorker::~PasswordModelWorker() {} 57 58 void PasswordModelWorker::CallDoWorkAndSignalTask( 59 const syncer::WorkCallback& work, 60 WaitableEvent* done, 61 syncer::SyncerError *error) { 62 *error = work.Run(); 63 done->Signal(); 64 } 65 66 void PasswordModelWorker::RegisterForPasswordLoopDestruction() { 67 SetWorkingLoopToCurrent(); 68 } 69 70 void PasswordModelWorker::RequestStop() { 71 ModelSafeWorker::RequestStop(); 72 73 base::AutoLock lock(password_store_lock_); 74 password_store_ = NULL; 75 } 76 77 } // namespace browser_sync 78