summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cloud_print_helpers.cc
blob: 8479c16b1631021afaec4b5cd0c5b044cf635d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/service/cloud_print/cloud_print_helpers.h"

#include "base/strings/stringprintf.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "chrome/common/cloud_print/cloud_print_helpers.h"
#include "chrome/service/cloud_print/cloud_print_token_store.h"
#include "chrome/service/service_process.h"

namespace {

std::string StringFromJobStatus(cloud_print::PrintJobStatus status) {
  std::string ret;
  switch (status) {
    case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS:
      ret = "IN_PROGRESS";
      break;
    case cloud_print::PRINT_JOB_STATUS_ERROR:
      ret = "ERROR";
      break;
    case cloud_print::PRINT_JOB_STATUS_COMPLETED:
      ret = "DONE";
      break;
    default:
      ret = "UNKNOWN";
      NOTREACHED();
      break;
  }
  return ret;
}

}  // namespace

namespace cloud_print {

GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
                              const std::string& job_id,
                              PrintJobStatus status,
                              int connector_code) {
  return GetUrlForJobStatusUpdate(cloud_print_server_url,
                                  job_id,
                                  StringFromJobStatus(status),
                                  connector_code);
}

GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
                              const std::string& job_id,
                              const PrintJobDetails& details) {
  std::string status_string = StringFromJobStatus(details.status);
  std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
  GURL::Replacements replacements;
  replacements.SetPathStr(path);
  std::string query =
      base::StringPrintf("jobid=%s&status=%s&code=%d&message=%s"
                         "&numpages=%d&pagesprinted=%d",
                         job_id.c_str(),
                         status_string.c_str(),
                         details.platform_status_flags,
                         details.status_message.c_str(),
                         details.total_pages,
                         details.pages_printed);
  replacements.SetQueryStr(query);
  return cloud_print_server_url.ReplaceComponents(replacements);
}

std::string GetHashOfPrinterInfo(
    const printing::PrinterBasicInfo& printer_info) {
  return GetHashOfPrinterTags(printer_info.options);
}

std::string GetPostDataForPrinterInfo(
    const printing::PrinterBasicInfo& printer_info,
    const std::string& mime_boundary) {
  return GetPostDataForPrinterTags(
      printer_info.options,
      mime_boundary,
      kCloudPrintServiceProxyTagPrefix,
      kCloudPrintServiceTagsHashTagName);
}

bool IsDryRunJob(const std::vector<std::string>& tags) {
  return std::find(tags.begin(), tags.end(),
                   std::string(kCloudPrintServiceTagDryRunFlag)) != tags.end();
}

std::string GetCloudPrintAuthHeaderFromStore() {
  CloudPrintTokenStore* token_store = CloudPrintTokenStore::current();
  if (!token_store || token_store->token().empty()) {
    // Using LOG here for critical errors. GCP connector may run in the headless
    // mode and error indication might be useful for user in that case.
    LOG(ERROR) << "CP_PROXY: Missing OAuth token for request";
    return std::string();
  }
  return GetCloudPrintAuthHeader(token_store->token());
}

}  // namespace cloud_print