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 const char kDefaultCloudPrintOAuthClientId[] = |
| 25 "551556820943.apps.googleusercontent.com"; |
| 26 |
| 27 std::string AppendPathToUrl(const GURL& url, const std::string& path) { |
| 28 DCHECK(path[0] != '/'); |
| 29 std::string ret = url.path(); |
| 30 if (url.has_path() && (ret[ret.length() - 1] != '/')) |
| 31 ret += '/'; |
| 32 ret += path; |
| 33 return ret; |
| 34 } |
| 35 |
| 36 GURL GetUrlForSearch(const GURL& cloud_print_server_url) { |
| 37 std::string path(AppendPathToUrl(cloud_print_server_url, "search")); |
| 38 GURL::Replacements replacements; |
| 39 replacements.SetPathStr(path); |
| 40 return cloud_print_server_url.ReplaceComponents(replacements); |
| 41 } |
| 42 |
| 43 GURL GetUrlForSubmit(const GURL& cloud_print_server_url) { |
| 44 std::string path(AppendPathToUrl(cloud_print_server_url, "submit")); |
| 45 GURL::Replacements replacements; |
| 46 replacements.SetPathStr(path); |
| 47 return cloud_print_server_url.ReplaceComponents(replacements); |
| 48 } |
| 49 |
| 50 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url, |
| 51 const std::string& oauth_client_id, |
| 52 const std::string& proxy_id) { |
| 53 // We use the internal API "createrobot" instead of "getauthcode". This API |
| 54 // will add the robot as owner to all the existing printers for this user. |
| 55 std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot")); |
| 56 GURL::Replacements replacements; |
| 57 replacements.SetPathStr(path); |
| 58 std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", |
| 59 oauth_client_id.c_str(), |
| 60 proxy_id.c_str()); |
| 61 replacements.SetQueryStr(query); |
| 62 return cloud_print_server_url.ReplaceComponents(replacements); |
| 63 } |
| 64 |
| 65 bool ParseResponseJSON(const std::string& response_data, |
| 66 bool* succeeded, |
| 67 DictionaryValue** response_dict) { |
| 68 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); |
| 69 if (!message_value.get()) |
| 70 return false; |
| 71 |
| 72 if (!message_value->IsType(Value::TYPE_DICTIONARY)) |
| 73 return false; |
| 74 |
| 75 scoped_ptr<DictionaryValue> response_dict_local( |
| 76 static_cast<DictionaryValue*>(message_value.release())); |
| 77 if (succeeded && |
| 78 !response_dict_local->GetBoolean(cloud_print::kSuccessValue, succeeded)) |
| 79 *succeeded = false; |
| 80 if (response_dict) |
| 81 *response_dict = response_dict_local.release(); |
| 82 return true; |
| 83 } |
| 84 |
| 85 void AddMultipartValueForUpload(const std::string& value_name, |
| 86 const std::string& value, |
| 87 const std::string& mime_boundary, |
| 88 const std::string& content_type, |
| 89 std::string* post_data) { |
| 90 DCHECK(post_data); |
| 91 // First line is the boundary |
| 92 post_data->append("--" + mime_boundary + "\r\n"); |
| 93 // Next line is the Content-disposition |
| 94 post_data->append(StringPrintf("Content-Disposition: form-data; " |
| 95 "name=\"%s\"\r\n", value_name.c_str())); |
| 96 if (!content_type.empty()) { |
| 97 // If Content-type is specified, the next line is that |
| 98 post_data->append(StringPrintf("Content-Type: %s\r\n", |
| 99 content_type.c_str())); |
| 100 } |
| 101 // Leave an empty line and append the value. |
| 102 post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); |
| 103 } |
| 104 |
| 105 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). |
| 106 void CreateMimeBoundaryForUpload(std::string* out) { |
| 107 int r1 = base::RandInt(0, kint32max); |
| 108 int r2 = base::RandInt(0, kint32max); |
| 109 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); |
| 110 } |
| 111 |
| 112 std::string GenerateProxyId() { |
| 113 return std::string("CP_PROXY_").append(guid::GenerateGUID()); |
| 114 } |
| 115 |
| 116 } // namespace cloud_print |
OLD | NEW |