| 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/service/cloud_print/cloud_print_helpers.h" | 5 #include "chrome/service/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/md5.h" | 8 #include "base/md5.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
| 15 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 15 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_consts.h" | 16 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 17 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 17 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
| 18 #include "chrome/service/service_process.h" | 18 #include "chrome/service/service_process.h" |
| 19 | 19 |
| 20 namespace { | |
| 21 | |
| 22 typedef std::map<std::string, std::string> PrinterTags; | |
| 23 | |
| 24 void GetPrinterTags(const printing::PrinterBasicInfo& printer, | |
| 25 PrinterTags* printer_tags) { | |
| 26 *printer_tags = printer.options; | |
| 27 chrome::VersionInfo version_info; | |
| 28 DCHECK(version_info.is_valid()); | |
| 29 (*printer_tags)[kChromeVersionTagName] = version_info.CreateVersionString(); | |
| 30 using base::SysInfo; | |
| 31 (*printer_tags)[kSystemNameTagName] = SysInfo::OperatingSystemName(); | |
| 32 (*printer_tags)[kSystemVersionTagName] = SysInfo::OperatingSystemVersion(); | |
| 33 } | |
| 34 | |
| 35 std::string HashPrinterTags(const PrinterTags& strings) { | |
| 36 std::string values_list; | |
| 37 PrinterTags::const_iterator it; | |
| 38 for (it = strings.begin(); it != strings.end(); ++it) { | |
| 39 values_list.append(it->first); | |
| 40 values_list.append(it->second); | |
| 41 } | |
| 42 return base::MD5String(values_list); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { | 20 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { |
| 48 std::string ret; | 21 std::string ret; |
| 49 switch (status) { | 22 switch (status) { |
| 50 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: | 23 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: |
| 51 ret = "IN_PROGRESS"; | 24 ret = "IN_PROGRESS"; |
| 52 break; | 25 break; |
| 53 case cloud_print::PRINT_JOB_STATUS_ERROR: | 26 case cloud_print::PRINT_JOB_STATUS_ERROR: |
| 54 ret = "ERROR"; | 27 ret = "ERROR"; |
| 55 break; | 28 break; |
| 56 case cloud_print::PRINT_JOB_STATUS_COMPLETED: | 29 case cloud_print::PRINT_JOB_STATUS_COMPLETED: |
| 57 ret = "DONE"; | 30 ret = "DONE"; |
| 58 break; | 31 break; |
| 59 default: | 32 default: |
| 60 ret = "UNKNOWN"; | 33 ret = "UNKNOWN"; |
| 61 NOTREACHED(); | 34 NOTREACHED(); |
| 62 break; | 35 break; |
| 63 } | 36 } |
| 64 return ret; | 37 return ret; |
| 65 } | 38 } |
| 66 | 39 |
| 67 GURL CloudPrintHelpers::GetUrlForPrinterRegistration( | |
| 68 const GURL& cloud_print_server_url) { | |
| 69 std::string path( | |
| 70 cloud_print::AppendPathToUrl(cloud_print_server_url, "register")); | |
| 71 GURL::Replacements replacements; | |
| 72 replacements.SetPathStr(path); | |
| 73 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 74 } | |
| 75 | |
| 76 GURL CloudPrintHelpers::GetUrlForPrinterUpdate( | |
| 77 const GURL& cloud_print_server_url, | |
| 78 const std::string& printer_id) { | |
| 79 std::string path( | |
| 80 cloud_print::AppendPathToUrl(cloud_print_server_url, "update")); | |
| 81 GURL::Replacements replacements; | |
| 82 replacements.SetPathStr(path); | |
| 83 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); | |
| 84 replacements.SetQueryStr(query); | |
| 85 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 86 } | |
| 87 | |
| 88 GURL CloudPrintHelpers::GetUrlForPrinterDelete( | |
| 89 const GURL& cloud_print_server_url, | |
| 90 const std::string& printer_id, | |
| 91 const std::string& reason) { | |
| 92 std::string path( | |
| 93 cloud_print::AppendPathToUrl(cloud_print_server_url, "delete")); | |
| 94 GURL::Replacements replacements; | |
| 95 replacements.SetPathStr(path); | |
| 96 std::string query = StringPrintf("printerid=%s&reason=%s", | |
| 97 printer_id.c_str(), reason.c_str()); | |
| 98 replacements.SetQueryStr(query); | |
| 99 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 100 } | |
| 101 | |
| 102 GURL CloudPrintHelpers::GetUrlForPrinterList(const GURL& cloud_print_server_url, | |
| 103 const std::string& proxy_id) { | |
| 104 std::string path( | |
| 105 cloud_print::AppendPathToUrl(cloud_print_server_url, "list")); | |
| 106 GURL::Replacements replacements; | |
| 107 replacements.SetPathStr(path); | |
| 108 std::string query = StringPrintf("proxy=%s", proxy_id.c_str()); | |
| 109 replacements.SetQueryStr(query); | |
| 110 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 111 } | |
| 112 | |
| 113 GURL CloudPrintHelpers::GetUrlForJobFetch(const GURL& cloud_print_server_url, | |
| 114 const std::string& printer_id, | |
| 115 const std::string& reason) { | |
| 116 std::string path( | |
| 117 cloud_print::AppendPathToUrl(cloud_print_server_url, "fetch")); | |
| 118 GURL::Replacements replacements; | |
| 119 replacements.SetPathStr(path); | |
| 120 std::string query = StringPrintf("printerid=%s&deb=%s", | |
| 121 printer_id.c_str(), | |
| 122 reason.c_str()); | |
| 123 replacements.SetQueryStr(query); | |
| 124 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 125 } | |
| 126 | |
| 127 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( | 40 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( |
| 128 const GURL& cloud_print_server_url, | 41 const GURL& cloud_print_server_url, |
| 129 const std::string& job_id, | 42 const std::string& job_id, |
| 130 cloud_print::PrintJobStatus status) { | 43 cloud_print::PrintJobStatus status) { |
| 131 std::string status_string = StringFromJobStatus(status); | 44 return cloud_print::GetUrlForJobStatusUpdate(cloud_print_server_url, |
| 132 std::string path( | 45 job_id, |
| 133 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); | 46 StringFromJobStatus(status)); |
| 134 GURL::Replacements replacements; | |
| 135 replacements.SetPathStr(path); | |
| 136 std::string query = StringPrintf("jobid=%s&status=%s", | |
| 137 job_id.c_str(), status_string.c_str()); | |
| 138 replacements.SetQueryStr(query); | |
| 139 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 140 } | 47 } |
| 141 | 48 |
| 142 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( | 49 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( |
| 143 const GURL& cloud_print_server_url, | 50 const GURL& cloud_print_server_url, |
| 144 const std::string& job_id, | 51 const std::string& job_id, |
| 145 const cloud_print::PrintJobDetails& details) { | 52 const cloud_print::PrintJobDetails& details) { |
| 146 std::string status_string = StringFromJobStatus(details.status); | 53 std::string status_string = StringFromJobStatus(details.status); |
| 147 std::string path( | 54 std::string path( |
| 148 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); | 55 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); |
| 149 GURL::Replacements replacements; | 56 GURL::Replacements replacements; |
| 150 replacements.SetPathStr(path); | 57 replacements.SetPathStr(path); |
| 151 std::string query = | 58 std::string query = |
| 152 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" | 59 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" |
| 153 "&numpages=%d&pagesprinted=%d", | 60 "&numpages=%d&pagesprinted=%d", |
| 154 job_id.c_str(), | 61 job_id.c_str(), |
| 155 status_string.c_str(), | 62 status_string.c_str(), |
| 156 details.platform_status_flags, | 63 details.platform_status_flags, |
| 157 details.status_message.c_str(), | 64 details.status_message.c_str(), |
| 158 details.total_pages, | 65 details.total_pages, |
| 159 details.pages_printed); | 66 details.pages_printed); |
| 160 replacements.SetQueryStr(query); | 67 replacements.SetQueryStr(query); |
| 161 return cloud_print_server_url.ReplaceComponents(replacements); | 68 return cloud_print_server_url.ReplaceComponents(replacements); |
| 162 } | 69 } |
| 163 | 70 |
| 164 GURL CloudPrintHelpers::GetUrlForUserMessage(const GURL& cloud_print_server_url, | |
| 165 const std::string& message_id) { | |
| 166 std::string path( | |
| 167 cloud_print::AppendPathToUrl(cloud_print_server_url, "message")); | |
| 168 GURL::Replacements replacements; | |
| 169 replacements.SetPathStr(path); | |
| 170 std::string query = StringPrintf("code=%s", message_id.c_str()); | |
| 171 replacements.SetQueryStr(query); | |
| 172 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 173 } | |
| 174 | |
| 175 GURL CloudPrintHelpers::GetUrlForGetAuthCode(const GURL& cloud_print_server_url, | |
| 176 const std::string& oauth_client_id, | |
| 177 const std::string& proxy_id) { | |
| 178 // We use the internal API "createrobot" instead of "getauthcode". This API | |
| 179 // will add the robot as owner to all the existing printers for this user. | |
| 180 std::string path( | |
| 181 cloud_print::AppendPathToUrl(cloud_print_server_url, "createrobot")); | |
| 182 GURL::Replacements replacements; | |
| 183 replacements.SetPathStr(path); | |
| 184 std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", | |
| 185 oauth_client_id.c_str(), | |
| 186 proxy_id.c_str()); | |
| 187 replacements.SetQueryStr(query); | |
| 188 return cloud_print_server_url.ReplaceComponents(replacements); | |
| 189 } | |
| 190 | |
| 191 std::string CloudPrintHelpers::GetHashOfPrinterTags( | 71 std::string CloudPrintHelpers::GetHashOfPrinterTags( |
| 192 const printing::PrinterBasicInfo& printer) { | 72 const printing::PrinterBasicInfo& printer) { |
| 193 PrinterTags printer_tags; | 73 return cloud_print::GetHashOfPrinterTags(printer.options); |
| 194 GetPrinterTags(printer, &printer_tags); | |
| 195 std::string values_list; | |
| 196 for (PrinterTags::const_iterator it = printer_tags.begin(); | |
| 197 it != printer_tags.end(); ++it) { | |
| 198 values_list.append(it->first); | |
| 199 values_list.append(it->second); | |
| 200 } | |
| 201 return base::MD5String(values_list); | |
| 202 } | 74 } |
| 203 | 75 |
| 204 std::string CloudPrintHelpers::GetPostDataForPrinterTags( | 76 std::string CloudPrintHelpers::GetPostDataForPrinterTags( |
| 205 const printing::PrinterBasicInfo& printer, | 77 const printing::PrinterBasicInfo& printer, |
| 206 const std::string& mime_boundary) { | 78 const std::string& mime_boundary) { |
| 207 PrinterTags printer_tags; | 79 return cloud_print::GetPostDataForPrinterTags( |
| 208 GetPrinterTags(printer, &printer_tags); | 80 printer.options, mime_boundary, kProxyTagPrefix, kTagsHashTagName); |
| 209 std::string post_data; | |
| 210 for (PrinterTags::const_iterator it = printer_tags.begin(); | |
| 211 it != printer_tags.end(); ++it) { | |
| 212 // TODO(gene) Escape '=' char from name. Warning for now. | |
| 213 if (it->first.find('=') != std::string::npos) { | |
| 214 LOG(WARNING) << | |
| 215 "CP_PROXY: Printer option name contains '=' character"; | |
| 216 NOTREACHED(); | |
| 217 } | |
| 218 // All our tags have a special prefix to identify them as such. | |
| 219 std::string msg(kProxyTagPrefix); | |
| 220 msg += it->first; | |
| 221 msg += "="; | |
| 222 msg += it->second; | |
| 223 cloud_print::AddMultipartValueForUpload(kPrinterTagValue, msg, | |
| 224 mime_boundary, std::string(), &post_data); | |
| 225 } | |
| 226 std::string tags_hash_msg(kTagsHashTagName); | |
| 227 tags_hash_msg += "="; | |
| 228 tags_hash_msg += HashPrinterTags(printer_tags); | |
| 229 cloud_print::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, | |
| 230 mime_boundary, std::string(), | |
| 231 &post_data); | |
| 232 return post_data; | |
| 233 } | |
| 234 | |
| 235 bool CloudPrintHelpers::IsDryRunJob(const std::vector<std::string>& tags) { | |
| 236 std::vector<std::string>::const_iterator it; | |
| 237 for (it = tags.begin(); it != tags.end(); ++it) { | |
| 238 if (*it == kTagDryRunFlag) | |
| 239 return true; | |
| 240 } | |
| 241 return false; | |
| 242 } | 81 } |
| 243 | 82 |
| 244 std::string CloudPrintHelpers::GetCloudPrintAuthHeaderFromStore() { | 83 std::string CloudPrintHelpers::GetCloudPrintAuthHeaderFromStore() { |
| 245 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); | 84 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); |
| 246 if (!token_store || token_store->token().empty()) { | 85 if (!token_store || token_store->token().empty()) { |
| 247 // Using LOG here for critical errors. GCP connector may run in the headless | 86 // Using LOG here for critical errors. GCP connector may run in the headless |
| 248 // mode and error indication might be useful for user in that case. | 87 // mode and error indication might be useful for user in that case. |
| 249 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; | 88 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; |
| 250 return std::string(); | 89 return std::string(); |
| 251 } | 90 } |
| 252 return GetCloudPrintAuthHeader(token_store->token()); | 91 return cloud_print::GetCloudPrintAuthHeader(token_store->token()); |
| 253 } | 92 } |
| 254 | 93 |
| 255 std::string CloudPrintHelpers::GetCloudPrintAuthHeader( | 94 bool CloudPrintHelpers::IsDryRunJob(const std::vector<std::string>& tags) { |
| 256 const std::string& auth_token) { | 95 return cloud_print::IsDryRunJob(tags, kTagDryRunFlag); |
| 257 std::string header; | |
| 258 header = "Authorization: OAuth "; | |
| 259 header += auth_token; | |
| 260 return header; | |
| 261 } | 96 } |
| OLD | NEW |