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 #include "chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "chrome/browser/chromeos/login/ui/login_display_host.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/ui/browser_finder.h" |
| 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "ui/views/window/dialog_client_view.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // Define timeout for issued sign_request_id. |
| 18 const int SIGN_REQUEST_ID_TIMEOUT = 10; // 10 minutes |
| 19 |
| 20 PinDialogManager::PinDialogManager() : weak_factory_(this) {} |
| 21 |
| 22 PinDialogManager::~PinDialogManager() { |
| 23 // Close the active dialog if present to avoid leaking callbacks. |
| 24 if (active_pin_dialog_) { |
| 25 CloseDialog(active_dialog_extension_id_); |
| 26 } |
| 27 } |
| 28 |
| 29 bool PinDialogManager::AddSignRequestId(const std::string& extension_id, |
| 30 int sign_request_id) { |
| 31 std::pair<std::string, int> key(extension_id, sign_request_id); |
| 32 if (sign_request_times_.find(key) != sign_request_times_.end()) { |
| 33 return false; |
| 34 } |
| 35 // Cache the ID with current timestamp. |
| 36 base::Time current_time = base::Time::Now(); |
| 37 sign_request_times_[key] = current_time; |
| 38 |
| 39 return true; |
| 40 } |
| 41 |
| 42 PinDialogManager::RequestPinResponse PinDialogManager::ShowPinDialog( |
| 43 const std::string& extension_id, |
| 44 const std::string& extension_name, |
| 45 int sign_request_id, |
| 46 RequestPinCodeType code_type, |
| 47 RequestPinErrorType error_type, |
| 48 int attempts_left, |
| 49 const RequestPinView::RequestPinCallback& callback) { |
| 50 bool accept_input = (attempts_left != 0); |
| 51 // If active dialog exists already, we need to make sure it belongs to the |
| 52 // same extension and the user submitted some input. |
| 53 if (active_pin_dialog_ != nullptr) { |
| 54 DCHECK(!active_dialog_extension_id_.empty()); |
| 55 if (extension_id != active_dialog_extension_id_) { |
| 56 return RequestPinResponse::OTHER_FLOW_IN_PROGRESS; |
| 57 } |
| 58 |
| 59 // Extension requests a PIN without having received any input from its |
| 60 // previous request. Reject the new request. |
| 61 if (!active_pin_dialog_->IsLocked()) { |
| 62 return RequestPinResponse::DIALOG_DISPLAYED_ALREADY; |
| 63 } |
| 64 |
| 65 // Set the new callback to be used by the view. |
| 66 active_pin_dialog_->SetCallback(callback); |
| 67 active_pin_dialog_->SetDialogParameters(code_type, error_type, |
| 68 attempts_left, accept_input); |
| 69 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); |
| 70 return RequestPinResponse::SUCCESS; |
| 71 } |
| 72 |
| 73 // Check the validity of sign_request_id |
| 74 std::pair<std::string, int> key(extension_id, sign_request_id); |
| 75 if (sign_request_times_.find(key) == sign_request_times_.end()) { |
| 76 return RequestPinResponse::INVALID_ID; |
| 77 } |
| 78 |
| 79 base::Time current_time = base::Time::Now(); |
| 80 if ((current_time - sign_request_times_[key]).InMinutes() > |
| 81 SIGN_REQUEST_ID_TIMEOUT) { |
| 82 return RequestPinResponse::INVALID_ID; |
| 83 } |
| 84 |
| 85 active_dialog_extension_id_ = extension_id; |
| 86 active_pin_dialog_ = new chromeos::RequestPinView( |
| 87 extension_name, code_type, attempts_left, callback, this); |
| 88 |
| 89 gfx::NativeWindow parent = nullptr; |
| 90 if (chromeos::LoginDisplayHost::default_host()) { |
| 91 parent = chromeos::LoginDisplayHost::default_host()->GetNativeWindow(); |
| 92 } else { |
| 93 Browser* browser = chrome::FindTabbedBrowser( |
| 94 ProfileManager::GetPrimaryUserProfile(), true); |
| 95 if (browser) { |
| 96 parent = browser->window()->GetNativeWindow(); |
| 97 } |
| 98 } |
| 99 gfx::NativeWindow context = |
| 100 parent ? nullptr : ash::Shell::GetPrimaryRootWindow(); |
| 101 active_window_ = views::DialogDelegate::CreateDialogWidget(active_pin_dialog_, |
| 102 context, parent); |
| 103 active_window_->Show(); |
| 104 |
| 105 return RequestPinResponse::SUCCESS; |
| 106 } |
| 107 |
| 108 void PinDialogManager::OnPinDialogInput() { |
| 109 last_response_closed_[active_dialog_extension_id_] = false; |
| 110 } |
| 111 |
| 112 void PinDialogManager::OnPinDialogClosed() { |
| 113 last_response_closed_[active_dialog_extension_id_] = true; |
| 114 // |active_pin_dialog_| is managed by |active_window_|. This local copy of |
| 115 // the pointer is reset here to allow a new dialog to be created when a new |
| 116 // request comes. |
| 117 active_pin_dialog_ = nullptr; |
| 118 } |
| 119 |
| 120 bool PinDialogManager::UpdatePinDialog( |
| 121 const std::string& extension_id, |
| 122 RequestPinErrorType error_type, |
| 123 bool accept_input, |
| 124 const RequestPinView::RequestPinCallback& callback) { |
| 125 if (active_pin_dialog_ == nullptr) { |
| 126 return false; |
| 127 } |
| 128 |
| 129 if (extension_id != active_dialog_extension_id_ || |
| 130 !active_pin_dialog_->IsLocked()) { |
| 131 return false; |
| 132 } |
| 133 |
| 134 active_pin_dialog_->SetCallback(callback); |
| 135 active_pin_dialog_->SetDialogParameters(RequestPinCodeType::UNCHANGED, |
| 136 error_type, -1, accept_input); |
| 137 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); |
| 138 return true; |
| 139 } |
| 140 |
| 141 bool PinDialogManager::LastPinDialogClosed(const std::string& extension_id) { |
| 142 return last_response_closed_[extension_id]; |
| 143 } |
| 144 |
| 145 bool PinDialogManager::CloseDialog(const std::string& extension_id) { |
| 146 if (extension_id != active_dialog_extension_id_ || |
| 147 active_pin_dialog_ == nullptr) { |
| 148 LOG(ERROR) << "StopPinRequest called by wrong extension"; |
| 149 return false; |
| 150 } |
| 151 |
| 152 // Close the window. |active_pin_dialog_| gets deleted inside Close(). |
| 153 active_window_->Close(); |
| 154 active_pin_dialog_ = nullptr; |
| 155 |
| 156 return true; |
| 157 } |
| 158 |
| 159 void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { |
| 160 if (active_pin_dialog_ && active_dialog_extension_id_ == extension_id) { |
| 161 CloseDialog(extension_id); |
| 162 } |
| 163 |
| 164 last_response_closed_[extension_id] = false; |
| 165 |
| 166 for (auto it = sign_request_times_.cbegin(); |
| 167 it != sign_request_times_.cend();) { |
| 168 if (it->first.first == extension_id) { |
| 169 sign_request_times_.erase(it++); |
| 170 } else { |
| 171 ++it; |
| 172 } |
| 173 } |
| 174 } |
| 175 |
| 176 } // namespace chromeos |
OLD | NEW |