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