summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/crypto_module_password_dialog.cc
blob: 0fc08846cba3c2691c615fb1965c6fa1f5dcd3a6 (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
// Copyright (c) 2011 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/ui/crypto_module_password_dialog.h"

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/synchronization/waitable_event.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/views/window.h"
#include "crypto/crypto_module_blocking_password_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/crypto_module_password_dialog_view.h"
#include "ui/views/widget/widget.h"
#endif

using content::BrowserThread;

namespace {

class CryptoModuleBlockingDialogDelegate
    : public crypto::CryptoModuleBlockingPasswordDelegate {
 public:
  CryptoModuleBlockingDialogDelegate(browser::CryptoModulePasswordReason reason,
                                     const std::string& server)
      : event_(false, false),
        reason_(reason),
        server_(server),
        cancelled_(false) {
  }

  virtual ~CryptoModuleBlockingDialogDelegate() {
    // Make sure we clear the password in memory.
    password_.replace(0, password_.size(), password_.size(), 0);
  }

  // crypto::CryptoModuleBlockingDialogDelegate implementation.
  virtual std::string RequestPassword(const std::string& slot_name,
                                      bool retry,
                                      bool* cancelled) OVERRIDE {
    DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
    DCHECK(!event_.IsSignaled());
    event_.Reset();

    if (BrowserThread::PostTask(
            BrowserThread::UI, FROM_HERE,
            base::Bind(&CryptoModuleBlockingDialogDelegate::ShowDialog,
                       // We block on event_ until the task completes, so
                       // there's no need to ref-count.
                       base::Unretained(this),
                       slot_name,
                       retry))) {
      event_.Wait();
    }
    *cancelled = cancelled_;
    return password_;
  }

 private:
  void ShowDialog(const std::string& slot_name,
                  bool retry) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    ShowCryptoModulePasswordDialog(
        slot_name, retry, reason_, server_,
        base::Bind(&CryptoModuleBlockingDialogDelegate::GotPassword,
                   // We block on event_ until the task completes, so
                   // there's no need to ref-count.
                   base::Unretained(this)));
  }

  void GotPassword(const char* password) {
    if (password)
      password_ = password;
    else
      cancelled_ = true;
    event_.Signal();
  }

  base::WaitableEvent event_;
  browser::CryptoModulePasswordReason reason_;
  std::string server_;
  std::string password_;
  bool cancelled_;

  DISALLOW_COPY_AND_ASSIGN(CryptoModuleBlockingDialogDelegate);
};
}  // namespace

namespace browser {

void ShowCryptoModulePasswordDialog(
    const std::string& slot_name,
    bool retry,
    CryptoModulePasswordReason reason,
    const std::string& server,
    const CryptoModulePasswordCallback& callback) {
#if defined(TOOLKIT_VIEWS)
  CryptoModulePasswordDialogView* dialog =
      new CryptoModulePasswordDialogView(
          slot_name, reason, server, callback);
  views::Widget* widget = CreateViewsWindow(NULL, dialog, STYLE_GENERIC);
  widget->Show();
#endif
}

crypto::CryptoModuleBlockingPasswordDelegate*
    NewCryptoModuleBlockingDialogDelegate(
        CryptoModulePasswordReason reason,
        const std::string& server) {
  return new CryptoModuleBlockingDialogDelegate(reason, server);
}
}  // namespace browser