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

Side by Side Diff: chrome/common/cloud_print/cloud_print_helpers.cc

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

Powered by Google App Engine
This is Rietveld 408576698