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

Side by Side Diff: chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc

Issue 1898503002: Cleanup LocalDiscoveryUIHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 years, 8 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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/ui/webui/local_discovery/local_discovery_ui_handler.h" 5 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" 17 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
18 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" 18 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
19 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" 19 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h"
20 #include "chrome/browser/printing/cloud_print/privet_confirm_api_flow.h" 20 #include "chrome/browser/printing/cloud_print/privet_confirm_api_flow.h"
21 #include "chrome/browser/printing/cloud_print/privet_constants.h" 21 #include "chrome/browser/printing/cloud_print/privet_constants.h"
22 #include "chrome/browser/printing/cloud_print/privet_device_lister_impl.h" 22 #include "chrome/browser/printing/cloud_print/privet_device_lister_impl.h"
23 #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h " 23 #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h "
24 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
26 #include "chrome/browser/signin/signin_manager_factory.h" 26 #include "chrome/browser/signin/signin_manager_factory.h"
27 #include "chrome/browser/ui/browser_finder.h" 27 #include "chrome/browser/ui/browser_finder.h"
28 #include "chrome/browser/ui/browser_tabstrip.h" 28 #include "chrome/browser/ui/browser_tabstrip.h"
29 #include "chrome/browser/ui/chrome_pages.h" 29 #include "chrome/browser/ui/chrome_pages.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
32 #include "chrome/grit/generated_resources.h" 31 #include "chrome/grit/generated_resources.h"
33 #include "components/cloud_devices/common/cloud_devices_urls.h" 32 #include "components/cloud_devices/common/cloud_devices_urls.h"
34 #include "components/prefs/pref_service.h" 33 #include "components/prefs/pref_service.h"
35 #include "components/signin/core/browser/profile_oauth2_token_service.h" 34 #include "components/signin/core/browser/profile_oauth2_token_service.h"
36 #include "content/public/browser/user_metrics.h" 35 #include "content/public/browser/user_metrics.h"
37 #include "content/public/browser/web_ui.h" 36 #include "content/public/browser/web_ui.h"
38 #include "ui/base/l10n/l10n_util.h" 37 #include "ui/base/l10n/l10n_util.h"
39 38
40 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS) 39 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 return_value->SetString(kDictionaryKeyDescription, description.description); 72 return_value->SetString(kDictionaryKeyDescription, description.description);
74 return_value->SetString(kDictionaryKeyType, "printer"); 73 return_value->SetString(kDictionaryKeyType, "printer");
75 74
76 return return_value; 75 return return_value;
77 } 76 }
78 77
79 void ReadDevicesList(const CloudPrintPrinterList::DeviceList& devices, 78 void ReadDevicesList(const CloudPrintPrinterList::DeviceList& devices,
80 const std::set<std::string>& local_ids, 79 const std::set<std::string>& local_ids,
81 base::ListValue* devices_list) { 80 base::ListValue* devices_list) {
82 for (const auto& i : devices) { 81 for (const auto& i : devices) {
83 if (local_ids.count(i.id) > 0) { 82 if (ContainsKey(local_ids, i.id)) {
84 devices_list->Append(CreateDeviceInfo(i).release()); 83 devices_list->Append(CreateDeviceInfo(i).release());
85 } 84 }
86 } 85 }
87 86
88 for (const auto& i : devices) { 87 for (const auto& i : devices) {
89 if (local_ids.count(i.id) == 0) { 88 if (!ContainsKey(local_ids, i.id)) {
90 devices_list->Append(CreateDeviceInfo(i).release()); 89 devices_list->Append(CreateDeviceInfo(i).release());
91 } 90 }
92 } 91 }
93 } 92 }
94 93
95 } // namespace 94 } // namespace
96 95
97 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() 96 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler()
98 : is_visible_(false), 97 : is_visible_(false),
99 failed_list_count_(0), 98 failed_list_count_(0),
100 succeded_list_count_(0) { 99 succeded_list_count_(0) {
101 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
102 #if !defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
103 // On Windows, we need the PDF plugin which is only guaranteed to exist on
104 // Google Chrome builds. Use a command-line switch for Windows non-Google
105 // Chrome builds.
106 cloud_print_connector_ui_enabled_ =
107 base::CommandLine::ForCurrentProcess()->HasSwitch(
108 switches::kEnableCloudPrintProxy);
109 #else
110 // Always enabled for Linux and Google Chrome Windows builds.
111 // Never enabled for Chrome OS, we don't even need to indicate it.
112 cloud_print_connector_ui_enabled_ = true;
113 #endif
114 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
115 } 100 }
116 101
117 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() { 102 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() {
118 Profile* profile = Profile::FromWebUI(web_ui()); 103 Profile* profile = Profile::FromWebUI(web_ui());
119 SigninManagerBase* signin_manager = 104 SigninManagerBase* signin_manager =
120 SigninManagerFactory::GetInstance()->GetForProfile(profile); 105 SigninManagerFactory::GetInstance()->GetForProfile(profile);
121 if (signin_manager) 106 if (signin_manager)
122 signin_manager->RemoveObserver(this); 107 signin_manager->RemoveObserver(this);
123 ResetCurrentRegistration(); 108 ResetCurrentRegistration();
124 SetIsVisible(false); 109 SetIsVisible(false);
(...skipping 23 matching lines...) Expand all
148 base::Unretained(this))); 133 base::Unretained(this)));
149 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind( 134 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind(
150 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL, 135 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL,
151 base::Unretained(this))); 136 base::Unretained(this)));
152 web_ui()->RegisterMessageCallback("showSyncUI", base::Bind( 137 web_ui()->RegisterMessageCallback("showSyncUI", base::Bind(
153 &LocalDiscoveryUIHandler::HandleShowSyncUI, 138 &LocalDiscoveryUIHandler::HandleShowSyncUI,
154 base::Unretained(this))); 139 base::Unretained(this)));
155 140
156 // Cloud print connector related messages 141 // Cloud print connector related messages
157 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 142 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
158 if (cloud_print_connector_ui_enabled_) { 143 web_ui()->RegisterMessageCallback(
159 web_ui()->RegisterMessageCallback( 144 "showCloudPrintSetupDialog",
160 "showCloudPrintSetupDialog", 145 base::Bind(&LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog,
161 base::Bind(&LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog, 146 base::Unretained(this)));
162 base::Unretained(this))); 147 web_ui()->RegisterMessageCallback(
163 web_ui()->RegisterMessageCallback( 148 "disableCloudPrintConnector",
164 "disableCloudPrintConnector", 149 base::Bind(&LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector,
165 base::Bind(&LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector, 150 base::Unretained(this)));
166 base::Unretained(this)));
167 }
168 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 151 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
169 } 152 }
170 153
171 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { 154 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) {
172 Profile* profile = Profile::FromWebUI(web_ui()); 155 Profile* profile = Profile::FromWebUI(web_ui());
173 156
174 // If privet_lister_ is already set, it is a mock used for tests or the result 157 // If |privet_lister_| is already set, it is a mock used for tests or the
175 // of a reload. 158 // result of a reload.
176 if (!privet_lister_) { 159 if (!privet_lister_) {
177 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); 160 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance();
178 privet_lister_.reset( 161 privet_lister_.reset(
179 new cloud_print::PrivetDeviceListerImpl(service_discovery_client_.get(), 162 new cloud_print::PrivetDeviceListerImpl(service_discovery_client_.get(),
180 this)); 163 this));
181 privet_http_factory_ = 164 privet_http_factory_ =
182 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance( 165 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance(
183 profile->GetRequestContext()); 166 profile->GetRequestContext());
184 167
185 SigninManagerBase* signin_manager = 168 SigninManagerBase* signin_manager =
(...skipping 15 matching lines...) Expand all
201 void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) { 184 void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) {
202 bool is_visible = false; 185 bool is_visible = false;
203 bool rv = args->GetBoolean(0, &is_visible); 186 bool rv = args->GetBoolean(0, &is_visible);
204 DCHECK(rv); 187 DCHECK(rv);
205 SetIsVisible(is_visible); 188 SetIsVisible(is_visible);
206 } 189 }
207 190
208 void LocalDiscoveryUIHandler::HandleRegisterDevice( 191 void LocalDiscoveryUIHandler::HandleRegisterDevice(
209 const base::ListValue* args) { 192 const base::ListValue* args) {
210 std::string device; 193 std::string device;
211
212 bool rv = args->GetString(0, &device); 194 bool rv = args->GetString(0, &device);
213 DCHECK(rv); 195 DCHECK(rv);
214 196
215 DeviceDescriptionMap::iterator found = device_descriptions_.find(device); 197 DeviceDescriptionMap::iterator it = device_descriptions_.find(device);
216 if (found == device_descriptions_.end()) { 198 if (it == device_descriptions_.end()) {
217 OnSetupError(); 199 OnSetupError();
218 return; 200 return;
219 } 201 }
220 202
221 if (found->second.version < kCloudDevicesPrivetVersion) { 203 if (it->second.version < kCloudDevicesPrivetVersion) {
222 privet_resolution_ = privet_http_factory_->CreatePrivetHTTP(device); 204 privet_resolution_ = privet_http_factory_->CreatePrivetHTTP(device);
223 privet_resolution_->Start( 205 privet_resolution_->Start(
224 found->second.address, 206 it->second.address,
225 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP, 207 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP,
226 base::Unretained(this))); 208 base::Unretained(this)));
227 } else { 209 } else {
228 OnSetupError(); 210 OnSetupError();
229 } 211 }
230 } 212 }
231 213
232 void LocalDiscoveryUIHandler::HandleCancelRegistration( 214 void LocalDiscoveryUIHandler::HandleCancelRegistration(
233 const base::ListValue* args) { 215 const base::ListValue* args) {
234 ResetCurrentRegistration(); 216 ResetCurrentRegistration();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 DCHECK(browser); 254 DCHECK(browser);
273 chrome::ShowBrowserSignin( 255 chrome::ShowBrowserSignin(
274 browser, signin_metrics::AccessPoint::ACCESS_POINT_DEVICES_PAGE); 256 browser, signin_metrics::AccessPoint::ACCESS_POINT_DEVICES_PAGE);
275 } 257 }
276 258
277 void LocalDiscoveryUIHandler::StartRegisterHTTP( 259 void LocalDiscoveryUIHandler::StartRegisterHTTP(
278 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { 260 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) {
279 current_http_client_ = 261 current_http_client_ =
280 cloud_print::PrivetV1HTTPClient::CreateDefault(std::move(http_client)); 262 cloud_print::PrivetV1HTTPClient::CreateDefault(std::move(http_client));
281 263
282 std::string user = GetSyncAccount();
283
284 if (!current_http_client_) { 264 if (!current_http_client_) {
285 SendRegisterError(); 265 SendRegisterError();
286 return; 266 return;
287 } 267 }
288 268
289 current_register_operation_ = 269 current_register_operation_ =
290 current_http_client_->CreateRegisterOperation(user, this); 270 current_http_client_->CreateRegisterOperation(GetSyncAccount(), this);
291 current_register_operation_->Start(); 271 current_register_operation_->Start();
292 } 272 }
293 273
294 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( 274 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
295 cloud_print::PrivetRegisterOperation* operation, 275 PrivetRegisterOperation* operation,
296 const std::string& token, 276 const std::string& token,
297 const GURL& url) { 277 const GURL& url) {
298 web_ui()->CallJavascriptFunction( 278 web_ui()->CallJavascriptFunction(
299 "local_discovery.onRegistrationConfirmedOnPrinter"); 279 "local_discovery.onRegistrationConfirmedOnPrinter");
300 if (device_descriptions_.count(current_http_client_->GetName()) == 0) { 280 if (!ContainsKey(device_descriptions_, current_http_client_->GetName())) {
301 SendRegisterError(); 281 SendRegisterError();
302 return; 282 return;
303 } 283 }
304 284
305 confirm_api_call_flow_ = CreateApiFlow(); 285 confirm_api_call_flow_ = CreateApiFlow();
306 if (!confirm_api_call_flow_) { 286 if (!confirm_api_call_flow_) {
307 SendRegisterError(); 287 SendRegisterError();
308 return; 288 return;
309 } 289 }
310 confirm_api_call_flow_->Start(base::WrapUnique<GCDApiFlow::Request>( 290 confirm_api_call_flow_->Start(base::WrapUnique<GCDApiFlow::Request>(
311 new cloud_print::PrivetConfirmApiCallFlow( 291 new cloud_print::PrivetConfirmApiCallFlow(
312 token, base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, 292 token, base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone,
313 base::Unretained(this))))); 293 base::Unretained(this)))));
314 } 294 }
315 295
316 void LocalDiscoveryUIHandler::OnPrivetRegisterError( 296 void LocalDiscoveryUIHandler::OnPrivetRegisterError(
317 PrivetRegisterOperation* operation, 297 PrivetRegisterOperation* operation,
318 const std::string& action, 298 const std::string& action,
319 PrivetRegisterOperation::FailureReason reason, 299 PrivetRegisterOperation::FailureReason reason,
320 int printer_http_code, 300 int printer_http_code,
321 const base::DictionaryValue* json) { 301 const base::DictionaryValue* json) {
322 std::string error; 302 std::string error;
323 303
324 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR && 304 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR &&
325 json->GetString(cloud_print::kPrivetKeyError, &error)) { 305 json->GetString(cloud_print::kPrivetKeyError, &error)) {
326 if (error == cloud_print::kPrivetErrorTimeout) { 306 if (error == cloud_print::kPrivetErrorTimeout) {
327 web_ui()->CallJavascriptFunction( 307 web_ui()->CallJavascriptFunction(
328 "local_discovery.onRegistrationTimeout"); 308 "local_discovery.onRegistrationTimeout");
329 return; 309 return;
330 } else if (error == cloud_print::kPrivetErrorCancel) { 310 }
311 if (error == cloud_print::kPrivetErrorCancel) {
331 web_ui()->CallJavascriptFunction( 312 web_ui()->CallJavascriptFunction(
332 "local_discovery.onRegistrationCanceledPrinter"); 313 "local_discovery.onRegistrationCanceledPrinter");
333 return; 314 return;
334 } 315 }
335 } 316 }
336 317
337 SendRegisterError(); 318 SendRegisterError();
338 } 319 }
339 320
340 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( 321 void LocalDiscoveryUIHandler::OnPrivetRegisterDone(
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 void LocalDiscoveryUIHandler::SendRegisterError() { 409 void LocalDiscoveryUIHandler::SendRegisterError() {
429 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed"); 410 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed");
430 } 411 }
431 412
432 void LocalDiscoveryUIHandler::SendRegisterDone( 413 void LocalDiscoveryUIHandler::SendRegisterDone(
433 const std::string& service_name) { 414 const std::string& service_name) {
434 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't 415 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't
435 // block the printer's announcement. 416 // block the printer's announcement.
436 privet_lister_->DiscoverNewDevices(false); 417 privet_lister_->DiscoverNewDevices(false);
437 418
438 DeviceDescriptionMap::iterator found = 419 DeviceDescriptionMap::iterator it = device_descriptions_.find(service_name);
439 device_descriptions_.find(service_name);
440 420
441 if (found == device_descriptions_.end()) { 421 if (it == device_descriptions_.end()) {
442 // TODO(noamsml): Handle the case where a printer's record is not present at 422 // TODO(noamsml): Handle the case where a printer's record is not present at
443 // the end of registration. 423 // the end of registration.
444 SendRegisterError(); 424 SendRegisterError();
445 return; 425 return;
446 } 426 }
447 427
448 const DeviceDescription& device = found->second; 428 const DeviceDescription& device = it->second;
449 base::DictionaryValue device_value; 429 base::DictionaryValue device_value;
450 430
451 device_value.SetString(kDictionaryKeyType, device.type); 431 device_value.SetString(kDictionaryKeyType, device.type);
452 device_value.SetString(kDictionaryKeyID, device.id); 432 device_value.SetString(kDictionaryKeyID, device.id);
453 device_value.SetString(kDictionaryKeyDisplayName, device.name); 433 device_value.SetString(kDictionaryKeyDisplayName, device.name);
454 device_value.SetString(kDictionaryKeyDescription, device.description); 434 device_value.SetString(kDictionaryKeyDescription, device.description);
455 device_value.SetString(kDictionaryKeyServiceName, service_name); 435 device_value.SetString(kDictionaryKeyServiceName, service_name);
456 436
457 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess", 437 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess",
458 device_value); 438 device_value);
459 } 439 }
460 440
461 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { 441 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) {
462 if (visible != is_visible_) { 442 if (visible == is_visible_)
463 g_num_visible += visible ? 1 : -1; 443 return;
464 is_visible_ = visible; 444
465 } 445 g_num_visible += visible ? 1 : -1;
446 is_visible_ = visible;
466 } 447 }
467 448
468 std::string LocalDiscoveryUIHandler::GetSyncAccount() { 449 std::string LocalDiscoveryUIHandler::GetSyncAccount() const {
469 Profile* profile = Profile::FromWebUI(web_ui()); 450 Profile* profile = Profile::FromWebUI(web_ui());
470 SigninManagerBase* signin_manager = 451 SigninManagerBase* signin_manager =
471 SigninManagerFactory::GetForProfileIfExists(profile); 452 SigninManagerFactory::GetForProfileIfExists(profile);
472 453
473 if (!signin_manager) { 454 std::string email;
474 return ""; 455 if (signin_manager)
475 } 456 email = signin_manager->GetAuthenticatedAccountInfo().email;
476 457 return email;
477 return signin_manager->GetAuthenticatedAccountInfo().email;
478 } 458 }
479 459
480 // TODO(noamsml): Create master object for registration flow. 460 // TODO(noamsml): Create master object for registration flow.
481 void LocalDiscoveryUIHandler::ResetCurrentRegistration() { 461 void LocalDiscoveryUIHandler::ResetCurrentRegistration() {
482 if (current_register_operation_) { 462 if (current_register_operation_) {
483 current_register_operation_->Cancel(); 463 current_register_operation_->Cancel();
484 current_register_operation_.reset(); 464 current_register_operation_.reset();
485 } 465 }
486 466
487 confirm_api_call_flow_.reset(); 467 confirm_api_call_flow_.reset();
488 privet_resolution_.reset(); 468 privet_resolution_.reset();
489 current_http_client_.reset(); 469 current_http_client_.reset();
490 } 470 }
491 471
492 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { 472 void LocalDiscoveryUIHandler::CheckUserLoggedIn() {
493 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); 473 base::FundamentalValue logged_in_value(!GetSyncAccount().empty());
494 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord()); 474 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord());
495 web_ui()->CallJavascriptFunction( 475 web_ui()->CallJavascriptFunction(
496 "local_discovery.setUserLoggedIn", logged_in_value, is_supervised_value); 476 "local_discovery.setUserLoggedIn", logged_in_value, is_supervised_value);
497 } 477 }
498 478
499 void LocalDiscoveryUIHandler::CheckListingDone() { 479 void LocalDiscoveryUIHandler::CheckListingDone() {
500 int started = 0; 480 int started = cloud_print_printer_list_ ? 1 : 0;
501 if (cloud_print_printer_list_)
502 ++started;
503
504 if (started > failed_list_count_ + succeded_list_count_) 481 if (started > failed_list_count_ + succeded_list_count_)
505 return; 482 return;
506 483
507 if (succeded_list_count_ <= 0) { 484 if (succeded_list_count_ <= 0) {
508 web_ui()->CallJavascriptFunction( 485 web_ui()->CallJavascriptFunction(
509 "local_discovery.onCloudDeviceListUnavailable"); 486 "local_discovery.onCloudDeviceListUnavailable");
510 return; 487 return;
511 } 488 }
512 489
513 base::ListValue devices_list; 490 base::ListValue devices_list;
514 std::set<std::string> local_ids; 491 std::set<std::string> local_ids;
515 492
516 for (DeviceDescriptionMap::iterator i = device_descriptions_.begin(); 493 for (const auto& it : device_descriptions_)
517 i != device_descriptions_.end(); i++) { 494 local_ids.insert(it.second.id);
518 local_ids.insert(i->second.id);
519 }
520 495
521 ReadDevicesList(cloud_devices_, local_ids, &devices_list); 496 ReadDevicesList(cloud_devices_, local_ids, &devices_list);
522 497
523 web_ui()->CallJavascriptFunction( 498 web_ui()->CallJavascriptFunction(
524 "local_discovery.onCloudDeviceListAvailable", devices_list); 499 "local_discovery.onCloudDeviceListAvailable", devices_list);
525 cloud_print_printer_list_.reset(); 500 cloud_print_printer_list_.reset();
526 } 501 }
527 502
528 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() { 503 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() {
529 Profile* profile = Profile::FromWebUI(web_ui()); 504 Profile* profile = Profile::FromWebUI(web_ui());
530 if (!profile) 505 if (!profile)
531 return std::unique_ptr<GCDApiFlow>(); 506 return std::unique_ptr<GCDApiFlow>();
507
532 ProfileOAuth2TokenService* token_service = 508 ProfileOAuth2TokenService* token_service =
533 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 509 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
534 if (!token_service) 510 if (!token_service)
535 return std::unique_ptr<GCDApiFlow>(); 511 return std::unique_ptr<GCDApiFlow>();
512
536 SigninManagerBase* signin_manager = 513 SigninManagerBase* signin_manager =
537 SigninManagerFactory::GetInstance()->GetForProfile(profile); 514 SigninManagerFactory::GetInstance()->GetForProfile(profile);
538 if (!signin_manager) 515 if (!signin_manager)
539 return std::unique_ptr<GCDApiFlow>(); 516 return std::unique_ptr<GCDApiFlow>();
517
540 return GCDApiFlow::Create(profile->GetRequestContext(), 518 return GCDApiFlow::Create(profile->GetRequestContext(),
541 token_service, 519 token_service,
542 signin_manager->GetAuthenticatedAccountId()); 520 signin_manager->GetAuthenticatedAccountId());
543 } 521 }
544 522
545 bool LocalDiscoveryUIHandler::IsUserSupervisedOrOffTheRecord() { 523 bool LocalDiscoveryUIHandler::IsUserSupervisedOrOffTheRecord() {
546 Profile* profile = Profile::FromWebUI(web_ui()); 524 Profile* profile = Profile::FromWebUI(web_ui());
547
548 return profile->IsSupervised() || profile->IsOffTheRecord(); 525 return profile->IsSupervised() || profile->IsOffTheRecord();
549 } 526 }
550 527
551 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 528 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
552 void LocalDiscoveryUIHandler::StartCloudPrintConnector() { 529 void LocalDiscoveryUIHandler::StartCloudPrintConnector() {
553 Profile* profile = Profile::FromWebUI(web_ui()); 530 Profile* profile = Profile::FromWebUI(web_ui());
554 531
555 base::Closure cloud_print_callback = base::Bind( 532 base::Closure cloud_print_callback = base::Bind(
556 &LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged, 533 &LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged,
557 base::Unretained(this)); 534 base::Unretained(this));
558 535
559 if (cloud_print_connector_email_.GetPrefName().empty()) { 536 if (cloud_print_connector_email_.GetPrefName().empty()) {
560 cloud_print_connector_email_.Init( 537 cloud_print_connector_email_.Init(
561 prefs::kCloudPrintEmail, profile->GetPrefs(), cloud_print_callback); 538 prefs::kCloudPrintEmail, profile->GetPrefs(), cloud_print_callback);
562 } 539 }
563 540
564 if (cloud_print_connector_enabled_.GetPrefName().empty()) { 541 if (cloud_print_connector_enabled_.GetPrefName().empty()) {
565 cloud_print_connector_enabled_.Init( 542 cloud_print_connector_enabled_.Init(
566 prefs::kCloudPrintProxyEnabled, profile->GetPrefs(), 543 prefs::kCloudPrintProxyEnabled, profile->GetPrefs(),
567 cloud_print_callback); 544 cloud_print_callback);
568 } 545 }
569 546
570 if (cloud_print_connector_ui_enabled_) { 547 SetupCloudPrintConnectorSection();
571 SetupCloudPrintConnectorSection(); 548 RefreshCloudPrintStatusFromService();
572 RefreshCloudPrintStatusFromService();
573 } else {
574 RemoveCloudPrintConnectorSection();
575 }
576 } 549 }
577 550
578 void LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged() { 551 void LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged() {
579 if (cloud_print_connector_ui_enabled_) 552 SetupCloudPrintConnectorSection();
580 SetupCloudPrintConnectorSection();
581 } 553 }
582 554
583 void LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog( 555 void LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog(
584 const base::ListValue* args) { 556 const base::ListValue* args) {
585 content::RecordAction( 557 content::RecordAction(
586 base::UserMetricsAction("Options_EnableCloudPrintProxy")); 558 base::UserMetricsAction("Options_EnableCloudPrintProxy"));
587 // Open the connector enable page in the current tab. 559 // Open the connector enable page in the current tab.
588 Profile* profile = Profile::FromWebUI(web_ui()); 560 content::OpenURLParams params(cloud_devices::GetCloudPrintEnableURL(
589 content::OpenURLParams params( 561 GetCloudPrintProxyService()->proxy_id()),
590 cloud_devices::GetCloudPrintEnableURL( 562 content::Referrer(), CURRENT_TAB,
591 CloudPrintProxyServiceFactory::GetForProfile(profile)->proxy_id()), 563 ui::PAGE_TRANSITION_LINK, false);
592 content::Referrer(),
593 CURRENT_TAB,
594 ui::PAGE_TRANSITION_LINK,
595 false);
596 web_ui()->GetWebContents()->OpenURL(params); 564 web_ui()->GetWebContents()->OpenURL(params);
597 } 565 }
598 566
599 void LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector( 567 void LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector(
600 const base::ListValue* args) { 568 const base::ListValue* args) {
601 content::RecordAction( 569 content::RecordAction(
602 base::UserMetricsAction("Options_DisableCloudPrintProxy")); 570 base::UserMetricsAction("Options_DisableCloudPrintProxy"));
603 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))-> 571 GetCloudPrintProxyService()->DisableForUser();
604 DisableForUser();
605 } 572 }
606 573
607 void LocalDiscoveryUIHandler::SetupCloudPrintConnectorSection() { 574 void LocalDiscoveryUIHandler::SetupCloudPrintConnectorSection() {
608 Profile* profile = Profile::FromWebUI(web_ui());
609
610 if (!CloudPrintProxyServiceFactory::GetForProfile(profile)) {
611 cloud_print_connector_ui_enabled_ = false;
612 RemoveCloudPrintConnectorSection();
613 return;
614 }
615
616 bool cloud_print_connector_allowed = 575 bool cloud_print_connector_allowed =
617 !cloud_print_connector_enabled_.IsManaged() || 576 !cloud_print_connector_enabled_.IsManaged() ||
618 cloud_print_connector_enabled_.GetValue(); 577 cloud_print_connector_enabled_.GetValue();
619 base::FundamentalValue allowed(cloud_print_connector_allowed); 578 base::FundamentalValue allowed(cloud_print_connector_allowed);
620 579
621 std::string email; 580 std::string email;
581 Profile* profile = Profile::FromWebUI(web_ui());
622 if (profile->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) && 582 if (profile->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) &&
623 cloud_print_connector_allowed) { 583 cloud_print_connector_allowed) {
624 email = profile->GetPrefs()->GetString(prefs::kCloudPrintEmail); 584 email = profile->GetPrefs()->GetString(prefs::kCloudPrintEmail);
625 } 585 }
626 base::FundamentalValue disabled(email.empty()); 586 base::FundamentalValue disabled(email.empty());
627 587
628 base::string16 label_str; 588 base::string16 label_str;
629 if (email.empty()) { 589 if (email.empty()) {
630 label_str = l10n_util::GetStringFUTF16( 590 label_str = l10n_util::GetStringFUTF16(
631 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL, 591 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL,
632 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); 592 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT));
633 } else { 593 } else {
634 label_str = l10n_util::GetStringFUTF16( 594 label_str = l10n_util::GetStringFUTF16(
635 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL, 595 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL,
636 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), 596 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT),
637 base::UTF8ToUTF16(email)); 597 base::UTF8ToUTF16(email));
638 } 598 }
639 base::StringValue label(label_str); 599 base::StringValue label(label_str);
640 600
641 web_ui()->CallJavascriptFunction( 601 web_ui()->CallJavascriptFunction(
642 "local_discovery.setupCloudPrintConnectorSection", disabled, label, 602 "local_discovery.setupCloudPrintConnectorSection", disabled, label,
643 allowed); 603 allowed);
644 } 604 }
645 605
646 void LocalDiscoveryUIHandler::RemoveCloudPrintConnectorSection() { 606 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() {
647 web_ui()->CallJavascriptFunction( 607 GetCloudPrintProxyService()->RefreshStatusFromService();
648 "local_discovery.removeCloudPrintConnectorSection");
649 } 608 }
650 609
651 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { 610 CloudPrintProxyService* LocalDiscoveryUIHandler::GetCloudPrintProxyService() {
652 if (cloud_print_connector_ui_enabled_) 611 return CloudPrintProxyServiceFactory::GetForProfile(
653 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))-> 612 Profile::FromWebUI(web_ui()));
654 RefreshStatusFromService();
655 } 613 }
656 614 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
657 #endif // cloud print connector option stuff
658 615
659 } // namespace local_discovery 616 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698