15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_CRYPTO_MODULE_DELEGATE_NSS_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CHROME_BROWSER_UI_CRYPTO_MODULE_DELEGATE_NSS_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/synchronization/waitable_event.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/crypto_module_password_dialog.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "crypto/nss_crypto_module_delegate.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/host_port_pair.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ResourceContext;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Delegate to handle unlocking a slot or indicating which slot to store a key
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// in. When passing to NSS functions which take a wincx argument, use the value
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// returned from the wincx() method.
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ChromeNSSCryptoModuleDelegate
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public crypto::NSSCryptoModuleDelegate {
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Create a ChromeNSSCryptoModuleDelegate. |reason| is used to select what
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // string to show the user, |server| is displayed to indicate which connection
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is causing the dialog to appear.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ChromeNSSCryptoModuleDelegate(chrome::CryptoModulePasswordReason reason,
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                const net::HostPortPair& server);
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~ChromeNSSCryptoModuleDelegate();
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Must be called on IO thread. Returns true if the delegate is ready for use.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Otherwise, if |initialization_complete_callback| is non-null, the
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // initialization will proceed asynchronously and the callback will be run
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // once the delegate is ready to use. In that case, the caller must ensure the
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // delegate remains alive until the callback is run.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool InitializeSlot(content::ResourceContext* context,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      const base::Closure& initialization_complete_callback)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WARN_UNUSED_RESULT;
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // crypto::NSSCryptoModuleDelegate implementation.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual crypto::ScopedPK11Slot RequestSlot() OVERRIDE;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // crypto::CryptoModuleBlockingPasswordDelegate implementation.
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual std::string RequestPassword(const std::string& slot_name,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      bool retry,
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      bool* cancelled) OVERRIDE;
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ShowDialog(const std::string& slot_name, bool retry);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void GotPassword(const std::string& password);
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void DidGetSlot(const base::Closure& callback, crypto::ScopedPK11Slot slot);
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Parameters displayed in the dialog.
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const chrome::CryptoModulePasswordReason reason_;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::HostPortPair server_;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Event to block worker thread while waiting for dialog on UI thread.
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::WaitableEvent event_;
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Stores the results from the dialog for access on worker thread.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string password_;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool cancelled_;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The slot which will be returned by RequestSlot.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crypto::ScopedPK11Slot slot_;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ChromeNSSCryptoModuleDelegate);
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Create a delegate which only handles unlocking slots.
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)crypto::CryptoModuleBlockingPasswordDelegate*
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CreateCryptoModuleBlockingPasswordDelegate(
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        chrome::CryptoModulePasswordReason reason,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const net::HostPortPair& server);
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // CHROME_BROWSER_UI_CRYPTO_MODULE_DELEGATE_NSS_H_
82