Commit 785028ba authored by noamsml@chromium.org's avatar noamsml@chromium.org

Actual cancelation of registration when cancel buttons pressed

Send cancel request to printer when registration is canceled.

BUG=245375

Review URL: https://chromiumcodereview.appspot.com/23780002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220646 0039d316-1c4b-4281-b951-d872f2087c98
parent 7852aa6c
...@@ -20,6 +20,7 @@ const char kPrivetErrorInvalidXPrivetToken[] = "invalid_x_privet_token"; ...@@ -20,6 +20,7 @@ const char kPrivetErrorInvalidXPrivetToken[] = "invalid_x_privet_token";
const char kPrivetActionStart[] = "start"; const char kPrivetActionStart[] = "start";
const char kPrivetActionGetClaimToken[] = "getClaimToken"; const char kPrivetActionGetClaimToken[] = "getClaimToken";
const char kPrivetActionComplete[] = "complete"; const char kPrivetActionComplete[] = "complete";
const char kPrivetActionCancel[] = "cancel";
const char kPrivetActionNameInfo[] = "info"; const char kPrivetActionNameInfo[] = "info";
......
...@@ -21,6 +21,7 @@ extern const char kPrivetErrorInvalidXPrivetToken[]; ...@@ -21,6 +21,7 @@ extern const char kPrivetErrorInvalidXPrivetToken[];
extern const char kPrivetActionStart[]; extern const char kPrivetActionStart[];
extern const char kPrivetActionGetClaimToken[]; extern const char kPrivetActionGetClaimToken[];
extern const char kPrivetActionComplete[]; extern const char kPrivetActionComplete[];
extern const char kPrivetActionCancel[];
// Name for pseudo-action "info", used only to show info stage in errors. // Name for pseudo-action "info", used only to show info stage in errors.
extern const char kPrivetActionNameInfo[]; extern const char kPrivetActionNameInfo[];
......
...@@ -22,6 +22,8 @@ const char kPrivetInfoURLFormat[] = "http://%s:%d/privet/info"; ...@@ -22,6 +22,8 @@ const char kPrivetInfoURLFormat[] = "http://%s:%d/privet/info";
// (string) is the user name. // (string) is the user name.
const char kPrivetRegisterURLFormat[] = const char kPrivetRegisterURLFormat[] =
"http://%s:%d/privet/register?action=%s&user=%s"; "http://%s:%d/privet/register?action=%s&user=%s";
const int kPrivetCancelationTimeoutSeconds = 3;
} // namespace } // namespace
PrivetInfoOperationImpl::PrivetInfoOperationImpl( PrivetInfoOperationImpl::PrivetInfoOperationImpl(
...@@ -92,7 +94,19 @@ void PrivetRegisterOperationImpl::Start() { ...@@ -92,7 +94,19 @@ void PrivetRegisterOperationImpl::Start() {
void PrivetRegisterOperationImpl::Cancel() { void PrivetRegisterOperationImpl::Cancel() {
url_fetcher_.reset(); url_fetcher_.reset();
// TODO(noamsml): Proper cancelation.
if (ongoing_) {
// Owned by the message loop.
Cancelation* cancelation = new Cancelation(privet_client_, user_);
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PrivetRegisterOperationImpl::Cancelation::Cleanup,
base::Owned(cancelation)),
base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds));
ongoing_ = false;
}
} }
void PrivetRegisterOperationImpl::CompleteRegistration() { void PrivetRegisterOperationImpl::CompleteRegistration() {
...@@ -176,16 +190,11 @@ void PrivetRegisterOperationImpl::OnParsedJson( ...@@ -176,16 +190,11 @@ void PrivetRegisterOperationImpl::OnParsedJson(
} }
void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { void PrivetRegisterOperationImpl::SendRequest(const std::string& action) {
std::string url = base::StringPrintf( GURL url = GetURLForActionAndUser(privet_client_, action, user_);
kPrivetRegisterURLFormat,
privet_client_->host_port().host().c_str(),
privet_client_->host_port().port(),
action.c_str(),
user_.c_str());
current_action_ = action; current_action_ = action;
url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher(
GURL(url), net::URLFetcher::POST, this); url, net::URLFetcher::POST, this);
url_fetcher_->Start(); url_fetcher_->Start();
} }
...@@ -276,6 +285,49 @@ bool PrivetRegisterOperationImpl::PrivetErrorTransient( ...@@ -276,6 +285,49 @@ bool PrivetRegisterOperationImpl::PrivetErrorTransient(
(error == kPrivetErrorPendingUserAction); (error == kPrivetErrorPendingUserAction);
} }
// static
GURL PrivetRegisterOperationImpl::GetURLForActionAndUser(
PrivetHTTPClientImpl* privet_client,
const std::string& action,
const std::string& user) {
return GURL(base::StringPrintf(kPrivetRegisterURLFormat,
privet_client->host_port().host().c_str(),
privet_client->host_port().port(),
action.c_str(),
user.c_str()));
}
PrivetRegisterOperationImpl::Cancelation::Cancelation(
PrivetHTTPClientImpl* privet_client,
const std::string& user) {
GURL url = GetURLForActionAndUser(privet_client,
kPrivetActionCancel,
user);
url_fetcher_ = privet_client->fetcher_factory().CreateURLFetcher(
url, net::URLFetcher::POST, this);
url_fetcher_->Start();
}
PrivetRegisterOperationImpl::Cancelation::~Cancelation() {
}
void PrivetRegisterOperationImpl::Cancelation::OnError(
PrivetURLFetcher* fetcher,
PrivetURLFetcher::ErrorType error) {
}
void PrivetRegisterOperationImpl::Cancelation::OnParsedJson(
PrivetURLFetcher* fetcher,
const base::DictionaryValue* value,
bool has_error) {
}
void PrivetRegisterOperationImpl::Cancelation::Cleanup() {
// Nothing needs to be done, as base::Owned will delete this object,
// this callback is just here to pass ownership of the Cancelation to
// the message loop.
}
PrivetHTTPClientImpl::PrivetHTTPClientImpl( PrivetHTTPClientImpl::PrivetHTTPClientImpl(
const std::string& name, const std::string& name,
const net::HostPortPair& host_port, const net::HostPortPair& host_port,
......
...@@ -66,6 +66,25 @@ class PrivetRegisterOperationImpl ...@@ -66,6 +66,25 @@ class PrivetRegisterOperationImpl
virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE; virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
private: private:
class Cancelation : public PrivetURLFetcher::Delegate {
public:
Cancelation(PrivetHTTPClientImpl* privet_client,
const std::string& user);
virtual ~Cancelation();
virtual void OnError(PrivetURLFetcher* fetcher,
PrivetURLFetcher::ErrorType error) OVERRIDE;
virtual void OnParsedJson(PrivetURLFetcher* fetcher,
const base::DictionaryValue* value,
bool has_error) OVERRIDE;
void Cleanup();
private:
scoped_ptr<PrivetURLFetcher> url_fetcher_;
};
// Arguments is JSON value from request. // Arguments is JSON value from request.
typedef base::Callback<void(const base::DictionaryValue&)> typedef base::Callback<void(const base::DictionaryValue&)>
ResponseHandler; ResponseHandler;
...@@ -80,6 +99,11 @@ class PrivetRegisterOperationImpl ...@@ -80,6 +99,11 @@ class PrivetRegisterOperationImpl
bool PrivetErrorTransient(const std::string& error); bool PrivetErrorTransient(const std::string& error);
static GURL GetURLForActionAndUser(
PrivetHTTPClientImpl* privet_client,
const std::string& action,
const std::string& user);
std::string user_; std::string user_;
std::string current_action_; std::string current_action_;
scoped_ptr<PrivetURLFetcher> url_fetcher_; scoped_ptr<PrivetURLFetcher> url_fetcher_;
......
...@@ -75,6 +75,11 @@ const char kSampleRegisterErrorPermanent[] = ...@@ -75,6 +75,11 @@ const char kSampleRegisterErrorPermanent[] =
const char kSampleInfoResponseBadJson[] = "{"; const char kSampleInfoResponseBadJson[] = "{";
const char kSampleRegisterCancelResponse[] = "{"
"\"user\": \"example@google.com\","
"\"action\": \"cancel\""
"}";
class MockTestURLFetcherFactoryDelegate class MockTestURLFetcherFactoryDelegate
: public net::TestURLFetcher::DelegateForTests { : public net::TestURLFetcher::DelegateForTests {
public: public:
...@@ -457,6 +462,34 @@ TEST_F(PrivetRegisterTest, InfoFailure) { ...@@ -457,6 +462,34 @@ TEST_F(PrivetRegisterTest, InfoFailure) {
kSampleInfoResponseBadJson)); kSampleInfoResponseBadJson));
} }
TEST_F(PrivetRegisterTest, RegisterCancel) {
// Start with info request first to populate XSRF token.
info_operation_->Start();
EXPECT_TRUE(SuccessfulResponseToURL(
GURL("http://10.0.0.8:6006/privet/info"),
kSampleInfoResponse));
register_operation_->Start();
EXPECT_TRUE(SuccessfulResponseToURL(
GURL("http://10.0.0.8:6006/privet/register?"
"action=start&user=example@google.com"),
kSampleRegisterStartResponse));
register_operation_->Cancel();
EXPECT_TRUE(SuccessfulResponseToURL(
GURL("http://10.0.0.8:6006/privet/register?"
"action=cancel&user=example@google.com"),
kSampleRegisterCancelResponse));
// Must keep mocks alive for 3 seconds so the cancelation object can be
// deleted.
RunFor(base::TimeDelta::FromSeconds(3));
}
} // namespace } // namespace
} // namespace local_discovery } // namespace local_discovery
...@@ -147,6 +147,7 @@ cr.define('local_discovery', function() { ...@@ -147,6 +147,7 @@ cr.define('local_discovery', function() {
$('register-overlay').classList.remove('showing'); $('register-overlay').classList.remove('showing');
$('overlay').hidden = true; $('overlay').hidden = true;
uber.invokeMethodOnParent('stopInterceptingEvents'); uber.invokeMethodOnParent('stopInterceptingEvents');
chrome.send('cancelRegistration');
} }
/** /**
......
...@@ -41,6 +41,7 @@ LocalDiscoveryUIHandler::LocalDiscoveryUIHandler( ...@@ -41,6 +41,7 @@ LocalDiscoveryUIHandler::LocalDiscoveryUIHandler(
} }
LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() { LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() {
ResetCurrentRegistration();
SetIsVisible(false); SetIsVisible(false);
if (service_discovery_client_.get()) { if (service_discovery_client_.get()) {
service_discovery_client_ = NULL; service_discovery_client_ = NULL;
...@@ -77,6 +78,9 @@ void LocalDiscoveryUIHandler::RegisterMessages() { ...@@ -77,6 +78,9 @@ void LocalDiscoveryUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("chooseUser", base::Bind( web_ui()->RegisterMessageCallback("chooseUser", base::Bind(
&LocalDiscoveryUIHandler::HandleChooseUser, &LocalDiscoveryUIHandler::HandleChooseUser,
base::Unretained(this))); base::Unretained(this)));
web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind(
&LocalDiscoveryUIHandler::HandleCancelRegistration,
base::Unretained(this)));
} }
void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) {
...@@ -137,6 +141,11 @@ void LocalDiscoveryUIHandler::HandleChooseUser(const base::ListValue* args) { ...@@ -137,6 +141,11 @@ void LocalDiscoveryUIHandler::HandleChooseUser(const base::ListValue* args) {
privet_resolution_->Start(); privet_resolution_->Start();
} }
void LocalDiscoveryUIHandler::HandleCancelRegistration(
const base::ListValue* args) {
ResetCurrentRegistration();
}
void LocalDiscoveryUIHandler::StartRegisterHTTP( void LocalDiscoveryUIHandler::StartRegisterHTTP(
const std::string& user, const std::string& user,
scoped_ptr<PrivetHTTPClient> http_client) { scoped_ptr<PrivetHTTPClient> http_client) {
...@@ -353,4 +362,20 @@ void LocalDiscoveryUIHandler::StartCookieConfirmFlow( ...@@ -353,4 +362,20 @@ void LocalDiscoveryUIHandler::StartCookieConfirmFlow(
confirm_api_call_flow_->Start(); confirm_api_call_flow_->Start();
} }
// 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();
}
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();
}
} // namespace local_discovery } // namespace local_discovery
...@@ -86,6 +86,9 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler, ...@@ -86,6 +86,9 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
// For when a user choice is made. // For when a user choice is made.
void HandleChooseUser(const base::ListValue* args); void HandleChooseUser(const base::ListValue* args);
// For when a cancelation is made.
void HandleCancelRegistration(const base::ListValue* args);
// For when the IP address of the printer has been resolved for registration. // For when the IP address of the printer has been resolved for registration.
void StartRegisterHTTP( void StartRegisterHTTP(
const std::string& user, const std::string& user,
...@@ -126,6 +129,9 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler, ...@@ -126,6 +129,9 @@ class LocalDiscoveryUIHandler : public content::WebUIMessageHandler,
const std::string& xsrf_token, const std::string& xsrf_token,
const GURL& automatic_claim_url); const GURL& automatic_claim_url);
// Reset and cancel the current registration.
void ResetCurrentRegistration();
// The current HTTP client (used for the current operation). // The current HTTP client (used for the current operation).
scoped_ptr<PrivetHTTPClient> current_http_client_; scoped_ptr<PrivetHTTPClient> current_http_client_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment