Roll src/third_party/WebKit 6f84130:7353389 (svn 184386:184391)
[chromium-blink-merge.git] / chrome / common / cloud_print / cloud_print_helpers.cc
blobff1eaeb786fe102439d085c2157482fe7377dcc9
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/chrome_version_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 chrome::VersionInfo version_info;
29 printer_tags_out[kChromeVersionTagName] =
30 version_info.CreateVersionString();
31 printer_tags_out[kSystemNameTagName] =
32 base::SysInfo::OperatingSystemName();
33 printer_tags_out[kSystemVersionTagName] =
34 base::SysInfo::OperatingSystemVersion();
35 return printer_tags_out;
38 // Returns the hash of |printer_tags|.
39 std::string HashPrinterTags(const PrinterTags& printer_tags) {
40 std::string values_list;
41 PrinterTags::const_iterator it;
42 for (it = printer_tags.begin(); it != printer_tags.end(); ++it) {
43 values_list.append(it->first);
44 values_list.append(it->second);
46 return base::MD5String(values_list);
49 } // namespace
51 std::string AppendPathToUrl(const GURL& url, const std::string& path) {
52 DCHECK_NE(path[0], '/');
53 std::string ret = url.path();
54 if (url.has_path() && (ret[ret.length() - 1] != '/'))
55 ret += '/';
56 ret += path;
57 return ret;
60 GURL GetUrlForSearch(const GURL& cloud_print_server_url) {
61 std::string path(AppendPathToUrl(cloud_print_server_url, "search"));
62 GURL::Replacements replacements;
63 replacements.SetPathStr(path);
64 return cloud_print_server_url.ReplaceComponents(replacements);
67 GURL GetUrlForSubmit(const GURL& cloud_print_server_url) {
68 std::string path(AppendPathToUrl(cloud_print_server_url, "submit"));
69 GURL::Replacements replacements;
70 replacements.SetPathStr(path);
71 return cloud_print_server_url.ReplaceComponents(replacements);
74 GURL GetUrlForPrinterList(const GURL& cloud_print_server_url,
75 const std::string& proxy_id) {
76 std::string path(AppendPathToUrl(cloud_print_server_url, "list"));
77 GURL::Replacements replacements;
78 replacements.SetPathStr(path);
79 std::string query = base::StringPrintf("proxy=%s", proxy_id.c_str());
80 replacements.SetQueryStr(query);
81 return cloud_print_server_url.ReplaceComponents(replacements);
84 GURL GetUrlForPrinterRegistration(const GURL& cloud_print_server_url) {
85 std::string path(AppendPathToUrl(cloud_print_server_url, "register"));
86 GURL::Replacements replacements;
87 replacements.SetPathStr(path);
88 return cloud_print_server_url.ReplaceComponents(replacements);
91 GURL GetUrlForPrinterUpdate(const GURL& cloud_print_server_url,
92 const std::string& printer_id) {
93 std::string path(AppendPathToUrl(cloud_print_server_url, "update"));
94 GURL::Replacements replacements;
95 replacements.SetPathStr(path);
96 std::string query = base::StringPrintf("printerid=%s", printer_id.c_str());
97 replacements.SetQueryStr(query);
98 return cloud_print_server_url.ReplaceComponents(replacements);
101 GURL GetUrlForPrinterDelete(const GURL& cloud_print_server_url,
102 const std::string& printer_id,
103 const std::string& reason) {
104 std::string path(AppendPathToUrl(cloud_print_server_url, "delete"));
105 GURL::Replacements replacements;
106 replacements.SetPathStr(path);
107 std::string query = base::StringPrintf(
108 "printerid=%s&reason=%s", printer_id.c_str(), reason.c_str());
109 replacements.SetQueryStr(query);
110 return cloud_print_server_url.ReplaceComponents(replacements);
113 GURL GetUrlForJobFetch(const GURL& cloud_print_server_url,
114 const std::string& printer_id,
115 const std::string& reason) {
116 std::string path(AppendPathToUrl(cloud_print_server_url, "fetch"));
117 GURL::Replacements replacements;
118 replacements.SetPathStr(path);
119 std::string query = base::StringPrintf(
120 "printerid=%s&deb=%s", printer_id.c_str(), reason.c_str());
121 replacements.SetQueryStr(query);
122 return cloud_print_server_url.ReplaceComponents(replacements);
125 GURL GetUrlForJobCjt(const GURL& cloud_print_server_url,
126 const std::string& job_id,
127 const std::string& reason) {
128 std::string path(AppendPathToUrl(cloud_print_server_url, "ticket"));
129 GURL::Replacements replacements;
130 replacements.SetPathStr(path);
131 std::string query = base::StringPrintf(
132 "jobid=%s&deb=%s&use_cjt=true", job_id.c_str(), reason.c_str());
133 replacements.SetQueryStr(query);
134 return cloud_print_server_url.ReplaceComponents(replacements);
137 GURL GetUrlForJobDelete(const GURL& cloud_print_server_url,
138 const std::string& job_id) {
139 std::string path(AppendPathToUrl(cloud_print_server_url, "deletejob"));
140 GURL::Replacements replacements;
141 replacements.SetPathStr(path);
142 std::string query = base::StringPrintf("jobid=%s", job_id.c_str());
143 replacements.SetQueryStr(query);
144 return cloud_print_server_url.ReplaceComponents(replacements);
147 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
148 const std::string& job_id,
149 const std::string& status_string,
150 int connector_code) {
151 std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
152 GURL::Replacements replacements;
153 replacements.SetPathStr(path);
154 std::string query = base::StringPrintf(
155 "jobid=%s&status=%s&connector_code=%d", job_id.c_str(),
156 status_string.c_str(), connector_code);
157 replacements.SetQueryStr(query);
158 return cloud_print_server_url.ReplaceComponents(replacements);
161 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url,
162 const std::string& message_id) {
163 std::string path(AppendPathToUrl(cloud_print_server_url, "message"));
164 GURL::Replacements replacements;
165 replacements.SetPathStr(path);
166 std::string query = base::StringPrintf("code=%s", message_id.c_str());
167 replacements.SetQueryStr(query);
168 return cloud_print_server_url.ReplaceComponents(replacements);
171 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url,
172 const std::string& oauth_client_id,
173 const std::string& proxy_id) {
174 // We use the internal API "createrobot" instead of "getauthcode". This API
175 // will add the robot as owner to all the existing printers for this user.
176 std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot"));
177 GURL::Replacements replacements;
178 replacements.SetPathStr(path);
179 std::string query = base::StringPrintf("oauth_client_id=%s&proxy=%s",
180 oauth_client_id.c_str(),
181 proxy_id.c_str());
182 replacements.SetQueryStr(query);
183 return cloud_print_server_url.ReplaceComponents(replacements);
186 scoped_ptr<base::DictionaryValue> ParseResponseJSON(
187 const std::string& response_data,
188 bool* succeeded) {
189 scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data));
190 if (!message_value.get())
191 return scoped_ptr<base::DictionaryValue>();
193 if (!message_value->IsType(base::Value::TYPE_DICTIONARY))
194 return scoped_ptr<base::DictionaryValue>();
196 scoped_ptr<base::DictionaryValue> response_dict(
197 static_cast<base::DictionaryValue*>(message_value.release()));
198 if (succeeded &&
199 !response_dict->GetBoolean(kSuccessValue, succeeded))
200 *succeeded = false;
201 return response_dict.Pass();
204 std::string GetMultipartMimeType(const std::string& mime_boundary) {
205 return std::string("multipart/form-data; boundary=") + mime_boundary;
208 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
209 void CreateMimeBoundaryForUpload(std::string* out) {
210 int r1 = base::RandInt(0, kint32max);
211 int r2 = base::RandInt(0, kint32max);
212 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2);
215 std::string GetHashOfPrinterTags(const PrinterTags& printer_tags) {
216 return HashPrinterTags(PreparePrinterTags(printer_tags));
219 std::string GetPostDataForPrinterTags(
220 const PrinterTags& printer_tags,
221 const std::string& mime_boundary,
222 const std::string& proxy_tag_prefix,
223 const std::string& tags_hash_tag_name) {
224 PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags);
225 std::string post_data;
226 for (PrinterTags::const_iterator it = printer_tags_prepared.begin();
227 it != printer_tags_prepared.end(); ++it) {
228 // TODO(gene) Escape '=' char from name. Warning for now.
229 if (it->first.find('=') != std::string::npos) {
230 LOG(WARNING) <<
231 "CP_PROXY: Printer option name contains '=' character";
232 NOTREACHED();
234 // All our tags have a special prefix to identify them as such.
235 std::string msg = base::StringPrintf("%s%s=%s",
236 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
237 net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
238 std::string(), &post_data);
240 std::string tags_hash_msg = base::StringPrintf("%s=%s",
241 tags_hash_tag_name.c_str(),
242 HashPrinterTags(printer_tags_prepared).c_str());
243 net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg,
244 mime_boundary, std::string(), &post_data);
245 return post_data;
248 std::string GetCloudPrintAuthHeader(const std::string& auth_token) {
249 return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str());
252 } // namespace cloud_print