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(
GURL get_authcode_url = GetUrlForGetAuthCode(cloud_print_server_url_,
oauth_client_info_.client_id,
proxy_id_);
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(get_authcode_url,
this,
kCloudPrintAuthMaxRetryCount,
......
......@@ -289,7 +289,7 @@ void CloudPrintConnector::StartGetRequest(const GURL& url,
int max_retries,
ResponseHandler handler) {
next_response_handler_ = handler;
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(url, this, max_retries, std::string());
}
......@@ -299,7 +299,7 @@ void CloudPrintConnector::StartPostRequest(const GURL& url,
const std::string& post_data,
ResponseHandler handler) {
next_response_handler_ = handler;
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartPostRequest(
url, this, max_retries, mime_type, post_data, std::string());
}
......@@ -318,7 +318,7 @@ void CloudPrintConnector::ReportUserMessage(const std::string& message_id,
post_data.append("--" + mime_boundary + "--\r\n");
std::string mime_type("multipart/form-data; 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,
std::string());
}
......
......@@ -20,6 +20,25 @@
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::Delegate::HandleRawResponse(
const net::URLFetcher* source,
......
......@@ -25,6 +25,13 @@ class URLRequestStatus;
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
// 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
......@@ -92,7 +99,9 @@ class CloudPrintURLFetcher
protected:
virtual ~Delegate() {}
};
CloudPrintURLFetcher();
static CloudPrintURLFetcher* Create();
static void set_factory(CloudPrintURLFetcherFactory* factory);
bool IsSameRequest(const net::URLFetcher* source);
......@@ -111,6 +120,7 @@ class CloudPrintURLFetcher
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
protected:
CloudPrintURLFetcher();
friend class base::RefCountedThreadSafe<CloudPrintURLFetcher>;
virtual ~CloudPrintURLFetcher();
......@@ -126,6 +136,7 @@ class CloudPrintURLFetcher
const std::string& post_data,
const std::string& additional_headers);
void SetupRequestHeaders();
static CloudPrintURLFetcherFactory* factory();
scoped_ptr<net::URLFetcher> request_;
Delegate* delegate_;
......
......@@ -38,7 +38,7 @@ void CloudPrintWipeout::UnregisterNextPrinter() {
GURL url = GetUrlForPrinterDelete(cloud_print_server_url_,
printer_id,
"connector_disabled");
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(url, this, kMaxWipeoutAttempts, std::string());
}
......
......@@ -58,7 +58,7 @@ void JobStatusUpdater::UpdateStatus() {
}
}
if (need_update) {
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(
GetUrlForJobStatusUpdate(
cloud_print_server_url_, job_id_, last_job_details_),
......
......@@ -281,7 +281,7 @@ PrinterJobHandler::HandleJobMetadataResponse(
}
}
SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse);
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(GURL(print_ticket_url.c_str()),
this,
kCloudPrintAPIMaxRetryCount,
......@@ -308,7 +308,7 @@ PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source,
if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) {
job_details_.print_ticket_ = data;
SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse);
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
std::string accept_headers = "Accept: ";
accept_headers += print_system_->GetSupportedMimeTypes();
request_->StartGetRequest(GURL(print_data_url_.c_str()),
......@@ -422,7 +422,7 @@ void PrinterJobHandler::Start() {
job_check_pending_ = false;
// We need to fetch any pending jobs for this printer
SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse);
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(
GetUrlForJobFetch(
cloud_print_server_url_, printer_info_cloud_.printer_id,
......@@ -504,7 +504,7 @@ void PrinterJobHandler::UpdateJobStatus(PrintJobStatus status,
SetNextJSONHandler(
&PrinterJobHandler::HandleFailureStatusUpdateResponse);
}
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_,
job_details_.job_id_,
status),
......@@ -652,7 +652,7 @@ void PrinterJobHandler::OnReceivePrinterCaps(
std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary;
SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse);
request_ = new CloudPrintURLFetcher;
request_ = CloudPrintURLFetcher::Create();
request_->StartPostRequest(
GetUrlForPrinterUpdate(
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