Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: chrome/browser/chromeos/options/network_connect.cc

Issue 21046008: Convert all connect code to use NetworkHandler instead of NetworkLibrary (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restore check VPN PassphraseRequred Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/options/network_connect.h" 5 #include "chrome/browser/chromeos/options/network_connect.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h" 8 #include "ash/shell_delegate.h"
9 #include "ash/system/chromeos/network/network_connect.h" 9 #include "ash/system/chromeos/network/network_connect.h"
10 #include "ash/system/chromeos/network/network_observer.h" 10 #include "ash/system/chromeos/network/network_observer.h"
11 #include "ash/system/tray/system_tray_notifier.h" 11 #include "ash/system/tray/system_tray_notifier.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/chromeos/cros/network_library.h"
16 #include "chrome/browser/chromeos/enrollment_dialog_view.h" 15 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
16 #include "chrome/browser/chromeos/login/user_manager.h"
17 #include "chrome/browser/chromeos/options/network_config_view.h" 17 #include "chrome/browser/chromeos/options/network_config_view.h"
18 #include "chrome/browser/profiles/profile_manager.h" 18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser_finder.h" 19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/chrome_pages.h" 20 #include "chrome/browser/ui/chrome_pages.h"
21 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h" 21 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h"
22 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
23 #include "chromeos/chromeos_switches.h" 23 #include "chromeos/chromeos_switches.h"
24 #include "chromeos/network/certificate_pattern.h"
25 #include "chromeos/network/certificate_pattern_matcher.h"
26 #include "chromeos/network/managed_network_configuration_handler.h"
27 #include "chromeos/network/network_event_log.h"
28 #include "chromeos/network/network_handler.h"
29 #include "chromeos/network/network_state.h"
30 #include "chromeos/network/network_state_handler.h"
24 #include "content/public/browser/user_metrics.h" 31 #include "content/public/browser/user_metrics.h"
25 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
26 #include "net/base/escape.h" 33 #include "net/base/escape.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h" 34 #include "third_party/cros_system_api/dbus/service_constants.h"
28 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
29 36
30 namespace chromeos { 37 namespace chromeos {
31 namespace network_connect {
32 38
33 namespace { 39 namespace {
34 40
35 void DoConnect(Network* network, gfx::NativeWindow parent_window) { 41 void EnrollmentComplete(const std::string& service_path) {
36 NetworkLibrary* cros = NetworkLibrary::Get(); 42 NET_LOG_USER("Enrollment Complete", service_path);
37 if (network->type() == TYPE_VPN) {
38 VirtualNetwork* vpn = static_cast<VirtualNetwork*>(network);
39 if (vpn->NeedMoreInfoToConnect()) {
40 // Show the connection UI if info for a field is missing.
41 NetworkConfigView::Show(vpn, parent_window);
42 } else {
43 cros->ConnectToVirtualNetwork(vpn);
44 // Connection failures are responsible for updating the UI, including
45 // reopening dialogs.
46 }
47 } else if (network->type() == TYPE_WIFI) {
48 WifiNetwork* wifi = static_cast<WifiNetwork*>(network);
49 if (wifi->IsPassphraseRequired()) {
50 // Show the connection UI if we require a passphrase.
51 NetworkConfigView::Show(wifi, parent_window);
52 } else {
53 cros->ConnectToWifiNetwork(wifi);
54 // Connection failures are responsible for updating the UI, including
55 // reopening dialogs.
56 }
57 } else if (network->type() == TYPE_WIMAX) {
58 WimaxNetwork* wimax = static_cast<WimaxNetwork*>(network);
59 if (wimax->passphrase_required()) {
60 // Show the connection UI if we require a passphrase.
61 NetworkConfigView::Show(wimax, parent_window);
62 } else {
63 cros->ConnectToWimaxNetwork(wimax);
64 // Connection failures are responsible for updating the UI, including
65 // reopening dialogs.
66 }
67 } else if (network->type() == TYPE_CELLULAR) {
68 CellularNetwork* cellular = static_cast<CellularNetwork*>(network);
69 if (cellular->activation_state() != ACTIVATION_STATE_ACTIVATED ||
70 cellular->out_of_credits()) {
71 ActivateCellular(cellular->service_path());
72 } else {
73 cros->ConnectToCellularNetwork(cellular);
74 }
75 }
76 } 43 }
77 44
78 } // namespace
79
80 void ActivateCellular(const std::string& service_path) {
81 chromeos::NetworkLibrary* cros =
82 chromeos::NetworkLibrary::Get();
83 if (!cros->CellularDeviceUsesDirectActivation()) {
84 // For non direct activation, show the mobile setup dialog which can be
85 // used to activate the network.
86 ShowMobileSetup(service_path);
87 return;
88 }
89 chromeos::CellularNetwork* cellular =
90 cros->FindCellularNetworkByPath(service_path);
91 if (!cellular)
92 return;
93 if (cellular->activation_state() != chromeos::ACTIVATION_STATE_ACTIVATED)
94 cellular->StartActivation();
95 return;
96 } 45 }
97 46
47 namespace network_connect {
48
98 void ShowMobileSetup(const std::string& service_path) { 49 void ShowMobileSetup(const std::string& service_path) {
99 NetworkLibrary* cros = NetworkLibrary::Get(); 50 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
100 const CellularNetwork* cellular = 51 const NetworkState* cellular =
101 cros->FindCellularNetworkByPath(service_path); 52 handler->FirstNetworkByType(flimflam::kTypeCellular);
102 if (cellular && !cellular->activated() && 53 if (cellular &&
103 cellular->activate_over_non_cellular_network() && 54 cellular->activation_state() != flimflam::kActivationStateActivated &&
104 (!cros->connected_network() || !cros->connected_network()->online())) { 55 cellular->activate_over_non_cellular_networks() &&
105 NetworkTechnology technology = cellular->network_technology(); 56 !handler->DefaultNetwork()) {
57 std::string technology = cellular->network_technology();
106 ash::NetworkObserver::NetworkType network_type = 58 ash::NetworkObserver::NetworkType network_type =
107 (technology == chromeos::NETWORK_TECHNOLOGY_LTE || 59 (technology == flimflam::kNetworkTechnologyLte ||
108 technology == chromeos::NETWORK_TECHNOLOGY_LTE_ADVANCED) 60 technology == flimflam::kNetworkTechnologyLteAdvanced)
109 ? ash::NetworkObserver::NETWORK_CELLULAR_LTE 61 ? ash::NetworkObserver::NETWORK_CELLULAR_LTE
110 : ash::NetworkObserver::NETWORK_CELLULAR; 62 : ash::NetworkObserver::NETWORK_CELLULAR;
111 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( 63 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage(
112 NULL, 64 NULL,
113 ash::NetworkObserver::ERROR_CONNECT_FAILED, 65 ash::NetworkObserver::ERROR_CONNECT_FAILED,
114 network_type, 66 network_type,
115 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE), 67 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
116 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION, 68 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
117 UTF8ToUTF16((cellular->name()))), 69 UTF8ToUTF16((cellular->name()))),
118 std::vector<string16>()); 70 std::vector<string16>());
119 return; 71 return;
120 } 72 }
121 MobileSetupDialog::Show(service_path); 73 MobileSetupDialog::Show(service_path);
122 } 74 }
123 75
124 ConnectResult ConnectToNetwork(const std::string& service_path, 76 void ShowNetworkSettings(const std::string& service_path) {
125 gfx::NativeWindow parent_window) { 77 std::string page = chrome::kInternetOptionsSubPage;
126 if (CommandLine::ForCurrentProcess()->HasSwitch( 78 const NetworkState* network = service_path.empty() ? NULL :
127 chromeos::switches::kUseNewNetworkConnectionHandler)) { 79 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
128 ash::network_connect::ConnectToNetwork(service_path); 80 service_path);
129 return CONNECT_STARTED; 81 if (network) {
82 std::string name(network->name());
83 if (name.empty() && network->type() == flimflam::kTypeEthernet)
84 name = l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
85 page += base::StringPrintf(
86 "?servicePath=%s&networkType=%s&networkName=%s",
87 net::EscapeUrlEncodedData(service_path, true).c_str(),
88 net::EscapeUrlEncodedData(network->type(), true).c_str(),
89 net::EscapeUrlEncodedData(name, false).c_str());
130 } 90 }
131
132 NetworkLibrary* cros = NetworkLibrary::Get();
133 Network* network = cros->FindNetworkByPath(service_path);
134 if (!network)
135 return NETWORK_NOT_FOUND;
136
137 if (network->connecting_or_connected())
138 return CONNECT_NOT_STARTED;
139
140 if (network->type() == TYPE_ETHERNET)
141 return CONNECT_NOT_STARTED; // Normally this shouldn't happen
142
143 if (network->type() == TYPE_WIFI || network->type() == TYPE_VPN) {
144 network->SetEnrollmentDelegate(chromeos::CreateEnrollmentDelegate(
145 parent_window, network->name(), ProfileManager::GetLastUsedProfile()));
146 network->AttemptConnection(base::Bind(&DoConnect, network, parent_window));
147 return CONNECT_STARTED;
148 }
149
150 if (network->type() == TYPE_WIMAX) {
151 WimaxNetwork* wimax = static_cast<WimaxNetwork*>(network);
152 wimax->AttemptConnection(base::Bind(&DoConnect, wimax, parent_window));
153 return CONNECT_STARTED;
154 }
155
156 if (network->type() == TYPE_CELLULAR) {
157 CellularNetwork* cellular = static_cast<CellularNetwork*>(network);
158 if (cellular->NeedsActivation() || cellular->out_of_credits()) {
159 ActivateCellular(service_path);
160 return CONNECT_STARTED;
161 }
162 if (cellular->activation_state() == ACTIVATION_STATE_ACTIVATING)
163 return CONNECT_NOT_STARTED;
164 cros->ConnectToCellularNetwork(cellular);
165 return CONNECT_STARTED;
166 }
167
168 NOTREACHED();
169 return CONNECT_NOT_STARTED;
170 }
171
172 void HandleUnconfiguredNetwork(const std::string& service_path,
173 gfx::NativeWindow parent_window) {
174 NetworkLibrary* cros = NetworkLibrary::Get();
175 Network* network = cros->FindNetworkByPath(service_path);
176 if (!network) {
177 LOG(WARNING) << "Unknown network: " << service_path;
178 return;
179 }
180
181 if (network->type() == TYPE_WIFI || network->type() == TYPE_VPN) {
182 network->SetEnrollmentDelegate(chromeos::CreateEnrollmentDelegate(
183 parent_window, network->name(), ProfileManager::GetLastUsedProfile()));
184 // This will connect to the network only if the network just needs to have
185 // its certificate configured. Otherwise it will show an enrollment dialog
186 // if available, or call NetworkConfigView::Show().
187 network->AttemptConnection(
188 base::Bind(&NetworkConfigView::Show, network, parent_window));
189 return;
190 }
191
192 if (network->type() == TYPE_WIMAX) {
193 NetworkConfigView::Show(network, parent_window);
194 return;
195 }
196
197 if (network->type() == TYPE_CELLULAR) {
198 CellularNetwork* cellular = static_cast<CellularNetwork*>(network);
199 if (cellular->NeedsActivation()) {
200 ActivateCellular(service_path);
201 return;
202 } else if (cellular->out_of_credits()) {
203 ShowMobileSetup(service_path);
204 return;
205 }
206 }
207
208 // No special configure or setup for |service_path|, show the settings UI.
209 std::string page = chrome::kInternetOptionsSubPage;
210 std::string name = network->name();
211 if (name.empty() && network->type() == TYPE_ETHERNET)
212 name = l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
213 page += base::StringPrintf(
214 "?servicePath=%s&networkType=%d&networkName=%s",
215 net::EscapeUrlEncodedData(service_path, true).c_str(),
216 network->type(),
217 net::EscapeUrlEncodedData(name, false).c_str());
218 content::RecordAction( 91 content::RecordAction(
219 content::UserMetricsAction("OpenInternetOptionsDialog")); 92 content::UserMetricsAction("OpenInternetOptionsDialog"));
220 Browser* browser = chrome::FindOrCreateTabbedBrowser( 93 Browser* browser = chrome::FindOrCreateTabbedBrowser(
221 ProfileManager::GetDefaultProfileOrOffTheRecord(), 94 ProfileManager::GetDefaultProfileOrOffTheRecord(),
222 chrome::HOST_DESKTOP_TYPE_ASH); 95 chrome::HOST_DESKTOP_TYPE_ASH);
223 chrome::ShowSettingsSubPage(browser, page); 96 chrome::ShowSettingsSubPage(browser, page);
224 } 97 }
225 98
99 void HandleUnconfiguredNetwork(const std::string& service_path,
100 gfx::NativeWindow parent_window) {
101 const NetworkState* network = NetworkHandler::Get()->network_state_handler()->
102 GetNetworkState(service_path);
103 if (!network) {
104 NET_LOG_ERROR("Configuring Unknown network", service_path);
105 return;
106 }
107
108 if (network->type() == flimflam::kTypeWifi ||
109 network->type() == flimflam::kTypeWimax ||
110 network->type() == flimflam::kTypeVPN) {
111 NetworkConfigView::Show(service_path, parent_window);
112 return;
113 }
114
115 if (network->type() == flimflam::kTypeCellular) {
116 if (network->activation_state() != flimflam::kActivationStateActivated) {
117 ash::network_connect::ActivateCellular(service_path);
118 return;
119 }
120 if (network->cellular_out_of_credits()) {
121 ShowMobileSetup(service_path);
122 return;
123 }
124 }
125 // No special configure or setup for |service_path|, show the settings UI.
126 ShowNetworkSettings(service_path);
127 }
128
129 bool EnrollNetwork(const std::string& service_path,
130 gfx::NativeWindow parent_window) {
131 const NetworkState* network = NetworkHandler::Get()->network_state_handler()->
132 GetNetworkState(service_path);
133 if (!network) {
134 NET_LOG_ERROR("Enrolling Unknown network", service_path);
135 return false;
136 }
137 // We skip certificate patterns for device policy ONC so that an unmanaged
138 // user can't get to the place where a cert is presented for them
139 // involuntarily.
140 if (network->ui_data().onc_source() == onc::ONC_SOURCE_DEVICE_POLICY)
141 return false;
142
143 const CertificatePattern& certificate_pattern =
144 network->ui_data().certificate_pattern();
145 if (certificate_pattern.Empty())
146 return false;
147
148 NET_LOG_USER("Enrolling", service_path);
149
150 EnrollmentDelegate* enrollment = CreateEnrollmentDelegate(
151 parent_window, network->name(), ProfileManager::GetDefaultProfile());
152 return enrollment->Enroll(certificate_pattern.enrollment_uri_list(),
153 base::Bind(&EnrollmentComplete, service_path));
154 }
155
156 const base::DictionaryValue* FindPolicyForActiveUser(
157 const NetworkState* network,
158 onc::ONCSource* onc_source) {
159 const User* user = UserManager::Get()->GetActiveUser();
160 std::string username_hash = user ? user->username_hash() : std::string();
161 return NetworkHandler::Get()->managed_network_configuration_handler()->
162 FindPolicyByGUID(username_hash, network->guid(), onc_source);
163 }
164
226 } // namespace network_connect 165 } // namespace network_connect
227 } // namespace chromeos 166 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698