1// Copyright 2013 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_service_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
13namespace {
14
15std::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}  // namespace
36
37namespace cloud_print {
38
39GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
40                              const std::string& job_id,
41                              PrintJobStatus status,
42                              int connector_code) {
43  return GetUrlForJobStatusUpdate(cloud_print_server_url,
44                                  job_id,
45                                  StringFromJobStatus(status),
46                                  connector_code);
47}
48
49GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
50                              const std::string& job_id,
51                              const PrintJobDetails& details) {
52  std::string status_string = StringFromJobStatus(details.status);
53  std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
54  GURL::Replacements replacements;
55  replacements.SetPathStr(path);
56  std::string query =
57      base::StringPrintf("jobid=%s&status=%s&code=%d&message=%s"
58                         "&numpages=%d&pagesprinted=%d",
59                         job_id.c_str(),
60                         status_string.c_str(),
61                         details.platform_status_flags,
62                         details.status_message.c_str(),
63                         details.total_pages,
64                         details.pages_printed);
65  replacements.SetQueryStr(query);
66  return cloud_print_server_url.ReplaceComponents(replacements);
67}
68
69std::string GetHashOfPrinterInfo(
70    const printing::PrinterBasicInfo& printer_info) {
71  return GetHashOfPrinterTags(printer_info.options);
72}
73
74std::string GetPostDataForPrinterInfo(
75    const printing::PrinterBasicInfo& printer_info,
76    const std::string& mime_boundary) {
77  return GetPostDataForPrinterTags(
78      printer_info.options,
79      mime_boundary,
80      kCloudPrintServiceProxyTagPrefix,
81      kCloudPrintServiceTagsHashTagName);
82}
83
84bool IsDryRunJob(const std::vector<std::string>& tags) {
85  return std::find(tags.begin(), tags.end(),
86                   std::string(kCloudPrintServiceTagDryRunFlag)) != tags.end();
87}
88
89std::string GetCloudPrintAuthHeaderFromStore() {
90  CloudPrintTokenStore* token_store = CloudPrintTokenStore::current();
91  if (!token_store || token_store->token().empty()) {
92    // Using LOG here for critical errors. GCP connector may run in the headless
93    // mode and error indication might be useful for user in that case.
94    LOG(ERROR) << "CP_PROXY: Missing OAuth token for request";
95    return std::string();
96  }
97  return GetCloudPrintAuthHeader(token_store->token());
98}
99
100}  // namespace cloud_print
101