Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/enrollment_dialog_view.h" | 5 #include "chrome/browser/chromeos/enrollment_dialog_view.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/chromeos/cros/network_library.h" | 8 #include "chrome/browser/chromeos/cros/network_library.h" |
| 9 #include "chrome/browser/extensions/extension_host.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/themes/theme_service.h" | 11 #include "chrome/browser/themes/theme_service.h" |
| 12 #include "chrome/browser/ui/views/extensions/extension_dialog.h" | |
| 13 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" | |
| 14 #include "content/public/common/page_transition_types.h" | |
| 11 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/views/border.h" | 18 #include "ui/views/border.h" |
| 15 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/controls/webview/webview.h" | 20 #include "ui/views/controls/webview/webview.h" |
| 17 #include "ui/views/layout/grid_layout.h" | 21 #include "ui/views/layout/grid_layout.h" |
| 18 #include "ui/views/layout/layout_constants.h" | 22 #include "ui/views/layout/layout_constants.h" |
| 19 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 20 | 24 |
| 21 using views::Border; | 25 using views::Border; |
| 22 using views::ColumnSet; | 26 using views::ColumnSet; |
| 23 using views::GridLayout; | 27 using views::GridLayout; |
| 24 using views::Label; | 28 using views::Label; |
| 25 using views::LayoutManager; | 29 using views::LayoutManager; |
| 26 using views::View; | 30 using views::View; |
| 27 | 31 |
| 28 namespace chromeos { | 32 namespace chromeos { |
| 29 | 33 |
| 30 namespace { | 34 namespace { |
| 31 | 35 |
| 32 // Default width/height of the dialog. | 36 // Default width/height of the dialog. |
| 33 const int kDefaultWidth = 640; | 37 const int kDefaultWidth = 400; |
| 34 const int kDefaultHeight = 480; | 38 const int kDefaultHeight = 200; |
| 35 | 39 |
| 36 // Border around the WebView | 40 //////////////////////////////////////////////////////////////////////////////// |
| 37 const int kWebViewBorderSize = 1; | 41 // Dialog for certificate enrollment. This displays the content from the |
| 42 // certificate enrollment URI. | |
| 43 class EnrollmentDialogView : public views::DialogDelegateView { | |
| 44 public: | |
| 45 virtual ~EnrollmentDialogView(); | |
| 38 | 46 |
| 39 // TODO(gspencer): Move this into ui/views/layout, perhaps just adding insets | 47 static void ShowDialog(gfx::NativeWindow owning_window, |
| 40 // to FillLayout. | 48 Profile* profile, |
| 41 class BorderLayout : public LayoutManager { | 49 const GURL& target_uri, |
| 42 public: | 50 const base::Closure& connect); |
| 43 explicit BorderLayout(const gfx::Insets& insets) : insets_(insets) {} | 51 void Close(); |
| 44 virtual ~BorderLayout() {} | |
| 45 | 52 |
| 46 // Overridden from LayoutManager: | 53 // views::DialogDelegateView overrides |
| 47 virtual void Layout(View* host) OVERRIDE { | 54 virtual int GetDialogButtons() const OVERRIDE; |
| 48 if (!host->has_children()) | 55 bool Accept(); |
|
stevenjb
2012/05/02 00:31:23
move Accept() and Close() below virtual overrides.
Greg Spencer (Chromium)
2012/05/02 21:08:59
Actually they are overrides too. Fixed their sign
| |
| 49 return; | |
| 50 | 56 |
| 51 View* frame_view = host->child_at(0); | 57 // views::WidgetDelegate overrides |
| 52 frame_view->SetBounds(insets_.left(), | 58 virtual ui::ModalType GetModalType() const OVERRIDE; |
| 53 insets_.top(), | 59 virtual string16 GetWindowTitle() const OVERRIDE; |
| 54 host->width() - insets_.right() - insets_.left(), | |
| 55 host->height() - insets_.bottom() - insets_.top()); | |
| 56 } | |
| 57 | 60 |
| 58 virtual gfx::Size GetPreferredSize(View* host) OVERRIDE { | 61 // views::View overrides |
| 59 DCHECK_EQ(1, host->child_count()); | 62 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 60 gfx::Size child_size = host->child_at(0)->GetPreferredSize(); | 63 |
| 61 child_size.Enlarge(insets_.left() + insets_.right(), | 64 // views::Widget overrides |
| 62 insets_.top() + insets_.bottom()); | 65 virtual views::View* GetContentsView() OVERRIDE; |
| 63 return child_size; | |
| 64 } | |
| 65 | 66 |
| 66 private: | 67 private: |
| 67 gfx::Insets insets_; | 68 EnrollmentDialogView(Profile* profile, |
| 68 DISALLOW_COPY_AND_ASSIGN(BorderLayout); | 69 const GURL& target_uri, |
| 70 const base::Closure& connect); | |
| 71 void InitDialog(); | |
| 72 | |
| 73 Profile* profile_; | |
| 74 GURL target_uri_; | |
| 75 base::Closure connect_; | |
| 76 bool added_cert_; | |
| 69 }; | 77 }; |
| 70 | 78 |
| 71 // Handler for certificate enrollment. This displays the content from the | |
| 72 // certificate enrollment URI and listens for a certificate to be added. If a | |
| 73 // certificate is added, then it invokes the closure to allow the network to | |
| 74 // continue to connect. | |
| 75 class DialogEnrollmentDelegate : public EnrollmentDelegate { | |
| 76 public: | |
| 77 // |owning_window| is the window that will own the dialog. | |
| 78 explicit DialogEnrollmentDelegate(gfx::NativeWindow owning_window); | |
| 79 virtual ~DialogEnrollmentDelegate(); | |
| 80 | |
| 81 // EnrollmentDelegate overrides | |
| 82 virtual void Enroll(const std::vector<std::string>& uri_list, | |
| 83 const base::Closure& connect) OVERRIDE; | |
| 84 | |
| 85 private: | |
| 86 gfx::NativeWindow owning_window_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(DialogEnrollmentDelegate); | |
| 89 }; | |
| 90 | |
| 91 DialogEnrollmentDelegate::DialogEnrollmentDelegate( | |
| 92 gfx::NativeWindow owning_window) | |
| 93 : owning_window_(owning_window) {} | |
| 94 | |
| 95 DialogEnrollmentDelegate::~DialogEnrollmentDelegate() {} | |
| 96 | |
| 97 void DialogEnrollmentDelegate::Enroll(const std::vector<std::string>& uri_list, | |
| 98 const base::Closure& connect) { | |
| 99 // Keep the closure for later activation if we notice that | |
| 100 // a certificate has been added. | |
| 101 for (std::vector<std::string>::const_iterator iter = uri_list.begin(); | |
| 102 iter != uri_list.end(); ++iter) { | |
| 103 GURL uri(*iter); | |
| 104 if (uri.IsStandard() || uri.scheme() == "chrome-extension") { | |
| 105 // If this is a "standard" scheme, like http, ftp, etc., then open that in | |
| 106 // the enrollment dialog. If this is a chrome extension, then just open | |
| 107 // that extension in a tab and let it provide any UI that it needs to | |
| 108 // complete. | |
| 109 EnrollmentDialogView::ShowDialog(owning_window_, uri, connect); | |
| 110 return; | |
| 111 } | |
| 112 } | |
| 113 // If we didn't find a scheme we could handle, then don't continue connecting. | |
| 114 // TODO(gspencer): provide a path to display this failure to the user. | |
| 115 VLOG(1) << "Couldn't find usable scheme in enrollment URI(s)"; | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 //////////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////////// |
| 121 // EnrollmentDialogView implementation. | 80 // EnrollmentDialogView implementation. |
| 122 | 81 |
| 123 EnrollmentDialogView::EnrollmentDialogView(const GURL& target_uri, | 82 EnrollmentDialogView::EnrollmentDialogView(Profile* profile, |
| 83 const GURL& target_uri, | |
| 124 const base::Closure& connect) | 84 const base::Closure& connect) |
| 125 : target_uri_(target_uri), | 85 : profile_(profile), |
| 86 target_uri_(target_uri), | |
| 126 connect_(connect), | 87 connect_(connect), |
| 127 added_cert_(false) { | 88 added_cert_(false) { |
| 128 net::CertDatabase::AddObserver(this); | |
| 129 } | 89 } |
| 130 | 90 |
| 131 EnrollmentDialogView::~EnrollmentDialogView() { | 91 EnrollmentDialogView::~EnrollmentDialogView() { |
| 132 net::CertDatabase::RemoveObserver(this); | |
| 133 } | |
| 134 | |
| 135 // static | |
| 136 EnrollmentDelegate* EnrollmentDialogView::CreateEnrollmentDelegate( | |
| 137 gfx::NativeWindow owning_window) { | |
| 138 return new DialogEnrollmentDelegate(owning_window); | |
| 139 } | 92 } |
| 140 | 93 |
| 141 // static | 94 // static |
| 142 void EnrollmentDialogView::ShowDialog(gfx::NativeWindow owning_window, | 95 void EnrollmentDialogView::ShowDialog(gfx::NativeWindow owning_window, |
| 96 Profile* profile, | |
| 143 const GURL& target_uri, | 97 const GURL& target_uri, |
| 144 const base::Closure& connect) { | 98 const base::Closure& connect) { |
| 145 EnrollmentDialogView* dialog_view = | 99 EnrollmentDialogView* dialog_view = |
| 146 new EnrollmentDialogView(target_uri, connect); | 100 new EnrollmentDialogView(profile, target_uri, connect); |
| 147 views::Widget::CreateWindowWithParent(dialog_view, owning_window); | 101 views::Widget::CreateWindowWithParent(dialog_view, owning_window); |
| 148 dialog_view->InitDialog(); | 102 dialog_view->InitDialog(); |
| 149 views::Widget* widget = dialog_view->GetWidget(); | 103 views::Widget* widget = dialog_view->GetWidget(); |
| 150 DCHECK(widget); | 104 DCHECK(widget); |
| 151 widget->Show(); | 105 widget->Show(); |
| 152 } | 106 } |
| 153 | 107 |
| 154 void EnrollmentDialogView::Close() { | 108 void EnrollmentDialogView::Close() { |
| 155 GetWidget()->Close(); | 109 GetWidget()->Close(); |
| 156 } | 110 } |
| 157 | 111 |
| 158 int EnrollmentDialogView::GetDialogButtons() const { | 112 int EnrollmentDialogView::GetDialogButtons() const { |
| 159 return ui::DIALOG_BUTTON_CANCEL; | 113 return ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; |
| 160 } | 114 } |
| 161 | 115 |
| 162 void EnrollmentDialogView::OnClose() { | 116 bool EnrollmentDialogView::Accept() { |
| 163 if (added_cert_) | 117 // Navigate to the target URI in a browser tab. |
| 164 connect_.Run(); | 118 Browser* browser = BrowserList::FindTabbedBrowser(profile_, false); |
| 119 if (!browser) { | |
| 120 // Couldn't find a tabbed browser: create one. | |
| 121 browser = Browser::Create(profile_); | |
| 122 } | |
| 123 DCHECK(browser); | |
| 124 browser->AddSelectedTabWithURL( | |
| 125 target_uri_, | |
| 126 content::PAGE_TRANSITION_LINK); | |
| 127 return true; | |
| 165 } | 128 } |
| 166 | 129 |
| 167 ui::ModalType EnrollmentDialogView::GetModalType() const { | 130 ui::ModalType EnrollmentDialogView::GetModalType() const { |
| 168 return ui::MODAL_TYPE_SYSTEM; | 131 return ui::MODAL_TYPE_SYSTEM; |
| 169 } | 132 } |
| 170 | 133 |
| 171 string16 EnrollmentDialogView::GetWindowTitle() const { | 134 string16 EnrollmentDialogView::GetWindowTitle() const { |
| 172 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE); | 135 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE); |
| 173 } | 136 } |
| 174 | 137 |
| 175 gfx::Size EnrollmentDialogView::GetPreferredSize() { | 138 gfx::Size EnrollmentDialogView::GetPreferredSize() { |
| 176 return gfx::Size(kDefaultWidth, kDefaultHeight); | 139 return gfx::Size(kDefaultWidth, kDefaultHeight); |
| 177 } | 140 } |
| 178 | 141 |
| 179 views::View* EnrollmentDialogView::GetContentsView() { | 142 views::View* EnrollmentDialogView::GetContentsView() { |
| 180 return this; | 143 return this; |
| 181 } | 144 } |
| 182 | 145 |
| 183 void EnrollmentDialogView::OnUserCertAdded( | |
| 184 const net::X509Certificate* cert) { | |
| 185 added_cert_ = true; | |
| 186 Close(); | |
| 187 } | |
| 188 | |
| 189 void EnrollmentDialogView::InitDialog() { | 146 void EnrollmentDialogView::InitDialog() { |
| 190 added_cert_ = false; | 147 added_cert_ = false; |
| 191 // Create the views and layout manager and set them up. | 148 // Create the views and layout manager and set them up. |
| 192 Label* label = new Label( | 149 Label* label = new Label( |
| 193 l10n_util::GetStringUTF16( | 150 l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TAB_ENROLL)); |
| 194 IDS_NETWORK_ENROLLMENT_HANDLER_EMBEDDED_ENROLL)); | |
| 195 label->SetFont( | 151 label->SetFont( |
| 196 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); | 152 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); |
| 197 label->SetHorizontalAlignment(Label::ALIGN_LEFT); | 153 label->SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 198 label->SetMultiLine(true); | 154 label->SetMultiLine(true); |
| 199 label->SetAllowCharacterBreak(true); | 155 label->SetAllowCharacterBreak(true); |
| 200 | 156 |
| 201 // In order to get a border that shows around the WebView, we need to embed it | |
| 202 // into another view because it hosts a native window that fills the view. | |
| 203 View* web_view_border_view = new View(); | |
| 204 web_view_border_view->SetLayoutManager( | |
| 205 new BorderLayout(gfx::Insets(kWebViewBorderSize, | |
| 206 kWebViewBorderSize, | |
| 207 kWebViewBorderSize, | |
| 208 kWebViewBorderSize))); | |
| 209 SkColor frame_color = ThemeService::GetDefaultColor( | |
| 210 ThemeService::COLOR_FRAME); | |
| 211 Border* border = views::Border::CreateSolidBorder(kWebViewBorderSize, | |
| 212 frame_color); | |
| 213 web_view_border_view->set_border(border); | |
| 214 views::WebView* web_view = | |
| 215 new views::WebView(ProfileManager::GetLastUsedProfile()); | |
| 216 web_view_border_view->AddChildView(web_view); | |
| 217 web_view->SetVisible(true); | |
| 218 | |
| 219 GridLayout* grid_layout = GridLayout::CreatePanel(this); | 157 GridLayout* grid_layout = GridLayout::CreatePanel(this); |
| 220 SetLayoutManager(grid_layout); | 158 SetLayoutManager(grid_layout); |
| 221 | 159 |
| 222 views::ColumnSet* columns = grid_layout->AddColumnSet(0); | 160 views::ColumnSet* columns = grid_layout->AddColumnSet(0); |
| 223 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. | 161 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. |
| 224 views::GridLayout::FILL, // Vertical resize. | 162 views::GridLayout::FILL, // Vertical resize. |
| 225 1, // Resize weight. | 163 1, // Resize weight. |
| 226 views::GridLayout::USE_PREF, // Size type. | 164 views::GridLayout::USE_PREF, // Size type. |
| 227 0, // Ignored for USE_PREF. | 165 0, // Ignored for USE_PREF. |
| 228 0); // Minimum size. | 166 0); // Minimum size. |
| 229 // Add a column set for aligning the text when it has no icons (such as the | |
| 230 // help center link). | |
| 231 columns = grid_layout->AddColumnSet(1); | 167 columns = grid_layout->AddColumnSet(1); |
| 232 columns->AddPaddingColumn( | 168 columns->AddPaddingColumn( |
| 233 0, views::kUnrelatedControlHorizontalSpacing); | 169 0, views::kUnrelatedControlHorizontalSpacing); |
| 234 columns->AddColumn(views::GridLayout::LEADING, // Horizontal leading. | 170 columns->AddColumn(views::GridLayout::LEADING, // Horizontal leading. |
| 235 views::GridLayout::FILL, // Vertical resize. | 171 views::GridLayout::FILL, // Vertical resize. |
| 236 1, // Resize weight. | 172 1, // Resize weight. |
| 237 views::GridLayout::USE_PREF, // Size type. | 173 views::GridLayout::USE_PREF, // Size type. |
| 238 0, // Ignored for USE_PREF. | 174 0, // Ignored for USE_PREF. |
| 239 0); // Minimum size. | 175 0); // Minimum size. |
| 240 | 176 |
| 241 grid_layout->StartRow(0, 0); | 177 grid_layout->StartRow(0, 0); |
| 242 grid_layout->AddView(label); | 178 grid_layout->AddView(label); |
| 243 grid_layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | 179 grid_layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 244 grid_layout->StartRow(100.0f, 0); | |
| 245 grid_layout->AddView(web_view_border_view); | |
| 246 grid_layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
| 247 grid_layout->Layout(this); | 180 grid_layout->Layout(this); |
| 248 web_view->LoadInitialURL(target_uri_); | 181 } |
| 182 | |
| 183 //////////////////////////////////////////////////////////////////////////////// | |
| 184 // Handler for certificate enrollment. | |
| 185 | |
| 186 class DialogEnrollmentDelegate : public EnrollmentDelegate { | |
| 187 public: | |
| 188 // |owning_window| is the window that will own the dialog. | |
| 189 explicit DialogEnrollmentDelegate(gfx::NativeWindow owning_window, | |
| 190 Profile* profile); | |
| 191 virtual ~DialogEnrollmentDelegate(); | |
| 192 | |
| 193 // EnrollmentDelegate overrides | |
| 194 virtual void Enroll(const std::vector<std::string>& uri_list, | |
| 195 const base::Closure& connect) OVERRIDE; | |
| 196 | |
| 197 private: | |
| 198 gfx::NativeWindow owning_window_; | |
| 199 Profile* profile_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(DialogEnrollmentDelegate); | |
| 202 }; | |
| 203 | |
| 204 DialogEnrollmentDelegate::DialogEnrollmentDelegate( | |
| 205 gfx::NativeWindow owning_window, | |
| 206 Profile* profile) : owning_window_(owning_window), profile_(profile) {} | |
| 207 | |
| 208 DialogEnrollmentDelegate::~DialogEnrollmentDelegate() {} | |
| 209 | |
| 210 void DialogEnrollmentDelegate::Enroll(const std::vector<std::string>& uri_list, | |
| 211 const base::Closure& connect) { | |
| 212 // Keep the closure for later activation if we notice that | |
| 213 // a certificate has been added. | |
| 214 | |
| 215 // TODO(gspencer): Do something smart with the closure. At the moment it is | |
| 216 // being ignored because we don't know when the enrollment tab is closed. | |
|
stevenjb
2012/05/02 00:31:23
This sounds like something we should have an issue
Greg Spencer (Chromium)
2012/05/02 21:08:59
Done. http://crosbug.com/30422
| |
| 217 for (std::vector<std::string>::const_iterator iter = uri_list.begin(); | |
| 218 iter != uri_list.end(); ++iter) { | |
| 219 GURL uri(*iter); | |
| 220 if (uri.IsStandard() || uri.scheme() == "chrome-extension") { | |
|
stevenjb
2012/05/02 00:31:23
Is there a constant for "chrome-extension"?
Greg Spencer (Chromium)
2012/05/02 21:08:59
Not that I could find: mostly we just use raw stri
stevenjb
2012/05/02 21:12:44
Yeah, we should change that, but if that's the cur
Mihai Parparita -not on Chrome
2012/05/02 21:30:14
There is chrome::kExtensionScheme (from chrome/com
Greg Spencer (Chromium)
2012/05/02 21:44:26
Huh, I guess I just didn't see it in the search re
| |
| 221 // If this is a "standard" scheme, like http, ftp, etc., then open that in | |
| 222 // the enrollment dialog. | |
| 223 EnrollmentDialogView::ShowDialog(owning_window_, profile_, uri, connect); | |
| 224 return; | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 // If we didn't find a scheme we could handle, then don't continue connecting. | |
| 229 // TODO(gspencer): provide a path to display this failure to the user. (but | |
| 230 // for the most part they won't know what it means, since it's probably coming | |
| 231 // from a policy-pushed ONC file). | |
| 232 VLOG(1) << "Couldn't find usable scheme in enrollment URI(s)"; | |
| 233 } | |
| 234 | |
| 235 } // namespace | |
| 236 | |
| 237 //////////////////////////////////////////////////////////////////////////////// | |
| 238 // Factory function. | |
| 239 | |
| 240 EnrollmentDelegate* CreateEnrollmentDelegate(gfx::NativeWindow owning_window, | |
| 241 Profile* profile) { | |
| 242 return new DialogEnrollmentDelegate(owning_window, profile); | |
| 249 } | 243 } |
| 250 | 244 |
| 251 } // namespace chromeos | 245 } // namespace chromeos |
| OLD | NEW |