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/options/network_config_view.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
13 #include "chrome/browser/chromeos/login/user.h"
14 #include "chrome/browser/chromeos/options/network_property_ui_data.h"
15 #include "chrome/browser/chromeos/options/vpn_config_view.h"
16 #include "chrome/browser/chromeos/options/wifi_config_view.h"
17 #include "chrome/browser/chromeos/options/wimax_config_view.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/host_desktop.h"
23 #include "chromeos/network/network_state.h"
24 #include "chromeos/network/network_state_handler.h"
25 #include "grit/chromium_strings.h"
26 #include "grit/generated_resources.h"
27 #include "grit/locale_settings.h"
28 #include "grit/theme_resources.h"
29 #include "ui/aura/root_window.h"
30 #include "ui/base/accessibility/accessible_view_state.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/gfx/image/image.h"
34 #include "ui/gfx/rect.h"
35 #include "ui/views/controls/button/label_button.h"
36 #include "ui/views/controls/image_view.h"
37 #include "ui/views/layout/layout_constants.h"
38 #include "ui/views/widget/widget.h"
44 gfx::NativeWindow
GetParentForUnhostedDialog() {
45 if (chromeos::LoginDisplayHostImpl::default_host()) {
46 return chromeos::LoginDisplayHostImpl::default_host()->GetNativeWindow();
48 Browser
* browser
= chrome::FindTabbedBrowser(
49 ProfileManager::GetPrimaryUserProfile(),
51 chrome::HOST_DESKTOP_TYPE_ASH
);
53 return browser
->window()->GetNativeWindow();
58 // Avoid global static initializer.
59 chromeos::NetworkConfigView
** GetActiveDialogPointer() {
60 static chromeos::NetworkConfigView
* active_dialog
= NULL
;
61 return &active_dialog
;
64 chromeos::NetworkConfigView
* GetActiveDialog() {
65 return *(GetActiveDialogPointer());
68 void SetActiveDialog(chromeos::NetworkConfigView
* dialog
) {
69 *(GetActiveDialogPointer()) = dialog
;
77 const int ChildNetworkConfigView::kInputFieldMinWidth
= 270;
79 NetworkConfigView::NetworkConfigView()
80 : child_config_view_(NULL
),
82 advanced_button_(NULL
) {
83 DCHECK(GetActiveDialog() == NULL
);
84 SetActiveDialog(this);
87 bool NetworkConfigView::InitWithNetworkState(const NetworkState
* network
) {
89 std::string service_path
= network
->path();
90 if (network
->type() == shill::kTypeWifi
)
91 child_config_view_
= new WifiConfigView(this, service_path
, false);
92 else if (network
->type() == shill::kTypeWimax
)
93 child_config_view_
= new WimaxConfigView(this, service_path
);
94 else if (network
->type() == shill::kTypeVPN
)
95 child_config_view_
= new VPNConfigView(this, service_path
);
96 return child_config_view_
!= NULL
;
99 bool NetworkConfigView::InitWithType(const std::string
& type
) {
100 if (type
== shill::kTypeWifi
) {
101 child_config_view_
= new WifiConfigView(this,
102 "" /* service_path */,
103 false /* show_8021x */);
104 advanced_button_
= new views::LabelButton(this, l10n_util::GetStringUTF16(
105 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_ADVANCED_BUTTON
));
106 advanced_button_
->SetStyle(views::Button::STYLE_BUTTON
);
107 } else if (type
== shill::kTypeVPN
) {
108 child_config_view_
= new VPNConfigView(this,
109 "" /* service_path */);
111 return child_config_view_
!= NULL
;
114 NetworkConfigView::~NetworkConfigView() {
115 DCHECK(GetActiveDialog() == this);
116 SetActiveDialog(NULL
);
120 void NetworkConfigView::Show(const std::string
& service_path
,
121 gfx::NativeWindow parent
) {
122 if (GetActiveDialog() != NULL
)
124 NetworkConfigView
* view
= new NetworkConfigView();
125 const NetworkState
* network
= NetworkHandler::Get()->network_state_handler()->
126 GetNetworkState(service_path
);
128 LOG(ERROR
) << "NetworkConfigView::Show called with invalid service_path";
131 if (!view
->InitWithNetworkState(network
)) {
132 LOG(ERROR
) << "NetworkConfigView::Show called with invalid network type: "
137 view
->ShowDialog(parent
);
141 void NetworkConfigView::ShowForType(const std::string
& type
,
142 gfx::NativeWindow parent
) {
143 if (GetActiveDialog() != NULL
)
145 NetworkConfigView
* view
= new NetworkConfigView();
146 if (!view
->InitWithType(type
)) {
147 LOG(ERROR
) << "NetworkConfigView::ShowForType called with invalid type: "
152 view
->ShowDialog(parent
);
155 gfx::NativeWindow
NetworkConfigView::GetNativeWindow() const {
156 return GetWidget()->GetNativeWindow();
159 base::string16
NetworkConfigView::GetDialogButtonLabel(
160 ui::DialogButton button
) const {
161 if (button
== ui::DIALOG_BUTTON_OK
)
162 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONNECT
);
163 return views::DialogDelegateView::GetDialogButtonLabel(button
);
166 bool NetworkConfigView::IsDialogButtonEnabled(ui::DialogButton button
) const {
167 // Disable connect button if cannot login.
168 if (button
== ui::DIALOG_BUTTON_OK
)
169 return child_config_view_
->CanLogin();
173 bool NetworkConfigView::Cancel() {
175 delegate_
->OnDialogCancelled();
176 child_config_view_
->Cancel();
180 bool NetworkConfigView::Accept() {
181 // Do not attempt login if it is guaranteed to fail, keep the dialog open.
182 if (!child_config_view_
->CanLogin())
184 bool result
= child_config_view_
->Login();
185 if (result
&& delegate_
)
186 delegate_
->OnDialogAccepted();
190 views::View
* NetworkConfigView::CreateExtraView() {
191 return advanced_button_
;
194 views::View
* NetworkConfigView::GetInitiallyFocusedView() {
195 return child_config_view_
->GetInitiallyFocusedView();
198 base::string16
NetworkConfigView::GetWindowTitle() const {
199 DCHECK(!child_config_view_
->GetTitle().empty());
200 return child_config_view_
->GetTitle();
203 ui::ModalType
NetworkConfigView::GetModalType() const {
204 return ui::MODAL_TYPE_SYSTEM
;
207 void NetworkConfigView::GetAccessibleState(ui::AccessibleViewState
* state
) {
209 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS
);
210 state
->role
= ui::AccessibilityTypes::ROLE_DIALOG
;
213 void NetworkConfigView::ButtonPressed(views::Button
* sender
,
214 const ui::Event
& event
) {
215 if (advanced_button_
&& sender
== advanced_button_
) {
216 advanced_button_
->SetVisible(false);
221 void NetworkConfigView::ShowAdvancedView() {
222 // Clear out the old widgets and build new ones.
223 RemoveChildView(child_config_view_
);
224 delete child_config_view_
;
225 // For now, there is only an advanced view for Wi-Fi 802.1X.
226 child_config_view_
= new WifiConfigView(this,
227 "" /* service_path */,
228 true /* show_8021x */);
229 AddChildView(child_config_view_
);
230 // Resize the window to be able to hold the new widgets.
231 gfx::Size size
= views::Widget::GetLocalizedContentsSize(
232 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_WIDTH_CHARS
,
233 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_MINIMUM_HEIGHT_LINES
);
234 // Get the new bounds with desired size at the same center point.
235 gfx::Rect bounds
= GetWidget()->GetWindowBoundsInScreen();
236 int horiz_padding
= bounds
.width() - size
.width();
237 int vert_padding
= bounds
.height() - size
.height();
238 bounds
.Inset(horiz_padding
/ 2, vert_padding
/ 2,
239 horiz_padding
/ 2, vert_padding
/ 2);
240 GetWidget()->SetBoundsConstrained(bounds
);
242 child_config_view_
->InitFocus();
245 void NetworkConfigView::Layout() {
246 child_config_view_
->SetBounds(0, 0, width(), height());
249 gfx::Size
NetworkConfigView::GetPreferredSize() {
250 gfx::Size
result(views::Widget::GetLocalizedContentsSize(
251 IDS_JOIN_WIFI_NETWORK_DIALOG_WIDTH_CHARS
,
252 IDS_JOIN_WIFI_NETWORK_DIALOG_MINIMUM_HEIGHT_LINES
));
253 gfx::Size size
= child_config_view_
->GetPreferredSize();
254 result
.set_height(size
.height());
255 if (size
.width() > result
.width())
256 result
.set_width(size
.width());
260 void NetworkConfigView::ViewHierarchyChanged(
261 const ViewHierarchyChangedDetails
& details
) {
262 // Can't init before we're inserted into a Container, because we require
263 // a HWND to parent native child controls to.
264 if (details
.is_add
&& details
.child
== this) {
265 AddChildView(child_config_view_
);
269 void NetworkConfigView::ShowDialog(gfx::NativeWindow parent
) {
271 parent
= GetParentForUnhostedDialog();
272 // Failed connections may result in a pop-up with no natural parent window,
273 // so provide a fallback context on the primary display. This is necessary
274 // becase one of parent or context must be non NULL.
275 gfx::NativeWindow context
=
276 parent
? NULL
: ash::Shell::GetPrimaryRootWindow();
277 Widget
* window
= DialogDelegate::CreateDialogWidget(this, context
, parent
);
278 window
->SetAlwaysOnTop(true);
282 // ChildNetworkConfigView
284 ChildNetworkConfigView::ChildNetworkConfigView(
285 NetworkConfigView
* parent
,
286 const std::string
& service_path
)
288 service_path_(service_path
) {
291 ChildNetworkConfigView::~ChildNetworkConfigView() {
294 // ControlledSettingIndicatorView
296 ControlledSettingIndicatorView::ControlledSettingIndicatorView()
302 ControlledSettingIndicatorView::ControlledSettingIndicatorView(
303 const NetworkPropertyUIData
& ui_data
)
310 ControlledSettingIndicatorView::~ControlledSettingIndicatorView() {}
312 void ControlledSettingIndicatorView::Update(
313 const NetworkPropertyUIData
& ui_data
) {
314 if (managed_
== ui_data
.IsManaged())
317 managed_
= ui_data
.IsManaged();
318 PreferredSizeChanged();
321 gfx::Size
ControlledSettingIndicatorView::GetPreferredSize() {
322 return (managed_
&& visible()) ? image_view_
->GetPreferredSize()
326 void ControlledSettingIndicatorView::Layout() {
327 image_view_
->SetBounds(0, 0, width(), height());
330 void ControlledSettingIndicatorView::Init() {
331 image_
= ResourceBundle::GetSharedInstance().GetImageNamed(
332 IDR_CONTROLLED_SETTING_MANDATORY
).ToImageSkia();
333 image_view_
= new views::ImageView();
334 // Disable |image_view_| so mouse events propagate to the parent.
335 image_view_
->SetEnabled(false);
336 image_view_
->SetImage(image_
);
337 image_view_
->SetTooltipText(
338 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_POLICY
));
339 AddChildView(image_view_
);
342 } // namespace chromeos