| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 5 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/md5.h" | 9 #include "base/md5.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 16 #include "chrome/common/cloud_print/cloud_print_constants.h" | 16 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 #include "net/base/mime_util.h" |
| 18 | 19 |
| 19 namespace cloud_print { | 20 namespace cloud_print { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Returns printer tags generated from |printer_tags| and the default tags | 24 // Returns printer tags generated from |printer_tags| and the default tags |
| 24 // required by cloud print server. | 25 // required by cloud print server. |
| 25 PrinterTags PreparePrinterTags(const PrinterTags& printer_tags) { | 26 PrinterTags PreparePrinterTags(const PrinterTags& printer_tags) { |
| 26 PrinterTags printer_tags_out = printer_tags; | 27 PrinterTags printer_tags_out = printer_tags; |
| 27 chrome::VersionInfo version_info; | 28 chrome::VersionInfo version_info; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 return scoped_ptr<base::DictionaryValue>(); | 182 return scoped_ptr<base::DictionaryValue>(); |
| 182 | 183 |
| 183 scoped_ptr<base::DictionaryValue> response_dict( | 184 scoped_ptr<base::DictionaryValue> response_dict( |
| 184 static_cast<base::DictionaryValue*>(message_value.release())); | 185 static_cast<base::DictionaryValue*>(message_value.release())); |
| 185 if (succeeded && | 186 if (succeeded && |
| 186 !response_dict->GetBoolean(kSuccessValue, succeeded)) | 187 !response_dict->GetBoolean(kSuccessValue, succeeded)) |
| 187 *succeeded = false; | 188 *succeeded = false; |
| 188 return response_dict.Pass(); | 189 return response_dict.Pass(); |
| 189 } | 190 } |
| 190 | 191 |
| 191 void AddMultipartValueForUpload(const std::string& value_name, | |
| 192 const std::string& value, | |
| 193 const std::string& mime_boundary, | |
| 194 const std::string& content_type, | |
| 195 std::string* post_data) { | |
| 196 DCHECK(post_data); | |
| 197 // First line is the boundary | |
| 198 post_data->append("--" + mime_boundary + "\r\n"); | |
| 199 // Next line is the Content-disposition | |
| 200 post_data->append(base::StringPrintf("Content-Disposition: form-data; " | |
| 201 "name=\"%s\"\r\n", value_name.c_str())); | |
| 202 if (!content_type.empty()) { | |
| 203 // If Content-type is specified, the next line is that | |
| 204 post_data->append(base::StringPrintf("Content-Type: %s\r\n", | |
| 205 content_type.c_str())); | |
| 206 } | |
| 207 // Leave an empty line and append the value. | |
| 208 post_data->append(base::StringPrintf("\r\n%s\r\n", value.c_str())); | |
| 209 } | |
| 210 | |
| 211 std::string GetMultipartMimeType(const std::string& mime_boundary) { | 192 std::string GetMultipartMimeType(const std::string& mime_boundary) { |
| 212 return std::string("multipart/form-data; boundary=") + mime_boundary; | 193 return std::string("multipart/form-data; boundary=") + mime_boundary; |
| 213 } | 194 } |
| 214 | 195 |
| 215 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). | 196 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). |
| 216 void CreateMimeBoundaryForUpload(std::string* out) { | 197 void CreateMimeBoundaryForUpload(std::string* out) { |
| 217 int r1 = base::RandInt(0, kint32max); | 198 int r1 = base::RandInt(0, kint32max); |
| 218 int r2 = base::RandInt(0, kint32max); | 199 int r2 = base::RandInt(0, kint32max); |
| 219 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); | 200 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); |
| 220 } | 201 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 234 it != printer_tags_prepared.end(); ++it) { | 215 it != printer_tags_prepared.end(); ++it) { |
| 235 // TODO(gene) Escape '=' char from name. Warning for now. | 216 // TODO(gene) Escape '=' char from name. Warning for now. |
| 236 if (it->first.find('=') != std::string::npos) { | 217 if (it->first.find('=') != std::string::npos) { |
| 237 LOG(WARNING) << | 218 LOG(WARNING) << |
| 238 "CP_PROXY: Printer option name contains '=' character"; | 219 "CP_PROXY: Printer option name contains '=' character"; |
| 239 NOTREACHED(); | 220 NOTREACHED(); |
| 240 } | 221 } |
| 241 // All our tags have a special prefix to identify them as such. | 222 // All our tags have a special prefix to identify them as such. |
| 242 std::string msg = base::StringPrintf("%s%s=%s", | 223 std::string msg = base::StringPrintf("%s%s=%s", |
| 243 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str()); | 224 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str()); |
| 244 AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary, | 225 net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary, |
| 245 std::string(), &post_data); | 226 std::string(), &post_data); |
| 246 } | 227 } |
| 247 std::string tags_hash_msg = base::StringPrintf("%s=%s", | 228 std::string tags_hash_msg = base::StringPrintf("%s=%s", |
| 248 tags_hash_tag_name.c_str(), | 229 tags_hash_tag_name.c_str(), |
| 249 HashPrinterTags(printer_tags_prepared).c_str()); | 230 HashPrinterTags(printer_tags_prepared).c_str()); |
| 250 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary, | 231 net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, |
| 251 std::string(), &post_data); | 232 mime_boundary, std::string(), &post_data); |
| 252 return post_data; | 233 return post_data; |
| 253 } | 234 } |
| 254 | 235 |
| 255 std::string GetCloudPrintAuthHeader(const std::string& auth_token) { | 236 std::string GetCloudPrintAuthHeader(const std::string& auth_token) { |
| 256 return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str()); | 237 return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str()); |
| 257 } | 238 } |
| 258 | 239 |
| 259 } // namespace cloud_print | 240 } // namespace cloud_print |
| OLD | NEW |