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 "chrome/browser/chromeos/login/ui/login_display_host.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | |
| 10 #include "chrome/browser/ui/browser_finder.h" | |
| 11 #include "chrome/browser/ui/browser_window.h" | |
| 12 #include "ui/views/widget/widget.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 uint64_t SIGN_REQUEST_ID_TIMEOUT = 10 * 60; // 10 minutes | |
| 19 | |
| 20 // The limit of the sign_request_id map size, after which the sanitizing process | |
|
emaxx
2016/09/06 15:02:10
This looks a bit hacky, and may be not needed at a
igorcov
2016/09/07 09:12:28
Implemented the same way the storage is in SignReq
| |
| 21 // will be applied to remove expired entries. | |
| 22 const int MAX_SIGN_REQUESTS_MAP_SIZE = 10; | |
| 23 | |
| 24 PinDialogManager::PinDialogManager() : weak_factory_(this) {} | |
| 25 | |
| 26 PinDialogManager::~PinDialogManager() {} | |
| 27 | |
| 28 void PinDialogManager::OnPinDialogInput(const std::string& extension_id, | |
| 29 const bool closed) { | |
| 30 last_response_closed_[extension_id] = closed; | |
| 31 if (closed) { | |
| 32 // |active_pin_dialog_| is managed by |active_window_|. This local copy of | |
| 33 // the pointer is reset here to allow a new dialog to be created when a new | |
| 34 // request comes. | |
| 35 active_pin_dialog_ = nullptr; | |
| 36 return; | |
| 37 } | |
| 38 | |
| 39 // Set the temporary callback to be called if user closes the dialog while | |
| 40 // request is processing by the extension. | |
| 41 active_pin_dialog_->SetCallback( | |
| 42 base::Bind(&PinDialogManager::OnFlowInterrupted, base::Unretained(this))); | |
|
stevenjb
2016/08/11 18:12:02
base::Unretained is unsafe here. The view (dialog)
igorcov
2016/09/06 13:22:02
Included in destructor of the class to close the a
| |
| 43 } | |
| 44 | |
| 45 void PinDialogManager::OnFlowInterrupted(const base::string16& value) { | |
| 46 DCHECK(value.empty()); | |
| 47 OnPinDialogInput(active_dialog_extension_id_, true); | |
| 48 } | |
| 49 | |
| 50 bool PinDialogManager::LastPinDialogClosed(const std::string& extension_id) { | |
| 51 return last_response_closed_[extension_id]; | |
| 52 } | |
| 53 | |
| 54 bool PinDialogManager::UpdatePinDialog( | |
| 55 const std::string& extension_id, | |
| 56 RequestPinErrorType error_type, | |
| 57 const bool accept_input, | |
| 58 const RequestPinView::RequestPinCallback& callback) { | |
| 59 if (active_pin_dialog_ == nullptr) { | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 if (extension_id != active_dialog_extension_id_ || | |
| 64 !active_pin_dialog_->IsLocked()) { | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 active_pin_dialog_->SetCallback(callback); | |
| 69 active_pin_dialog_->SetDialogParameters(RequestPinCodeType::UNCHANGED, | |
| 70 error_type, nullptr, accept_input); | |
| 71 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 RequestPinResponse PinDialogManager::ShowPinDialog( | |
| 76 const std::string& extension_id, | |
| 77 const std::string& extension_name, | |
| 78 const long long sign_request_id, | |
| 79 RequestPinCodeType code_type, | |
| 80 RequestPinErrorType error_type, | |
| 81 std::unique_ptr<int> attempts_left, | |
| 82 const RequestPinView::RequestPinCallback& callback) { | |
| 83 const bool accept_input = (!attempts_left || *(attempts_left.get()) > 0); | |
| 84 // If active dialog exists already, we need to make sure it belongs to the | |
| 85 // same extension and the user submitted some input. | |
| 86 if (active_pin_dialog_ != nullptr) { | |
| 87 DCHECK(!active_dialog_extension_id_.empty()); | |
| 88 if (extension_id != active_dialog_extension_id_) { | |
| 89 return RequestPinResponse::OTHER_FLOW_IN_PROGRESS; | |
| 90 } | |
| 91 | |
| 92 // Extension requests a PIN without having received any input from its | |
| 93 // previous request. Reject the new request. | |
| 94 if (!active_pin_dialog_->IsLocked()) { | |
| 95 return RequestPinResponse::DIALOG_DISPLAYED_ALREADY; | |
| 96 } | |
| 97 | |
| 98 // Set the new callback to be used by the view. | |
| 99 active_pin_dialog_->SetCallback(callback); | |
| 100 active_pin_dialog_->SetDialogParameters( | |
| 101 code_type, error_type, std::move(attempts_left), accept_input); | |
| 102 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); | |
| 103 return RequestPinResponse::SUCCESS; | |
| 104 } | |
| 105 | |
| 106 // Check the validity of sign_request_id | |
| 107 if (sign_request_times_.find(sign_request_id) == sign_request_times_.end()) { | |
| 108 return RequestPinResponse::INVALID_ID; | |
| 109 } | |
| 110 | |
| 111 struct timeval tv; | |
| 112 int result = gettimeofday(&tv, nullptr); | |
|
emaxx
2016/09/06 15:02:10
The same question as in the header - would it be p
igorcov
2016/09/07 09:12:28
Done.
| |
| 113 DCHECK_EQ(0, result); | |
| 114 if (tv.tv_sec - sign_request_times_[sign_request_id] > | |
| 115 SIGN_REQUEST_ID_TIMEOUT) | |
| 116 return RequestPinResponse::INVALID_ID; | |
|
stevenjb
2016/08/11 18:12:02
{}
igorcov
2016/09/06 13:22:02
Done.
| |
| 117 | |
| 118 active_dialog_extension_id_ = extension_id; | |
| 119 active_pin_dialog_ = new chromeos::RequestPinView( | |
| 120 extension_name, code_type, std::move(attempts_left), callback); | |
| 121 | |
| 122 gfx::NativeWindow parent = nullptr; | |
| 123 if (chromeos::LoginDisplayHost::default_host()) { | |
| 124 parent = chromeos::LoginDisplayHost::default_host()->GetNativeWindow(); | |
| 125 } else { | |
| 126 Browser* browser = chrome::FindTabbedBrowser( | |
| 127 ProfileManager::GetPrimaryUserProfile(), true); | |
| 128 if (browser) { | |
| 129 parent = browser->window()->GetNativeWindow(); | |
| 130 } | |
| 131 } | |
| 132 gfx::NativeWindow context = | |
| 133 parent ? nullptr : ash::Shell::GetPrimaryRootWindow(); | |
| 134 active_window_ = views::DialogDelegate::CreateDialogWidget(active_pin_dialog_, | |
| 135 context, parent); | |
| 136 active_window_->Show(); | |
| 137 | |
| 138 return RequestPinResponse::SUCCESS; | |
| 139 } | |
| 140 | |
| 141 bool PinDialogManager::CloseDialog(const std::string& extension_id) { | |
| 142 if (extension_id != active_dialog_extension_id_ || | |
| 143 active_pin_dialog_ == nullptr) { | |
| 144 LOG(ERROR) << "StopPinRequest called by wrong extension"; | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 // Notifying the old callback if present. | |
| 149 active_pin_dialog_->Cancel(); | |
| 150 | |
| 151 // Close the window. |active_pin_dialog_| gets deleted inside Close(). | |
| 152 active_window_->Close(); | |
| 153 active_pin_dialog_ = nullptr; | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 bool PinDialogManager::AddSignRequestId(const uint64_t sign_request_id) { | |
| 159 if (sign_request_times_[sign_request_id]) { | |
| 160 return false; | |
| 161 } | |
| 162 // Cache the ID with current timestamp. | |
| 163 struct timeval tv; | |
| 164 int result = gettimeofday(&tv, nullptr); | |
| 165 DCHECK_EQ(0, result); | |
| 166 sign_request_times_[sign_request_id] = tv.tv_sec; | |
| 167 if (sign_request_times_.size() > MAX_SIGN_REQUESTS_MAP_SIZE) { | |
| 168 RemoveExpiredSignRequests(&tv); | |
| 169 } | |
| 170 | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { | |
| 175 if (active_pin_dialog_ && active_dialog_extension_id_ == extension_id) { | |
| 176 CloseDialog(extension_id); | |
| 177 } | |
| 178 | |
| 179 last_response_closed_[extension_id] = false; | |
| 180 } | |
| 181 | |
| 182 void PinDialogManager::RemoveExpiredSignRequests(timeval* tv) { | |
| 183 for (auto it = sign_request_times_.cbegin(); | |
| 184 it != sign_request_times_.cend();) { | |
| 185 if (tv->tv_sec - it->second > SIGN_REQUEST_ID_TIMEOUT) { | |
| 186 sign_request_times_.erase(it++); | |
| 187 } else { | |
| 188 ++it; | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 } // namespace chromeos | |
| OLD | NEW |