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