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/login/helper.h" | 5 #include "chrome/browser/chromeos/login/helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/task_scheduler/post_task.h" | 12 #include "base/task_scheduler/post_task.h" |
13 #include "chrome/browser/chromeos/login/ui/login_display_host.h" | 13 #include "chrome/browser/chromeos/login/ui/login_display_host.h" |
14 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" | 14 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" |
15 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
17 #include "chromeos/chromeos_switches.h" | 17 #include "chromeos/chromeos_switches.h" |
18 #include "chromeos/network/managed_network_configuration_handler.h" | 18 #include "chromeos/network/managed_network_configuration_handler.h" |
19 #include "chromeos/network/network_connection_handler.h" | 19 #include "chromeos/network/network_connection_handler.h" |
20 #include "chromeos/network/network_handler.h" | 20 #include "chromeos/network/network_handler.h" |
21 #include "chromeos/network/network_handler_callbacks.h" | |
21 #include "chromeos/network/network_state.h" | 22 #include "chromeos/network/network_state.h" |
22 #include "chromeos/network/network_state_handler.h" | 23 #include "chromeos/network/network_state_handler.h" |
23 #include "chromeos/network/network_util.h" | 24 #include "chromeos/network/network_util.h" |
24 #include "components/guest_view/browser/guest_view_manager.h" | 25 #include "components/guest_view/browser/guest_view_manager.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/storage_partition.h" | 27 #include "content/public/browser/storage_partition.h" |
27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
28 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 29 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
29 #include "third_party/cros_system_api/dbus/service_constants.h" | 30 #include "third_party/cros_system_api/dbus/service_constants.h" |
30 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/display/display.h" | 32 #include "ui/display/display.h" |
32 #include "ui/display/screen.h" | 33 #include "ui/display/screen.h" |
33 #include "ui/gfx/image/image_skia.h" | 34 #include "ui/gfx/image/image_skia.h" |
34 | 35 |
35 namespace chromeos { | 36 namespace chromeos { |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
40 constexpr char kInvalidJsonError[] = "Invalid JSON Dictionary"; | |
41 | |
39 // Gets the WebContents instance of current login display. If there is none, | 42 // Gets the WebContents instance of current login display. If there is none, |
40 // returns nullptr. | 43 // returns nullptr. |
41 content::WebContents* GetLoginWebContents() { | 44 content::WebContents* GetLoginWebContents() { |
42 LoginDisplayHost* host = LoginDisplayHost::default_host(); | 45 LoginDisplayHost* host = LoginDisplayHost::default_host(); |
43 if (!host || !host->GetWebUILoginView()) | 46 if (!host || !host->GetWebUILoginView()) |
44 return nullptr; | 47 return nullptr; |
45 | 48 |
46 return host->GetWebUILoginView()->GetWebContents(); | 49 return host->GetWebUILoginView()->GetWebContents(); |
47 } | 50 } |
48 | 51 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 copied_onc->Set(onc::network_config::WifiProperty(onc::wifi::kHexSSID), | 160 copied_onc->Set(onc::network_config::WifiProperty(onc::wifi::kHexSSID), |
158 base::MakeUnique<base::Value>(hex_ssid)); | 161 base::MakeUnique<base::Value>(hex_ssid)); |
159 copied_onc->Set(onc::network_config::WifiProperty(onc::wifi::kSecurity), | 162 copied_onc->Set(onc::network_config::WifiProperty(onc::wifi::kSecurity), |
160 base::MakeUnique<base::Value>(security)); | 163 base::MakeUnique<base::Value>(security)); |
161 base::JSONWriter::Write(*copied_onc.get(), out_onc_spec); | 164 base::JSONWriter::Write(*copied_onc.get(), out_onc_spec); |
162 } | 165 } |
163 | 166 |
164 void NetworkStateHelper::CreateAndConnectNetworkFromOnc( | 167 void NetworkStateHelper::CreateAndConnectNetworkFromOnc( |
165 const std::string& onc_spec, | 168 const std::string& onc_spec, |
166 const base::Closure& success_callback, | 169 const base::Closure& success_callback, |
167 const base::Closure& error_callback) const { | 170 const network_handler::ErrorCallback& error_callback) const { |
168 std::string error; | 171 std::string error; |
169 std::unique_ptr<base::Value> root = base::JSONReader::ReadAndReturnError( | 172 std::unique_ptr<base::Value> root = base::JSONReader::ReadAndReturnError( |
170 onc_spec, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); | 173 onc_spec, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); |
171 | 174 |
172 base::DictionaryValue* toplevel_onc = nullptr; | 175 base::DictionaryValue* toplevel_onc = nullptr; |
173 if (!root || !root->GetAsDictionary(&toplevel_onc)) { | 176 if (!root || !root->GetAsDictionary(&toplevel_onc)) { |
174 LOG(ERROR) << "Invalid JSON Dictionary: " << error; | 177 LOG(ERROR) << kInvalidJsonError << ": " << error; |
175 error_callback.Run(); | 178 std::unique_ptr<base::DictionaryValue> error_data( |
179 new base::DictionaryValue); | |
achuithb
2017/06/17 00:11:59
base::MakeUnique?
xdai1
2017/06/19 18:14:59
Done.
| |
180 error_data->SetString(network_handler::kErrorName, kInvalidJsonError); | |
181 error_data->SetString(network_handler::kErrorDetail, error); | |
182 error_callback.Run(kInvalidJsonError, std::move(error_data)); | |
176 return; | 183 return; |
177 } | 184 } |
178 | 185 |
179 NetworkHandler::Get() | 186 NetworkHandler::Get() |
180 ->managed_network_configuration_handler() | 187 ->managed_network_configuration_handler() |
181 ->CreateConfiguration( | 188 ->CreateConfiguration( |
182 "", *toplevel_onc, | 189 "", *toplevel_onc, |
183 base::Bind(&NetworkStateHelper::OnCreateConfiguration, | 190 base::Bind(&NetworkStateHelper::OnCreateConfiguration, |
184 base::Unretained(this), success_callback, error_callback), | 191 base::Unretained(this), success_callback, error_callback), |
185 base::Bind(&NetworkStateHelper::OnCreateOrConnectNetworkFailed, | 192 error_callback); |
186 base::Unretained(this), error_callback)); | |
187 } | 193 } |
188 | 194 |
189 bool NetworkStateHelper::IsConnected() const { | 195 bool NetworkStateHelper::IsConnected() const { |
190 chromeos::NetworkStateHandler* nsh = | 196 chromeos::NetworkStateHandler* nsh = |
191 chromeos::NetworkHandler::Get()->network_state_handler(); | 197 chromeos::NetworkHandler::Get()->network_state_handler(); |
192 return nsh->ConnectedNetworkByType(chromeos::NetworkTypePattern::Default()) != | 198 return nsh->ConnectedNetworkByType(chromeos::NetworkTypePattern::Default()) != |
193 nullptr; | 199 nullptr; |
194 } | 200 } |
195 | 201 |
196 bool NetworkStateHelper::IsConnecting() const { | 202 bool NetworkStateHelper::IsConnecting() const { |
197 chromeos::NetworkStateHandler* nsh = | 203 chromeos::NetworkStateHandler* nsh = |
198 chromeos::NetworkHandler::Get()->network_state_handler(); | 204 chromeos::NetworkHandler::Get()->network_state_handler(); |
199 return nsh->ConnectingNetworkByType( | 205 return nsh->ConnectingNetworkByType( |
200 chromeos::NetworkTypePattern::Default()) != nullptr; | 206 chromeos::NetworkTypePattern::Default()) != nullptr; |
201 } | 207 } |
202 | 208 |
203 void NetworkStateHelper::OnCreateConfiguration( | 209 void NetworkStateHelper::OnCreateConfiguration( |
204 const base::Closure& success_callback, | 210 const base::Closure& success_callback, |
205 const base::Closure& error_callback, | 211 const network_handler::ErrorCallback& error_callback, |
206 const std::string& service_path, | 212 const std::string& service_path, |
207 const std::string& guid) const { | 213 const std::string& guid) const { |
208 // Connect to the network. | 214 // Connect to the network. |
209 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( | 215 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( |
210 service_path, success_callback, | 216 service_path, success_callback, error_callback, false); |
211 base::Bind(&NetworkStateHelper::OnCreateOrConnectNetworkFailed, | |
212 base::Unretained(this), error_callback), | |
213 false); | |
214 } | |
215 | |
216 void NetworkStateHelper::OnCreateOrConnectNetworkFailed( | |
217 const base::Closure& error_callback, | |
218 const std::string& error_name, | |
219 std::unique_ptr<base::DictionaryValue> error_data) const { | |
220 LOG(ERROR) << "Failed to create or connect to network: " << error_name; | |
221 error_callback.Run(); | |
222 } | 217 } |
223 | 218 |
224 content::StoragePartition* GetSigninPartition() { | 219 content::StoragePartition* GetSigninPartition() { |
225 content::WebContents* embedder = GetLoginWebContents(); | 220 content::WebContents* embedder = GetLoginWebContents(); |
226 if (!embedder) | 221 if (!embedder) |
227 return nullptr; | 222 return nullptr; |
228 | 223 |
229 // Note the partition name must match the sign-in webview used. For now, | 224 // Note the partition name must match the sign-in webview used. For now, |
230 // this is the default unnamed, shared, in-memory partition. | 225 // this is the default unnamed, shared, in-memory partition. |
231 return GetPartition(embedder, std::string()); | 226 return GetPartition(embedder, std::string()); |
232 } | 227 } |
233 | 228 |
234 net::URLRequestContextGetter* GetSigninContext() { | 229 net::URLRequestContextGetter* GetSigninContext() { |
235 content::StoragePartition* signin_partition = GetSigninPartition(); | 230 content::StoragePartition* signin_partition = GetSigninPartition(); |
236 | 231 |
237 // Special case for unit tests. There's no LoginDisplayHost thus no | 232 // Special case for unit tests. There's no LoginDisplayHost thus no |
238 // webview instance. See http://crbug.com/477402 | 233 // webview instance. See http://crbug.com/477402 |
239 if (!signin_partition && !LoginDisplayHost::default_host()) | 234 if (!signin_partition && !LoginDisplayHost::default_host()) |
240 return ProfileHelper::GetSigninProfile()->GetRequestContext(); | 235 return ProfileHelper::GetSigninProfile()->GetRequestContext(); |
241 | 236 |
242 if (!signin_partition) | 237 if (!signin_partition) |
243 return nullptr; | 238 return nullptr; |
244 | 239 |
245 return signin_partition->GetURLRequestContext(); | 240 return signin_partition->GetURLRequestContext(); |
246 } | 241 } |
247 | 242 |
248 } // namespace login | 243 } // namespace login |
249 | 244 |
250 } // namespace chromeos | 245 } // namespace chromeos |
OLD | NEW |