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