Added GetURL, GetOAuthScope, GetExtraRequestHeaders into GCDBaseApiFlow::Delegate.

TBR=noamsml
NOTRY=True

Review URL: https://codereview.chromium.org/298883012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272933 0039d316-1c4b-4281-b951-d872f2087c98
parent 7b4783cf
......@@ -15,12 +15,10 @@ CloudDeviceList::CloudDeviceList(net::URLRequestContextGetter* request_context,
OAuth2TokenService* token_service,
const std::string& account_id,
CloudDeviceListDelegate* delegate)
: request_context_(request_context),
delegate_(delegate),
api_flow_(request_context_,
: delegate_(delegate),
api_flow_(request_context,
token_service,
account_id,
cloud_devices::GetCloudDevicesRelativeURL("devices"),
this) {
}
......@@ -62,8 +60,8 @@ void CloudDeviceList::OnGCDAPIFlowComplete(GCDBaseApiFlow* flow,
delegate_->OnDeviceListReady();
}
bool CloudDeviceList::GCDIsCloudPrint() {
return false;
GURL CloudDeviceList::GetURL() {
return cloud_devices::GetCloudDevicesRelativeURL("devices");
}
bool CloudDeviceList::FillDeviceDetails(
......
......@@ -33,7 +33,7 @@ class CloudDeviceList : public GCDBaseApiFlow::Delegate {
GCDBaseApiFlow* flow,
const base::DictionaryValue* value) OVERRIDE;
virtual bool GCDIsCloudPrint() OVERRIDE;
virtual GURL GetURL() OVERRIDE;
GCDBaseApiFlow* GetOAuth2ApiFlowForTests() { return &api_flow_; }
......@@ -45,7 +45,6 @@ class CloudDeviceList : public GCDBaseApiFlow::Delegate {
bool FillDeviceDetails(const base::DictionaryValue* value,
CloudDeviceListDelegate::Device* device);
scoped_refptr<net::URLRequestContextGetter> request_context_;
DeviceList device_list_;
CloudDeviceListDelegate* delegate_;
GCDBaseApiFlow api_flow_;
......
......@@ -17,12 +17,10 @@ CloudPrintPrinterList::CloudPrintPrinterList(
OAuth2TokenService* token_service,
const std::string& account_id,
CloudDeviceListDelegate* delegate)
: request_context_(request_context),
delegate_(delegate),
api_flow_(request_context_,
: delegate_(delegate),
api_flow_(request_context,
token_service,
account_id,
cloud_devices::GetCloudPrintRelativeURL("search"),
this) {
}
......@@ -66,7 +64,9 @@ void CloudPrintPrinterList::OnGCDAPIFlowComplete(
delegate_->OnDeviceListReady();
}
bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; }
GURL CloudPrintPrinterList::GetURL() {
return cloud_devices::GetCloudPrintRelativeURL("search");
}
bool CloudPrintPrinterList::FillPrinterDetails(
const base::DictionaryValue* printer_value,
......
......@@ -13,7 +13,7 @@
namespace local_discovery {
class CloudPrintPrinterList : public GCDBaseApiFlow::Delegate {
class CloudPrintPrinterList : public CloudPrintApiFlowDelegate {
public:
typedef std::vector<CloudDeviceListDelegate::Device> PrinterList;
typedef PrinterList::const_iterator iterator;
......@@ -33,7 +33,7 @@ class CloudPrintPrinterList : public GCDBaseApiFlow::Delegate {
GCDBaseApiFlow* flow,
const base::DictionaryValue* value) OVERRIDE;
virtual bool GCDIsCloudPrint() OVERRIDE;
virtual GURL GetURL() OVERRIDE;
GCDBaseApiFlow* GetOAuth2ApiFlowForTests() { return &api_flow_; }
......@@ -45,7 +45,6 @@ class CloudPrintPrinterList : public GCDBaseApiFlow::Delegate {
bool FillPrinterDetails(const base::DictionaryValue* printer_value,
CloudDeviceListDelegate::Device* printer_details);
scoped_refptr<net::URLRequestContextGetter> request_context_;
PrinterList printer_list_;
CloudDeviceListDelegate* delegate_;
GCDBaseApiFlow api_flow_;
......
......@@ -23,10 +23,18 @@ namespace {
const char kCloudPrintOAuthHeaderFormat[] = "Authorization: Bearer %s";
}
std::string GCDBaseApiFlow::Delegate::GetOAuthScope() {
return cloud_devices::kCloudDevicesAuthScope;
}
net::URLFetcher::RequestType GCDBaseApiFlow::Delegate::GetRequestType() {
return net::URLFetcher::GET;
}
std::vector<std::string> GCDBaseApiFlow::Delegate::GetExtraRequestHeaders() {
return std::vector<std::string>();
}
void GCDBaseApiFlow::Delegate::GetUploadData(std::string* upload_type,
std::string* upload_data) {
*upload_type = std::string();
......@@ -36,13 +44,11 @@ void GCDBaseApiFlow::Delegate::GetUploadData(std::string* upload_type,
GCDBaseApiFlow::GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
OAuth2TokenService* token_service,
const std::string& account_id,
const GURL& url,
Delegate* delegate)
: OAuth2TokenService::Consumer("cloud_print"),
request_context_(request_context),
token_service_(token_service),
account_id_(account_id),
url_(url),
delegate_(delegate) {
}
......@@ -50,11 +56,7 @@ GCDBaseApiFlow::~GCDBaseApiFlow() {}
void GCDBaseApiFlow::Start() {
OAuth2TokenService::ScopeSet oauth_scopes;
if (delegate_->GCDIsCloudPrint()) {
oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope);
} else {
oauth_scopes.insert(cloud_devices::kCloudDevicesAuthScope);
}
oauth_scopes.insert(delegate_->GetOAuthScope());
oauth_request_ =
token_service_->StartRequest(account_id_, oauth_scopes, this);
}
......@@ -63,7 +65,7 @@ void GCDBaseApiFlow::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
CreateRequest(url_);
CreateRequest(delegate_->GetURL());
std::string authorization_header =
base::StringPrintf(kCloudPrintOAuthHeaderFormat, access_token.c_str());
......@@ -94,10 +96,9 @@ void GCDBaseApiFlow::CreateRequest(const GURL& url) {
url_fetcher_->SetRequestContext(request_context_.get());
if (delegate_->GCDIsCloudPrint()) {
url_fetcher_->AddExtraRequestHeader(
cloud_print::kChromeCloudPrintProxyHeader);
}
std::vector<std::string> extra_headers = delegate_->GetExtraRequestHeaders();
for (size_t i = 0; i < extra_headers.size(); ++i)
url_fetcher_->AddExtraRequestHeader(extra_headers[i]);
}
void GCDBaseApiFlow::OnURLFetchComplete(const net::URLFetcher* source) {
......@@ -130,4 +131,16 @@ void GCDBaseApiFlow::OnURLFetchComplete(const net::URLFetcher* source) {
delegate_->OnGCDAPIFlowComplete(this, dictionary_value);
}
CloudPrintApiFlowDelegate::CloudPrintApiFlowDelegate() {}
CloudPrintApiFlowDelegate::~CloudPrintApiFlowDelegate() {}
std::string CloudPrintApiFlowDelegate::GetOAuthScope() {
return cloud_devices::kCloudPrintAuthScope;
}
std::vector<std::string> CloudPrintApiFlowDelegate::GetExtraRequestHeaders() {
return std::vector<std::string>(1, cloud_print::kChromeCloudPrintProxyHeader);
}
} // namespace local_discovery
......@@ -39,11 +39,14 @@ class GCDBaseApiFlow : public net::URLFetcherDelegate,
virtual void OnGCDAPIFlowComplete(GCDBaseApiFlow* flow,
const base::DictionaryValue* value) = 0;
// Return 1 if flow is for a Cloud Print device
virtual bool GCDIsCloudPrint() = 0;
virtual GURL GetURL() = 0;
virtual std::string GetOAuthScope();
virtual net::URLFetcher::RequestType GetRequestType();
virtual std::vector<std::string> GetExtraRequestHeaders();
// If there is no data, set upload_type and upload_data to ""
virtual void GetUploadData(std::string* upload_type,
std::string* upload_data);
......@@ -53,21 +56,6 @@ class GCDBaseApiFlow : public net::URLFetcherDelegate,
GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
OAuth2TokenService* token_service,
const std::string& account_id,
const GURL& url,
Delegate* delegate);
// Create a cookie-based confirmation.
GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
int user_index,
const std::string& xsrf_token,
const GURL& url,
Delegate* delegate);
// Create a cookie-based confirmation with no XSRF token (for requests that
// don't need an XSRF token).
GCDBaseApiFlow(net::URLRequestContextGetter* request_context,
int user_index,
const GURL& url,
Delegate* delegate);
virtual ~GCDBaseApiFlow();
......@@ -92,8 +80,21 @@ class GCDBaseApiFlow : public net::URLFetcherDelegate,
scoped_refptr<net::URLRequestContextGetter> request_context_;
OAuth2TokenService* token_service_;
std::string account_id_;
GURL url_;
Delegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(GCDBaseApiFlow);
};
class CloudPrintApiFlowDelegate : public GCDBaseApiFlow::Delegate {
public:
CloudPrintApiFlowDelegate();
virtual ~CloudPrintApiFlowDelegate();
// GCDBaseApiFlow::Delegate implementation
virtual std::string GetOAuthScope() OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(CloudPrintApiFlowDelegate);
};
} // namespace local_discovery
......
......@@ -17,16 +17,9 @@ namespace local_discovery {
namespace {
const char kGCDAutomatedClaimUploadData[] = "{ \"userEmail\": \"me\" }";
const char kGCDKindRegistrationTicket[] = "clouddevices#registrationTicket";
GURL GetConfirmFlowUrl(bool is_cloud_print, const std::string& token) {
if (is_cloud_print) {
return net::AppendQueryParameter(
cloud_devices::GetCloudPrintRelativeURL("confirm"), "token", token);
}
return cloud_devices::GetCloudDevicesRelativeURL("registrationTickets/" +
token);
GURL GetConfirmFlowUrl(const std::string& token) {
return net::AppendQueryParameter(
cloud_devices::GetCloudPrintRelativeURL("confirm"), "token", token);
}
} // namespace
......@@ -35,16 +28,14 @@ PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow(
net::URLRequestContextGetter* request_context,
OAuth2TokenService* token_service,
const std::string& account_id,
bool is_cloud_print,
const std::string& token,
const ResponseCallback& callback)
: is_cloud_print_(is_cloud_print),
flow_(request_context,
: flow_(request_context,
token_service,
account_id,
GetConfirmFlowUrl(is_cloud_print, token),
this),
callback_(callback) {
callback_(callback),
token_(token) {
}
PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() {
......@@ -65,15 +56,9 @@ void PrivetConfirmApiCallFlow::OnGCDAPIFlowComplete(
const base::DictionaryValue* value) {
bool success = false;
if (is_cloud_print_) {
if (!value->GetBoolean(cloud_print::kSuccessValue, &success)) {
callback_.Run(GCDBaseApiFlow::ERROR_MALFORMED_RESPONSE);
return;
}
} else {
std::string kind;
value->GetString(kGCDKeyKind, &kind);
success = (kind == kGCDKindRegistrationTicket);
if (!value->GetBoolean(cloud_print::kSuccessValue, &success)) {
callback_.Run(GCDBaseApiFlow::ERROR_MALFORMED_RESPONSE);
return;
}
if (success) {
......@@ -83,21 +68,12 @@ void PrivetConfirmApiCallFlow::OnGCDAPIFlowComplete(
}
}
bool PrivetConfirmApiCallFlow::GCDIsCloudPrint() { return is_cloud_print_; }
net::URLFetcher::RequestType PrivetConfirmApiCallFlow::GetRequestType() {
return (is_cloud_print_) ? net::URLFetcher::GET : net::URLFetcher::PATCH;
return net::URLFetcher::GET;
}
void PrivetConfirmApiCallFlow::GetUploadData(std::string* upload_type,
std::string* upload_data) {
if (is_cloud_print_) {
*upload_type = "";
*upload_data = "";
} else {
*upload_type = cloud_print::kContentTypeJSON;
*upload_data = kGCDAutomatedClaimUploadData;
}
GURL PrivetConfirmApiCallFlow::GetURL() {
return GetConfirmFlowUrl(token_);
}
} // namespace local_discovery
......@@ -14,7 +14,7 @@
namespace local_discovery {
// API call flow for server-side communication with cloudprint for registration.
class PrivetConfirmApiCallFlow : public GCDBaseApiFlow::Delegate {
class PrivetConfirmApiCallFlow : public CloudPrintApiFlowDelegate {
public:
typedef base::Callback<void(GCDBaseApiFlow::Status /*success*/)>
ResponseCallback;
......@@ -23,7 +23,6 @@ class PrivetConfirmApiCallFlow : public GCDBaseApiFlow::Delegate {
PrivetConfirmApiCallFlow(net::URLRequestContextGetter* request_context,
OAuth2TokenService* token_service_,
const std::string& account_id,
bool is_cloud_print,
const std::string& token,
const ResponseCallback& callback);
......@@ -36,17 +35,16 @@ class PrivetConfirmApiCallFlow : public GCDBaseApiFlow::Delegate {
virtual void OnGCDAPIFlowComplete(GCDBaseApiFlow* flow,
const base::DictionaryValue* value)
OVERRIDE;
virtual bool GCDIsCloudPrint() OVERRIDE;
virtual net::URLFetcher::RequestType GetRequestType() OVERRIDE;
virtual void GetUploadData(std::string* upload_type,
std::string* upload_data) OVERRIDE;
virtual GURL GetURL() OVERRIDE;
GCDBaseApiFlow* GetBaseApiFlowForTests() { return &flow_; }
private:
bool is_cloud_print_;
GCDBaseApiFlow flow_;
ResponseCallback callback_;
std::string token_;
};
} // namespace local_discovery
......
......@@ -34,11 +34,6 @@ const char kFailedConfirmResponseBadJson[] = "["
" \"success\""
"]";
const char kGCDConfirmResponse[] =
"{"
" \"kind\": \"clouddevices#registrationTicket\""
"}";
const char kAccountId[] = "account_id";
class MockableConfirmCallback {
......@@ -81,7 +76,6 @@ TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) {
PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
&token_service_,
account_id_,
true,
"SomeToken",
callback_.callback());
GCDBaseApiFlow* cloudprint_flow = confirm_flow.GetBaseApiFlowForTests();
......@@ -117,7 +111,6 @@ TEST_F(PrivetConfirmApiFlowTest, BadToken) {
PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
&token_service_,
account_id_,
true,
"SomeCloudprintToken",
callback_.callback());
......@@ -134,7 +127,6 @@ TEST_F(PrivetConfirmApiFlowTest, ServerFailure) {
PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
&token_service_,
account_id_,
true,
"SomeToken",
callback_.callback());
......@@ -162,7 +154,6 @@ TEST_F(PrivetConfirmApiFlowTest, BadJson) {
PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
&token_service_,
account_id_,
true,
"SomeToken",
callback_.callback());
......@@ -188,44 +179,6 @@ TEST_F(PrivetConfirmApiFlowTest, BadJson) {
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(PrivetConfirmApiFlowTest, SuccessGCD) {
PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
&token_service_,
account_id_,
false,
"SomeGcdToken",
callback_.callback());
GCDBaseApiFlow* gcd_flow = confirm_flow.GetBaseApiFlowForTests();
confirm_flow.Start();
gcd_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
EXPECT_EQ(GURL("https://www.googleapis.com/clouddevices/v1/"
"registrationTickets/SomeGcdToken"),
fetcher->GetOriginalURL());
EXPECT_EQ("{ \"userEmail\": \"me\" }", fetcher->upload_data());
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
std::string oauth_header;
std::string proxy;
EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header));
EXPECT_EQ("Bearer SomeToken", oauth_header);
EXPECT_FALSE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
fetcher->SetResponseString(kGCDConfirmResponse);
fetcher->set_status(
net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
fetcher->set_response_code(200);
EXPECT_CALL(callback_, ConfirmCallback(GCDBaseApiFlow::SUCCESS));
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
} // namespace
} // namespace local_discovery
......@@ -165,8 +165,8 @@ void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) {
// of a reload.
if (!privet_lister_) {
service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance();
privet_lister_.reset(new PrivetDeviceListerImpl(
service_discovery_client_.get(), this));
privet_lister_.reset(
new PrivetDeviceListerImpl(service_discovery_client_.get(), this));
privet_http_factory_ =
PrivetHTTPAsynchronousFactory::CreateInstance(
service_discovery_client_.get(), profile->GetRequestContext());
......@@ -301,10 +301,6 @@ void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
return;
}
bool is_cloud_print =
device_descriptions_[current_http_client_->GetName()].type ==
kDeviceTypePrinter;
Profile* profile = Profile::FromWebUI(web_ui());
ProfileOAuth2TokenService* token_service =
......@@ -326,7 +322,6 @@ void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
profile->GetRequestContext(),
token_service,
signin_manager->GetAuthenticatedAccountId(),
is_cloud_print,
token,
base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone,
base::Unretained(this))));
......
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