Commit a91bfa88 authored by noamsml@google.com's avatar noamsml@google.com

Changing printer job handler to be more testable

1. Instead of using "new" to create cloud print URL fetchers,
use protected virtual ::CreateCloudPrintURLFetcher()

2. Instead of having a private destructor, have a protected one,
allowing for sub-classing (useful for testing purposes)

BUG=


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@181997 0039d316-1c4b-4281-b951-d872f2087c98
parent 887c918b
...@@ -70,7 +70,7 @@ void CloudPrintAuth::AuthenticateWithToken( ...@@ -70,7 +70,7 @@ void CloudPrintAuth::AuthenticateWithToken(
GURL get_authcode_url = GetUrlForGetAuthCode(cloud_print_server_url_, GURL get_authcode_url = GetUrlForGetAuthCode(cloud_print_server_url_,
oauth_client_info_.client_id, oauth_client_info_.client_id,
proxy_id_); proxy_id_);
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(get_authcode_url, request_->StartGetRequest(get_authcode_url,
this, this,
kCloudPrintAuthMaxRetryCount, kCloudPrintAuthMaxRetryCount,
......
...@@ -289,7 +289,7 @@ void CloudPrintConnector::StartGetRequest(const GURL& url, ...@@ -289,7 +289,7 @@ void CloudPrintConnector::StartGetRequest(const GURL& url,
int max_retries, int max_retries,
ResponseHandler handler) { ResponseHandler handler) {
next_response_handler_ = handler; next_response_handler_ = handler;
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(url, this, max_retries, std::string()); request_->StartGetRequest(url, this, max_retries, std::string());
} }
...@@ -299,7 +299,7 @@ void CloudPrintConnector::StartPostRequest(const GURL& url, ...@@ -299,7 +299,7 @@ void CloudPrintConnector::StartPostRequest(const GURL& url,
const std::string& post_data, const std::string& post_data,
ResponseHandler handler) { ResponseHandler handler) {
next_response_handler_ = handler; next_response_handler_ = handler;
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartPostRequest( request_->StartPostRequest(
url, this, max_retries, mime_type, post_data, std::string()); url, this, max_retries, mime_type, post_data, std::string());
} }
...@@ -318,7 +318,7 @@ void CloudPrintConnector::ReportUserMessage(const std::string& message_id, ...@@ -318,7 +318,7 @@ void CloudPrintConnector::ReportUserMessage(const std::string& message_id,
post_data.append("--" + mime_boundary + "--\r\n"); post_data.append("--" + mime_boundary + "--\r\n");
std::string mime_type("multipart/form-data; boundary="); std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary; mime_type += mime_boundary;
user_message_request_ = new CloudPrintURLFetcher; user_message_request_ = CloudPrintURLFetcher::Create();
user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data, user_message_request_->StartPostRequest(url, this, 1, mime_type, post_data,
std::string()); std::string());
} }
......
...@@ -20,6 +20,25 @@ ...@@ -20,6 +20,25 @@
namespace cloud_print { namespace cloud_print {
static CloudPrintURLFetcherFactory* g_factory = NULL;
// static
CloudPrintURLFetcher* CloudPrintURLFetcher::Create() {
CloudPrintURLFetcherFactory* factory = CloudPrintURLFetcher::factory();
return factory ? factory->CreateCloudPrintURLFetcher() :
new CloudPrintURLFetcher;
}
// static
CloudPrintURLFetcherFactory* CloudPrintURLFetcher::factory() {
return g_factory;
}
// static
void CloudPrintURLFetcher::set_factory(CloudPrintURLFetcherFactory* factory) {
g_factory = factory;
}
CloudPrintURLFetcher::ResponseAction CloudPrintURLFetcher::ResponseAction
CloudPrintURLFetcher::Delegate::HandleRawResponse( CloudPrintURLFetcher::Delegate::HandleRawResponse(
const net::URLFetcher* source, const net::URLFetcher* source,
......
...@@ -25,6 +25,13 @@ class URLRequestStatus; ...@@ -25,6 +25,13 @@ class URLRequestStatus;
namespace cloud_print { namespace cloud_print {
// Factory for creating CloudPrintURLFetchers.
class CloudPrintURLFetcher;
class CloudPrintURLFetcherFactory {
public:
virtual CloudPrintURLFetcher* CreateCloudPrintURLFetcher() = 0;
};
// A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic // A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic
// only on HTTP response codes >= 500. In the cloud print case, we want to // only on HTTP response codes >= 500. In the cloud print case, we want to
// retry on all network errors. In addition, we want to treat non-JSON responses // retry on all network errors. In addition, we want to treat non-JSON responses
...@@ -92,7 +99,9 @@ class CloudPrintURLFetcher ...@@ -92,7 +99,9 @@ class CloudPrintURLFetcher
protected: protected:
virtual ~Delegate() {} virtual ~Delegate() {}
}; };
CloudPrintURLFetcher();
static CloudPrintURLFetcher* Create();
static void set_factory(CloudPrintURLFetcherFactory* factory);
bool IsSameRequest(const net::URLFetcher* source); bool IsSameRequest(const net::URLFetcher* source);
...@@ -111,6 +120,7 @@ class CloudPrintURLFetcher ...@@ -111,6 +120,7 @@ class CloudPrintURLFetcher
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
protected: protected:
CloudPrintURLFetcher();
friend class base::RefCountedThreadSafe<CloudPrintURLFetcher>; friend class base::RefCountedThreadSafe<CloudPrintURLFetcher>;
virtual ~CloudPrintURLFetcher(); virtual ~CloudPrintURLFetcher();
...@@ -126,6 +136,7 @@ class CloudPrintURLFetcher ...@@ -126,6 +136,7 @@ class CloudPrintURLFetcher
const std::string& post_data, const std::string& post_data,
const std::string& additional_headers); const std::string& additional_headers);
void SetupRequestHeaders(); void SetupRequestHeaders();
static CloudPrintURLFetcherFactory* factory();
scoped_ptr<net::URLFetcher> request_; scoped_ptr<net::URLFetcher> request_;
Delegate* delegate_; Delegate* delegate_;
......
...@@ -38,7 +38,7 @@ void CloudPrintWipeout::UnregisterNextPrinter() { ...@@ -38,7 +38,7 @@ void CloudPrintWipeout::UnregisterNextPrinter() {
GURL url = GetUrlForPrinterDelete(cloud_print_server_url_, GURL url = GetUrlForPrinterDelete(cloud_print_server_url_,
printer_id, printer_id,
"connector_disabled"); "connector_disabled");
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(url, this, kMaxWipeoutAttempts, std::string()); request_->StartGetRequest(url, this, kMaxWipeoutAttempts, std::string());
} }
......
...@@ -58,7 +58,7 @@ void JobStatusUpdater::UpdateStatus() { ...@@ -58,7 +58,7 @@ void JobStatusUpdater::UpdateStatus() {
} }
} }
if (need_update) { if (need_update) {
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest( request_->StartGetRequest(
GetUrlForJobStatusUpdate( GetUrlForJobStatusUpdate(
cloud_print_server_url_, job_id_, last_job_details_), cloud_print_server_url_, job_id_, last_job_details_),
......
...@@ -281,7 +281,7 @@ PrinterJobHandler::HandleJobMetadataResponse( ...@@ -281,7 +281,7 @@ PrinterJobHandler::HandleJobMetadataResponse(
} }
} }
SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse);
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(GURL(print_ticket_url.c_str()), request_->StartGetRequest(GURL(print_ticket_url.c_str()),
this, this,
kCloudPrintAPIMaxRetryCount, kCloudPrintAPIMaxRetryCount,
...@@ -308,7 +308,7 @@ PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source, ...@@ -308,7 +308,7 @@ PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source,
if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) {
job_details_.print_ticket_ = data; job_details_.print_ticket_ = data;
SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse);
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
std::string accept_headers = "Accept: "; std::string accept_headers = "Accept: ";
accept_headers += print_system_->GetSupportedMimeTypes(); accept_headers += print_system_->GetSupportedMimeTypes();
request_->StartGetRequest(GURL(print_data_url_.c_str()), request_->StartGetRequest(GURL(print_data_url_.c_str()),
...@@ -422,7 +422,7 @@ void PrinterJobHandler::Start() { ...@@ -422,7 +422,7 @@ void PrinterJobHandler::Start() {
job_check_pending_ = false; job_check_pending_ = false;
// We need to fetch any pending jobs for this printer // We need to fetch any pending jobs for this printer
SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse);
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest( request_->StartGetRequest(
GetUrlForJobFetch( GetUrlForJobFetch(
cloud_print_server_url_, printer_info_cloud_.printer_id, cloud_print_server_url_, printer_info_cloud_.printer_id,
...@@ -504,7 +504,7 @@ void PrinterJobHandler::UpdateJobStatus(PrintJobStatus status, ...@@ -504,7 +504,7 @@ void PrinterJobHandler::UpdateJobStatus(PrintJobStatus status,
SetNextJSONHandler( SetNextJSONHandler(
&PrinterJobHandler::HandleFailureStatusUpdateResponse); &PrinterJobHandler::HandleFailureStatusUpdateResponse);
} }
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_, request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_,
job_details_.job_id_, job_details_.job_id_,
status), status),
...@@ -652,7 +652,7 @@ void PrinterJobHandler::OnReceivePrinterCaps( ...@@ -652,7 +652,7 @@ void PrinterJobHandler::OnReceivePrinterCaps(
std::string mime_type("multipart/form-data; boundary="); std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary; mime_type += mime_boundary;
SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse);
request_ = new CloudPrintURLFetcher; request_ = CloudPrintURLFetcher::Create();
request_->StartPostRequest( request_->StartPostRequest(
GetUrlForPrinterUpdate( GetUrlForPrinterUpdate(
cloud_print_server_url_, printer_info_cloud_.printer_id), cloud_print_server_url_, printer_info_cloud_.printer_id),
......
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