Commit 7b3b2fa4 authored by noamsml@chromium.org's avatar noamsml@chromium.org

Initial support for /privet/printer/createjob

Support /privet/printer/createjob. Currently does not correctly fall back for
all error conditions. Also, simplify PrivetLocalPrinterOperation by removing the
callback for current request (since it was used only by the retry system, which
was moved to PrivetURLFetcher).

BUG=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233451 0039d316-1c4b-4281-b951-d872f2087c98
parent 4ce59755
......@@ -25,12 +25,15 @@ const char kPrivetURLValueOffline[] = "1";
const char kPrivetContentTypePDF[] = "application/pdf";
const char kPrivetContentTypePWGRaster[] = "image/pwg-raster";
const char kPrivetContentTypeCJT[] = "application/json";
const char kPrivetCDDKeySupportedContentTypes[] =
"printer.supported_content_type";
const char kPrivetCDDKeyContentType[] = "content_type";
const char kPrivetKeyJobID[] = "job_id";
const int kPrivetCancelationTimeoutSeconds = 3;
GURL CreatePrivetURL(const std::string& path) {
......@@ -365,10 +368,6 @@ PrivetLocalPrintOperationImpl::~PrivetLocalPrintOperationImpl() {
void PrivetLocalPrintOperationImpl::Start() {
DCHECK(!started_);
current_request_ =
base::Bind(&PrivetLocalPrintOperationImpl::StartInitialRequest,
base::Unretained(this));
// We need to get the /info response so we can know which APIs are available.
// TODO(noamsml): Use cached info when available.
info_operation_ = privet_client_->CreateInfoOperation(this);
......@@ -377,11 +376,6 @@ void PrivetLocalPrintOperationImpl::Start() {
started_ = true;
}
void PrivetLocalPrintOperationImpl::StartCurrentRequest() {
DCHECK(!current_request_.is_null());
current_request_.Run();
}
void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
PrivetInfoOperation* operation,
int http_code,
......@@ -411,7 +405,7 @@ void PrivetLocalPrintOperationImpl::OnPrivetInfoDone(
return;
}
StartCurrentRequest();
StartInitialRequest();
} else {
delegate_->OnPrivetPrintingError(this, http_code);
}
......@@ -430,7 +424,7 @@ void PrivetLocalPrintOperationImpl::StartInitialRequest() {
void PrivetLocalPrintOperationImpl::GetCapabilities() {
current_response_ = base::Bind(
&PrivetLocalPrintOperationImpl::OnCapabilities,
&PrivetLocalPrintOperationImpl::OnCapabilitiesResponse,
base::Unretained(this));
url_fetcher_= privet_client_->CreateURLFetcher(
......@@ -440,6 +434,18 @@ void PrivetLocalPrintOperationImpl::GetCapabilities() {
url_fetcher_->Start();
}
void PrivetLocalPrintOperationImpl::DoCreatejob() {
current_response_ = base::Bind(
&PrivetLocalPrintOperationImpl::OnCreatejobResponse,
base::Unretained(this));
url_fetcher_= privet_client_->CreateURLFetcher(
CreatePrivetURL(kPrivetCreatejobPath), net::URLFetcher::POST, this);
url_fetcher_->SetUploadData(kPrivetContentTypeCJT, ticket_);
url_fetcher_->Start();
}
void PrivetLocalPrintOperationImpl::DoSubmitdoc() {
current_response_ = base::Bind(
&PrivetLocalPrintOperationImpl::OnSubmitdocResponse,
......@@ -459,6 +465,12 @@ void PrivetLocalPrintOperationImpl::DoSubmitdoc() {
jobname_);
}
if (!jobid_.empty()) {
url = net::AppendQueryParameter(url,
kPrivetKeyJobID,
jobid_);
}
if (offline_) {
url = net::AppendQueryParameter(url,
kPrivetURLKeyOffline,
......@@ -476,7 +488,7 @@ void PrivetLocalPrintOperationImpl::DoSubmitdoc() {
url_fetcher_->Start();
}
void PrivetLocalPrintOperationImpl::OnCapabilities(
void PrivetLocalPrintOperationImpl::OnCapabilitiesResponse(
const base::DictionaryValue* value) {
const base::ListValue* supported_content_types;
use_pdf_ = false;
......@@ -511,6 +523,15 @@ void PrivetLocalPrintOperationImpl::OnSubmitdocResponse(
delegate_->OnPrivetPrintingDone(this);
}
void PrivetLocalPrintOperationImpl::OnCreatejobResponse(
const base::DictionaryValue* value) {
// Try to get job ID from value. If not, jobid_ will be empty and we will use
// simple printing.
value->GetString(kPrivetKeyJobID, &jobid_);
DoSubmitdoc();
}
PrivetHTTPClient* PrivetLocalPrintOperationImpl::GetHTTPClient() {
return privet_client_;
}
......@@ -543,11 +564,12 @@ void PrivetLocalPrintOperationImpl::OnNeedPrivetToken(
void PrivetLocalPrintOperationImpl::SendData(const std::string& data) {
DCHECK(started_);
data_ = data;
current_request_ = base::Bind(
&PrivetLocalPrintOperationImpl::DoSubmitdoc,
base::Unretained(this));
StartCurrentRequest();
if (has_extended_workflow_ && !ticket_.empty()) {
DoCreatejob();
} else {
DoSubmitdoc();
}
}
void PrivetLocalPrintOperationImpl::SetTicket(const std::string& ticket) {
......
......@@ -183,20 +183,18 @@ class PrivetLocalPrintOperationImpl
typedef base::Callback<void(const base::DictionaryValue* value)>
ResponseCallback;
void StartCurrentRequest();
void StartInitialRequest();
void GetCapabilities();
// TODO(noamsml): void DoCreatejob();
void DoCreatejob();
void DoSubmitdoc();
void OnCapabilities(const base::DictionaryValue* value);
void OnCapabilitiesResponse(const base::DictionaryValue* value);
void OnSubmitdocResponse(const base::DictionaryValue* value);
void OnCreatejobResponse(const base::DictionaryValue* value);
PrivetHTTPClientImpl* privet_client_;
PrivetLocalPrintOperation::Delegate* delegate_;
base::Closure current_request_;
ResponseCallback current_response_;
std::string ticket_;
......@@ -211,6 +209,8 @@ class PrivetLocalPrintOperationImpl
std::string user_;
std::string jobname_;
std::string jobid_;
scoped_ptr<PrivetURLFetcher> url_fetcher_;
scoped_ptr<PrivetInfoOperation> info_operation_;
};
......
......@@ -73,6 +73,33 @@ const char kSampleInfoResponseRegistered[] = "{"
" ]"
"}";
const char kSampleInfoResponseWithCreatejob[] = "{"
" \"version\": \"1.0\","
" \"name\": \"Common printer\","
" \"description\": \"Printer connected through Chrome connector\","
" \"url\": \"https://www.google.com/cloudprint\","
" \"type\": ["
" \"printer\""
" ],"
" \"id\": \"\","
" \"device_state\": \"idle\","
" \"connection_state\": \"online\","
" \"manufacturer\": \"Google\","
" \"model\": \"Google Chrome\","
" \"serial_number\": \"1111-22222-33333-4444\","
" \"firmware\": \"24.0.1312.52\","
" \"uptime\": 600,"
" \"setup_url\": \"http://support.google.com/\","
" \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
" \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
" \"x-privet-token\": \"SampleTokenForTesting\","
" \"api\": ["
" \"/privet/accesstoken\","
" \"/privet/capabilities\","
" \"/privet/printer/createjob\","
" \"/privet/printer/submitdoc\","
" ]"
"}";
const char kSampleRegisterStartResponse[] = "{"
"\"user\": \"example@google.com\","
......@@ -135,6 +162,8 @@ const char kSampleCapabilitiesResponsePWGOnly[] = "{"
"}"
"}";
const char kSampleCreatejobResponse[] = "{ \"job_id\": \"1234\" }";
class MockTestURLFetcherFactoryDelegate
: public net::TestURLFetcher::DelegateForTests {
public:
......@@ -790,6 +819,40 @@ TEST_F(PrivetLocalPrintTest, SuccessfulPWGLocalPrint) {
kSampleLocalPrintResponse));
};
TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) {
local_print_operation_->SetUsername("sample@gmail.com");
local_print_operation_->SetJobname("Sample job name");
local_print_operation_->SetTicket("Sample print ticket");
local_print_operation_->Start();
EXPECT_TRUE(SuccessfulResponseToURL(
GURL("http://10.0.0.8:6006/privet/info"),
kSampleInfoResponseWithCreatejob));
EXPECT_CALL(local_print_delegate_, OnPrivetPrintingRequestPDFInternal());
EXPECT_TRUE(SuccessfulResponseToURL(
GURL("http://10.0.0.8:6006/privet/capabilities"),
kSampleCapabilitiesResponse));
local_print_operation_->SendData("Sample print data");
EXPECT_TRUE(SuccessfulResponseToURLAndData(
GURL("http://10.0.0.8:6006/privet/printer/createjob"),
"Sample print ticket",
kSampleCreatejobResponse));
EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal());
// TODO(noamsml): Is encoding spaces as pluses standard?
EXPECT_TRUE(SuccessfulResponseToURLAndData(
GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
"user=sample%40gmail.com&jobname=Sample+job+name&job_id=1234"),
"Sample print data",
kSampleLocalPrintResponse));
};
} // namespace
} // namespace local_discovery
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