1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/browser_navigator.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chromeos/network/client_cert_util.h"
16 #include "chromeos/network/managed_network_configuration_handler.h"
17 #include "chromeos/network/network_event_log.h"
18 #include "chromeos/network/network_state.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "extensions/browser/extension_host.h"
21 #include "extensions/common/constants.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/page_transition_types.h"
24 #include "ui/views/controls/label.h"
25 #include "ui/views/layout/grid_layout.h"
26 #include "ui/views/layout/layout_constants.h"
27 #include "ui/views/widget/widget.h"
28 #include "ui/views/window/dialog_delegate.h"
34 // Default width/height of the dialog.
35 const int kDefaultWidth
= 350;
36 const int kDefaultHeight
= 100;
38 ////////////////////////////////////////////////////////////////////////////////
39 // Dialog for certificate enrollment. This displays the content from the
40 // certificate enrollment URI.
41 class EnrollmentDialogView
: public views::DialogDelegateView
{
43 virtual ~EnrollmentDialogView();
45 static void ShowDialog(gfx::NativeWindow owning_window
,
46 const std::string
& network_name
,
48 const GURL
& target_uri
,
49 const base::Closure
& connect
);
51 // views::DialogDelegateView overrides
52 virtual int GetDialogButtons() const override
;
53 virtual bool Accept() override
;
54 virtual void OnClosed() override
;
55 virtual base::string16
GetDialogButtonLabel(
56 ui::DialogButton button
) const override
;
58 // views::WidgetDelegate overrides
59 virtual ui::ModalType
GetModalType() const override
;
60 virtual base::string16
GetWindowTitle() const override
;
62 // views::View overrides
63 virtual gfx::Size
GetPreferredSize() const override
;
66 EnrollmentDialogView(const std::string
& network_name
,
68 const GURL
& target_uri
,
69 const base::Closure
& connect
);
73 std::string network_name_
;
76 base::Closure connect_
;
80 ////////////////////////////////////////////////////////////////////////////////
81 // EnrollmentDialogView implementation.
83 EnrollmentDialogView::EnrollmentDialogView(const std::string
& network_name
,
85 const GURL
& target_uri
,
86 const base::Closure
& connect
)
88 network_name_(network_name
),
90 target_uri_(target_uri
),
95 EnrollmentDialogView::~EnrollmentDialogView() {
99 void EnrollmentDialogView::ShowDialog(gfx::NativeWindow owning_window
,
100 const std::string
& network_name
,
102 const GURL
& target_uri
,
103 const base::Closure
& connect
) {
104 EnrollmentDialogView
* dialog_view
=
105 new EnrollmentDialogView(network_name
, profile
, target_uri
, connect
);
106 views::DialogDelegate::CreateDialogWidget(dialog_view
, NULL
, owning_window
);
107 dialog_view
->InitDialog();
108 views::Widget
* widget
= dialog_view
->GetWidget();
113 int EnrollmentDialogView::GetDialogButtons() const {
114 return ui::DIALOG_BUTTON_CANCEL
| ui::DIALOG_BUTTON_OK
;
117 bool EnrollmentDialogView::Accept() {
122 void EnrollmentDialogView::OnClosed() {
125 chrome::NavigateParams
params(profile_
,
127 ui::PAGE_TRANSITION_LINK
);
128 params
.disposition
= NEW_FOREGROUND_TAB
;
129 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
130 chrome::Navigate(¶ms
);
133 base::string16
EnrollmentDialogView::GetDialogButtonLabel(
134 ui::DialogButton button
) const {
135 if (button
== ui::DIALOG_BUTTON_OK
)
136 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_BUTTON
);
137 return views::DialogDelegateView::GetDialogButtonLabel(button
);
140 ui::ModalType
EnrollmentDialogView::GetModalType() const {
141 return ui::MODAL_TYPE_SYSTEM
;
144 base::string16
EnrollmentDialogView::GetWindowTitle() const {
145 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE
);
148 gfx::Size
EnrollmentDialogView::GetPreferredSize() const {
149 return gfx::Size(kDefaultWidth
, kDefaultHeight
);
152 void EnrollmentDialogView::InitDialog() {
154 // Create the views and layout manager and set them up.
155 views::Label
* label
= new views::Label(
156 l10n_util::GetStringFUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_INSTRUCTIONS
,
157 base::UTF8ToUTF16(network_name_
)));
158 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
159 label
->SetMultiLine(true);
160 label
->SetAllowCharacterBreak(true);
162 views::GridLayout
* grid_layout
= views::GridLayout::CreatePanel(this);
163 SetLayoutManager(grid_layout
);
165 views::ColumnSet
* columns
= grid_layout
->AddColumnSet(0);
166 columns
->AddColumn(views::GridLayout::FILL
, // Horizontal resize.
167 views::GridLayout::FILL
, // Vertical resize.
169 views::GridLayout::USE_PREF
, // Size type.
170 0, // Ignored for USE_PREF.
172 columns
= grid_layout
->AddColumnSet(1);
173 columns
->AddPaddingColumn(
174 0, views::kUnrelatedControlHorizontalSpacing
);
175 columns
->AddColumn(views::GridLayout::LEADING
, // Horizontal leading.
176 views::GridLayout::FILL
, // Vertical resize.
178 views::GridLayout::USE_PREF
, // Size type.
179 0, // Ignored for USE_PREF.
182 grid_layout
->StartRow(0, 0);
183 grid_layout
->AddView(label
);
184 grid_layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
185 grid_layout
->Layout(this);
188 ////////////////////////////////////////////////////////////////////////////////
189 // Handler for certificate enrollment.
191 class DialogEnrollmentDelegate
{
193 // |owning_window| is the window that will own the dialog.
194 DialogEnrollmentDelegate(gfx::NativeWindow owning_window
,
195 const std::string
& network_name
,
197 ~DialogEnrollmentDelegate();
199 // EnrollmentDelegate overrides
200 bool Enroll(const std::vector
<std::string
>& uri_list
,
201 const base::Closure
& connect
);
204 gfx::NativeWindow owning_window_
;
205 std::string network_name_
;
208 DISALLOW_COPY_AND_ASSIGN(DialogEnrollmentDelegate
);
211 DialogEnrollmentDelegate::DialogEnrollmentDelegate(
212 gfx::NativeWindow owning_window
,
213 const std::string
& network_name
,
214 Profile
* profile
) : owning_window_(owning_window
),
215 network_name_(network_name
),
218 DialogEnrollmentDelegate::~DialogEnrollmentDelegate() {}
220 bool DialogEnrollmentDelegate::Enroll(const std::vector
<std::string
>& uri_list
,
221 const base::Closure
& post_action
) {
222 // Keep the closure for later activation if we notice that
223 // a certificate has been added.
225 // TODO(gspencer): Do something smart with the closure. At the moment it is
226 // being ignored because we don't know when the enrollment tab is closed.
227 // http://crosbug.com/30422
228 for (std::vector
<std::string
>::const_iterator iter
= uri_list
.begin();
229 iter
!= uri_list
.end(); ++iter
) {
231 if (uri
.IsStandard() || uri
.scheme() == extensions::kExtensionScheme
) {
232 // If this is a "standard" scheme, like http, ftp, etc., then open that in
233 // the enrollment dialog.
234 NET_LOG_EVENT("Showing enrollment dialog", network_name_
);
235 EnrollmentDialogView::ShowDialog(owning_window_
,
241 NET_LOG_DEBUG("Nonstandard URI: " + uri
.spec(), network_name_
);
244 // No appropriate scheme was found.
245 NET_LOG_ERROR("No usable enrollment URI", network_name_
);
249 void EnrollmentComplete(const std::string
& service_path
) {
250 NET_LOG_USER("Enrollment Complete", service_path
);
255 ////////////////////////////////////////////////////////////////////////////////
258 namespace enrollment
{
260 bool CreateDialog(const std::string
& service_path
,
261 gfx::NativeWindow owning_window
) {
262 const NetworkState
* network
= NetworkHandler::Get()->network_state_handler()->
263 GetNetworkState(service_path
);
265 NET_LOG_ERROR("Enrolling Unknown network", service_path
);
268 Browser
* browser
= chrome::FindBrowserWithWindow(owning_window
);
270 browser
? browser
->profile() : ProfileManager::GetPrimaryUserProfile();
271 std::string username_hash
= ProfileHelper::GetUserIdHashFromProfile(profile
);
273 onc::ONCSource onc_source
= onc::ONC_SOURCE_NONE
;
274 const base::DictionaryValue
* policy
=
275 NetworkHandler::Get()
276 ->managed_network_configuration_handler()
277 ->FindPolicyByGUID(username_hash
, network
->guid(), &onc_source
);
279 // We skip certificate patterns for device policy ONC so that an unmanaged
280 // user can't get to the place where a cert is presented for them
282 if (!policy
|| onc_source
== onc::ONC_SOURCE_DEVICE_POLICY
)
285 client_cert::ClientCertConfig cert_config
;
286 OncToClientCertConfig(*policy
, &cert_config
);
288 if (cert_config
.client_cert_type
!= onc::client_cert::kPattern
)
291 if (cert_config
.pattern
.Empty())
292 NET_LOG_ERROR("Certificate pattern is empty", service_path
);
294 if (cert_config
.pattern
.enrollment_uri_list().empty()) {
295 NET_LOG_EVENT("No enrollment URIs", service_path
);
299 NET_LOG_USER("Enrolling", service_path
);
301 DialogEnrollmentDelegate
* enrollment
=
302 new DialogEnrollmentDelegate(owning_window
, network
->name(), profile
);
303 return enrollment
->Enroll(cert_config
.pattern
.enrollment_uri_list(),
304 base::Bind(&EnrollmentComplete
, service_path
));
307 } // namespace enrollment
309 } // namespace chromeos