Add ICU message format support
[chromium-blink-merge.git] / chrome / common / cloud_print / cloud_print_helpers.cc
blobc2e1614735120b2b88116822ff0a9570fd7ef513
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.
5 #include "chrome/common/cloud_print/cloud_print_helpers.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/md5.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/rand_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/sys_info.h"
14 #include "base/values.h"
15 #include "chrome/common/channel_info.h"
16 #include "chrome/common/cloud_print/cloud_print_constants.h"
17 #include "net/base/mime_util.h"
18 #include "url/gurl.h"
20 namespace cloud_print {
22 namespace {
24 // Returns printer tags generated from |printer_tags| and the default tags
25 // required by cloud print server.
26 PrinterTags PreparePrinterTags(const PrinterTags& printer_tags) {
27 PrinterTags printer_tags_out = printer_tags;
28 printer_tags_out[kChromeVersionTagName] = chrome::GetVersionString();
29 printer_tags_out[kSystemNameTagName] =
30 base::SysInfo::OperatingSystemName();
31 printer_tags_out[kSystemVersionTagName] =
32 base::SysInfo::OperatingSystemVersion();
33 return printer_tags_out;
36 // Returns the hash of |printer_tags|.
37 std::string HashPrinterTags(const PrinterTags& printer_tags) {
38 std::string values_list;
39 PrinterTags::const_iterator it;
40 for (it = printer_tags.begin(); it != printer_tags.end(); ++it) {
41 values_list.append(it->first);
42 values_list.append(it->second);
44 return base::MD5String(values_list);
47 } // namespace
49 std::string AppendPathToUrl(const GURL& url, const std::string& path) {
50 DCHECK_NE(path[0], '/');
51 std::string ret = url.path();
52 if (url.has_path() && (ret[ret.length() - 1] != '/'))
53 ret += '/';
54 ret += path;
55 return ret;
58 GURL GetUrlForSearch(const GURL& cloud_print_server_url) {
59 std::string path(AppendPathToUrl(cloud_print_server_url, "search"));
60 GURL::Replacements replacements;
61 replacements.SetPathStr(path);
62 return cloud_print_server_url.ReplaceComponents(replacements);
65 GURL GetUrlForSubmit(const GURL& cloud_print_server_url) {
66 std::string path(AppendPathToUrl(cloud_print_server_url, "submit"));
67 GURL::Replacements replacements;
68 replacements.SetPathStr(path);
69 return cloud_print_server_url.ReplaceComponents(replacements);
72 GURL GetUrlForPrinterList(const GURL& cloud_print_server_url,
73 const std::string& proxy_id) {
74 std::string path(AppendPathToUrl(cloud_print_server_url, "list"));
75 GURL::Replacements replacements;
76 replacements.SetPathStr(path);
77 std::string query = base::StringPrintf("proxy=%s", proxy_id.c_str());
78 replacements.SetQueryStr(query);
79 return cloud_print_server_url.ReplaceComponents(replacements);
82 GURL GetUrlForPrinterRegistration(const GURL& cloud_print_server_url) {
83 std::string path(AppendPathToUrl(cloud_print_server_url, "register"));
84 GURL::Replacements replacements;
85 replacements.SetPathStr(path);
86 return cloud_print_server_url.ReplaceComponents(replacements);
89 GURL GetUrlForPrinterUpdate(const GURL& cloud_print_server_url,
90 const std::string& printer_id) {
91 std::string path(AppendPathToUrl(cloud_print_server_url, "update"));
92 GURL::Replacements replacements;
93 replacements.SetPathStr(path);
94 std::string query = base::StringPrintf("printerid=%s", printer_id.c_str());
95 replacements.SetQueryStr(query);
96 return cloud_print_server_url.ReplaceComponents(replacements);
99 GURL GetUrlForPrinterDelete(const GURL& cloud_print_server_url,
100 const std::string& printer_id,
101 const std::string& reason) {
102 std::string path(AppendPathToUrl(cloud_print_server_url, "delete"));
103 GURL::Replacements replacements;
104 replacements.SetPathStr(path);
105 std::string query = base::StringPrintf(
106 "printerid=%s&reason=%s", printer_id.c_str(), reason.c_str());
107 replacements.SetQueryStr(query);
108 return cloud_print_server_url.ReplaceComponents(replacements);
111 GURL GetUrlForJobFetch(const GURL& cloud_print_server_url,
112 const std::string& printer_id,
113 const std::string& reason) {
114 std::string path(AppendPathToUrl(cloud_print_server_url, "fetch"));
115 GURL::Replacements replacements;
116 replacements.SetPathStr(path);
117 std::string query = base::StringPrintf(
118 "printerid=%s&deb=%s", printer_id.c_str(), reason.c_str());
119 replacements.SetQueryStr(query);
120 return cloud_print_server_url.ReplaceComponents(replacements);
123 GURL GetUrlForJobCjt(const GURL& cloud_print_server_url,
124 const std::string& job_id,
125 const std::string& reason) {
126 std::string path(AppendPathToUrl(cloud_print_server_url, "ticket"));
127 GURL::Replacements replacements;
128 replacements.SetPathStr(path);
129 std::string query = base::StringPrintf(
130 "jobid=%s&deb=%s&use_cjt=true", job_id.c_str(), reason.c_str());
131 replacements.SetQueryStr(query);
132 return cloud_print_server_url.ReplaceComponents(replacements);
135 GURL GetUrlForJobDelete(const GURL& cloud_print_server_url,
136 const std::string& job_id) {
137 std::string path(AppendPathToUrl(cloud_print_server_url, "deletejob"));
138 GURL::Replacements replacements;
139 replacements.SetPathStr(path);
140 std::string query = base::StringPrintf("jobid=%s", job_id.c_str());
141 replacements.SetQueryStr(query);
142 return cloud_print_server_url.ReplaceComponents(replacements);
145 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
146 const std::string& job_id,
147 const std::string& status_string,
148 int connector_code) {
149 std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
150 GURL::Replacements replacements;
151 replacements.SetPathStr(path);
152 std::string query = base::StringPrintf(
153 "jobid=%s&status=%s&connector_code=%d", job_id.c_str(),
154 status_string.c_str(), connector_code);
155 replacements.SetQueryStr(query);
156 return cloud_print_server_url.ReplaceComponents(replacements);
159 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url,
160 const std::string& message_id) {
161 std::string path(AppendPathToUrl(cloud_print_server_url, "message"));
162 GURL::Replacements replacements;
163 replacements.SetPathStr(path);
164 std::string query = base::StringPrintf("code=%s", message_id.c_str());
165 replacements.SetQueryStr(query);
166 return cloud_print_server_url.ReplaceComponents(replacements);
169 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url,
170 const std::string& oauth_client_id,
171 const std::string& proxy_id) {
172 // We use the internal API "createrobot" instead of "getauthcode". This API
173 // will add the robot as owner to all the existing printers for this user.
174 std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot"));
175 GURL::Replacements replacements;
176 replacements.SetPathStr(path);
177 std::string query = base::StringPrintf("oauth_client_id=%s&proxy=%s",
178 oauth_client_id.c_str(),
179 proxy_id.c_str());
180 replacements.SetQueryStr(query);
181 return cloud_print_server_url.ReplaceComponents(replacements);
184 scoped_ptr<base::DictionaryValue> ParseResponseJSON(
185 const std::string& response_data,
186 bool* succeeded) {
187 scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data));
188 if (!message_value.get())
189 return scoped_ptr<base::DictionaryValue>();
191 if (!message_value->IsType(base::Value::TYPE_DICTIONARY))
192 return scoped_ptr<base::DictionaryValue>();
194 scoped_ptr<base::DictionaryValue> response_dict(
195 static_cast<base::DictionaryValue*>(message_value.release()));
196 if (succeeded &&
197 !response_dict->GetBoolean(kSuccessValue, succeeded))
198 *succeeded = false;
199 return response_dict.Pass();
202 std::string GetMultipartMimeType(const std::string& mime_boundary) {
203 return std::string("multipart/form-data; boundary=") + mime_boundary;
206 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
207 void CreateMimeBoundaryForUpload(std::string* out) {
208 int r1 = base::RandInt(0, kint32max);
209 int r2 = base::RandInt(0, kint32max);
210 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2);
213 std::string GetHashOfPrinterTags(const PrinterTags& printer_tags) {
214 return HashPrinterTags(PreparePrinterTags(printer_tags));
217 std::string GetPostDataForPrinterTags(
218 const PrinterTags& printer_tags,
219 const std::string& mime_boundary,
220 const std::string& proxy_tag_prefix,
221 const std::string& tags_hash_tag_name) {
222 PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags);
223 std::string post_data;
224 for (PrinterTags::const_iterator it = printer_tags_prepared.begin();
225 it != printer_tags_prepared.end(); ++it) {
226 // TODO(gene) Escape '=' char from name. Warning for now.
227 if (it->first.find('=') != std::string::npos) {
228 LOG(WARNING) <<
229 "CP_PROXY: Printer option name contains '=' character";
230 NOTREACHED();
232 // All our tags have a special prefix to identify them as such.
233 std::string msg = base::StringPrintf("%s%s=%s",
234 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
235 net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
236 std::string(), &post_data);
238 std::string tags_hash_msg = base::StringPrintf("%s=%s",
239 tags_hash_tag_name.c_str(),
240 HashPrinterTags(printer_tags_prepared).c_str());
241 net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg,
242 mime_boundary, std::string(), &post_data);
243 return post_data;
246 std::string GetCloudPrintAuthHeader(const std::string& auth_token) {
247 return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str());
250 } // namespace cloud_print