OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/rand_util.h" |
| 11 #include "base/stringprintf.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/common/guid.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 |
| 16 namespace cloud_print { |
| 17 |
| 18 const char kPrinterListValue[] = "printers"; |
| 19 const char kSuccessValue[] = "success"; |
| 20 |
| 21 // Certain cloud print requests require Chrome's X-CloudPrint-Proxy header. |
| 22 const char kChromeCloudPrintProxyHeader[] = "X-CloudPrint-Proxy: Chrome"; |
| 23 |
| 24 std::string AppendPathToUrl(const GURL& url, const std::string& path) { |
| 25 DCHECK_NE(path[0], '/'); |
| 26 std::string ret = url.path(); |
| 27 if (url.has_path() && (ret[ret.length() - 1] != '/')) |
| 28 ret += '/'; |
| 29 ret += path; |
| 30 return ret; |
| 31 } |
| 32 |
| 33 GURL GetUrlForSearch(const GURL& cloud_print_server_url) { |
| 34 std::string path(AppendPathToUrl(cloud_print_server_url, "search")); |
| 35 GURL::Replacements replacements; |
| 36 replacements.SetPathStr(path); |
| 37 return cloud_print_server_url.ReplaceComponents(replacements); |
| 38 } |
| 39 |
| 40 GURL GetUrlForSubmit(const GURL& cloud_print_server_url) { |
| 41 std::string path(AppendPathToUrl(cloud_print_server_url, "submit")); |
| 42 GURL::Replacements replacements; |
| 43 replacements.SetPathStr(path); |
| 44 return cloud_print_server_url.ReplaceComponents(replacements); |
| 45 } |
| 46 |
| 47 bool ParseResponseJSON(const std::string& response_data, |
| 48 bool* succeeded, |
| 49 DictionaryValue** response_dict) { |
| 50 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); |
| 51 if (!message_value.get()) |
| 52 return false; |
| 53 |
| 54 if (!message_value->IsType(Value::TYPE_DICTIONARY)) |
| 55 return false; |
| 56 |
| 57 scoped_ptr<DictionaryValue> response_dict_local( |
| 58 static_cast<DictionaryValue*>(message_value.release())); |
| 59 if (succeeded && |
| 60 !response_dict_local->GetBoolean(cloud_print::kSuccessValue, succeeded)) |
| 61 *succeeded = false; |
| 62 if (response_dict) |
| 63 *response_dict = response_dict_local.release(); |
| 64 return true; |
| 65 } |
| 66 |
| 67 void AddMultipartValueForUpload(const std::string& value_name, |
| 68 const std::string& value, |
| 69 const std::string& mime_boundary, |
| 70 const std::string& content_type, |
| 71 std::string* post_data) { |
| 72 DCHECK(post_data); |
| 73 // First line is the boundary |
| 74 post_data->append("--" + mime_boundary + "\r\n"); |
| 75 // Next line is the Content-disposition |
| 76 post_data->append(StringPrintf("Content-Disposition: form-data; " |
| 77 "name=\"%s\"\r\n", value_name.c_str())); |
| 78 if (!content_type.empty()) { |
| 79 // If Content-type is specified, the next line is that |
| 80 post_data->append(StringPrintf("Content-Type: %s\r\n", |
| 81 content_type.c_str())); |
| 82 } |
| 83 // Leave an empty line and append the value. |
| 84 post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); |
| 85 } |
| 86 |
| 87 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). |
| 88 void CreateMimeBoundaryForUpload(std::string* out) { |
| 89 int r1 = base::RandInt(0, kint32max); |
| 90 int r2 = base::RandInt(0, kint32max); |
| 91 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); |
| 92 } |
| 93 |
| 94 } // namespace cloud_print |
OLD | NEW |