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