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