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" | |
8 #include "base/md5.h" | |
9 #include "base/rand_util.h" | |
10 #include "base/string_util.h" | |
11 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
12 #include "base/sys_info.h" | 8 #include "chrome/common/cloud_print/cloud_print_constants.h" |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/common/chrome_version_info.h" | |
15 #include "chrome/common/cloud_print/cloud_print_helpers.h" | 9 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
16 #include "chrome/service/cloud_print/cloud_print_consts.h" | |
17 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 10 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
18 #include "chrome/service/service_process.h" | 11 #include "chrome/service/service_process.h" |
19 | 12 |
20 namespace { | 13 namespace { |
21 | 14 |
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) { | 15 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { |
48 std::string ret; | 16 std::string ret; |
49 switch (status) { | 17 switch (status) { |
50 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: | 18 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: |
51 ret = "IN_PROGRESS"; | 19 ret = "IN_PROGRESS"; |
52 break; | 20 break; |
53 case cloud_print::PRINT_JOB_STATUS_ERROR: | 21 case cloud_print::PRINT_JOB_STATUS_ERROR: |
54 ret = "ERROR"; | 22 ret = "ERROR"; |
55 break; | 23 break; |
56 case cloud_print::PRINT_JOB_STATUS_COMPLETED: | 24 case cloud_print::PRINT_JOB_STATUS_COMPLETED: |
57 ret = "DONE"; | 25 ret = "DONE"; |
58 break; | 26 break; |
59 default: | 27 default: |
60 ret = "UNKNOWN"; | 28 ret = "UNKNOWN"; |
61 NOTREACHED(); | 29 NOTREACHED(); |
62 break; | 30 break; |
63 } | 31 } |
64 return ret; | 32 return ret; |
65 } | 33 } |
66 | 34 |
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 } | 35 } |
75 | 36 |
76 GURL CloudPrintHelpers::GetUrlForPrinterUpdate( | 37 namespace cloud_print { |
77 const GURL& cloud_print_server_url, | 38 |
78 const std::string& printer_id) { | 39 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url, |
79 std::string path( | 40 const std::string& job_id, |
80 cloud_print::AppendPathToUrl(cloud_print_server_url, "update")); | 41 PrintJobStatus status) { |
81 GURL::Replacements replacements; | 42 return GetUrlForJobStatusUpdate(cloud_print_server_url, |
82 replacements.SetPathStr(path); | 43 job_id, |
83 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); | 44 StringFromJobStatus(status)); |
84 replacements.SetQueryStr(query); | |
85 return cloud_print_server_url.ReplaceComponents(replacements); | |
86 } | 45 } |
87 | 46 |
88 GURL CloudPrintHelpers::GetUrlForPrinterDelete( | 47 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url, |
89 const GURL& cloud_print_server_url, | 48 const std::string& job_id, |
90 const std::string& printer_id, | 49 const PrintJobDetails& details) { |
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( | |
128 const GURL& cloud_print_server_url, | |
129 const std::string& job_id, | |
130 cloud_print::PrintJobStatus status) { | |
131 std::string status_string = StringFromJobStatus(status); | |
132 std::string path( | |
133 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); | |
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 } | |
141 | |
142 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate( | |
143 const GURL& cloud_print_server_url, | |
144 const std::string& job_id, | |
145 const cloud_print::PrintJobDetails& details) { | |
146 std::string status_string = StringFromJobStatus(details.status); | 50 std::string status_string = StringFromJobStatus(details.status); |
147 std::string path( | 51 std::string path(AppendPathToUrl(cloud_print_server_url, "control")); |
148 cloud_print::AppendPathToUrl(cloud_print_server_url, "control")); | |
149 GURL::Replacements replacements; | 52 GURL::Replacements replacements; |
150 replacements.SetPathStr(path); | 53 replacements.SetPathStr(path); |
151 std::string query = | 54 std::string query = |
152 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" | 55 StringPrintf("jobid=%s&status=%s&code=%d&message=%s" |
153 "&numpages=%d&pagesprinted=%d", | 56 "&numpages=%d&pagesprinted=%d", |
154 job_id.c_str(), | 57 job_id.c_str(), |
155 status_string.c_str(), | 58 status_string.c_str(), |
156 details.platform_status_flags, | 59 details.platform_status_flags, |
157 details.status_message.c_str(), | 60 details.status_message.c_str(), |
158 details.total_pages, | 61 details.total_pages, |
159 details.pages_printed); | 62 details.pages_printed); |
160 replacements.SetQueryStr(query); | 63 replacements.SetQueryStr(query); |
161 return cloud_print_server_url.ReplaceComponents(replacements); | 64 return cloud_print_server_url.ReplaceComponents(replacements); |
162 } | 65 } |
163 | 66 |
164 GURL CloudPrintHelpers::GetUrlForUserMessage(const GURL& cloud_print_server_url, | 67 std::string GetHashOfPrinterInfo( |
165 const std::string& message_id) { | 68 const printing::PrinterBasicInfo& printer_info) { |
166 std::string path( | 69 return GetHashOfPrinterTags(printer_info.options); |
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 } | 70 } |
174 | 71 |
175 GURL CloudPrintHelpers::GetUrlForGetAuthCode(const GURL& cloud_print_server_url, | 72 std::string GetPostDataForPrinterInfo( |
176 const std::string& oauth_client_id, | 73 const printing::PrinterBasicInfo& printer_info, |
177 const std::string& proxy_id) { | 74 const std::string& mime_boundary) { |
178 // We use the internal API "createrobot" instead of "getauthcode". This API | 75 return GetPostDataForPrinterTags( |
179 // will add the robot as owner to all the existing printers for this user. | 76 printer_info.options, |
180 std::string path( | 77 mime_boundary, |
181 cloud_print::AppendPathToUrl(cloud_print_server_url, "createrobot")); | 78 kCloudPrintServiceProxyTagPrefix, |
182 GURL::Replacements replacements; | 79 kCloudPrintServiceTagsHashTagName); |
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 } | 80 } |
190 | 81 |
191 std::string CloudPrintHelpers::GetHashOfPrinterTags( | 82 bool IsDryRunJob(const std::vector<std::string>& tags) { |
192 const printing::PrinterBasicInfo& printer) { | 83 return std::find(tags.begin(), tags.end(), kCloudPrintServiceTagDryRunFlag) != |
193 PrinterTags printer_tags; | 84 tags.end(); |
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 } | 85 } |
203 | 86 |
204 std::string CloudPrintHelpers::GetPostDataForPrinterTags( | 87 std::string GetCloudPrintAuthHeaderFromStore() { |
205 const printing::PrinterBasicInfo& printer, | |
206 const std::string& mime_boundary) { | |
207 PrinterTags printer_tags; | |
208 GetPrinterTags(printer, &printer_tags); | |
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 } | |
243 | |
244 std::string CloudPrintHelpers::GetCloudPrintAuthHeaderFromStore() { | |
245 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); | 88 CloudPrintTokenStore* token_store = CloudPrintTokenStore::current(); |
246 if (!token_store || token_store->token().empty()) { | 89 if (!token_store || token_store->token().empty()) { |
247 // Using LOG here for critical errors. GCP connector may run in the headless | 90 // 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. | 91 // mode and error indication might be useful for user in that case. |
249 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; | 92 LOG(ERROR) << "CP_PROXY: Missing OAuth token for request"; |
250 return std::string(); | 93 return std::string(); |
251 } | 94 } |
252 return GetCloudPrintAuthHeader(token_store->token()); | 95 return GetCloudPrintAuthHeader(token_store->token()); |
253 } | 96 } |
254 | 97 |
255 std::string CloudPrintHelpers::GetCloudPrintAuthHeader( | 98 } // namespace cloud_print |
256 const std::string& auth_token) { | |
257 std::string header; | |
258 header = "Authorization: OAuth "; | |
259 header += auth_token; | |
260 return header; | |
261 } | |
OLD | NEW |