Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <unordered_map> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/strings/string16.h" | |
| 15 #include "chrome/browser/chromeos/ui/request_pin_view.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 // Manages the state of the dialog that requests the PIN from user. Used by the | |
| 21 // extensions that need to request the PIN. Implemented as requirement for | |
| 22 // crbug.com/612886 | |
| 23 class PinDialogManager : RequestPinView::Delegate { | |
| 24 public: | |
| 25 enum RequestPinResponse { | |
| 26 SUCCESS, | |
| 27 INVALID_ID, | |
| 28 OTHER_FLOW_IN_PROGRESS, | |
| 29 DIALOG_DISPLAYED_ALREADY | |
| 30 }; | |
| 31 | |
| 32 PinDialogManager(); | |
| 33 ~PinDialogManager(); | |
| 34 | |
| 35 // Stores internally the |signRequestId| along with current timestamp. Also | |
| 36 // cleans up the storage from expired IDs. In unlikely case that the ID | |
| 37 // exists in the storage, returns false. Otherwise returns true. | |
| 38 bool AddSignRequestId(const std::string& extension_id, int sign_request_id); | |
| 39 | |
| 40 // Creates a new RequestPinView object and displays it in a dialog or reuses | |
| 41 // the old dialog if active one exists just updating the parameters. | |
| 42 // |extension_id| - the ID of the extension requesting the dialog. | |
| 43 // |extension_name| - the name of the extension requesting the dialog. | |
| 44 // |sign_request_id| - the ID given by Chrome when the extension was asked to | |
| 45 // sign the data. It should be a valid, not expired ID at the time the | |
| 46 // extension is requesting PIN the first time. | |
| 47 // |code_type| - the type of input requested: either "PIN" or "PUK". | |
| 48 // |error_type| - the error template to be displayed inside the dialog. If | |
| 49 // NONE, no error is displayed. | |
| 50 // |attempts_left| - the number of attempts the user has to try the code. It | |
| 51 // is informational only, and enforced on Chrome side only in case it's | |
| 52 // zero. In that case the textfield is disabled and the user can't provide | |
| 53 // any input to extension. If -1 the textfield from the dialog is enabled | |
| 54 // but no information about the attepts left is not given to the user. | |
| 55 // |callback| - used to notify about the user input in the text_field from the | |
| 56 // dialog. | |
| 57 // Returns SUCCESS if the dialog is displayed and extension owns it. Otherwise | |
| 58 // the specific error is returned. | |
| 59 RequestPinResponse ShowPinDialog( | |
| 60 const std::string& extension_id, | |
| 61 const std::string& extension_name, | |
| 62 int sign_request_id, | |
| 63 RequestPinCodeType code_type, | |
| 64 RequestPinErrorType error_type, | |
| 65 int attempts_left, | |
| 66 const RequestPinView::RequestPinCallback& callback); | |
| 67 | |
| 68 // The user provided input to dialog. | |
| 69 void OnPinDialogInput(); | |
| 70 | |
| 71 // chromeos::CloseViewDelegate | |
| 72 void OnPinDialogClosed() override; | |
| 73 | |
| 74 // Updates the existing dialog with new error message. Uses |callback| with | |
| 75 // empty string when user closes the dialog. Returns whether the provided | |
| 76 // |extension_id| matches the extension owning the active dialog. | |
| 77 bool UpdatePinDialog(const std::string& extension_id, | |
| 78 RequestPinErrorType error_type, | |
| 79 bool accept_input, | |
| 80 const RequestPinView::RequestPinCallback& callback); | |
| 81 | |
| 82 // Returns whether the last PIN dialog from this extension was closed by the | |
| 83 // user. | |
| 84 bool LastPinDialogClosed(const std::string& extension_id); | |
| 85 | |
| 86 // Called when extension calls the stopPinRequest method. The active dialog is | |
| 87 // closed if the |extension_id| matches the |active_dialog_extension_id_|. | |
| 88 // Returns whether the dialog was closed. | |
| 89 bool CloseDialog(const std::string& extension_id); | |
| 90 | |
| 91 // Resets the manager data related to the extension. | |
| 92 void ExtensionUnloaded(const std::string& extension_id); | |
| 93 | |
| 94 RequestPinView* active_view_for_testing() { return active_pin_dialog_; } | |
| 95 views::Widget* active_window_for_testing() { return active_window_; } | |
| 96 | |
| 97 private: | |
| 98 // State about the last response from user to the requestPin from extension. | |
| 99 std::unordered_map<std::string, bool> last_response_closed_; | |
| 100 | |
| 101 // The map with extension_id and sign request id issued by Chrome as key while | |
| 102 // the time when the id was generated is the value. | |
| 103 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.
| |
| 104 | |
| 105 // There can be only one active dialog to request PIN from this extension. | |
| 106 // Owned by |active_window_|. | |
| 107 chromeos::RequestPinView* active_pin_dialog_ = nullptr; | |
| 108 std::string active_dialog_extension_id_; | |
| 109 views::Widget* active_window_ = nullptr; | |
| 110 | |
| 111 base::WeakPtrFactory<PinDialogManager> weak_factory_; | |
| 112 }; | |
| 113 | |
| 114 } // namespace chromeos | |
| 115 | |
| 116 #endif // CHROME_BROWSER_CHROMEOS_CERTIFICATE_PROVIDER_PIN_DIALOG_MANAGER_H_ | |
| OLD | NEW |