Home | History | Annotate | Download | only in cloud_print
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/service/cloud_print/cloud_print_helpers.h"
      6 
      7 #include "base/strings/stringprintf.h"
      8 #include "chrome/common/cloud_print/cloud_print_constants.h"
      9 #include "chrome/common/cloud_print/cloud_print_helpers.h"
     10 #include "chrome/service/cloud_print/cloud_print_token_store.h"
     11 #include "chrome/service/service_process.h"
     12 
     13 namespace {
     14 
     15 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) {
     16   std::string ret;
     17   switch (status) {
     18     case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS:
     19       ret = "IN_PROGRESS";
     20       break;
     21     case cloud_print::PRINT_JOB_STATUS_ERROR:
     22       ret = "ERROR";
     23       break;
     24     case cloud_print::PRINT_JOB_STATUS_COMPLETED:
     25       ret = "DONE";
     26       break;
     27     default:
     28       ret = "UNKNOWN";
     29       NOTREACHED();
     30       break;
     31   }
     32   return ret;
     33 }
     34 
     35 }
     36 
     37 namespace cloud_print {
     38 
     39 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
     40                               const std::string& job_id,
     41                               PrintJobStatus status) {
     42   return GetUrlForJobStatusUpdate(cloud_print_server_url,
     43                                   job_id,
     44                                   StringFromJobStatus(status));
     45 }
     46 
     47 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
     48                               const std::string& job_id,
     49                               const PrintJobDetails& details) {
     50   std::string status_string = StringFromJobStatus(details.status);
     51   std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
     52   GURL::Replacements replacements;
     53   replacements.SetPathStr(path);
     54   std::string query =
     55       base::StringPrintf("jobid=%s&status=%s&code=%d&message=%s"
     56                          "&numpages=%d&pagesprinted=%d",
     57                          job_id.c_str(),
     58                          status_string.c_str(),
     59                          details.platform_status_flags,
     60                          details.status_message.c_str(),
     61                          details.total_pages,
     62                          details.pages_printed);
     63   replacements.SetQueryStr(query);
     64   return cloud_print_server_url.ReplaceComponents(replacements);
     65 }
     66 
     67 std::string GetHashOfPrinterInfo(
     68     const printing::PrinterBasicInfo& printer_info) {
     69   return GetHashOfPrinterTags(printer_info.options);
     70 }
     71 
     72 std::string GetPostDataForPrinterInfo(
     73     const printing::PrinterBasicInfo& printer_info,
     74     const std::string& mime_boundary) {
     75   return GetPostDataForPrinterTags(
     76       printer_info.options,
     77       mime_boundary,
     78       kCloudPrintServiceProxyTagPrefix,
     79       kCloudPrintServiceTagsHashTagName);
     80 }
     81 
     82 bool IsDryRunJob(const std::vector<std::string>& tags) {
     83   return std::find(tags.begin(), tags.end(),
     84                    std::string(kCloudPrintServiceTagDryRunFlag)) != tags.end();
     85 }
     86 
     87 std::string GetCloudPrintAuthHeaderFromStore() {
     88   CloudPrintTokenStore* token_store = CloudPrintTokenStore::current();
     89   if (!token_store || token_store->token().empty()) {
     90     // Using LOG here for critical errors. GCP connector may run in the headless
     91     // mode and error indication might be useful for user in that case.
     92     LOG(ERROR) << "CP_PROXY: Missing OAuth token for request";
     93     return std::string();
     94   }
     95   return GetCloudPrintAuthHeader(token_store->token());
     96 }
     97 
     98 }  // namespace cloud_print
     99