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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h"
10 #include "base/command_line.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/local_discovery/cloud_device_list.h"
16 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h"
17 #include "chrome/browser/local_discovery/privet_constants.h"
18 #include "chrome/browser/local_discovery/privet_device_lister_impl.h"
19 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
20 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
21 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
22 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
25 #include "chrome/browser/signin/signin_manager_factory.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/browser_tabstrip.h"
28 #include "chrome/browser/ui/chrome_pages.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/grit/generated_resources.h"
32 #include "components/cloud_devices/common/cloud_devices_urls.h"
33 #include "components/signin/core/browser/profile_oauth2_token_service.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/browser/web_ui.h"
36 #include "ui/base/l10n/l10n_util.h"
38 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS)
39 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE
42 namespace local_discovery
{
46 const char kDictionaryKeyServiceName
[] = "service_name";
47 const char kDictionaryKeyDisplayName
[] = "display_name";
48 const char kDictionaryKeyDescription
[] = "description";
49 const char kDictionaryKeyType
[] = "type";
50 const char kDictionaryKeyIsWifi
[] = "is_wifi";
51 const char kDictionaryKeyID
[] = "id";
53 const char kKeyPrefixMDns
[] = "MDns:";
55 int g_num_visible
= 0;
57 const int kCloudDevicesPrivetVersion
= 3;
59 scoped_ptr
<base::DictionaryValue
> CreateDeviceInfo(
60 const CloudDeviceListDelegate::Device
& description
) {
61 scoped_ptr
<base::DictionaryValue
> return_value(new base::DictionaryValue
);
63 return_value
->SetString(kDictionaryKeyID
, description
.id
);
64 return_value
->SetString(kDictionaryKeyDisplayName
, description
.display_name
);
65 return_value
->SetString(kDictionaryKeyDescription
, description
.description
);
66 return_value
->SetString(kDictionaryKeyType
, description
.type
);
68 return return_value
.Pass();
72 const std::vector
<CloudDeviceListDelegate::Device
>& devices
,
73 const std::set
<std::string
>& local_ids
,
74 base::ListValue
* devices_list
) {
75 for (CloudDeviceList::iterator i
= devices
.begin(); i
!= devices
.end(); i
++) {
76 if (local_ids
.count(i
->id
) > 0) {
77 devices_list
->Append(CreateDeviceInfo(*i
).release());
81 for (CloudDeviceList::iterator i
= devices
.begin(); i
!= devices
.end(); i
++) {
82 if (local_ids
.count(i
->id
) == 0) {
83 devices_list
->Append(CreateDeviceInfo(*i
).release());
90 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler()
92 failed_list_count_(0),
93 succeded_list_count_(0) {
94 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
95 #if !defined(GOOGLE_CHROME_BUILD) && defined(OS_WIN)
96 // On Windows, we need the PDF plugin which is only guaranteed to exist on
97 // Google Chrome builds. Use a command-line switch for Windows non-Google
99 cloud_print_connector_ui_enabled_
=
100 base::CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kEnableCloudPrintProxy
);
103 // Always enabled for Linux and Google Chrome Windows builds.
104 // Never enabled for Chrome OS, we don't even need to indicate it.
105 cloud_print_connector_ui_enabled_
= true;
107 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
110 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() {
111 Profile
* profile
= Profile::FromWebUI(web_ui());
112 SigninManagerBase
* signin_manager
=
113 SigninManagerFactory::GetInstance()->GetForProfile(profile
);
115 signin_manager
->RemoveObserver(this);
116 ResetCurrentRegistration();
121 bool LocalDiscoveryUIHandler::GetHasVisible() {
122 return g_num_visible
!= 0;
125 void LocalDiscoveryUIHandler::RegisterMessages() {
126 web_ui()->RegisterMessageCallback("start", base::Bind(
127 &LocalDiscoveryUIHandler::HandleStart
,
128 base::Unretained(this)));
129 web_ui()->RegisterMessageCallback("isVisible", base::Bind(
130 &LocalDiscoveryUIHandler::HandleIsVisible
,
131 base::Unretained(this)));
132 web_ui()->RegisterMessageCallback("registerDevice", base::Bind(
133 &LocalDiscoveryUIHandler::HandleRegisterDevice
,
134 base::Unretained(this)));
135 web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind(
136 &LocalDiscoveryUIHandler::HandleCancelRegistration
,
137 base::Unretained(this)));
138 web_ui()->RegisterMessageCallback(
140 base::Bind(&LocalDiscoveryUIHandler::HandleRequestDeviceList
,
141 base::Unretained(this)));
142 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind(
143 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL
,
144 base::Unretained(this)));
145 web_ui()->RegisterMessageCallback("showSyncUI", base::Bind(
146 &LocalDiscoveryUIHandler::HandleShowSyncUI
,
147 base::Unretained(this)));
149 // Cloud print connector related messages
150 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
151 if (cloud_print_connector_ui_enabled_
) {
152 web_ui()->RegisterMessageCallback(
153 "showCloudPrintSetupDialog",
154 base::Bind(&LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog
,
155 base::Unretained(this)));
156 web_ui()->RegisterMessageCallback(
157 "disableCloudPrintConnector",
158 base::Bind(&LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector
,
159 base::Unretained(this)));
161 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
164 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue
* args
) {
165 Profile
* profile
= Profile::FromWebUI(web_ui());
167 // If privet_lister_ is already set, it is a mock used for tests or the result
169 if (!privet_lister_
) {
170 service_discovery_client_
= ServiceDiscoverySharedClient::GetInstance();
171 privet_lister_
.reset(
172 new PrivetDeviceListerImpl(service_discovery_client_
.get(), this));
173 privet_http_factory_
= PrivetHTTPAsynchronousFactory::CreateInstance(
174 service_discovery_client_
.get(), profile
->GetRequestContext());
176 SigninManagerBase
* signin_manager
=
177 SigninManagerFactory::GetInstance()->GetForProfile(profile
);
179 signin_manager
->AddObserver(this);
182 privet_lister_
->Start();
183 privet_lister_
->DiscoverNewDevices(false);
185 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
186 StartCloudPrintConnector();
192 void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue
* args
) {
193 bool is_visible
= false;
194 bool rv
= args
->GetBoolean(0, &is_visible
);
196 SetIsVisible(is_visible
);
199 void LocalDiscoveryUIHandler::HandleRegisterDevice(
200 const base::ListValue
* args
) {
203 bool rv
= args
->GetString(0, &device
);
206 DeviceDescriptionMap::iterator found
= device_descriptions_
.find(device
);
207 if (found
== device_descriptions_
.end()) {
212 if (found
->second
.version
< kCloudDevicesPrivetVersion
) {
213 privet_resolution_
= privet_http_factory_
->CreatePrivetHTTP(
215 found
->second
.address
,
216 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP
,
217 base::Unretained(this)));
218 privet_resolution_
->Start();
224 void LocalDiscoveryUIHandler::HandleCancelRegistration(
225 const base::ListValue
* args
) {
226 ResetCurrentRegistration();
229 void LocalDiscoveryUIHandler::HandleRequestDeviceList(
230 const base::ListValue
* args
) {
231 failed_list_count_
= 0;
232 succeded_list_count_
= 0;
233 cloud_devices_
.clear();
235 cloud_print_printer_list_
= CreateApiFlow();
237 if (cloud_print_printer_list_
) {
238 cloud_print_printer_list_
->Start(
239 make_scoped_ptr
<GCDApiFlow::Request
>(new CloudPrintPrinterList(this)));
245 void LocalDiscoveryUIHandler::HandleOpenCloudPrintURL(
246 const base::ListValue
* args
) {
248 bool rv
= args
->GetString(0, &id
);
251 Browser
* browser
= chrome::FindBrowserWithWebContents(
252 web_ui()->GetWebContents());
255 chrome::AddSelectedTabWithURL(browser
,
256 cloud_devices::GetCloudPrintManageDeviceURL(id
),
257 ui::PAGE_TRANSITION_FROM_API
);
260 void LocalDiscoveryUIHandler::HandleShowSyncUI(
261 const base::ListValue
* args
) {
262 Browser
* browser
= chrome::FindBrowserWithWebContents(
263 web_ui()->GetWebContents());
265 chrome::ShowBrowserSignin(browser
, signin_metrics::SOURCE_DEVICES_PAGE
);
268 void LocalDiscoveryUIHandler::StartRegisterHTTP(
269 scoped_ptr
<PrivetHTTPClient
> http_client
) {
270 current_http_client_
= PrivetV1HTTPClient::CreateDefault(http_client
.Pass());
272 std::string user
= GetSyncAccount();
274 if (!current_http_client_
) {
279 current_register_operation_
=
280 current_http_client_
->CreateRegisterOperation(user
, this);
281 current_register_operation_
->Start();
284 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
285 PrivetRegisterOperation
* operation
,
286 const std::string
& token
,
288 web_ui()->CallJavascriptFunction(
289 "local_discovery.onRegistrationConfirmedOnPrinter");
290 if (device_descriptions_
.count(current_http_client_
->GetName()) == 0) {
295 confirm_api_call_flow_
= CreateApiFlow();
296 if (!confirm_api_call_flow_
) {
300 confirm_api_call_flow_
->Start(
301 make_scoped_ptr
<GCDApiFlow::Request
>(new PrivetConfirmApiCallFlow(
303 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone
,
304 base::Unretained(this)))));
307 void LocalDiscoveryUIHandler::OnPrivetRegisterError(
308 PrivetRegisterOperation
* operation
,
309 const std::string
& action
,
310 PrivetRegisterOperation::FailureReason reason
,
311 int printer_http_code
,
312 const base::DictionaryValue
* json
) {
315 if (reason
== PrivetRegisterOperation::FAILURE_JSON_ERROR
&&
316 json
->GetString(kPrivetKeyError
, &error
)) {
317 if (error
== kPrivetErrorTimeout
) {
318 web_ui()->CallJavascriptFunction(
319 "local_discovery.onRegistrationTimeout");
321 } else if (error
== kPrivetErrorCancel
) {
322 web_ui()->CallJavascriptFunction(
323 "local_discovery.onRegistrationCanceledPrinter");
331 void LocalDiscoveryUIHandler::OnPrivetRegisterDone(
332 PrivetRegisterOperation
* operation
,
333 const std::string
& device_id
) {
334 std::string name
= operation
->GetHTTPClient()->GetName();
335 current_register_operation_
.reset();
336 current_http_client_
.reset();
337 SendRegisterDone(name
);
340 void LocalDiscoveryUIHandler::OnSetupError() {
341 ResetCurrentRegistration();
345 void LocalDiscoveryUIHandler::OnConfirmDone(GCDApiFlow::Status status
) {
346 if (status
== GCDApiFlow::SUCCESS
) {
347 confirm_api_call_flow_
.reset();
348 current_register_operation_
->CompleteRegistration();
354 void LocalDiscoveryUIHandler::DeviceChanged(
356 const std::string
& name
,
357 const DeviceDescription
& description
) {
358 device_descriptions_
[name
] = description
;
360 base::DictionaryValue info
;
362 base::StringValue
service_key(kKeyPrefixMDns
+ name
);
364 if (description
.id
.empty()) {
365 info
.SetString(kDictionaryKeyServiceName
, name
);
366 info
.SetString(kDictionaryKeyDisplayName
, description
.name
);
367 info
.SetString(kDictionaryKeyDescription
, description
.description
);
368 info
.SetString(kDictionaryKeyType
, description
.type
);
369 info
.SetBoolean(kDictionaryKeyIsWifi
, false);
371 web_ui()->CallJavascriptFunction(
372 "local_discovery.onUnregisteredDeviceUpdate", service_key
, info
);
374 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
376 web_ui()->CallJavascriptFunction(
377 "local_discovery.onUnregisteredDeviceUpdate", service_key
, *null_value
);
381 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string
& name
) {
382 device_descriptions_
.erase(name
);
383 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
384 base::StringValue
name_value(kKeyPrefixMDns
+ name
);
386 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate",
387 name_value
, *null_value
);
390 void LocalDiscoveryUIHandler::DeviceCacheFlushed() {
391 web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed");
392 privet_lister_
->DiscoverNewDevices(false);
395 void LocalDiscoveryUIHandler::OnDeviceListReady(
396 const std::vector
<Device
>& devices
) {
397 cloud_devices_
.insert(cloud_devices_
.end(), devices
.begin(), devices
.end());
398 ++succeded_list_count_
;
402 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() {
403 ++failed_list_count_
;
407 void LocalDiscoveryUIHandler::GoogleSigninSucceeded(
408 const std::string
& account_id
,
409 const std::string
& username
,
410 const std::string
& password
) {
414 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string
& account_id
,
415 const std::string
& username
) {
419 void LocalDiscoveryUIHandler::SendRegisterError() {
420 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed");
423 void LocalDiscoveryUIHandler::SendRegisterDone(
424 const std::string
& service_name
) {
425 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't
426 // block the printer's announcement.
427 privet_lister_
->DiscoverNewDevices(false);
429 DeviceDescriptionMap::iterator found
=
430 device_descriptions_
.find(service_name
);
432 if (found
== device_descriptions_
.end()) {
433 // TODO(noamsml): Handle the case where a printer's record is not present at
434 // the end of registration.
439 const DeviceDescription
& device
= found
->second
;
440 base::DictionaryValue device_value
;
442 device_value
.SetString(kDictionaryKeyType
, device
.type
);
443 device_value
.SetString(kDictionaryKeyID
, device
.id
);
444 device_value
.SetString(kDictionaryKeyDisplayName
, device
.name
);
445 device_value
.SetString(kDictionaryKeyDescription
, device
.description
);
446 device_value
.SetString(kDictionaryKeyServiceName
, service_name
);
448 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess",
452 void LocalDiscoveryUIHandler::SetIsVisible(bool visible
) {
453 if (visible
!= is_visible_
) {
454 g_num_visible
+= visible
? 1 : -1;
455 is_visible_
= visible
;
459 std::string
LocalDiscoveryUIHandler::GetSyncAccount() {
460 Profile
* profile
= Profile::FromWebUI(web_ui());
461 SigninManagerBase
* signin_manager
=
462 SigninManagerFactory::GetForProfileIfExists(profile
);
464 if (!signin_manager
) {
468 return signin_manager
->GetAuthenticatedUsername();
471 // TODO(noamsml): Create master object for registration flow.
472 void LocalDiscoveryUIHandler::ResetCurrentRegistration() {
473 if (current_register_operation_
) {
474 current_register_operation_
->Cancel();
475 current_register_operation_
.reset();
478 confirm_api_call_flow_
.reset();
479 privet_resolution_
.reset();
480 current_http_client_
.reset();
483 void LocalDiscoveryUIHandler::CheckUserLoggedIn() {
484 base::FundamentalValue
logged_in_value(!GetSyncAccount().empty());
485 base::FundamentalValue
is_supervised_value(IsUserSupervisedOrOffTheRecord());
486 web_ui()->CallJavascriptFunction(
487 "local_discovery.setUserLoggedIn", logged_in_value
, is_supervised_value
);
490 void LocalDiscoveryUIHandler::CheckListingDone() {
492 if (cloud_print_printer_list_
)
495 if (started
> failed_list_count_
+ succeded_list_count_
)
498 if (succeded_list_count_
<= 0) {
499 web_ui()->CallJavascriptFunction(
500 "local_discovery.onCloudDeviceListUnavailable");
504 base::ListValue devices_list
;
505 std::set
<std::string
> local_ids
;
507 for (DeviceDescriptionMap::iterator i
= device_descriptions_
.begin();
508 i
!= device_descriptions_
.end(); i
++) {
509 local_ids
.insert(i
->second
.id
);
512 ReadDevicesList(cloud_devices_
, local_ids
, &devices_list
);
514 web_ui()->CallJavascriptFunction(
515 "local_discovery.onCloudDeviceListAvailable", devices_list
);
516 cloud_print_printer_list_
.reset();
519 scoped_ptr
<GCDApiFlow
> LocalDiscoveryUIHandler::CreateApiFlow() {
520 Profile
* profile
= Profile::FromWebUI(web_ui());
522 return scoped_ptr
<GCDApiFlow
>();
523 ProfileOAuth2TokenService
* token_service
=
524 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
526 return scoped_ptr
<GCDApiFlow
>();
527 SigninManagerBase
* signin_manager
=
528 SigninManagerFactory::GetInstance()->GetForProfile(profile
);
530 return scoped_ptr
<GCDApiFlow
>();
531 return GCDApiFlow::Create(profile
->GetRequestContext(),
533 signin_manager
->GetAuthenticatedAccountId());
536 bool LocalDiscoveryUIHandler::IsUserSupervisedOrOffTheRecord() {
537 Profile
* profile
= Profile::FromWebUI(web_ui());
539 return profile
->IsSupervised() || profile
->IsOffTheRecord();
542 #if defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE)
543 void LocalDiscoveryUIHandler::StartCloudPrintConnector() {
544 Profile
* profile
= Profile::FromWebUI(web_ui());
546 base::Closure cloud_print_callback
= base::Bind(
547 &LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged
,
548 base::Unretained(this));
550 if (cloud_print_connector_email_
.GetPrefName().empty()) {
551 cloud_print_connector_email_
.Init(
552 prefs::kCloudPrintEmail
, profile
->GetPrefs(), cloud_print_callback
);
555 if (cloud_print_connector_enabled_
.GetPrefName().empty()) {
556 cloud_print_connector_enabled_
.Init(
557 prefs::kCloudPrintProxyEnabled
, profile
->GetPrefs(),
558 cloud_print_callback
);
561 if (cloud_print_connector_ui_enabled_
) {
562 SetupCloudPrintConnectorSection();
563 RefreshCloudPrintStatusFromService();
565 RemoveCloudPrintConnectorSection();
569 void LocalDiscoveryUIHandler::OnCloudPrintPrefsChanged() {
570 if (cloud_print_connector_ui_enabled_
)
571 SetupCloudPrintConnectorSection();
574 void LocalDiscoveryUIHandler::ShowCloudPrintSetupDialog(
575 const base::ListValue
* args
) {
576 content::RecordAction(
577 base::UserMetricsAction("Options_EnableCloudPrintProxy"));
578 // Open the connector enable page in the current tab.
579 Profile
* profile
= Profile::FromWebUI(web_ui());
580 content::OpenURLParams
params(
581 cloud_devices::GetCloudPrintEnableURL(
582 CloudPrintProxyServiceFactory::GetForProfile(profile
)->proxy_id()),
585 ui::PAGE_TRANSITION_LINK
,
587 web_ui()->GetWebContents()->OpenURL(params
);
590 void LocalDiscoveryUIHandler::HandleDisableCloudPrintConnector(
591 const base::ListValue
* args
) {
592 content::RecordAction(
593 base::UserMetricsAction("Options_DisableCloudPrintProxy"));
594 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))->
598 void LocalDiscoveryUIHandler::SetupCloudPrintConnectorSection() {
599 Profile
* profile
= Profile::FromWebUI(web_ui());
601 if (!CloudPrintProxyServiceFactory::GetForProfile(profile
)) {
602 cloud_print_connector_ui_enabled_
= false;
603 RemoveCloudPrintConnectorSection();
607 bool cloud_print_connector_allowed
=
608 !cloud_print_connector_enabled_
.IsManaged() ||
609 cloud_print_connector_enabled_
.GetValue();
610 base::FundamentalValue
allowed(cloud_print_connector_allowed
);
613 if (profile
->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail
) &&
614 cloud_print_connector_allowed
) {
615 email
= profile
->GetPrefs()->GetString(prefs::kCloudPrintEmail
);
617 base::FundamentalValue
disabled(email
.empty());
619 base::string16 label_str
;
621 label_str
= l10n_util::GetStringFUTF16(
622 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL
,
623 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT
));
625 label_str
= l10n_util::GetStringFUTF16(
626 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL
,
627 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT
),
628 base::UTF8ToUTF16(email
));
630 base::StringValue
label(label_str
);
632 web_ui()->CallJavascriptFunction(
633 "local_discovery.setupCloudPrintConnectorSection", disabled
, label
,
637 void LocalDiscoveryUIHandler::RemoveCloudPrintConnectorSection() {
638 web_ui()->CallJavascriptFunction(
639 "local_discovery.removeCloudPrintConnectorSection");
642 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() {
643 if (cloud_print_connector_ui_enabled_
)
644 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))->
645 RefreshStatusFromService();
648 #endif // cloud print connector option stuff
650 } // namespace local_discovery