OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 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/password_manager/password_store.h" | |
6 | |
7 #include "base/scoped_ptr.h" | |
8 #include "base/task.h" | |
9 | |
10 using std::vector; | |
11 | |
12 PasswordStore::PasswordStore() : handle_(0) { | |
13 } | |
14 | |
15 bool PasswordStore::Init() { | |
16 thread_.reset(new base::Thread("Chrome_PasswordStore_Thread")); | |
17 | |
18 if (!thread_->Start()) { | |
19 thread_.reset(NULL); | |
20 return false; | |
21 } | |
22 | |
23 return true; | |
24 } | |
25 | |
26 void PasswordStore::ScheduleTask(Task* task) { | |
27 if (thread_.get()) { | |
28 thread_->message_loop()->PostTask(FROM_HERE, task); | |
29 } | |
30 } | |
31 | |
32 void PasswordStore::AddLogin(const PasswordForm& form) { | |
33 ScheduleTask(NewRunnableMethod( | |
34 this, &PasswordStore::AddLoginImpl, form)); | |
35 } | |
36 | |
37 void PasswordStore::UpdateLogin(const PasswordForm& form) { | |
38 ScheduleTask(NewRunnableMethod( | |
39 this, &PasswordStore::UpdateLoginImpl, form)); | |
40 } | |
41 | |
42 void PasswordStore::RemoveLogin(const PasswordForm& form) { | |
43 ScheduleTask(NewRunnableMethod( | |
44 this, &PasswordStore::RemoveLoginImpl, form)); | |
45 } | |
46 | |
47 int PasswordStore::GetLogins(const PasswordForm& form, | |
48 PasswordStoreConsumer* consumer) { | |
49 int handle = handle_++; | |
50 GetLoginsRequest* request = new GetLoginsRequest(form, consumer, handle); | |
51 | |
52 AutoLock l(pending_requests_lock_); | |
53 pending_requests_.insert(handle); | |
54 | |
55 ScheduleTask(NewRunnableMethod(this, &PasswordStore::GetLoginsImpl, request)); | |
56 return handle; | |
57 } | |
58 | |
59 void PasswordStore::NotifyConsumer(GetLoginsRequest* request, | |
60 const vector<PasswordForm*> forms) { | |
61 scoped_ptr<GetLoginsRequest> request_ptr(request); | |
62 | |
63 request->message_loop->PostTask(FROM_HERE, | |
64 NewRunnableMethod(this, | |
65 &PasswordStore::NotifyConsumerImpl, | |
66 request->consumer, request->handle, forms)); | |
67 } | |
68 | |
69 void PasswordStore::NotifyConsumerImpl(PasswordStoreConsumer* consumer, | |
70 int handle, | |
71 const vector<PasswordForm*> forms) { | |
72 { // Scope for the AutoLock. | |
73 AutoLock l(pending_requests_lock_); | |
74 | |
75 // Don't notify the consumer if the request was canceled. | |
76 if (pending_requests_.find(handle) == pending_requests_.end()) | |
77 return; | |
78 pending_requests_.erase(handle); | |
79 } | |
80 | |
81 consumer->OnPasswordStoreRequestDone(handle, forms); | |
82 } | |
83 | |
84 void PasswordStore::CancelLoginsQuery(int handle) { | |
85 AutoLock l(pending_requests_lock_); | |
86 pending_requests_.erase(handle); | |
87 } | |
88 | |
89 PasswordStore::GetLoginsRequest::GetLoginsRequest( | |
90 const PasswordForm& form, | |
91 PasswordStoreConsumer* consumer, | |
92 int handle) | |
93 : form(form), | |
94 consumer(consumer), | |
95 handle(handle), | |
96 message_loop(MessageLoop::current()) { | |
97 } | |
OLD | NEW |