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/ui/webui/options/chromeos/internet_options_handler.h"
13 #include "base/basictypes.h"
14 #include "base/bind.h"
15 #include "base/bind_helpers.h"
16 #include "base/values.h"
17 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
18 #include "chrome/browser/chromeos/mobile_config.h"
19 #include "chrome/browser/chromeos/options/network_config_view.h"
20 #include "chrome/browser/chromeos/profiles/profile_helper.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "chrome/browser/chromeos/sim_dialog_delegate.h"
23 #include "chrome/browser/chromeos/ui/choose_mobile_network_dialog.h"
24 #include "chrome/browser/chromeos/ui/mobile_config_ui.h"
25 #include "chrome/browser/chromeos/ui_proxy_config_service.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h"
28 #include "chrome/browser/ui/webui/options/chromeos/internet_options_handler_strings.h"
29 #include "chromeos/login/login_state.h"
30 #include "chromeos/network/network_state.h"
31 #include "chromeos/network/network_state_handler.h"
32 #include "components/onc/onc_constants.h"
33 #include "components/user_manager/user_manager.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/browser/web_ui.h"
36 #include "extensions/browser/api/vpn_provider/vpn_service.h"
37 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h"
38 #include "third_party/cros_system_api/dbus/service_constants.h"
39 #include "ui/base/webui/web_ui_util.h"
40 #include "ui/chromeos/network/network_connect.h"
47 // Keys for the initial "localized" dictionary values.
48 const char kLoggedInAsOwnerKey
[] = "loggedInAsOwner";
50 // JS methods to show additional UI.
51 const char kShowMorePlanInfoMessage
[] = "showMorePlanInfo";
52 const char kSimOperationMessage
[] = "simOperation";
54 // TODO(stevenjb): Deprecate these and integrate with settings Web UI.
55 const char kAddVPNConnectionMessage
[] = "addVPNConnection";
56 const char kAddNonVPNConnectionMessage
[] = "addNonVPNConnection";
57 const char kConfigureNetworkMessage
[] = "configureNetwork";
59 // These are strings used to communicate with JavaScript.
60 const char kTagSimOpChangePin
[] = "changePin";
61 const char kTagSimOpConfigure
[] = "configure";
62 const char kTagSimOpSetLocked
[] = "setLocked";
63 const char kTagSimOpSetUnlocked
[] = "setUnlocked";
64 const char kTagSimOpUnlock
[] = "unlock";
66 const NetworkState
* GetNetworkState(const std::string
& service_path
) {
67 return NetworkHandler::Get()->network_state_handler()->
68 GetNetworkState(service_path
);
71 std::string
ServicePathFromGuid(const std::string
& guid
) {
72 const NetworkState
* network
=
73 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
75 return network
? network
->path() : "";
78 Profile
* GetProfileForPrimaryUser() {
79 return chromeos::ProfileHelper::Get()->GetProfileByUser(
80 user_manager::UserManager::Get()->GetPrimaryUser());
85 InternetOptionsHandler::InternetOptionsHandler()
86 : weak_factory_(this) {
89 InternetOptionsHandler::~InternetOptionsHandler() {
92 void InternetOptionsHandler::GetLocalizedValues(
93 base::DictionaryValue
* localized_strings
) {
94 DCHECK(localized_strings
);
95 internet_options_strings::RegisterLocalizedStrings(localized_strings
);
97 // TODO(stevenjb): Find a better way to populate initial data.
99 chromeos::CrosSettings::Get()->GetString(chromeos::kDeviceOwner
, &owner
);
100 bool logged_in_as_owner
= LoginState::Get()->GetLoggedInUserType() ==
101 LoginState::LOGGED_IN_USER_OWNER
;
102 localized_strings
->SetBoolean(kLoggedInAsOwnerKey
, logged_in_as_owner
);
105 void InternetOptionsHandler::RegisterMessages() {
106 web_ui()->RegisterMessageCallback(kAddVPNConnectionMessage
,
107 base::Bind(&InternetOptionsHandler::AddVPNConnection
,
108 base::Unretained(this)));
109 web_ui()->RegisterMessageCallback(kAddNonVPNConnectionMessage
,
110 base::Bind(&InternetOptionsHandler::AddNonVPNConnection
,
111 base::Unretained(this)));
112 web_ui()->RegisterMessageCallback(kConfigureNetworkMessage
,
113 base::Bind(&InternetOptionsHandler::ConfigureNetwork
,
114 base::Unretained(this)));
115 web_ui()->RegisterMessageCallback(kShowMorePlanInfoMessage
,
116 base::Bind(&InternetOptionsHandler::ShowMorePlanInfoCallback
,
117 base::Unretained(this)));
118 web_ui()->RegisterMessageCallback(kSimOperationMessage
,
119 base::Bind(&InternetOptionsHandler::SimOperationCallback
,
120 base::Unretained(this)));
123 void InternetOptionsHandler::ShowMorePlanInfoCallback(
124 const base::ListValue
* args
) {
128 if (args
->GetSize() != 1 || !args
->GetString(0, &guid
)) {
132 std::string service_path
= ServicePathFromGuid(guid
);
133 if (!service_path
.empty())
134 ui::NetworkConnect::Get()->ShowMobileSetup(service_path
);
137 void InternetOptionsHandler::SimOperationCallback(const base::ListValue
* args
) {
138 std::string operation
;
139 if (args
->GetSize() != 1 || !args
->GetString(0, &operation
)) {
143 if (operation
== kTagSimOpConfigure
) {
144 mobile_config_ui::DisplayConfigDialog();
147 // 1. Bring up SIM unlock dialog, pass new RequirePin setting in URL.
148 // 2. Dialog will ask for current PIN in any case.
149 // 3. If card is locked it will first call PIN unlock operation
150 // 4. Then it will call Set RequirePin, passing the same PIN.
151 // 5. The dialog may change device properties, in which case
152 // DevicePropertiesUpdated() will get called which will update the UI.
153 SimDialogDelegate::SimDialogMode mode
;
154 if (operation
== kTagSimOpSetLocked
) {
155 mode
= SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON
;
156 } else if (operation
== kTagSimOpSetUnlocked
) {
157 mode
= SimDialogDelegate::SIM_DIALOG_SET_LOCK_OFF
;
158 } else if (operation
== kTagSimOpUnlock
) {
159 mode
= SimDialogDelegate::SIM_DIALOG_UNLOCK
;
160 } else if (operation
== kTagSimOpChangePin
) {
161 mode
= SimDialogDelegate::SIM_DIALOG_CHANGE_PIN
;
166 SimDialogDelegate::ShowDialog(GetNativeWindow(), mode
);
169 ////////////////////////////////////////////////////////////////////////////////
171 gfx::NativeWindow
InternetOptionsHandler::GetNativeWindow() const {
172 return web_ui()->GetWebContents()->GetTopLevelNativeWindow();
175 const PrefService
* InternetOptionsHandler::GetPrefs() const {
176 return Profile::FromWebUI(web_ui())->GetPrefs();
180 void InternetOptionsHandler::AddVPNConnection(const base::ListValue
* args
) {
182 // Show the "add network" dialog for the built-in OpenVPN/L2TP provider.
183 NetworkConfigView::ShowForType(shill::kTypeVPN
, GetNativeWindow());
187 std::string extension_id
;
188 if (args
->GetSize() != 1 || !args
->GetString(0, &extension_id
)) {
193 // Request that the third-party VPN provider identified by |provider_id|
194 // show its "add network" dialog.
195 chromeos::VpnServiceFactory::GetForBrowserContext(
196 GetProfileForPrimaryUser())->SendShowAddDialogToExtension(extension_id
);
199 void InternetOptionsHandler::AddNonVPNConnection(const base::ListValue
* args
) {
200 std::string onc_type
;
201 if (args
->GetSize() != 1 || !args
->GetString(0, &onc_type
)) {
205 if (onc_type
== ::onc::network_type::kWiFi
) {
206 NetworkConfigView::ShowForType(shill::kTypeWifi
, GetNativeWindow());
207 } else if (onc_type
== ::onc::network_type::kCellular
) {
208 ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow());
210 LOG(ERROR
) << "Unsupported type for AddConnection";
214 void InternetOptionsHandler::ConfigureNetwork(const base::ListValue
* args
) {
216 if (args
->GetSize() < 1 || !args
->GetString(0, &guid
)) {
220 bool force_show
= false;
221 if (args
->GetSize() >= 2)
222 args
->GetBoolean(1, &force_show
);
224 const std::string service_path
= ServicePathFromGuid(guid
);
225 if (service_path
.empty())
228 const NetworkState
* network
= GetNetworkState(service_path
);
232 if (network
->type() == shill::kTypeVPN
&&
233 network
->vpn_provider_type() == shill::kProviderThirdPartyVpn
) {
234 // Request that the third-party VPN provider used by the |network| show a
235 // configuration dialog for it.
236 VpnServiceFactory::GetForBrowserContext(GetProfileForPrimaryUser())
237 ->SendShowConfigureDialogToExtension(
238 network
->third_party_vpn_provider_extension_id(), network
->name());
242 // If a network is not connectable, show the enrollment dialog if available.
243 if (!force_show
&& !network
->connectable() &&
244 enrollment::CreateDialog(service_path
, GetNativeWindow())) {
248 NetworkConfigView::Show(service_path
, GetNativeWindow());
251 } // namespace options
252 } // namespace chromeos