Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.cc

Issue 11038063: Support chrome_to_mobile job receiving Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix format Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util. h"
6
7 #include "base/logging.h"
8 #include "base/md5.h"
9 #include "base/values.h"
10 #include "chrome/common/chrome_version_info.h"
11 #include "chrome/common/cloud_print/cloud_print_helpers.h"
12 #include "chrome/common/cloud_print/cloud_print_consts.h"
13 #include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h"
14 #include "googleurl/src/gurl.h"
15
16 namespace {
17
18 const char* kCloudPrintServerUrl = "http://www.google.com/cloudprint/";
19 const char* kCloudPrintSandboxUrl =
20 "http://cloudprint.sandbox.google.com/cloudprint/";
21
22 const char kDefaultCapabilities[] =
23 "*PPD-Adobe: \"4.3\""
24 "*OpenUI *LanguageLevel/Language Level: PickOne"
25 "*DefaultLanguageLevel: 2"
26 "*LanguageLevel 2/Two 2:"
27 "*CloseUI *LanguageLevel"
28
29 "*OpenUI *ColorDevice/Color Device: PickOne"
30 "*DefaultColorDevice: True"
31 "*ColorDevice True/True:"
32 "*CloseUI *ColorDevice"
33
34 "*OpenUI *ColorSpace/Color Space: PickOne"
35 "*DefaultColorSpace: CMYK"
36 "*ColorSpace CMYK/CMYK:"
37 "*CloseUI *ColorSpace"
38
39 "*OpenUI *TTRasterizer/TT Rasterizer: PickOne"
40 "*DefaultTTRasterizer: Type42"
41 "*TTRasterizer Type42/Type42:"
42 "*CloseUI *TTRasterizer"
43
44 "*OpenUI *FileSystem/File System: PickOne"
45 "*DefaultFileSystem: False"
46 "*FileSystem False/False:"
47 "*CloseUI *FileSystem"
48
49 "*OpenUI *Throughput/Throughput: PickOne"
50 "*DefaultThroughput: 10"
51 "*Throughput 10/10:"
52 "*CloseUI *Throughput";
53
54 const char kPrinterStatusOK[] = "\0";
55
56 } // namespace
57
58 namespace chrome_to_mobile_receive {
59
60 const char kSnapshotIdTag[] = "__snapshot_id";
61 const char kSnapshotTypeTag[] = "__snapshot_type";
62 const char kSnapshotTypeURL[] = "url";
63 const char kSnapshotTypeURLWithDelayedSnapshot[] = "url_with_delayed_snapshot";
64 const char kSnapshotTypeSnapshot[] = "snapshot";
65
66 GURL GetCloudPrintServerURL() {
67 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
68 if (channel == chrome::VersionInfo::CHANNEL_STABLE ||
69 channel == chrome::VersionInfo::CHANNEL_CANARY ||
70 channel == chrome::VersionInfo::CHANNEL_BETA) {
71 return GURL(kCloudPrintServerUrl);
72 } else {
73 return GURL(kCloudPrintSandboxUrl);
74 }
75 }
76
77 std::string GeneratePrinterRegistrationPostData(
78 const std::map<std::string, std::string>& printer_tags,
79 std::string* post_mime_type) {
80 std::string post_mime_boundary;
81 cloud_print::CreateMimeBoundaryForUpload(&post_mime_boundary);
82 *post_mime_type = cloud_print::GetMultipartMimeType(post_mime_boundary);
83
84 std::string post_data = cloud_print::GetPostDataForPrinterTags(
85 printer_tags, post_mime_boundary, kProxyTagPrefix, kTagsHashTagName);
86
87 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterNameValue,
88 GenreatePrinterName(),
89 post_mime_boundary,
90 std::string(),
91 &post_data);
92 cloud_print::AddMultipartValueForUpload(cloud_print::kProxyIdValue,
93 GenerateProxyIdValue(),
94 post_mime_boundary,
95 std::string(),
96 &post_data);
97 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDescValue,
98 GeneratePrinterDescription(),
99 post_mime_boundary,
100 std::string() ,
101 &post_data);
102 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterTypeValue,
103 GeneratePrinterType(),
104 post_mime_boundary,
105 std::string() ,
106 &post_data);
107 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterStatusValue,
108 std::string(kPrinterStatusOK),
109 post_mime_boundary,
110 std::string(),
111 &post_data);
112 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsValue,
113 kDefaultCapabilities,
114 post_mime_boundary,
115 std::string(),
116 &post_data);
117 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDefaultsValue,
118 kDefaultCapabilities,
119 post_mime_boundary,
120 std::string(),
121 &post_data);
122 cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsHashValue,
123 base::MD5String(
124 kDefaultCapabilities),
125 post_mime_boundary,
126 std::string(),
127 &post_data);
128
129 return post_data;
130 }
131
132 std::string GeneratePrinterUpdatePostData(
133 const std::map<std::string, std::string>& printer_tags,
134 std::string* post_mime_type) {
135 std::string post_mime_boundary;
136 cloud_print::CreateMimeBoundaryForUpload(&post_mime_boundary);
137 *post_mime_type = cloud_print::GetMultipartMimeType(post_mime_boundary);
138
139 std::string post_data = cloud_print::GetPostDataForPrinterTags(
140 printer_tags, post_mime_boundary, kProxyTagPrefix, kTagsHashTagName);
141
142 std::string tag_wildcard(kProxyTagPrefix);
143 tag_wildcard += ".*";
144
145 cloud_print::AddMultipartValueForUpload(
146 cloud_print::kPrinterRemoveTagValue, tag_wildcard, post_mime_boundary,
147 std::string(), &post_data);
148
149 return post_data;
150 }
151
152 bool ParseSnapshotURLJobInformationFromCloudPrintFetchJobList(
153 const ListValue* job_list,
154 int index,
155 std::string* job_id,
156 std::string* snapshot_id,
157 std::string* snapshot_type,
158 std::string* original_url,
159 std::string* title,
160 std::string* job_creation_time) {
161 DCHECK(job_id);
162 DCHECK(snapshot_id);
163 DCHECK(snapshot_type);
164 DCHECK(original_url);
165 DCHECK(title);
166 DCHECK(job_creation_time);
167
168 const DictionaryValue* job_data = NULL;
169 if (!job_list->GetDictionary(index, &job_data) || !job_data)
170 return false;
171 // job id is required.
172 if (!job_data->GetString(cloud_print::kIdValue, job_id) || job_id->empty())
173 return false;
174 const ListValue* tags = NULL;
175 if (!job_data->GetList(cloud_print::kTagsValue, &tags) || !tags)
176 return false;
177
178 for (size_t i = 0; i < tags->GetSize(); ++i) {
179 std::string value;
180 if (tags->GetString(i, &value)) {
181 if (value.find(kSnapshotIdTag) == 0)
182 *snapshot_id = value.substr(value.find("=") + 1);
183 else if (value.find(kSnapshotTypeTag) == 0)
184 *snapshot_type = value.substr(value.find("=") + 1);
185 else if (value.find(kSnapshotOriginalUrlTag) == 0 && original_url)
186 *original_url = value.substr(value.find("=") + 1);
187
188 if (!original_url->empty() &&
189 !snapshot_id->empty() &&
190 !snapshot_type->empty()) {
191 break;
192 }
193 }
194 }
195 // snapshot id and type and the original url are required.
196 if (snapshot_id->empty() || snapshot_type->empty() || original_url->empty())
197 return false;
198 // Returns false if it is not a snapshot url job.
199 if (!(snapshot_type->compare(kSnapshotTypeURL) == 0) &&
200 !(snapshot_type->compare(kSnapshotTypeURLWithDelayedSnapshot) == 0)) {
201 return false;
202 }
203
204 job_data->GetString(cloud_print::kTitleValue, title);
205 job_data->GetString(cloud_print::kCreateTimeValue, job_creation_time);
206 return true;
207 }
208
209 bool ParseSnapshotOfflineDataJobInformationFromCloudPrintFetchJobList(
210 const ListValue* job_list,
211 int index,
212 std::string* job_id,
213 std::string* snapshot_id,
214 std::string* offline_data_download_url) {
215 DCHECK(job_id);
216 DCHECK(snapshot_id);
217 DCHECK(offline_data_download_url);
218
219 std::string snapshot_type;
220
221 const DictionaryValue* job_data = NULL;
222 if (!job_list->GetDictionary(index, &job_data) || !job_data)
223 return false;
224 // job id is required.
225 if (!job_data->GetString(cloud_print::kIdValue, job_id) || job_id->empty())
226 return false;
227 const ListValue* tags = NULL;
228 if (!job_data->GetList(cloud_print::kTagsValue, &tags) || !tags)
229 return false;
230
231 for (size_t i = 0; i < tags->GetSize(); ++i) {
232 std::string value;
233 if (tags->GetString(i, &value)) {
234 if (value.find(kSnapshotIdTag) == 0)
235 *snapshot_id = value.substr(value.find("=") + 1);
236 else if (value.find(kSnapshotTypeTag) == 0)
237 snapshot_type = value.substr(value.find("=") + 1);
238 if (!snapshot_id->empty() && !snapshot_type.empty())
239 break;
240 }
241 }
242 if (snapshot_id->empty() || snapshot_type.empty() ||
243 snapshot_type.compare(kSnapshotTypeSnapshot) != 0)
244 return false;
245
246 if (!job_data->GetString(cloud_print::kFileUrlValue,
247 offline_data_download_url) ||
248 offline_data_download_url->empty()) {
249 return false;
250 }
251
252 return true;
253 }
254
255 bool ParseQueuedPrintToPhoneJobFromFetchJobList(
256 const ListValue* job_list,
257 int index,
258 std::string* job_id,
259 std::string* title,
260 std::string* data_download_url) {
261 DCHECK(job_list);
262 DCHECK(job_id);
263 DCHECK(title);
264 DCHECK(data_download_url);
265
266 const DictionaryValue* job_data = NULL;
267 if (!job_list->GetDictionary(index, &job_data) || !job_data)
268 return false;
269 job_data->GetString(cloud_print::kIdValue, job_id);
270
271 std::string status;
272 job_data->GetString("status", &status);
273 if (status.compare("QUEUED") != 0)
274 return false;
275
276 // Checks if it is a print-to-phone job, which should contain no snapshot id
277 // tag.
278 const ListValue* tags = NULL;
279 if (job_data->GetList(cloud_print::kTagsValue, &tags) && tags) {
280 for (size_t i = 0; i < tags->GetSize(); ++i) {
281 std::string value;
282 if (tags->GetString(i, &value) && value.find(kSnapshotIdTag) == 0)
283 return false;
284 }
285 }
286 job_data->GetString(cloud_print::kTitleValue, title);
287 job_data->GetString(cloud_print::kFileUrlValue, data_download_url);
288 return true;
289 }
290
291 } // namespace chrome_to_mobile_receive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698