Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/browser/chromeos/certificate_provider/pin_dialog_manager.cc

Issue 2094333002: Implementation for chrome.certificateProvider.requestPin/stopPinRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implemented review comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
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 // Close the active dialog if present to avoid leaking callbacks.
28 if (active_pin_dialog_) {
29 CloseDialog(active_dialog_extension_id_);
30 }
31 }
32
33 void PinDialogManager::OnPinDialogInput(const std::string& extension_id,
34 const bool closed) {
35 last_response_closed_[extension_id] = closed;
36 if (closed) {
37 // |active_pin_dialog_| is managed by |active_window_|. This local copy of
38 // the pointer is reset here to allow a new dialog to be created when a new
39 // request comes.
40 active_pin_dialog_ = nullptr;
41 return;
42 }
43
44 // Set the temporary callback to be called if user closes the dialog while
45 // request is processing by the extension.
46 active_pin_dialog_->SetLockedCallback(
47 base::Bind(&PinDialogManager::OnFlowInterrupted, base::Unretained(this)));
48 }
49
50 void PinDialogManager::OnFlowInterrupted(bool dialog_closed) {
51 if (!dialog_closed) {
52 return;
53 }
54
55 OnPinDialogInput(active_dialog_extension_id_, true);
56 }
57
58 bool PinDialogManager::LastPinDialogClosed(const std::string& extension_id) {
59 return last_response_closed_[extension_id];
60 }
61
62 bool PinDialogManager::UpdatePinDialog(
63 const std::string& extension_id,
64 RequestPinErrorType error_type,
65 const bool accept_input,
66 const RequestPinView::RequestPinCallback& callback) {
67 if (active_pin_dialog_ == nullptr) {
68 return false;
69 }
70
71 if (extension_id != active_dialog_extension_id_ ||
72 !active_pin_dialog_->IsLocked()) {
73 return false;
74 }
75
76 active_pin_dialog_->SetCallback(callback);
77 active_pin_dialog_->SetDialogParameters(RequestPinCodeType::UNCHANGED,
78 error_type, -1, accept_input);
79 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons();
80 return true;
81 }
82
83 RequestPinResponse PinDialogManager::ShowPinDialog(
84 const std::string& extension_id,
85 const std::string& extension_name,
86 const long long sign_request_id,
87 RequestPinCodeType code_type,
88 RequestPinErrorType error_type,
89 const int attempts_left,
90 const RequestPinView::RequestPinCallback& callback) {
91 const bool accept_input = (attempts_left != 0);
92 // If active dialog exists already, we need to make sure it belongs to the
93 // same extension and the user submitted some input.
94 if (active_pin_dialog_ != nullptr) {
95 DCHECK(!active_dialog_extension_id_.empty());
96 if (extension_id != active_dialog_extension_id_) {
97 return RequestPinResponse::OTHER_FLOW_IN_PROGRESS;
98 }
99
100 // Extension requests a PIN without having received any input from its
101 // previous request. Reject the new request.
102 if (!active_pin_dialog_->IsLocked()) {
103 return RequestPinResponse::DIALOG_DISPLAYED_ALREADY;
104 }
105
106 // Set the new callback to be used by the view.
107 active_pin_dialog_->SetCallback(callback);
108 active_pin_dialog_->SetDialogParameters(code_type, error_type,
109 attempts_left, accept_input);
110 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons();
111 return RequestPinResponse::SUCCESS;
112 }
113
114 // Check the validity of sign_request_id
115 if (sign_request_times_.find(sign_request_id) == sign_request_times_.end()) {
116 return RequestPinResponse::INVALID_ID;
117 }
118
119 struct timeval tv;
120 int result = gettimeofday(&tv, nullptr);
121 DCHECK_EQ(0, result);
122 if (tv.tv_sec - sign_request_times_[sign_request_id] >
123 SIGN_REQUEST_ID_TIMEOUT) {
124 return RequestPinResponse::INVALID_ID;
125 }
126
127 active_dialog_extension_id_ = extension_id;
128 active_pin_dialog_ = new chromeos::RequestPinView(extension_name, code_type,
129 attempts_left, callback);
130
131 gfx::NativeWindow parent = nullptr;
132 if (chromeos::LoginDisplayHost::default_host()) {
133 parent = chromeos::LoginDisplayHost::default_host()->GetNativeWindow();
134 } else {
135 Browser* browser = chrome::FindTabbedBrowser(
136 ProfileManager::GetPrimaryUserProfile(), true);
137 if (browser) {
138 parent = browser->window()->GetNativeWindow();
139 }
140 }
141 gfx::NativeWindow context =
142 parent ? nullptr : ash::Shell::GetPrimaryRootWindow();
143 active_window_ = views::DialogDelegate::CreateDialogWidget(active_pin_dialog_,
144 context, parent);
145 active_window_->Show();
146
147 return RequestPinResponse::SUCCESS;
148 }
149
150 bool PinDialogManager::CloseDialog(const std::string& extension_id) {
151 if (extension_id != active_dialog_extension_id_ ||
152 active_pin_dialog_ == nullptr) {
153 LOG(ERROR) << "StopPinRequest called by wrong extension";
154 return false;
155 }
156
157 // Close the window. |active_pin_dialog_| gets deleted inside Close().
158 active_window_->Close();
159 active_pin_dialog_ = nullptr;
160
161 return true;
162 }
163
164 bool PinDialogManager::AddSignRequestId(const uint64_t sign_request_id) {
165 if (sign_request_times_[sign_request_id]) {
166 return false;
167 }
168 // Cache the ID with current timestamp.
169 struct timeval tv;
170 int result = gettimeofday(&tv, nullptr);
171 DCHECK_EQ(0, result);
172 sign_request_times_[sign_request_id] = tv.tv_sec;
173 if (sign_request_times_.size() > MAX_SIGN_REQUESTS_MAP_SIZE) {
174 RemoveExpiredSignRequests(&tv);
175 }
176
177 return true;
178 }
179
180 void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) {
181 if (active_pin_dialog_ && active_dialog_extension_id_ == extension_id) {
182 CloseDialog(extension_id);
183 }
184
185 last_response_closed_[extension_id] = false;
186 }
187
188 void PinDialogManager::RemoveExpiredSignRequests(timeval* tv) {
189 for (auto it = sign_request_times_.cbegin();
190 it != sign_request_times_.cend();) {
191 if (tv->tv_sec - it->second > SIGN_REQUEST_ID_TIMEOUT) {
192 sign_request_times_.erase(it++);
193 } else {
194 ++it;
195 }
196 }
197 }
198
199 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698