| 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 "chrome/common/url_constants.h" |
| 15 #include "content/public/common/page_transition_types.h" |
| 11 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
| 15 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/controls/webview/webview.h" | 21 #include "ui/views/controls/webview/webview.h" |
| 17 #include "ui/views/layout/grid_layout.h" | 22 #include "ui/views/layout/grid_layout.h" |
| 18 #include "ui/views/layout/layout_constants.h" | 23 #include "ui/views/layout/layout_constants.h" |
| 19 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 20 | 25 |
| 21 using views::Border; | 26 using views::Border; |
| 22 using views::ColumnSet; | 27 using views::ColumnSet; |
| 23 using views::GridLayout; | 28 using views::GridLayout; |
| 24 using views::Label; | 29 using views::Label; |
| 25 using views::LayoutManager; | 30 using views::LayoutManager; |
| 26 using views::View; | 31 using views::View; |
| 27 | 32 |
| 28 namespace chromeos { | 33 namespace chromeos { |
| 29 | 34 |
| 30 namespace { | 35 namespace { |
| 31 | 36 |
| 32 // Default width/height of the dialog. | 37 // Default width/height of the dialog. |
| 33 const int kDefaultWidth = 640; | 38 const int kDefaultWidth = 400; |
| 34 const int kDefaultHeight = 480; | 39 const int kDefaultHeight = 200; |
| 35 | 40 |
| 36 // Border around the WebView | 41 //////////////////////////////////////////////////////////////////////////////// |
| 37 const int kWebViewBorderSize = 1; | 42 // Dialog for certificate enrollment. This displays the content from the |
| 43 // certificate enrollment URI. |
| 44 class EnrollmentDialogView : public views::DialogDelegateView { |
| 45 public: |
| 46 virtual ~EnrollmentDialogView(); |
| 38 | 47 |
| 39 // TODO(gspencer): Move this into ui/views/layout, perhaps just adding insets | 48 static void ShowDialog(gfx::NativeWindow owning_window, |
| 40 // to FillLayout. | 49 Profile* profile, |
| 41 class BorderLayout : public LayoutManager { | 50 const GURL& target_uri, |
| 42 public: | 51 const base::Closure& connect); |
| 43 explicit BorderLayout(const gfx::Insets& insets) : insets_(insets) {} | |
| 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 virtual bool Accept() OVERRIDE; |
| 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 int EnrollmentDialogView::GetDialogButtons() const { |
| 155 GetWidget()->Close(); | 109 return ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; |
| 156 } | 110 } |
| 157 | 111 |
| 158 int EnrollmentDialogView::GetDialogButtons() const { | 112 bool EnrollmentDialogView::Accept() { |
| 159 return ui::DIALOG_BUTTON_CANCEL; | 113 // Navigate to the target URI in a browser tab. |
| 160 } | 114 Browser* browser = BrowserList::FindTabbedBrowser(profile_, false); |
| 161 | 115 if (!browser) { |
| 162 void EnrollmentDialogView::OnClose() { | 116 // Couldn't find a tabbed browser: create one. |
| 163 if (added_cert_) | 117 browser = Browser::Create(profile_); |
| 164 connect_.Run(); | 118 } |
| 119 DCHECK(browser); |
| 120 browser->AddSelectedTabWithURL( |
| 121 target_uri_, |
| 122 content::PAGE_TRANSITION_LINK); |
| 123 return true; |
| 165 } | 124 } |
| 166 | 125 |
| 167 ui::ModalType EnrollmentDialogView::GetModalType() const { | 126 ui::ModalType EnrollmentDialogView::GetModalType() const { |
| 168 return ui::MODAL_TYPE_SYSTEM; | 127 return ui::MODAL_TYPE_SYSTEM; |
| 169 } | 128 } |
| 170 | 129 |
| 171 string16 EnrollmentDialogView::GetWindowTitle() const { | 130 string16 EnrollmentDialogView::GetWindowTitle() const { |
| 172 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE); | 131 return l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TITLE); |
| 173 } | 132 } |
| 174 | 133 |
| 175 gfx::Size EnrollmentDialogView::GetPreferredSize() { | 134 gfx::Size EnrollmentDialogView::GetPreferredSize() { |
| 176 return gfx::Size(kDefaultWidth, kDefaultHeight); | 135 return gfx::Size(kDefaultWidth, kDefaultHeight); |
| 177 } | 136 } |
| 178 | 137 |
| 179 views::View* EnrollmentDialogView::GetContentsView() { | 138 views::View* EnrollmentDialogView::GetContentsView() { |
| 180 return this; | 139 return this; |
| 181 } | 140 } |
| 182 | 141 |
| 183 void EnrollmentDialogView::OnUserCertAdded( | |
| 184 const net::X509Certificate* cert) { | |
| 185 added_cert_ = true; | |
| 186 Close(); | |
| 187 } | |
| 188 | |
| 189 void EnrollmentDialogView::InitDialog() { | 142 void EnrollmentDialogView::InitDialog() { |
| 190 added_cert_ = false; | 143 added_cert_ = false; |
| 191 // Create the views and layout manager and set them up. | 144 // Create the views and layout manager and set them up. |
| 192 Label* label = new Label( | 145 Label* label = new Label( |
| 193 l10n_util::GetStringUTF16( | 146 l10n_util::GetStringUTF16(IDS_NETWORK_ENROLLMENT_HANDLER_TAB_ENROLL)); |
| 194 IDS_NETWORK_ENROLLMENT_HANDLER_EMBEDDED_ENROLL)); | |
| 195 label->SetFont( | 147 label->SetFont( |
| 196 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); | 148 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); |
| 197 label->SetHorizontalAlignment(Label::ALIGN_LEFT); | 149 label->SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 198 label->SetMultiLine(true); | 150 label->SetMultiLine(true); |
| 199 label->SetAllowCharacterBreak(true); | 151 label->SetAllowCharacterBreak(true); |
| 200 | 152 |
| 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); | 153 GridLayout* grid_layout = GridLayout::CreatePanel(this); |
| 220 SetLayoutManager(grid_layout); | 154 SetLayoutManager(grid_layout); |
| 221 | 155 |
| 222 views::ColumnSet* columns = grid_layout->AddColumnSet(0); | 156 views::ColumnSet* columns = grid_layout->AddColumnSet(0); |
| 223 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. | 157 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. |
| 224 views::GridLayout::FILL, // Vertical resize. | 158 views::GridLayout::FILL, // Vertical resize. |
| 225 1, // Resize weight. | 159 1, // Resize weight. |
| 226 views::GridLayout::USE_PREF, // Size type. | 160 views::GridLayout::USE_PREF, // Size type. |
| 227 0, // Ignored for USE_PREF. | 161 0, // Ignored for USE_PREF. |
| 228 0); // Minimum size. | 162 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); | 163 columns = grid_layout->AddColumnSet(1); |
| 232 columns->AddPaddingColumn( | 164 columns->AddPaddingColumn( |
| 233 0, views::kUnrelatedControlHorizontalSpacing); | 165 0, views::kUnrelatedControlHorizontalSpacing); |
| 234 columns->AddColumn(views::GridLayout::LEADING, // Horizontal leading. | 166 columns->AddColumn(views::GridLayout::LEADING, // Horizontal leading. |
| 235 views::GridLayout::FILL, // Vertical resize. | 167 views::GridLayout::FILL, // Vertical resize. |
| 236 1, // Resize weight. | 168 1, // Resize weight. |
| 237 views::GridLayout::USE_PREF, // Size type. | 169 views::GridLayout::USE_PREF, // Size type. |
| 238 0, // Ignored for USE_PREF. | 170 0, // Ignored for USE_PREF. |
| 239 0); // Minimum size. | 171 0); // Minimum size. |
| 240 | 172 |
| 241 grid_layout->StartRow(0, 0); | 173 grid_layout->StartRow(0, 0); |
| 242 grid_layout->AddView(label); | 174 grid_layout->AddView(label); |
| 243 grid_layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | 175 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); | 176 grid_layout->Layout(this); |
| 248 web_view->LoadInitialURL(target_uri_); | 177 } |
| 178 |
| 179 //////////////////////////////////////////////////////////////////////////////// |
| 180 // Handler for certificate enrollment. |
| 181 |
| 182 class DialogEnrollmentDelegate : public EnrollmentDelegate { |
| 183 public: |
| 184 // |owning_window| is the window that will own the dialog. |
| 185 explicit DialogEnrollmentDelegate(gfx::NativeWindow owning_window, |
| 186 Profile* profile); |
| 187 virtual ~DialogEnrollmentDelegate(); |
| 188 |
| 189 // EnrollmentDelegate overrides |
| 190 virtual void Enroll(const std::vector<std::string>& uri_list, |
| 191 const base::Closure& connect) OVERRIDE; |
| 192 |
| 193 private: |
| 194 gfx::NativeWindow owning_window_; |
| 195 Profile* profile_; |
| 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(DialogEnrollmentDelegate); |
| 198 }; |
| 199 |
| 200 DialogEnrollmentDelegate::DialogEnrollmentDelegate( |
| 201 gfx::NativeWindow owning_window, |
| 202 Profile* profile) : owning_window_(owning_window), profile_(profile) {} |
| 203 |
| 204 DialogEnrollmentDelegate::~DialogEnrollmentDelegate() {} |
| 205 |
| 206 void DialogEnrollmentDelegate::Enroll(const std::vector<std::string>& uri_list, |
| 207 const base::Closure& connect) { |
| 208 // Keep the closure for later activation if we notice that |
| 209 // a certificate has been added. |
| 210 |
| 211 // TODO(gspencer): Do something smart with the closure. At the moment it is |
| 212 // being ignored because we don't know when the enrollment tab is closed. |
| 213 // http://crosbug.com/30422 |
| 214 for (std::vector<std::string>::const_iterator iter = uri_list.begin(); |
| 215 iter != uri_list.end(); ++iter) { |
| 216 GURL uri(*iter); |
| 217 if (uri.IsStandard() || uri.scheme() == chrome::kExtensionScheme) { |
| 218 // If this is a "standard" scheme, like http, ftp, etc., then open that in |
| 219 // the enrollment dialog. |
| 220 EnrollmentDialogView::ShowDialog(owning_window_, profile_, uri, connect); |
| 221 return; |
| 222 } |
| 223 } |
| 224 |
| 225 // If we didn't find a scheme we could handle, then don't continue connecting. |
| 226 // TODO(gspencer): provide a path to display this failure to the user. (but |
| 227 // for the most part they won't know what it means, since it's probably coming |
| 228 // from a policy-pushed ONC file). |
| 229 VLOG(1) << "Couldn't find usable scheme in enrollment URI(s)"; |
| 230 } |
| 231 |
| 232 } // namespace |
| 233 |
| 234 //////////////////////////////////////////////////////////////////////////////// |
| 235 // Factory function. |
| 236 |
| 237 EnrollmentDelegate* CreateEnrollmentDelegate(gfx::NativeWindow owning_window, |
| 238 Profile* profile) { |
| 239 return new DialogEnrollmentDelegate(owning_window, profile); |
| 249 } | 240 } |
| 250 | 241 |
| 251 } // namespace chromeos | 242 } // namespace chromeos |
| OLD | NEW |