Index: chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
diff --git a/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
index b4bb6ea66f52bcfded663f28e1e2865dd78e3ef5..6bfb982ebc78ef532da5ecb7b460aec735a73b3c 100644 |
--- a/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
+++ b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
@@ -4,18 +4,25 @@ |
#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h" |
+#include <set> |
+ |
#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
#include "base/strings/stringprintf.h" |
#include "base/values.h" |
#include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
#include "chrome/browser/local_discovery/privet_http_impl.h" |
+#include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/signin/profile_oauth2_token_service.h" |
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
#include "chrome/browser/signin/signin_manager.h" |
#include "chrome/browser/signin/signin_manager_base.h" |
#include "chrome/browser/signin/signin_manager_factory.h" |
+#include "chrome/browser/ui/browser_finder.h" |
+#include "chrome/browser/ui/browser_tabstrip.h" |
#include "content/public/browser/web_ui.h" |
+#include "content/public/common/page_transition_types.h" |
#include "net/base/host_port_pair.h" |
#include "net/base/net_util.h" |
#include "net/http/http_status_code.h" |
@@ -23,12 +30,12 @@ |
namespace local_discovery { |
namespace { |
-// TODO(noamsml): This is a temporary shim until automated_url is in the |
-// response. |
const char kPrivetAutomatedClaimURLFormat[] = "%s/confirm?token=%s"; |
+const int kRegistrationAnnouncementTimeoutSeconds = 5; |
LocalDiscoveryUIHandler::Factory* g_factory = NULL; |
int g_num_visible = 0; |
+ |
} // namespace |
LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) { |
@@ -74,15 +81,20 @@ void LocalDiscoveryUIHandler::RegisterMessages() { |
web_ui()->RegisterMessageCallback("registerDevice", base::Bind( |
&LocalDiscoveryUIHandler::HandleRegisterDevice, |
base::Unretained(this))); |
- web_ui()->RegisterMessageCallback("chooseUser", base::Bind( |
- &LocalDiscoveryUIHandler::HandleChooseUser, |
- base::Unretained(this))); |
web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind( |
&LocalDiscoveryUIHandler::HandleCancelRegistration, |
base::Unretained(this))); |
+ web_ui()->RegisterMessageCallback("requestPrinterList", base::Bind( |
+ &LocalDiscoveryUIHandler::HandleRequestPrinterList, |
+ base::Unretained(this))); |
+ web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind( |
+ &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL, |
+ base::Unretained(this))); |
} |
void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { |
+ Profile* profile = Profile::FromWebUI(web_ui()); |
+ |
// If privet_lister_ is already set, it is a mock used for tests or the result |
// of a reload. |
if (!privet_lister_) { |
@@ -91,32 +103,13 @@ void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { |
service_discovery_client_.get(), this)); |
privet_http_factory_.reset(new PrivetHTTPAsynchronousFactoryImpl( |
service_discovery_client_.get(), |
- Profile::FromWebUI(web_ui())->GetRequestContext())); |
+ profile->GetRequestContext())); |
} |
privet_lister_->Start(); |
privet_lister_->DiscoverNewDevices(false); |
} |
-void LocalDiscoveryUIHandler::HandleRegisterDevice( |
- const base::ListValue* args) { |
- std::string device_name; |
- |
- bool rv = args->GetString(0, &device_name); |
- DCHECK(rv); |
- |
- current_register_device_ = device_name; |
- |
- cloud_print_account_manager_.reset(new CloudPrintAccountManager( |
- Profile::FromWebUI(web_ui())->GetRequestContext(), |
- GetCloudPrintBaseUrl(device_name), |
- 0 /* Get XSRF token for primary user */, |
- base::Bind(&LocalDiscoveryUIHandler::OnCloudPrintAccountsResolved, |
- base::Unretained(this)))); |
- |
- cloud_print_account_manager_->Start(); |
-} |
- |
void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) { |
bool is_visible = false; |
bool rv = args->GetBoolean(0, &is_visible); |
@@ -124,19 +117,18 @@ void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) { |
SetIsVisible(is_visible); |
} |
-void LocalDiscoveryUIHandler::HandleChooseUser(const base::ListValue* args) { |
- std::string user; |
+void LocalDiscoveryUIHandler::HandleRegisterDevice( |
+ const base::ListValue* args) { |
+ std::string device; |
- bool rv = args->GetInteger(0, ¤t_register_user_index_); |
- DCHECK(rv); |
- rv = args->GetString(1, &user); |
+ bool rv = args->GetString(0, &device); |
DCHECK(rv); |
privet_resolution_ = privet_http_factory_->CreatePrivetHTTP( |
- current_register_device_, |
- device_descriptions_[current_register_device_].address, |
+ device, |
+ device_descriptions_[device].address, |
base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP, |
- base::Unretained(this), user)); |
+ base::Unretained(this))); |
privet_resolution_->Start(); |
} |
@@ -145,11 +137,43 @@ void LocalDiscoveryUIHandler::HandleCancelRegistration( |
ResetCurrentRegistration(); |
} |
+void LocalDiscoveryUIHandler::HandleRequestPrinterList( |
+ const base::ListValue* args) { |
+ Profile* profile = Profile::FromWebUI(web_ui()); |
+ OAuth2TokenService* token_service = |
+ ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
+ |
+ cloud_print_printer_list_.reset(new CloudPrintPrinterList( |
+ profile->GetRequestContext(), |
+ GetCloudPrintBaseUrl(), |
+ token_service, |
+ this)); |
+ cloud_print_printer_list_->Start(); |
+} |
+ |
+void LocalDiscoveryUIHandler::HandleOpenCloudPrintURL( |
+ const base::ListValue* args) { |
+ std::string url; |
+ bool rv = args->GetString(0, &url); |
+ DCHECK(rv); |
+ |
+ GURL url_full(GetCloudPrintBaseUrl() + url); |
+ |
+ Browser* browser = chrome::FindBrowserWithWebContents( |
+ web_ui()->GetWebContents()); |
+ DCHECK(browser); |
+ |
+ chrome::AddSelectedTabWithURL(browser, |
+ url_full, |
+ content::PAGE_TRANSITION_FROM_API); |
+} |
+ |
void LocalDiscoveryUIHandler::StartRegisterHTTP( |
- const std::string& user, |
scoped_ptr<PrivetHTTPClient> http_client) { |
current_http_client_.swap(http_client); |
+ std::string user = GetSyncAccount(); |
+ |
if (!current_http_client_) { |
SendRegisterError(); |
return; |
@@ -165,13 +189,13 @@ void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
const std::string& token, |
const GURL& url) { |
web_ui()->CallJavascriptFunction( |
- "local_discovery.registrationConfirmedOnPrinter"); |
+ "local_discovery.onRegistrationConfirmedOnPrinter"); |
if (device_descriptions_.count(current_http_client_->GetName()) == 0) { |
SendRegisterError(); |
return; |
} |
- std::string base_url = GetCloudPrintBaseUrl(current_http_client_->GetName()); |
+ std::string base_url = GetCloudPrintBaseUrl(); |
GURL automated_claim_url(base::StringPrintf( |
kPrivetAutomatedClaimURLFormat, |
@@ -180,38 +204,21 @@ void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
Profile* profile = Profile::FromWebUI(web_ui()); |
- if (current_register_user_index_ == kAccountIndexUseOAuth2) { |
- OAuth2TokenService* token_service = |
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
+ OAuth2TokenService* token_service = |
+ ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
- if (!token_service) { |
- SendRegisterError(); |
- return; |
- } |
- |
- confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( |
- profile->GetRequestContext(), |
- token_service, |
- automated_claim_url, |
- base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
- base::Unretained(this)))); |
- confirm_api_call_flow_->Start(); |
- } else { |
- if (current_register_user_index_ == 0) { |
- StartCookieConfirmFlow(current_register_user_index_, |
- xsrf_token_for_primary_user_, |
- automated_claim_url); |
- } else { |
- cloud_print_account_manager_.reset(new CloudPrintAccountManager( |
- Profile::FromWebUI(web_ui())->GetRequestContext(), |
- base_url, |
- current_register_user_index_, |
- base::Bind(&LocalDiscoveryUIHandler::OnXSRFTokenForSecondaryAccount, |
- base::Unretained(this), automated_claim_url))); |
- |
- cloud_print_account_manager_->Start(); |
- } |
+ if (!token_service) { |
+ SendRegisterError(); |
+ return; |
} |
+ |
+ confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( |
+ profile->GetRequestContext(), |
+ token_service, |
+ automated_claim_url, |
+ base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
+ base::Unretained(this)))); |
+ confirm_api_call_flow_->Start(); |
} |
void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
@@ -227,10 +234,31 @@ void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
void LocalDiscoveryUIHandler::OnPrivetRegisterDone( |
PrivetRegisterOperation* operation, |
const std::string& device_id) { |
+ std::string name = operation->GetHTTPClient()->GetName(); |
+ |
current_register_operation_.reset(); |
current_http_client_.reset(); |
- SendRegisterDone(); |
+ DeviceDescriptionMap::iterator found = device_descriptions_.find(name); |
+ |
+ if (found == device_descriptions_.end() || found->second.id.empty()) { |
+ new_register_device_ = name; |
+ registration_announce_timeout_.Reset(base::Bind( |
+ &LocalDiscoveryUIHandler::OnAnnouncementTimeoutReached, |
+ base::Unretained(this))); |
+ |
+ base::MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ registration_announce_timeout_.callback(), |
+ base::TimeDelta::FromSeconds(kRegistrationAnnouncementTimeoutSeconds)); |
+ } |
+} |
+ |
+void LocalDiscoveryUIHandler::OnAnnouncementTimeoutReached() { |
+ new_register_device_.clear(); |
+ registration_announce_timeout_.Cancel(); |
+ |
+ SendRegisterError(); |
} |
void LocalDiscoveryUIHandler::OnConfirmDone( |
@@ -253,15 +281,26 @@ void LocalDiscoveryUIHandler::DeviceChanged( |
base::DictionaryValue info; |
base::StringValue service_name(name); |
+ scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); |
- info.SetString("service_name", name); |
- info.SetString("human_readable_name", description.name); |
- info.SetString("description", description.description); |
- info.SetBoolean("registered", !description.id.empty()); |
- info.SetBoolean("is_mine", !description.id.empty()); |
+ if (description.id.empty()) { |
+ info.SetString("service_name", name); |
+ info.SetString("human_readable_name", description.name); |
+ info.SetString("description", description.description); |
- web_ui()->CallJavascriptFunction("local_discovery.onDeviceUpdate", |
- service_name, info); |
+ web_ui()->CallJavascriptFunction( |
+ "local_discovery.onUnregisteredDeviceUpdate", |
+ service_name, info); |
+ } else { |
+ web_ui()->CallJavascriptFunction( |
+ "local_discovery.onUnregisteredDeviceUpdate", |
+ service_name, *null_value); |
+ |
+ if (name == new_register_device_) { |
+ new_register_device_.clear(); |
+ SendRegisterDone(); |
+ } |
+ } |
} |
void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
@@ -269,56 +308,50 @@ void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); |
base::StringValue name_value(name); |
- web_ui()->CallJavascriptFunction("local_discovery.onDeviceUpdate", |
+ web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate", |
name_value, *null_value); |
} |
-void LocalDiscoveryUIHandler::SendRegisterError() { |
- web_ui()->CallJavascriptFunction("local_discovery.registrationFailed"); |
-} |
- |
-void LocalDiscoveryUIHandler::SendRegisterDone() { |
- web_ui()->CallJavascriptFunction("local_discovery.registrationSuccess"); |
-} |
- |
-void LocalDiscoveryUIHandler::OnCloudPrintAccountsResolved( |
- const std::vector<std::string>& accounts, |
- const std::string& xsrf_token) { |
- xsrf_token_for_primary_user_ = xsrf_token; |
- |
- std::string sync_account = GetSyncAccount(); |
- base::ListValue accounts_annotated_list; |
+void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() { |
+ base::ListValue printer_object_list; |
+ std::set<std::string> local_ids; |
+ |
+ for (DeviceDescriptionMap::iterator i = device_descriptions_.begin(); |
+ i != device_descriptions_.end(); |
+ i++) { |
+ std::string device_id = i->second.id; |
+ if (!device_id.empty()) { |
+ const CloudPrintPrinterList::PrinterDetails* details = |
+ cloud_print_printer_list_->GetDetailsFor(device_id); |
+ |
+ if (details) { |
+ local_ids.insert(device_id); |
+ printer_object_list.Append(CreatePrinterInfo(*details).release()); |
+ } |
+ } |
+ } |
- if (!sync_account.empty()) { |
- scoped_ptr<base::ListValue> account_annotated(new base::ListValue); |
- account_annotated->AppendInteger(kAccountIndexUseOAuth2); |
- account_annotated->AppendString(sync_account); |
- accounts_annotated_list.Append(account_annotated.release()); |
+ for (CloudPrintPrinterList::iterator i = cloud_print_printer_list_->begin(); |
+ i != cloud_print_printer_list_->end(); i++) { |
+ if (local_ids.count(i->id) == 0) { |
+ printer_object_list.Append(CreatePrinterInfo(*i).release()); |
+ } |
} |
- int account_index = 0; |
- for (std::vector<std::string>::const_iterator i = accounts.begin(); |
- i != accounts.end(); i++, account_index++) { |
- if (*i == sync_account) continue; |
+ web_ui()->CallJavascriptFunction( |
+ "local_discovery.onCloudDeviceListAvailable", printer_object_list); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnCloudPrintPrinterListUnavailable() { |
+} |
- scoped_ptr<base::ListValue> account_annotated(new base::ListValue); |
- account_annotated->AppendInteger(account_index); |
- account_annotated->AppendString(*i); |
- accounts_annotated_list.Append(account_annotated.release()); |
- } |
- base::StringValue device_name(current_register_device_); |
- web_ui()->CallJavascriptFunction("local_discovery.requestUser", |
- accounts_annotated_list, device_name); |
+void LocalDiscoveryUIHandler::SendRegisterError() { |
+ web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed"); |
} |
-void LocalDiscoveryUIHandler::OnXSRFTokenForSecondaryAccount( |
- const GURL& automated_claim_url, |
- const std::vector<std::string>& accounts, |
- const std::string& xsrf_token) { |
- StartCookieConfirmFlow(current_register_user_index_, |
- xsrf_token, |
- automated_claim_url); |
+void LocalDiscoveryUIHandler::SendRegisterDone() { |
+ web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess"); |
} |
void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { |
@@ -340,29 +373,14 @@ std::string LocalDiscoveryUIHandler::GetSyncAccount() { |
return signin_manager->GetAuthenticatedUsername(); |
} |
-const std::string& LocalDiscoveryUIHandler::GetCloudPrintBaseUrl( |
- const std::string& device_name) { |
- return device_descriptions_[device_name].url; |
-} |
+std::string LocalDiscoveryUIHandler::GetCloudPrintBaseUrl() { |
+ CloudPrintURL cloud_print_url(Profile::FromWebUI(web_ui())); |
-void LocalDiscoveryUIHandler::StartCookieConfirmFlow( |
- int user_index, |
- const std::string& xsrf_token, |
- const GURL& automated_claim_url) { |
- confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( |
- Profile::FromWebUI(web_ui())->GetRequestContext(), |
- user_index, |
- xsrf_token, |
- automated_claim_url, |
- base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
- base::Unretained(this)))); |
- |
- confirm_api_call_flow_->Start(); |
+ return cloud_print_url.GetCloudPrintServiceURL().spec(); |
} |
// TODO(noamsml): Create master object for registration flow. |
void LocalDiscoveryUIHandler::ResetCurrentRegistration() { |
- current_register_device_.clear(); |
if (current_register_operation_.get()) { |
current_register_operation_->Cancel(); |
current_register_operation_.reset(); |
@@ -370,10 +388,18 @@ void LocalDiscoveryUIHandler::ResetCurrentRegistration() { |
confirm_api_call_flow_.reset(); |
privet_resolution_.reset(); |
- cloud_print_account_manager_.reset(); |
- xsrf_token_for_primary_user_.clear(); |
- current_register_user_index_ = 0; |
current_http_client_.reset(); |
} |
+scoped_ptr<base::DictionaryValue> LocalDiscoveryUIHandler::CreatePrinterInfo( |
+ const CloudPrintPrinterList::PrinterDetails& description) { |
+ scoped_ptr<base::DictionaryValue> return_value(new base::DictionaryValue); |
+ |
+ return_value->SetString("id", description.id); |
+ return_value->SetString("display_name", description.display_name); |
+ return_value->SetString("description", description.description); |
+ |
+ return return_value.Pass(); |
+} |
+ |
} // namespace local_discovery |