| 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/memory/scoped_ptr.h" | |
| 10 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 11 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 12 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 13 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
| 15 #include "chrome/service/cloud_print/cloud_print_consts.h" | 14 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 15 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
| 17 #include "chrome/service/service_process.h" | 16 #include "chrome/service/service_process.h" |
| 18 | 17 |
| 19 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { | 18 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { |
| 20 std::string ret; | 19 std::string ret; |
| 21 switch (status) { | 20 switch (status) { |
| 22 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: | 21 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: |
| 23 ret = "IN_PROGRESS"; | 22 ret = "IN_PROGRESS"; |
| 24 break; | 23 break; |
| 25 case cloud_print::PRINT_JOB_STATUS_ERROR: | 24 case cloud_print::PRINT_JOB_STATUS_ERROR: |
| 26 ret = "ERROR"; | 25 ret = "ERROR"; |
| 27 break; | 26 break; |
| 28 case cloud_print::PRINT_JOB_STATUS_COMPLETED: | 27 case cloud_print::PRINT_JOB_STATUS_COMPLETED: |
| 29 ret = "DONE"; | 28 ret = "DONE"; |
| 30 break; | 29 break; |
| 31 default: | 30 default: |
| 32 ret = "UNKNOWN"; | 31 ret = "UNKNOWN"; |
| 33 NOTREACHED(); | 32 NOTREACHED(); |
| 34 break; | 33 break; |
| 35 } | 34 } |
| 36 return ret; | 35 return ret; |
| 37 } | 36 } |
| 38 | 37 |
| 39 // Appends a relative path to the url making sure to append a '/' if the | |
| 40 // URL's path does not end with a slash. It is assumed that |path| does not | |
| 41 // begin with a '/'. | |
| 42 // NOTE: Since we ALWAYS want to append here, we simply append the path string | |
| 43 // instead of calling url_utils::ResolveRelative. The input |url| may or may not | |
| 44 // contain a '/' at the end. | |
| 45 std::string AppendPathToUrl(const GURL& url, const std::string& path) { | |
| 46 DCHECK(path[0] != '/'); | |
| 47 std::string ret = url.path(); | |
| 48 if (url.has_path() && (ret[ret.length() - 1] != '/')) { | |
| 49 ret += '/'; | |
| 50 } | |
| 51 ret += path; | |
| 52 return ret; | |
| 53 } | |
| 54 | |
| 55 GURL CloudPrintHelpers::GetUrlForPrinterRegistration( | 38 GURL CloudPrintHelpers::GetUrlForPrinterRegistration( |
| 56 const GURL& cloud_print_server_url) { | 39 const GURL& cloud_print_server_url) { |
| 57 std::string path(AppendPathToUrl(cloud_print_server_url, "register")); | 40 std::string path( |
| 41 cloud_print::AppendPathToUrl(cloud_print_server_url, "register")); |
| 58 GURL::Replacements replacements; | 42 GURL::Replacements replacements; |
| 59 replacements.SetPathStr(path); | 43 replacements.SetPathStr(path); |
| 60 return cloud_print_server_url.ReplaceComponents(replacements); | 44 return cloud_print_server_url.ReplaceComponents(replacements); |
| 61 } | 45 } |
| 62 | 46 |
| 63 GURL CloudPrintHelpers::GetUrlForPrinterUpdate( | 47 GURL CloudPrintHelpers::GetUrlForPrinterUpdate( |
| 64 const GURL& cloud_print_server_url, const std::string& printer_id) { | 48 const GURL& cloud_print_server_url, |
| 65 std::string path(AppendPathToUrl(cloud_print_server_url, "update")); | 49 const std::string& printer_id) { |
| 50 std::string path( |
| 51 cloud_print::AppendPathToUrl(cloud_print_server_url, "update")); |
| 66 GURL::Replacements replacements; | 52 GURL::Replacements replacements; |
| 67 replacements.SetPathStr(path); | 53 replacements.SetPathStr(path); |
| 68 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); | 54 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); |
| 69 replacements.SetQueryStr(query); | 55 replacements.SetQueryStr(query); |
| 70 return cloud_print_server_url.ReplaceComponents(replacements); | 56 return cloud_print_server_url.ReplaceComponents(replacements); |
| 71 } | 57 } |
| 72 | 58 |
| 73 GURL CloudPrintHelpers::GetUrlForPrinterDelete( | 59 GURL CloudPrintHelpers::GetUrlForPrinterDelete( |
| 74 const GURL& cloud_print_server_url, const std::string& printer_id) { | 60 const GURL& cloud_print_server_url, |
| 75 std::string path(AppendPathToUrl(cloud_print_server_url, "delete")); | 61 const std::string& printer_id) { |
| 62 std::string path( |
| 63 cloud_print::AppendPathToUrl(cloud_print_server_url, "delete")); |
| 76 GURL::Replacements replacements; | 64 GURL::Replacements replacements; |
| 77 replacements.SetPathStr(path); | 65 replacements.SetPathStr(path); |
| 78 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); | 66 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); |
| 79 replacements.SetQueryStr(query); | 67 replacements.SetQueryStr(query); |
| 80 return cloud_print_server_url.ReplaceComponents(replacements); | 68 return cloud_print_server_url.ReplaceComponents(replacements); |
| 81 } | 69 } |
| 82 | 70 |
| 83 GURL CloudPrintHelpers::GetUrlForPrinterList(const GURL& cloud_print_server_url, | 71 GURL CloudPrintHelpers::GetUrlForPrinterList(const GURL& cloud_print_server_url, |
| 84 const std::string& proxy_id) { | 72 const std::string& proxy_id) { |
| 85 std::string path(AppendPathToUrl(cloud_print_server_url, "list")); | 73 std::string path( |
| 74 cloud_print::AppendPathToUrl(cloud_print_server_url, "list")); |
| 86 GURL::Replacements replacements; | 75 GURL::Replacements replacements; |
| 87 replacements.SetPathStr(path); | 76 replacements.SetPathStr(path); |
| 88 std::string query = StringPrintf("proxy=%s", proxy_id.c_str()); | 77 std::string query = StringPrintf("proxy=%s", proxy_id.c_str()); |
| 89 replacements.SetQueryStr(query); | 78 replacements.SetQueryStr(query); |
| 90 return cloud_print_server_url.ReplaceComponents(replacements); | 79 return cloud_print_server_url.ReplaceComponents(replacements); |
| 91 } | 80 } |
| 92 | 81 |
| 93 GURL CloudPrintHelpers::GetUrlForJobFetch(const GURL& cloud_print_server_url, | 82 GURL CloudPrintHelpers::GetUrlForJobFetch(const GURL& cloud_print_server_url, |
| 94 const std::string& printer_id, | 83 const std::string& printer_id, |
| 95 const std::string& reason) { | 84 const std::string& reason) { |
| 96 std::string path(AppendPathToUrl(cloud_print_server_url, "fetch")); | 85 std::string path( |
| 86 cloud_print::AppendPathToUrl(cloud_print_server_url, "fetch")); |
| 97 GURL::Replacements replacements; | 87 GURL::Replacements replacements; |
| 98 replacements.SetPathStr(path); | 88 replacements.SetPathStr(path); |
| 99 std::string query = StringPrintf("printerid=%s&deb=%s", | 89 std::string query = StringPrintf("printerid=%s&deb=%s", |
| 100 printer_id.c_str(), | 90 printer_id.c_str(), |
| 101 reason.c_str()); | 91 reason.c_str()); |
| 102 replacements.SetQueryStr(query); | 92 replacements.SetQueryStr(query); |
| 103 return cloud_print_server_url.ReplaceComponents(replacements); | 93 return cloud_print_server_url.ReplaceComponents(replacements); |
| 104 } | 94 } |
| 105 | 95 |
| 106 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( | 96 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( |
| 107 const GURL& cloud_print_server_url, const std::string& job_id, | 97 const GURL& cloud_print_server_url, |
| 98 const std::string& job_id, |
| 108 cloud_print::PrintJobStatus status) { | 99 cloud_print::PrintJobStatus status) { |
| 109 std::string status_string = StringFromJobStatus(status); | 100 std::string status_string = StringFromJobStatus(status); |
| 110 std::string path(AppendPathToUrl(cloud_print_server_url, "control")); | 101 std::string path( |
| 102 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); |
| 111 GURL::Replacements replacements; | 103 GURL::Replacements replacements; |
| 112 replacements.SetPathStr(path); | 104 replacements.SetPathStr(path); |
| 113 std::string query = StringPrintf("jobid=%s&status=%s", | 105 std::string query = StringPrintf("jobid=%s&status=%s", |
| 114 job_id.c_str(), status_string.c_str()); | 106 job_id.c_str(), status_string.c_str()); |
| 115 replacements.SetQueryStr(query); | 107 replacements.SetQueryStr(query); |
| 116 return cloud_print_server_url.ReplaceComponents(replacements); | 108 return cloud_print_server_url.ReplaceComponents(replacements); |
| 117 } | 109 } |
| 118 | 110 |
| 119 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( | 111 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( |
| 120 const GURL& cloud_print_server_url, const std::string& job_id, | 112 const GURL& cloud_print_server_url, |
| 113 const std::string& job_id, |
| 121 const cloud_print::PrintJobDetails& details) { | 114 const cloud_print::PrintJobDetails& details) { |
| 122 std::string status_string = StringFromJobStatus(details.status); | 115 std::string status_string = StringFromJobStatus(details.status); |
| 123 std::string path(AppendPathToUrl(cloud_print_server_url, "control")); | 116 std::string path( |
| 117 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); |
| 124 GURL::Replacements replacements; | 118 GURL::Replacements replacements; |
| 125 replacements.SetPathStr(path); | 119 replacements.SetPathStr(path); |
| 126 std::string query = | 120 std::string query = |
| 127 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" | 121 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" |
| 128 "&numpages=%d&pagesprinted=%d", | 122 "&numpages=%d&pagesprinted=%d", |
| 129 job_id.c_str(), | 123 job_id.c_str(), |
| 130 status_string.c_str(), | 124 status_string.c_str(), |
| 131 details.platform_status_flags, | 125 details.platform_status_flags, |
| 132 details.status_message.c_str(), | 126 details.status_message.c_str(), |
| 133 details.total_pages, | 127 details.total_pages, |
| 134 details.pages_printed); | 128 details.pages_printed); |
| 135 replacements.SetQueryStr(query); | 129 replacements.SetQueryStr(query); |
| 136 return cloud_print_server_url.ReplaceComponents(replacements); | 130 return cloud_print_server_url.ReplaceComponents(replacements); |
| 137 } | 131 } |
| 138 | 132 |
| 139 GURL CloudPrintHelpers::GetUrlForUserMessage(const GURL& cloud_print_server_url, | 133 GURL CloudPrintHelpers::GetUrlForUserMessage(const GURL& cloud_print_server_url, |
| 140 const std::string& message_id) { | 134 const std::string& message_id) { |
| 141 std::string path(AppendPathToUrl(cloud_print_server_url, "message")); | 135 std::string path( |
| 136 cloud_print::AppendPathToUrl(cloud_print_server_url, "message")); |
| 142 GURL::Replacements replacements; | 137 GURL::Replacements replacements; |
| 143 replacements.SetPathStr(path); | 138 replacements.SetPathStr(path); |
| 144 std::string query = StringPrintf("code=%s", message_id.c_str()); | 139 std::string query = StringPrintf("code=%s", message_id.c_str()); |
| 145 replacements.SetQueryStr(query); | 140 replacements.SetQueryStr(query); |
| 146 return cloud_print_server_url.ReplaceComponents(replacements); | 141 return cloud_print_server_url.ReplaceComponents(replacements); |
| 147 } | 142 } |
| 148 | 143 |
| 149 GURL CloudPrintHelpers::GetUrlForGetAuthCode(const GURL& cloud_print_server_url, | 144 GURL CloudPrintHelpers::GetUrlForGetAuthCode(const GURL& cloud_print_server_url, |
| 150 const std::string& oauth_client_id, | 145 const std::string& oauth_client_id, |
| 151 const std::string& proxy_id) { | 146 const std::string& proxy_id) { |
| 152 // We use the internal API "createrobot" instead of "getauthcode". This API | 147 // We use the internal API "createrobot" instead of "getauthcode". This API |
| 153 // will add the robot as owner to all the existing printers for this user. | 148 // will add the robot as owner to all the existing printers for this user. |
| 154 std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot")); | 149 std::string path( |
| 150 cloud_print::AppendPathToUrl(cloud_print_server_url, "createrobot")); |
| 155 GURL::Replacements replacements; | 151 GURL::Replacements replacements; |
| 156 replacements.SetPathStr(path); | 152 replacements.SetPathStr(path); |
| 157 std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", | 153 std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", |
| 158 oauth_client_id.c_str(), | 154 oauth_client_id.c_str(), |
| 159 proxy_id.c_str()); | 155 proxy_id.c_str()); |
| 160 replacements.SetQueryStr(query); | 156 replacements.SetQueryStr(query); |
| 161 return cloud_print_server_url.ReplaceComponents(replacements); | 157 return cloud_print_server_url.ReplaceComponents(replacements); |
| 162 } | 158 } |
| 163 | 159 |
| 164 | |
| 165 bool CloudPrintHelpers::ParseResponseJSON( | |
| 166 const std::string& response_data, bool* succeeded, | |
| 167 DictionaryValue** response_dict) { | |
| 168 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); | |
| 169 if (!message_value.get()) | |
| 170 return false; | |
| 171 | |
| 172 if (!message_value->IsType(Value::TYPE_DICTIONARY)) | |
| 173 return false; | |
| 174 | |
| 175 scoped_ptr<DictionaryValue> response_dict_local( | |
| 176 static_cast<DictionaryValue*>(message_value.release())); | |
| 177 if (succeeded) { | |
| 178 if (!response_dict_local->GetBoolean(kSuccessValue, succeeded)) | |
| 179 *succeeded = false; | |
| 180 } | |
| 181 if (response_dict) | |
| 182 *response_dict = response_dict_local.release(); | |
| 183 return true; | |
| 184 } | |
| 185 | |
| 186 void CloudPrintHelpers::AddMultipartValueForUpload( | |
| 187 const std::string& value_name, const std::string& value, | |
| 188 const std::string& mime_boundary, const std::string& content_type, | |
| 189 std::string* post_data) { | |
| 190 DCHECK(post_data); | |
| 191 // First line is the boundary | |
| 192 post_data->append("--" + mime_boundary + "\r\n"); | |
| 193 // Next line is the Content-disposition | |
| 194 post_data->append(StringPrintf("Content-Disposition: form-data; " | |
| 195 "name=\"%s\"\r\n", value_name.c_str())); | |
| 196 if (!content_type.empty()) { | |
| 197 // If Content-type is specified, the next line is that | |
| 198 post_data->append(StringPrintf("Content-Type: %s\r\n", | |
| 199 content_type.c_str())); | |
| 200 } | |
| 201 // Leave an empty line and append the value. | |
| 202 post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); | |
| 203 } | |
| 204 | |
| 205 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). | |
| 206 void CloudPrintHelpers::CreateMimeBoundaryForUpload(std::string* out) { | |
| 207 int r1 = base::RandInt(0, kint32max); | |
| 208 int r2 = base::RandInt(0, kint32max); | |
| 209 base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); | |
| 210 } | |
| 211 | |
| 212 std::string CloudPrintHelpers::GenerateHashOfStringMap( | 160 std::string CloudPrintHelpers::GenerateHashOfStringMap( |
| 213 const std::map<std::string, std::string>& string_map) { | 161 const std::map<std::string, std::string>& string_map) { |
| 214 std::string values_list; | 162 std::string values_list; |
| 215 std::map<std::string, std::string>::const_iterator it; | 163 std::map<std::string, std::string>::const_iterator it; |
| 216 for (it = string_map.begin(); it != string_map.end(); ++it) { | 164 for (it = string_map.begin(); it != string_map.end(); ++it) { |
| 217 values_list.append(it->first); | 165 values_list.append(it->first); |
| 218 values_list.append(it->second); | 166 values_list.append(it->second); |
| 219 } | 167 } |
| 220 return base::MD5String(values_list); | 168 return base::MD5String(values_list); |
| 221 } | 169 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 240 NOTREACHED(); | 188 NOTREACHED(); |
| 241 } | 189 } |
| 242 tags_list.append(it->first); | 190 tags_list.append(it->first); |
| 243 tags_list.append(it->second); | 191 tags_list.append(it->second); |
| 244 | 192 |
| 245 // All our tags have a special prefix to identify them as such. | 193 // All our tags have a special prefix to identify them as such. |
| 246 std::string msg(kProxyTagPrefix); | 194 std::string msg(kProxyTagPrefix); |
| 247 msg += it->first; | 195 msg += it->first; |
| 248 msg += "="; | 196 msg += "="; |
| 249 msg += it->second; | 197 msg += it->second; |
| 250 AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary, | 198 cloud_print::AddMultipartValueForUpload(kPrinterTagValue, msg, |
| 251 std::string(), post_data); | 199 mime_boundary, std::string(), post_data); |
| 252 } | 200 } |
| 253 std::string tags_hash = base::MD5String(tags_list); | 201 std::string tags_hash = base::MD5String(tags_list); |
| 254 std::string tags_hash_msg(kTagsHashTagName); | 202 std::string tags_hash_msg(kTagsHashTagName); |
| 255 tags_hash_msg += "="; | 203 tags_hash_msg += "="; |
| 256 tags_hash_msg += tags_hash; | 204 tags_hash_msg += tags_hash; |
| 257 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary, | 205 cloud_print::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, |
| 258 std::string(), post_data); | 206 mime_boundary, std::string(), post_data); |
| 259 } | 207 } |
| 260 | 208 |
| 261 bool CloudPrintHelpers::IsDryRunJob(const std::vector<std::string>& tags) { | 209 bool CloudPrintHelpers::IsDryRunJob(const std::vector<std::string>& tags) { |
| 262 std::vector<std::string>::const_iterator it; | 210 std::vector<std::string>::const_iterator it; |
| 263 for (it = tags.begin(); it != tags.end(); ++it) { | 211 for (it = tags.begin(); it != tags.end(); ++it) { |
| 264 if (*it == kTagDryRunFlag) | 212 if (*it == kTagDryRunFlag) |
| 265 return true; | 213 return true; |
| 266 } | 214 } |
| 267 return false; | 215 return false; |
| 268 } | 216 } |
| 269 | 217 |
| 270 std::string CloudPrintHelpers::GetCloudPrintAuthHeader() { | 218 std::string CloudPrintHelpers::GetCloudPrintAuthHeader() { |
| 271 std::string header; | 219 std::string header; |
| 272 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); | 220 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); |
| 273 if (!token_store || token_store->token().empty()) { | 221 if (!token_store || token_store->token().empty()) { |
| 274 // Using LOG here for critical errors. GCP connector may run in the headless | 222 // Using LOG here for critical errors. GCP connector may run in the headless |
| 275 // mode and error indication might be useful for user in that case. | 223 // mode and error indication might be useful for user in that case. |
| 276 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; | 224 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; |
| 277 } | 225 } |
| 278 | 226 |
| 279 if (token_store) { | 227 if (token_store) { |
| 280 header = "Authorization: OAuth "; | 228 header = "Authorization: OAuth "; |
| 281 header += token_store->token(); | 229 header += token_store->token(); |
| 282 } | 230 } |
| 283 return header; | 231 return header; |
| 284 } | 232 } |
| 285 | |
| OLD | NEW |