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/login/login_state.h"
24 #include "chromeos/network/network_state.h"
25 #include "chromeos/network/network_state_handler.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "grit/locale_settings.h"
29 #include "grit/theme_resources.h"
30 #include "ui/accessibility/ax_view_state.h"
31 #include "ui/aura/window_event_dispatcher.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/resource/resource_bundle.h"
34 #include "ui/gfx/image/image.h"
35 #include "ui/gfx/rect.h"
36 #include "ui/views/controls/button/label_button.h"
37 #include "ui/views/controls/image_view.h"
38 #include "ui/views/layout/layout_constants.h"
39 #include "ui/views/widget/widget.h"
45 gfx::NativeWindow
GetParentForUnhostedDialog() {
46 if (chromeos::LoginDisplayHostImpl::default_host()) {
47 return chromeos::LoginDisplayHostImpl::default_host()->GetNativeWindow();
49 Browser
* browser
= chrome::FindTabbedBrowser(
50 ProfileManager::GetPrimaryUserProfile(),
52 chrome::HOST_DESKTOP_TYPE_ASH
);
54 return browser
->window()->GetNativeWindow();
59 // Avoid global static initializer.
60 chromeos::NetworkConfigView
** GetActiveDialogPointer() {
61 static chromeos::NetworkConfigView
* active_dialog
= NULL
;
62 return &active_dialog
;
65 chromeos::NetworkConfigView
* GetActiveDialog() {
66 return *(GetActiveDialogPointer());
69 void SetActiveDialog(chromeos::NetworkConfigView
* dialog
) {
70 *(GetActiveDialogPointer()) = dialog
;
78 const int ChildNetworkConfigView::kInputFieldMinWidth
= 270;
80 NetworkConfigView::NetworkConfigView()
81 : child_config_view_(NULL
),
83 advanced_button_(NULL
) {
84 DCHECK(GetActiveDialog() == NULL
);
85 SetActiveDialog(this);
88 bool NetworkConfigView::InitWithNetworkState(const NetworkState
* network
) {
90 std::string service_path
= network
->path();
91 if (network
->type() == shill::kTypeWifi
||
92 network
->type() == shill::kTypeEthernet
) {
93 child_config_view_
= new WifiConfigView(this, service_path
, false);
94 } else if (network
->type() == shill::kTypeWimax
) {
95 child_config_view_
= new WimaxConfigView(this, service_path
);
96 } else if (network
->type() == shill::kTypeVPN
) {
97 child_config_view_
= new VPNConfigView(this, service_path
);
99 return child_config_view_
!= NULL
;
102 bool NetworkConfigView::InitWithType(const std::string
& type
) {
103 if (type
== shill::kTypeWifi
) {
104 child_config_view_
= new WifiConfigView(this,
105 "" /* service_path */,
106 false /* show_8021x */);
107 advanced_button_
= new views::LabelButton(this, l10n_util::GetStringUTF16(
108 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_ADVANCED_BUTTON
));
109 advanced_button_
->SetStyle(views::Button::STYLE_BUTTON
);
110 } else if (type
== shill::kTypeVPN
) {
111 child_config_view_
= new VPNConfigView(this,
112 "" /* service_path */);
114 return child_config_view_
!= NULL
;
117 NetworkConfigView::~NetworkConfigView() {
118 DCHECK(GetActiveDialog() == this);
119 SetActiveDialog(NULL
);
123 void NetworkConfigView::Show(const std::string
& service_path
,
124 gfx::NativeWindow parent
) {
125 if (GetActiveDialog() != NULL
)
127 NetworkConfigView
* view
= new NetworkConfigView();
128 const NetworkState
* network
= NetworkHandler::Get()->network_state_handler()->
129 GetNetworkState(service_path
);
131 LOG(ERROR
) << "NetworkConfigView::Show called with invalid service_path";
134 if (!view
->InitWithNetworkState(network
)) {
135 LOG(ERROR
) << "NetworkConfigView::Show called with invalid network type: "
140 view
->ShowDialog(parent
);
144 void NetworkConfigView::ShowForType(const std::string
& type
,
145 gfx::NativeWindow parent
) {
146 if (GetActiveDialog() != NULL
)
148 NetworkConfigView
* view
= new NetworkConfigView();
149 if (!view
->InitWithType(type
)) {
150 LOG(ERROR
) << "NetworkConfigView::ShowForType called with invalid type: "
155 view
->ShowDialog(parent
);
158 gfx::NativeWindow
NetworkConfigView::GetNativeWindow() const {
159 return GetWidget()->GetNativeWindow();
162 base::string16
NetworkConfigView::GetDialogButtonLabel(
163 ui::DialogButton button
) const {
164 if (button
== ui::DIALOG_BUTTON_OK
) {
165 if (child_config_view_
->IsConfigureDialog())
166 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONFIGURE
);
167 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONNECT
);
169 return views::DialogDelegateView::GetDialogButtonLabel(button
);
172 bool NetworkConfigView::IsDialogButtonEnabled(ui::DialogButton button
) const {
173 // Disable connect button if cannot login.
174 if (button
== ui::DIALOG_BUTTON_OK
)
175 return child_config_view_
->CanLogin();
179 bool NetworkConfigView::Cancel() {
181 delegate_
->OnDialogCancelled();
182 child_config_view_
->Cancel();
186 bool NetworkConfigView::Accept() {
187 // Do not attempt login if it is guaranteed to fail, keep the dialog open.
188 if (!child_config_view_
->CanLogin())
190 bool result
= child_config_view_
->Login();
191 if (result
&& delegate_
)
192 delegate_
->OnDialogAccepted();
196 views::View
* NetworkConfigView::CreateExtraView() {
197 return advanced_button_
;
200 views::View
* NetworkConfigView::GetInitiallyFocusedView() {
201 return child_config_view_
->GetInitiallyFocusedView();
204 base::string16
NetworkConfigView::GetWindowTitle() const {
205 DCHECK(!child_config_view_
->GetTitle().empty());
206 return child_config_view_
->GetTitle();
209 ui::ModalType
NetworkConfigView::GetModalType() const {
210 return ui::MODAL_TYPE_SYSTEM
;
213 void NetworkConfigView::GetAccessibleState(ui::AXViewState
* state
) {
215 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS
);
216 state
->role
= ui::AX_ROLE_DIALOG
;
219 void NetworkConfigView::ButtonPressed(views::Button
* sender
,
220 const ui::Event
& event
) {
221 if (advanced_button_
&& sender
== advanced_button_
) {
222 advanced_button_
->SetVisible(false);
227 void NetworkConfigView::ShowAdvancedView() {
228 // Clear out the old widgets and build new ones.
229 RemoveChildView(child_config_view_
);
230 delete child_config_view_
;
231 // For now, there is only an advanced view for Wi-Fi 802.1X.
232 child_config_view_
= new WifiConfigView(this,
233 "" /* service_path */,
234 true /* show_8021x */);
235 AddChildView(child_config_view_
);
236 // Resize the window to be able to hold the new widgets.
237 gfx::Size size
= views::Widget::GetLocalizedContentsSize(
238 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_WIDTH_CHARS
,
239 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_MINIMUM_HEIGHT_LINES
);
240 // Get the new bounds with desired size at the same center point.
241 gfx::Rect bounds
= GetWidget()->GetWindowBoundsInScreen();
242 int horiz_padding
= bounds
.width() - size
.width();
243 int vert_padding
= bounds
.height() - size
.height();
244 bounds
.Inset(horiz_padding
/ 2, vert_padding
/ 2,
245 horiz_padding
/ 2, vert_padding
/ 2);
246 GetWidget()->SetBoundsConstrained(bounds
);
248 child_config_view_
->InitFocus();
251 void NetworkConfigView::Layout() {
252 child_config_view_
->SetBounds(0, 0, width(), height());
255 gfx::Size
NetworkConfigView::GetPreferredSize() {
256 gfx::Size
result(views::Widget::GetLocalizedContentsSize(
257 IDS_JOIN_WIFI_NETWORK_DIALOG_WIDTH_CHARS
,
258 IDS_JOIN_WIFI_NETWORK_DIALOG_MINIMUM_HEIGHT_LINES
));
259 gfx::Size size
= child_config_view_
->GetPreferredSize();
260 result
.set_height(size
.height());
261 if (size
.width() > result
.width())
262 result
.set_width(size
.width());
266 void NetworkConfigView::ViewHierarchyChanged(
267 const ViewHierarchyChangedDetails
& details
) {
268 // Can't init before we're inserted into a Container, because we require
269 // a HWND to parent native child controls to.
270 if (details
.is_add
&& details
.child
== this) {
271 AddChildView(child_config_view_
);
275 void NetworkConfigView::ShowDialog(gfx::NativeWindow parent
) {
277 parent
= GetParentForUnhostedDialog();
278 // Failed connections may result in a pop-up with no natural parent window,
279 // so provide a fallback context on the primary display. This is necessary
280 // becase one of parent or context must be non NULL.
281 gfx::NativeWindow context
=
282 parent
? NULL
: ash::Shell::GetPrimaryRootWindow();
283 Widget
* window
= DialogDelegate::CreateDialogWidget(this, context
, parent
);
284 window
->SetAlwaysOnTop(true);
288 // ChildNetworkConfigView
290 ChildNetworkConfigView::ChildNetworkConfigView(
291 NetworkConfigView
* parent
,
292 const std::string
& service_path
)
294 service_path_(service_path
) {
297 ChildNetworkConfigView::~ChildNetworkConfigView() {
300 bool ChildNetworkConfigView::IsConfigureDialog() {
305 void ChildNetworkConfigView::GetShareStateForLoginState(bool* default_value
,
307 *default_value
= !LoginState::Get()->UserHasNetworkProfile();
308 // Allow only authenticated user to change the share state.
309 *modifiable
= LoginState::Get()->IsUserAuthenticated();
312 // ControlledSettingIndicatorView
314 ControlledSettingIndicatorView::ControlledSettingIndicatorView()
320 ControlledSettingIndicatorView::ControlledSettingIndicatorView(
321 const NetworkPropertyUIData
& ui_data
)
328 ControlledSettingIndicatorView::~ControlledSettingIndicatorView() {}
330 void ControlledSettingIndicatorView::Update(
331 const NetworkPropertyUIData
& ui_data
) {
332 if (managed_
== ui_data
.IsManaged())
335 managed_
= ui_data
.IsManaged();
336 PreferredSizeChanged();
339 gfx::Size
ControlledSettingIndicatorView::GetPreferredSize() {
340 return (managed_
&& visible()) ? image_view_
->GetPreferredSize()
344 void ControlledSettingIndicatorView::Layout() {
345 image_view_
->SetBounds(0, 0, width(), height());
348 void ControlledSettingIndicatorView::Init() {
349 image_
= ResourceBundle::GetSharedInstance().GetImageNamed(
350 IDR_CONTROLLED_SETTING_MANDATORY
).ToImageSkia();
351 image_view_
= new views::ImageView();
352 // Disable |image_view_| so mouse events propagate to the parent.
353 image_view_
->SetEnabled(false);
354 image_view_
->SetImage(image_
);
355 image_view_
->SetTooltipText(
356 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_POLICY
));
357 AddChildView(image_view_
);
360 } // namespace chromeos