Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / enrollment_dialog_view.cc
blob79b9d2e9524a2aa357a22286f1a93fb50f182c32
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"
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_host.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 "chromeos/network/network_event_log.h"
15 #include "chromeos/network/network_state.h"
16 #include "chromeos/network/network_state_handler.h"
17 #include "content/public/common/page_transition_types.h"
18 #include "extensions/common/constants.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/views/controls/label.h"
22 #include "ui/views/layout/grid_layout.h"
23 #include "ui/views/layout/layout_constants.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/window/dialog_delegate.h"
27 namespace chromeos {
29 namespace {
31 // Default width/height of the dialog.
32 const int kDefaultWidth = 350;
33 const int kDefaultHeight = 100;
35 ////////////////////////////////////////////////////////////////////////////////
36 // Dialog for certificate enrollment. This displays the content from the
37 // certificate enrollment URI.
38 class EnrollmentDialogView : public views::DialogDelegateView {
39 public:
40 virtual ~EnrollmentDialogView();
42 static void ShowDialog(gfx::NativeWindow owning_window,
43 const std::string& network_name,
44 Profile* profile,
45 const GURL& target_uri,
46 const base::Closure& connect);
48 // views::DialogDelegateView overrides
49 virtual int GetDialogButtons() const OVERRIDE;
50 virtual bool Accept() OVERRIDE;
51 virtual void OnClosed() OVERRIDE;
52 virtual base::string16 GetDialogButtonLabel(
53 ui::DialogButton button) const OVERRIDE;
55 // views::WidgetDelegate overrides
56 virtual ui::ModalType GetModalType() const OVERRIDE;
57 virtual base::string16 GetWindowTitle() const OVERRIDE;
59 // views::View overrides
60 virtual gfx::Size GetPreferredSize() OVERRIDE;
62 private:
63 EnrollmentDialogView(const std::string& network_name,
64 Profile* profile,
65 const GURL& target_uri,
66 const base::Closure& connect);
67 void InitDialog();
69 bool accepted_;
70 std::string network_name_;
71 Profile* profile_;
72 GURL target_uri_;
73 base::Closure connect_;
74 bool added_cert_;
77 ////////////////////////////////////////////////////////////////////////////////
78 // EnrollmentDialogView implementation.
80 EnrollmentDialogView::EnrollmentDialogView(const std::string& network_name,
81 Profile* profile,
82 const GURL& target_uri,
83 const base::Closure& connect)
84 : accepted_(false),
85 network_name_(network_name),
86 profile_(profile),
87 target_uri_(target_uri),
88 connect_(connect),
89 added_cert_(false) {
92 EnrollmentDialogView::~EnrollmentDialogView() {
95 // static
96 void EnrollmentDialogView::ShowDialog(gfx::NativeWindow owning_window,
97 const std::string& network_name,
98 Profile* profile,
99 const GURL& target_uri,
100 const base::Closure& connect) {
101 EnrollmentDialogView* dialog_view =
102 new EnrollmentDialogView(network_name, profile, target_uri, connect);
103 views::DialogDelegate::CreateDialogWidget(dialog_view, NULL, owning_window);
104 dialog_view->InitDialog();
105 views::Widget* widget = dialog_view->GetWidget();
106 DCHECK(widget);
107 widget->Show();
110 int EnrollmentDialogView::GetDialogButtons() const {
111 return ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
114 bool EnrollmentDialogView::Accept() {
115 accepted_ = true;
116 return true;
119 void EnrollmentDialogView::OnClosed() {
120 if (!accepted_)
121 return;
122 chrome::NavigateParams params(profile_,
123 GURL(target_uri_),
124 content::PAGE_TRANSITION_LINK);
125 params.disposition = NEW_FOREGROUND_TAB;
126 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
127 chrome::Navigate(&params);
130 base::string16 EnrollmentDialogView::GetDialogButtonLabel(
131 ui::DialogButton button) const {
132 if (button == ui::DIALOG_BUTTON_OK)
133 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_BUTTON);
134 return views::DialogDelegateView::GetDialogButtonLabel(button);
137 ui::ModalType EnrollmentDialogView::GetModalType() const {
138 return ui::MODAL_TYPE_SYSTEM;
141 base::string16 EnrollmentDialogView::GetWindowTitle() const {
142 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE);
145 gfx::Size EnrollmentDialogView::GetPreferredSize() {
146 return gfx::Size(kDefaultWidth, kDefaultHeight);
149 void EnrollmentDialogView::InitDialog() {
150 added_cert_ = false;
151 // Create the views and layout manager and set them up.
152 views::Label* label = new views::Label(
153 l10n_util::GetStringFUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_INSTRUCTIONS,
154 base::UTF8ToUTF16(network_name_)));
155 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
156 label->SetMultiLine(true);
157 label->SetAllowCharacterBreak(true);
159 views::GridLayout* grid_layout = views::GridLayout::CreatePanel(this);
160 SetLayoutManager(grid_layout);
162 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
163 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize.
164 views::GridLayout::FILL, // Vertical resize.
165 1, // Resize weight.
166 views::GridLayout::USE_PREF, // Size type.
167 0, // Ignored for USE_PREF.
168 0); // Minimum size.
169 columns = grid_layout->AddColumnSet(1);
170 columns->AddPaddingColumn(
171 0, views::kUnrelatedControlHorizontalSpacing);
172 columns->AddColumn(views::GridLayout::LEADING, // Horizontal leading.
173 views::GridLayout::FILL, // Vertical resize.
174 1, // Resize weight.
175 views::GridLayout::USE_PREF, // Size type.
176 0, // Ignored for USE_PREF.
177 0); // Minimum size.
179 grid_layout->StartRow(0, 0);
180 grid_layout->AddView(label);
181 grid_layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
182 grid_layout->Layout(this);
185 ////////////////////////////////////////////////////////////////////////////////
186 // Handler for certificate enrollment.
188 class DialogEnrollmentDelegate {
189 public:
190 // |owning_window| is the window that will own the dialog.
191 DialogEnrollmentDelegate(gfx::NativeWindow owning_window,
192 const std::string& network_name,
193 Profile* profile);
194 ~DialogEnrollmentDelegate();
196 // EnrollmentDelegate overrides
197 bool Enroll(const std::vector<std::string>& uri_list,
198 const base::Closure& connect);
200 private:
201 gfx::NativeWindow owning_window_;
202 std::string network_name_;
203 Profile* profile_;
205 DISALLOW_COPY_AND_ASSIGN(DialogEnrollmentDelegate);
208 DialogEnrollmentDelegate::DialogEnrollmentDelegate(
209 gfx::NativeWindow owning_window,
210 const std::string& network_name,
211 Profile* profile) : owning_window_(owning_window),
212 network_name_(network_name),
213 profile_(profile) {}
215 DialogEnrollmentDelegate::~DialogEnrollmentDelegate() {}
217 bool DialogEnrollmentDelegate::Enroll(const std::vector<std::string>& uri_list,
218 const base::Closure& post_action) {
219 if (uri_list.empty()) {
220 NET_LOG_EVENT("No enrollment URIs", network_name_);
221 return false;
224 // Keep the closure for later activation if we notice that
225 // a certificate has been added.
227 // TODO(gspencer): Do something smart with the closure. At the moment it is
228 // being ignored because we don't know when the enrollment tab is closed.
229 // http://crosbug.com/30422
230 for (std::vector<std::string>::const_iterator iter = uri_list.begin();
231 iter != uri_list.end(); ++iter) {
232 GURL uri(*iter);
233 if (uri.IsStandard() || uri.scheme() == extensions::kExtensionScheme) {
234 // If this is a "standard" scheme, like http, ftp, etc., then open that in
235 // the enrollment dialog.
236 NET_LOG_EVENT("Showing enrollment dialog", network_name_);
237 EnrollmentDialogView::ShowDialog(owning_window_,
238 network_name_,
239 profile_,
240 uri, post_action);
241 return true;
243 NET_LOG_DEBUG("Nonstandard URI: " + uri.spec(), network_name_);
246 // No appropriate scheme was found.
247 NET_LOG_ERROR("No usable enrollment URI", network_name_);
248 return false;
251 void EnrollmentComplete(const std::string& service_path) {
252 NET_LOG_USER("Enrollment Complete", service_path);
255 } // namespace
257 ////////////////////////////////////////////////////////////////////////////////
258 // Factory function.
260 namespace enrollment {
262 bool CreateDialog(const std::string& service_path,
263 gfx::NativeWindow owning_window) {
264 const NetworkState* network = NetworkHandler::Get()->network_state_handler()->
265 GetNetworkState(service_path);
266 if (!network) {
267 NET_LOG_ERROR("Enrolling Unknown network", service_path);
268 return false;
270 // We skip certificate patterns for device policy ONC so that an unmanaged
271 // user can't get to the place where a cert is presented for them
272 // involuntarily.
273 if (network->ui_data().onc_source() == onc::ONC_SOURCE_DEVICE_POLICY)
274 return false;
276 const CertificatePattern& certificate_pattern =
277 network->ui_data().certificate_pattern();
278 if (certificate_pattern.Empty()) {
279 NET_LOG_EVENT("No certificate pattern found", service_path);
280 return false;
283 NET_LOG_USER("Enrolling", service_path);
285 Browser* browser = chrome::FindBrowserWithWindow(owning_window);
286 Profile* profile = browser ? browser->profile() :
287 ProfileManager::GetPrimaryUserProfile();
288 DialogEnrollmentDelegate* enrollment =
289 new DialogEnrollmentDelegate(owning_window, network->name(), profile);
290 return enrollment->Enroll(certificate_pattern.enrollment_uri_list(),
291 base::Bind(&EnrollmentComplete, service_path));
294 } // namespace enrollment
296 } // namespace chromeos