summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/password_store.cc
blob: a02d6662f62b3255a4110d474b4dfd3d7cb23e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/password_manager/password_store.h"

#include "base/message_loop.h"
#include "base/scoped_ptr.h"
#include "base/task.h"
#include "chrome/browser/chrome_thread.h"

using std::vector;
using webkit_glue::PasswordForm;

PasswordStore::PasswordStore() : handle_(0) {
}

bool PasswordStore::Init() {
  ReportMetrics();
  return true;
}

void PasswordStore::ScheduleTask(Task* task) {
  ChromeThread::PostTask(ChromeThread::DB, FROM_HERE, task);
}

void PasswordStore::ReportMetrics() {
  ScheduleTask(NewRunnableMethod(this, &PasswordStore::ReportMetricsImpl));
}

void PasswordStore::AddLogin(const PasswordForm& form) {
  ScheduleTask(NewRunnableMethod(this, &PasswordStore::AddLoginImpl, form));
}

void PasswordStore::UpdateLogin(const PasswordForm& form) {
  ScheduleTask(NewRunnableMethod(this, &PasswordStore::UpdateLoginImpl, form));
}

void PasswordStore::RemoveLogin(const PasswordForm& form) {
  ScheduleTask(NewRunnableMethod(this, &PasswordStore::RemoveLoginImpl, form));
}

void PasswordStore::RemoveLoginsCreatedBetween(const base::Time& delete_begin,
                                               const base::Time& delete_end) {
  ScheduleTask(NewRunnableMethod(this,
                                 &PasswordStore::RemoveLoginsCreatedBetweenImpl,
                                 delete_begin, delete_end));
}

int PasswordStore::GetLogins(const PasswordForm& form,
                             PasswordStoreConsumer* consumer) {
  int handle = GetNewRequestHandle();
  GetLoginsRequest* request = new GetLoginsRequest(consumer, handle);
  ScheduleTask(NewRunnableMethod(this, &PasswordStore::GetLoginsImpl, request,
                                 form));
  return handle;
}

int PasswordStore::GetAutofillableLogins(PasswordStoreConsumer* consumer) {
  int handle = GetNewRequestHandle();
  GetLoginsRequest* request = new GetLoginsRequest(consumer, handle);
  ScheduleTask(NewRunnableMethod(this,
                                 &PasswordStore::GetAutofillableLoginsImpl,
                                 request));
  return handle;
}

int PasswordStore::GetBlacklistLogins(PasswordStoreConsumer* consumer) {
  int handle = GetNewRequestHandle();
  GetLoginsRequest* request = new GetLoginsRequest(consumer, handle);
  ScheduleTask(NewRunnableMethod(this,
                                 &PasswordStore::GetBlacklistLoginsImpl,
                                 request));
  return handle;
}

void PasswordStore::NotifyConsumer(GetLoginsRequest* request,
                                   const vector<PasswordForm*> forms) {
  scoped_ptr<GetLoginsRequest> request_ptr(request);

#if !defined(OS_MACOSX)
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB));
#endif
  request->message_loop->PostTask(FROM_HERE,
      NewRunnableMethod(this,
                        &PasswordStore::NotifyConsumerImpl,
                        request->consumer, request->handle, forms));
}

void PasswordStore::NotifyConsumerImpl(PasswordStoreConsumer* consumer,
                                       int handle,
                                       const vector<PasswordForm*> forms) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  // Don't notify the consumer if the request was canceled.
  if (pending_requests_.find(handle) == pending_requests_.end()) {
    // |forms| is const so we iterate rather than use STLDeleteElements().
    for (size_t i = 0; i < forms.size(); ++i)
      delete forms[i];
    return;
  }
  pending_requests_.erase(handle);

  consumer->OnPasswordStoreRequestDone(handle, forms);
}

int PasswordStore::GetNewRequestHandle() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  int handle = handle_++;
  pending_requests_.insert(handle);
  return handle;
}

void PasswordStore::CancelLoginsQuery(int handle) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  pending_requests_.erase(handle);
}

PasswordStore::GetLoginsRequest::GetLoginsRequest(
    PasswordStoreConsumer* consumer, int handle)
    : consumer(consumer), handle(handle), message_loop(MessageLoop::current()) {
}