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 | |
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_rejected_[extension_id] = closed; | |
31 if (closed) { | |
32 active_pin_dialog_ = nullptr; | |
stevenjb
2016/08/11 01:58:54
Leaky?
igorcov1
2016/08/11 16:15:23
No. The active_window_ manages that object, sorry
| |
33 return; | |
34 } | |
35 | |
36 // Set the temporary callback to be called if user closes the dialog while | |
37 // request is processing by the extension. | |
38 active_pin_dialog_->SetCallback( | |
39 base::Bind(&PinDialogManager::OnFlowInterrupted, base::Unretained(this))); | |
40 } | |
41 | |
42 void PinDialogManager::OnFlowInterrupted(const base::string16& value) { | |
43 DCHECK(value.empty()); | |
44 OnPinDialogInput(active_dialog_extension_id_, true); | |
45 } | |
46 | |
47 bool PinDialogManager::LastPinDialogClosed(const std::string& extension_id) { | |
48 return last_rejected_[extension_id]; | |
stevenjb
2016/08/11 01:58:54
We should probably actually name this last_respons
igorcov1
2016/08/11 16:15:23
Done.
| |
49 } | |
50 | |
51 bool PinDialogManager::UpdatePinDialog( | |
52 const std::string& extension_id, | |
53 RequestPinErrorType error_type, | |
54 const bool accept_input, | |
55 const RequestPinView::RequestPinCallback& callback) { | |
56 if (active_pin_dialog_ == nullptr) { | |
57 return false; | |
58 } | |
59 | |
60 if (extension_id != active_dialog_extension_id_ || | |
61 !active_pin_dialog_->IsLocked()) { | |
62 return false; | |
63 } | |
64 | |
65 active_pin_dialog_->SetCallback(callback); | |
66 active_pin_dialog_->SetDialogParameters(RequestPinCodeType::UNCHANGED, | |
67 error_type, nullptr, accept_input); | |
68 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); | |
69 return true; | |
70 } | |
71 | |
72 RequestPinResponse PinDialogManager::ShowPinDialog( | |
73 const std::string& extension_id, | |
74 const std::string& extension_name, | |
75 const long long sign_request_id, | |
76 RequestPinCodeType code_type, | |
77 RequestPinErrorType error_type, | |
78 std::unique_ptr<int> attempts_left, | |
79 const RequestPinView::RequestPinCallback& callback) { | |
80 const bool accept_input = (!attempts_left || *(attempts_left.get()) > 0); | |
81 // Don't allow the extension to create anything if an active dialog already | |
82 // exists. Still if the dialog is there, initiated by the extension then skip | |
83 // the signRequestId check as it's possibly expired. | |
stevenjb
2016/08/11 01:58:54
This is confusing and I'm not sure I understand it
igorcov1
2016/08/11 16:15:22
Added another state in the enum, for the case when
| |
84 if (active_pin_dialog_ != nullptr) { | |
85 if (!active_dialog_extension_id_.empty() && | |
86 extension_id == active_dialog_extension_id_ && | |
87 active_pin_dialog_->IsLocked()) { | |
88 // Set the new callback to be used by the view. | |
89 active_pin_dialog_->SetCallback(callback); | |
90 active_pin_dialog_->SetDialogParameters( | |
91 code_type, error_type, std::move(attempts_left), accept_input); | |
92 active_pin_dialog_->GetDialogClientView()->UpdateDialogButtons(); | |
93 return RequestPinResponse::SUCCESS; | |
94 } | |
95 | |
96 return RequestPinResponse::OTHER_FLOW_IN_PROGRESS; | |
97 } | |
98 | |
99 // Check the validity of sign_request_id | |
100 if (sign_request_ids_.find(sign_request_id) == sign_request_ids_.end()) { | |
101 return RequestPinResponse::INVALID_ID; | |
102 } | |
103 | |
104 struct timeval tv; | |
105 int result = gettimeofday(&tv, NULL); | |
stevenjb
2016/08/11 01:58:54
nullptr throughout
igorcov1
2016/08/11 16:15:23
Done.
| |
106 DCHECK_EQ(0, result); | |
107 if (tv.tv_sec - sign_request_ids_[sign_request_id] > SIGN_REQUEST_ID_TIMEOUT) | |
108 return RequestPinResponse::INVALID_ID; | |
109 | |
110 active_dialog_extension_id_ = extension_id; | |
111 active_pin_dialog_ = new chromeos::RequestPinView( | |
112 extension_name, code_type, std::move(attempts_left), callback); | |
113 gfx::NativeWindow context = ash::Shell::GetPrimaryRootWindow(); | |
114 if (chromeos::LoginDisplayHost::default_host()) { | |
115 active_window_ = views::DialogDelegate::CreateDialogWidget( | |
116 active_pin_dialog_, context, | |
117 chromeos::LoginDisplayHost::default_host()->GetNativeWindow()); | |
118 active_window_->Show(); | |
119 } else { | |
120 Browser* browser = chrome::FindTabbedBrowser( | |
121 ProfileManager::GetPrimaryUserProfile(), true); | |
122 if (browser) { | |
123 gfx::NativeWindow native_window = browser->window()->GetNativeWindow(); | |
124 active_window_ = views::DialogDelegate::CreateDialogWidget( | |
125 active_pin_dialog_, context, native_window); | |
126 active_window_->Show(); | |
127 } | |
stevenjb
2016/08/11 01:58:55
We should test and comment what happens if there i
igorcov1
2016/08/11 16:15:23
I took the code from here:
https://cs.chromium.org
| |
128 } | |
129 | |
130 return RequestPinResponse::SUCCESS; | |
131 } | |
132 | |
133 bool PinDialogManager::CloseDialog(const std::string& extension_id) { | |
134 if (extension_id != active_dialog_extension_id_ || | |
135 active_pin_dialog_ == nullptr) { | |
136 LOG(ERROR) << "StopPinRequest called by wrong extension"; | |
137 return false; | |
138 } | |
139 | |
140 // Notifying the old callback if present. | |
141 active_pin_dialog_->Cancel(); | |
142 | |
143 // Close the window. |active_pin_dialog_| gets deleted inside Close(). | |
144 active_window_->Close(); | |
145 active_pin_dialog_ = nullptr; | |
stevenjb
2016/08/11 01:58:54
If active_pin_dialog_ is owned by acitve_window_,
igorcov1
2016/08/11 16:15:23
Done.
| |
146 | |
147 return true; | |
148 } | |
149 | |
150 bool PinDialogManager::AddSignRequestId(const uint64_t sign_request_id) { | |
151 if (sign_request_ids_[sign_request_id]) { | |
152 return false; | |
153 } | |
154 // Cache the ID with current timestamp. | |
155 struct timeval tv; | |
156 int result = gettimeofday(&tv, NULL); | |
157 DCHECK_EQ(0, result); | |
158 sign_request_ids_[sign_request_id] = tv.tv_sec; | |
159 if (sign_request_ids_.size() > MAX_SIGN_REQUESTS_MAP_SIZE) { | |
160 RemoveExpiredSignRequests(&tv); | |
161 } | |
162 | |
163 return true; | |
164 } | |
165 | |
166 void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { | |
167 if (active_pin_dialog_ && active_dialog_extension_id_ == extension_id) { | |
168 CloseDialog(extension_id); | |
169 } | |
170 | |
171 last_rejected_[extension_id] = false; | |
172 } | |
173 | |
174 void PinDialogManager::RemoveExpiredSignRequests(timeval* tv) { | |
175 for (auto it = sign_request_ids_.cbegin(); it != sign_request_ids_.cend();) { | |
176 if (tv->tv_sec - it->second > SIGN_REQUEST_ID_TIMEOUT) { | |
177 sign_request_ids_.erase(it++); | |
178 } else { | |
179 ++it; | |
180 } | |
181 } | |
182 } | |
183 | |
184 } // namespace chromeos | |
OLD | NEW |