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