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

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

Issue 284223004: List cloud devices in chrome://devices page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 11 matching lines...) Expand all
22 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" 22 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
23 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" 23 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_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/signin/signin_promo.h" 27 #include "chrome/browser/signin/signin_promo.h"
28 #include "chrome/browser/ui/browser_finder.h" 28 #include "chrome/browser/ui/browser_finder.h"
29 #include "chrome/browser/ui/browser_tabstrip.h" 29 #include "chrome/browser/ui/browser_tabstrip.h"
30 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
32 #include "components/cloud_devices/common/cloud_devices_switches.h"
32 #include "components/cloud_devices/common/cloud_devices_urls.h" 33 #include "components/cloud_devices/common/cloud_devices_urls.h"
33 #include "components/signin/core/browser/profile_oauth2_token_service.h" 34 #include "components/signin/core/browser/profile_oauth2_token_service.h"
34 #include "components/signin/core/browser/signin_manager_base.h" 35 #include "components/signin/core/browser/signin_manager_base.h"
35 #include "content/public/browser/user_metrics.h" 36 #include "content/public/browser/user_metrics.h"
36 #include "content/public/browser/web_ui.h" 37 #include "content/public/browser/web_ui.h"
37 #include "content/public/common/page_transition_types.h" 38 #include "content/public/common/page_transition_types.h"
38 #include "grit/generated_resources.h" 39 #include "grit/generated_resources.h"
39 #include "net/base/host_port_pair.h" 40 #include "net/base/host_port_pair.h"
40 #include "net/base/net_util.h" 41 #include "net/base/net_util.h"
41 #include "net/base/url_util.h" 42 #include "net/base/url_util.h"
42 #include "net/http/http_status_code.h" 43 #include "net/http/http_status_code.h"
43 #include "ui/base/l10n/l10n_util.h" 44 #include "ui/base/l10n/l10n_util.h"
44 45
45 #if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS) 46 #if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
46 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE 47 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
47 #endif 48 #endif
48 49
49 namespace local_discovery { 50 namespace local_discovery {
50 51
51 namespace { 52 namespace {
52 const char kDeviceTypePrinter[] = "printer"; 53
53 int g_num_visible = 0; 54 int g_num_visible = 0;
55
56 scoped_ptr<base::DictionaryValue> CreateDeviceInfo(
57 const CloudDeviceListDelegate::Device& description) {
58 scoped_ptr<base::DictionaryValue> return_value(new base::DictionaryValue);
59
60 return_value->SetString("id", description.id);
61 return_value->SetString("display_name", description.display_name);
62 return_value->SetString("description", description.description);
63 return_value->SetString("type", description.type);
64
65 return return_value.Pass();
66 }
67
68 void ReadDevicesList(
69 const std::vector<CloudDeviceListDelegate::Device>& devices,
70 const std::set<std::string>& local_ids,
71 base::ListValue* devices_list) {
72 for (CloudDeviceList::iterator i = devices.begin(); i != devices.end(); i++) {
73 if (local_ids.count(i->id) > 0) {
74 devices_list->Append(CreateDeviceInfo(*i).release());
75 }
76 }
77
78 for (CloudDeviceList::iterator i = devices.begin(); i != devices.end(); i++) {
79 if (local_ids.count(i->id) == 0) {
80 devices_list->Append(CreateDeviceInfo(*i).release());
81 }
82 }
83 }
84
54 } // namespace 85 } // namespace
55 86
56 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) { 87 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler()
88 : is_visible_(false),
89 failed_list_count_(0),
90 succeded_list_count_(0) {
57 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 91 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
58 #if !defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN) 92 #if !defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
59 // On Windows, we need the PDF plugin which is only guaranteed to exist on 93 // On Windows, we need the PDF plugin which is only guaranteed to exist on
60 // Google Chrome builds. Use a command-line switch for Windows non-Google 94 // Google Chrome builds. Use a command-line switch for Windows non-Google
61 // Chrome builds. 95 // Chrome builds.
62 cloud_print_connector_ui_enabled_ = 96 cloud_print_connector_ui_enabled_ =
63 CommandLine::ForCurrentProcess()->HasSwitch( 97 CommandLine::ForCurrentProcess()->HasSwitch(
64 switches::kEnableCloudPrintProxy); 98 switches::kEnableCloudPrintProxy);
65 #else 99 #else
66 // Always enabled for Linux and Google Chrome Windows builds. 100 // Always enabled for Linux and Google Chrome Windows builds.
(...skipping 24 matching lines...) Expand all
91 base::Unretained(this))); 125 base::Unretained(this)));
92 web_ui()->RegisterMessageCallback("isVisible", base::Bind( 126 web_ui()->RegisterMessageCallback("isVisible", base::Bind(
93 &LocalDiscoveryUIHandler::HandleIsVisible, 127 &LocalDiscoveryUIHandler::HandleIsVisible,
94 base::Unretained(this))); 128 base::Unretained(this)));
95 web_ui()->RegisterMessageCallback("registerDevice", base::Bind( 129 web_ui()->RegisterMessageCallback("registerDevice", base::Bind(
96 &LocalDiscoveryUIHandler::HandleRegisterDevice, 130 &LocalDiscoveryUIHandler::HandleRegisterDevice,
97 base::Unretained(this))); 131 base::Unretained(this)));
98 web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind( 132 web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind(
99 &LocalDiscoveryUIHandler::HandleCancelRegistration, 133 &LocalDiscoveryUIHandler::HandleCancelRegistration,
100 base::Unretained(this))); 134 base::Unretained(this)));
101 web_ui()->RegisterMessageCallback("requestPrinterList", base::Bind( 135 web_ui()->RegisterMessageCallback(
102 &LocalDiscoveryUIHandler::HandleRequestPrinterList, 136 "requestDeviceList",
103 base::Unretained(this))); 137 base::Bind(&LocalDiscoveryUIHandler::HandleRequestDeviceList,
138 base::Unretained(this)));
104 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind( 139 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind(
105 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL, 140 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL,
106 base::Unretained(this))); 141 base::Unretained(this)));
107 web_ui()->RegisterMessageCallback("showSyncUI", base::Bind( 142 web_ui()->RegisterMessageCallback("showSyncUI", base::Bind(
108 &LocalDiscoveryUIHandler::HandleShowSyncUI, 143 &LocalDiscoveryUIHandler::HandleShowSyncUI,
109 base::Unretained(this))); 144 base::Unretained(this)));
110 145
111 // Cloud print connector related messages 146 // Cloud print connector related messages
112 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 147 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
113 if (cloud_print_connector_ui_enabled_) { 148 if (cloud_print_connector_ui_enabled_) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP, 207 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP,
173 base::Unretained(this))); 208 base::Unretained(this)));
174 privet_resolution_->Start(); 209 privet_resolution_->Start();
175 } 210 }
176 211
177 void LocalDiscoveryUIHandler::HandleCancelRegistration( 212 void LocalDiscoveryUIHandler::HandleCancelRegistration(
178 const base::ListValue* args) { 213 const base::ListValue* args) {
179 ResetCurrentRegistration(); 214 ResetCurrentRegistration();
180 } 215 }
181 216
182 void LocalDiscoveryUIHandler::HandleRequestPrinterList( 217 void LocalDiscoveryUIHandler::HandleRequestDeviceList(
183 const base::ListValue* args) { 218 const base::ListValue* args) {
184 Profile* profile = Profile::FromWebUI(web_ui()); 219 Profile* profile = Profile::FromWebUI(web_ui());
185 ProfileOAuth2TokenService* token_service = 220 ProfileOAuth2TokenService* token_service =
186 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 221 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
187 222
188 SigninManagerBase* signin_manager = 223 SigninManagerBase* signin_manager =
189 SigninManagerFactory::GetInstance()->GetForProfile(profile); 224 SigninManagerFactory::GetInstance()->GetForProfile(profile);
190 225
226 failed_list_count_ = 0;
227 succeded_list_count_ = 0;
228
191 cloud_print_printer_list_.reset( 229 cloud_print_printer_list_.reset(
192 new CloudPrintPrinterList(profile->GetRequestContext(), 230 new CloudPrintPrinterList(profile->GetRequestContext(),
193 token_service, 231 token_service,
194 signin_manager->GetAuthenticatedAccountId(), 232 signin_manager->GetAuthenticatedAccountId(),
195 this)); 233 this));
234 if (CommandLine::ForCurrentProcess()->HasSwitch(
235 switches::kEnableCloudDevices)) {
236 cloud_device_list_.reset(
237 new CloudDeviceList(profile->GetRequestContext(),
238 token_service,
239 signin_manager->GetAuthenticatedAccountId(),
240 this));
241 }
242
196 cloud_print_printer_list_->Start(); 243 cloud_print_printer_list_->Start();
244 if (cloud_device_list_)
245 cloud_device_list_->Start();
197 } 246 }
198 247
199 void LocalDiscoveryUIHandler::HandleOpenCloudPrintURL( 248 void LocalDiscoveryUIHandler::HandleOpenCloudPrintURL(
200 const base::ListValue* args) { 249 const base::ListValue* args) {
201 std::string id; 250 std::string id;
202 bool rv = args->GetString(0, &id); 251 bool rv = args->GetString(0, &id);
203 DCHECK(rv); 252 DCHECK(rv);
204 253
205 Browser* browser = chrome::FindBrowserWithWebContents( 254 Browser* browser = chrome::FindBrowserWithWebContents(
206 web_ui()->GetWebContents()); 255 web_ui()->GetWebContents());
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 424
376 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate", 425 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate",
377 name_value, *null_value); 426 name_value, *null_value);
378 } 427 }
379 428
380 void LocalDiscoveryUIHandler::DeviceCacheFlushed() { 429 void LocalDiscoveryUIHandler::DeviceCacheFlushed() {
381 web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed"); 430 web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed");
382 privet_lister_->DiscoverNewDevices(false); 431 privet_lister_->DiscoverNewDevices(false);
383 } 432 }
384 433
385 void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() { 434 void LocalDiscoveryUIHandler::OnDeviceListReady() {
386 base::ListValue printer_object_list; 435 ++succeded_list_count_;
387 std::set<std::string> local_ids; 436 CheckListingDone();
388
389 for (DeviceDescriptionMap::iterator i = device_descriptions_.begin();
390 i != device_descriptions_.end();
391 i++) {
392 std::string device_id = i->second.id;
393 if (!device_id.empty()) {
394 const CloudPrintPrinterList::PrinterDetails* details =
395 cloud_print_printer_list_->GetDetailsFor(device_id);
396
397 if (details) {
398 local_ids.insert(device_id);
399 printer_object_list.Append(CreatePrinterInfo(*details).release());
400 }
401 }
402 }
403
404 for (CloudPrintPrinterList::iterator i = cloud_print_printer_list_->begin();
405 i != cloud_print_printer_list_->end(); i++) {
406 if (local_ids.count(i->id) == 0) {
407 printer_object_list.Append(CreatePrinterInfo(*i).release());
408 }
409 }
410
411 web_ui()->CallJavascriptFunction(
412 "local_discovery.onCloudDeviceListAvailable", printer_object_list);
413 } 437 }
414 438
415 void LocalDiscoveryUIHandler::OnCloudPrintPrinterListUnavailable() { 439 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() {
416 web_ui()->CallJavascriptFunction( 440 ++failed_list_count_;
417 "local_discovery.onCloudDeviceListUnavailable"); 441 CheckListingDone();
418 } 442 }
419 443
420 void LocalDiscoveryUIHandler::GoogleSigninSucceeded( 444 void LocalDiscoveryUIHandler::GoogleSigninSucceeded(
421 const std::string& username, 445 const std::string& username,
422 const std::string& password) { 446 const std::string& password) {
423 CheckUserLoggedIn(); 447 CheckUserLoggedIn();
424 } 448 }
425 449
426 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& username) { 450 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& username) {
427 CheckUserLoggedIn(); 451 CheckUserLoggedIn();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 if (current_register_operation_.get()) { 492 if (current_register_operation_.get()) {
469 current_register_operation_->Cancel(); 493 current_register_operation_->Cancel();
470 current_register_operation_.reset(); 494 current_register_operation_.reset();
471 } 495 }
472 496
473 confirm_api_call_flow_.reset(); 497 confirm_api_call_flow_.reset();
474 privet_resolution_.reset(); 498 privet_resolution_.reset();
475 current_http_client_.reset(); 499 current_http_client_.reset();
476 } 500 }
477 501
478 scoped_ptr<base::DictionaryValue> LocalDiscoveryUIHandler::CreatePrinterInfo(
479 const CloudPrintPrinterList::PrinterDetails& description) {
480 scoped_ptr<base::DictionaryValue> return_value(new base::DictionaryValue);
481
482 return_value->SetString("id", description.id);
483 return_value->SetString("display_name", description.display_name);
484 return_value->SetString("description", description.description);
485 return_value->SetString("type", "printer");
486
487 return return_value.Pass();
488 }
489
490 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { 502 void LocalDiscoveryUIHandler::CheckUserLoggedIn() {
491 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); 503 base::FundamentalValue logged_in_value(!GetSyncAccount().empty());
492 web_ui()->CallJavascriptFunction("local_discovery.setUserLoggedIn", 504 web_ui()->CallJavascriptFunction("local_discovery.setUserLoggedIn",
493 logged_in_value); 505 logged_in_value);
494 } 506 }
495 507
508 void LocalDiscoveryUIHandler::CheckListingDone() {
509 int started = 0;
510 if (cloud_print_printer_list_)
511 ++started;
512 if (cloud_device_list_)
513 ++started;
514
515 if (started > failed_list_count_ + succeded_list_count_)
516 return;
517
518 if (succeded_list_count_ <= 0) {
519 web_ui()->CallJavascriptFunction(
520 "local_discovery.onCloudDeviceListUnavailable");
521 return;
522 }
523
524 base::ListValue devices_list;
525 std::set<std::string> local_ids;
526
527 for (DeviceDescriptionMap::iterator i = device_descriptions_.begin();
528 i != device_descriptions_.end(); i++) {
529 local_ids.insert(i->second.id);
530 }
531
532 if (cloud_print_printer_list_) {
533 ReadDevicesList(cloud_print_printer_list_->printer_list(), local_ids,
534 &devices_list);
535 }
536 if (cloud_device_list_) {
537 ReadDevicesList(cloud_device_list_->device_list(), local_ids,
538 &devices_list);
539 }
540
541 web_ui()->CallJavascriptFunction(
542 "local_discovery.onCloudDeviceListAvailable", devices_list);
543 cloud_print_printer_list_.reset();
544 cloud_device_list_.reset();
545 }
546
496 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) 547 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
497 void LocalDiscoveryUIHandler::StartCloudPrintConnector() { 548 void LocalDiscoveryUIHandler::StartCloudPrintConnector() {
498 Profile* profile = Profile::FromWebUI(web_ui()); 549 Profile* profile = Profile::FromWebUI(web_ui());
499 550
500 base::Closure cloud_print_callback = base::Bind( 551 base::Closure cloud_print_callback = base::Bind(
501 &LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged, 552 &LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged,
502 base::Unretained(this)); 553 base::Unretained(this));
503 554
504 if (cloud_print_connector_email_.GetPrefName().empty()) { 555 if (cloud_print_connector_email_.GetPrefName().empty()) {
505 cloud_print_connector_email_.Init( 556 cloud_print_connector_email_.Init(
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 } 645 }
595 646
596 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { 647 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() {
597 if (cloud_print_connector_ui_enabled_) 648 if (cloud_print_connector_ui_enabled_)
598 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))-> 649 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))->
599 RefreshStatusFromService(); 650 RefreshStatusFromService();
600 } 651 }
601 #endif // cloud print connector option stuff 652 #endif // cloud print connector option stuff
602 653
603 } // namespace local_discovery 654 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698