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