Index: chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h |
diff --git a/chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h b/chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0754d7ac6171ee1f2919969392db7de868b1bad5 |
--- /dev/null |
+++ b/chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h |
@@ -0,0 +1,116 @@ |
+// Copyright 2016 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. |
+ |
+#ifndef CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ |
+#define CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ |
+ |
+#include <map> |
+#include <string> |
+#include <unordered_map> |
+ |
+#include "base/callback.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/strings/string16.h" |
+#include "chrome/browser/chromeos/ui/request_pin_view.h" |
+#include "ui/views/widget/widget.h" |
+ |
+namespace chromeos { |
+ |
+// Manages the state of the dialog that requests the PIN from user. Used by the |
+// extensions that need to request the PIN. Implemented as requirement for |
+// crbug.com/612886 |
+class PinDialogManager : RequestPinView::Delegate { |
+ public: |
+ enum RequestPinResponse { |
+ SUCCESS, |
+ INVALID_ID, |
+ OTHER_FLOW_IN_PROGRESS, |
+ DIALOG_DISPLAYED_ALREADY |
+ }; |
+ |
+ PinDialogManager(); |
+ ~PinDialogManager(); |
+ |
+ // Stores internally the |signRequestId| along with current timestamp. Also |
+ // cleans up the storage from expired IDs. In unlikely case that the ID |
+ // exists in the storage, returns false. Otherwise returns true. |
+ bool AddSignRequestId(const std::string& extension_id, int sign_request_id); |
+ |
+ // Creates a new RequestPinView object and displays it in a dialog or reuses |
+ // the old dialog if active one exists just updating the parameters. |
+ // |extension_id| - the ID of the extension requesting the dialog. |
+ // |extension_name| - the name of the extension requesting the dialog. |
+ // |sign_request_id| - the ID given by Chrome when the extension was asked to |
+ // sign the data. It should be a valid, not expired ID at the time the |
+ // extension is requesting PIN the first time. |
+ // |code_type| - the type of input requested: either "PIN" or "PUK". |
+ // |error_type| - the error template to be displayed inside the dialog. If |
+ // NONE, no error is displayed. |
+ // |attempts_left| - the number of attempts the user has to try the code. It |
+ // is informational only, and enforced on Chrome side only in case it's |
+ // zero. In that case the textfield is disabled and the user can't provide |
+ // any input to extension. If -1 the textfield from the dialog is enabled |
+ // but no information about the attepts left is not given to the user. |
+ // |callback| - used to notify about the user input in the text_field from the |
+ // dialog. |
+ // Returns SUCCESS if the dialog is displayed and extension owns it. Otherwise |
+ // the specific error is returned. |
+ RequestPinResponse ShowPinDialog( |
+ const std::string& extension_id, |
+ const std::string& extension_name, |
+ int sign_request_id, |
+ RequestPinCodeType code_type, |
+ RequestPinErrorType error_type, |
+ int attempts_left, |
+ const RequestPinView::RequestPinCallback& callback); |
+ |
+ // The user provided input to dialog. |
+ void OnPinDialogInput(); |
+ |
+ // chromeos::CloseViewDelegate |
+ void OnPinDialogClosed() override; |
+ |
+ // Updates the existing dialog with new error message. Uses |callback| with |
+ // empty string when user closes the dialog. Returns whether the provided |
+ // |extension_id| matches the extension owning the active dialog. |
+ bool UpdatePinDialog(const std::string& extension_id, |
+ RequestPinErrorType error_type, |
+ bool accept_input, |
+ const RequestPinView::RequestPinCallback& callback); |
+ |
+ // Returns whether the last PIN dialog from this extension was closed by the |
+ // user. |
+ bool LastPinDialogClosed(const std::string& extension_id); |
+ |
+ // Called when extension calls the stopPinRequest method. The active dialog is |
+ // closed if the |extension_id| matches the |active_dialog_extension_id_|. |
+ // Returns whether the dialog was closed. |
+ bool CloseDialog(const std::string& extension_id); |
+ |
+ // Resets the manager data related to the extension. |
+ void ExtensionUnloaded(const std::string& extension_id); |
+ |
+ RequestPinView* active_view_for_testing() { return active_pin_dialog_; } |
+ views::Widget* active_window_for_testing() { return active_window_; } |
+ |
+ private: |
+ // State about the last response from user to the requestPin from extension. |
+ std::unordered_map<std::string, bool> last_response_closed_; |
+ |
+ // The map with extension_id and sign request id issued by Chrome as key while |
+ // the time when the id was generated is the value. |
+ std::map<std::pair<std::string, int>, base::Time> sign_request_times_; |
stevenjb
2016/09/12 21:16:39
We should typedef the key and use the typedef in t
igorcov
2016/09/13 14:19:32
Done.
|
+ |
+ // There can be only one active dialog to request PIN from this extension. |
+ // Owned by |active_window_|. |
+ chromeos::RequestPinView* active_pin_dialog_ = nullptr; |
+ std::string active_dialog_extension_id_; |
+ views::Widget* active_window_ = nullptr; |
+ |
+ base::WeakPtrFactory<PinDialogManager> weak_factory_; |
+}; |
+ |
+} // namespace chromeos |
+ |
+#endif // CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ |