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

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

Issue 12767006: [Cleanup] Remove StringPrintf from global namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, once more Created 7 years, 9 months 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/md5.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 GURL::Replacements replacements; 69 GURL::Replacements replacements;
70 replacements.SetPathStr(path); 70 replacements.SetPathStr(path);
71 return cloud_print_server_url.ReplaceComponents(replacements); 71 return cloud_print_server_url.ReplaceComponents(replacements);
72 } 72 }
73 73
74 GURL GetUrlForPrinterList(const GURL& cloud_print_server_url, 74 GURL GetUrlForPrinterList(const GURL& cloud_print_server_url,
75 const std::string& proxy_id) { 75 const std::string& proxy_id) {
76 std::string path(AppendPathToUrl(cloud_print_server_url, "list")); 76 std::string path(AppendPathToUrl(cloud_print_server_url, "list"));
77 GURL::Replacements replacements; 77 GURL::Replacements replacements;
78 replacements.SetPathStr(path); 78 replacements.SetPathStr(path);
79 std::string query = StringPrintf("proxy=%s", proxy_id.c_str()); 79 std::string query = base::StringPrintf("proxy=%s", proxy_id.c_str());
80 replacements.SetQueryStr(query); 80 replacements.SetQueryStr(query);
81 return cloud_print_server_url.ReplaceComponents(replacements); 81 return cloud_print_server_url.ReplaceComponents(replacements);
82 } 82 }
83 83
84 GURL GetUrlForPrinterRegistration(const GURL& cloud_print_server_url) { 84 GURL GetUrlForPrinterRegistration(const GURL& cloud_print_server_url) {
85 std::string path(AppendPathToUrl(cloud_print_server_url, "register")); 85 std::string path(AppendPathToUrl(cloud_print_server_url, "register"));
86 GURL::Replacements replacements; 86 GURL::Replacements replacements;
87 replacements.SetPathStr(path); 87 replacements.SetPathStr(path);
88 return cloud_print_server_url.ReplaceComponents(replacements); 88 return cloud_print_server_url.ReplaceComponents(replacements);
89 } 89 }
90 90
91 GURL GetUrlForPrinterUpdate(const GURL& cloud_print_server_url, 91 GURL GetUrlForPrinterUpdate(const GURL& cloud_print_server_url,
92 const std::string& printer_id) { 92 const std::string& printer_id) {
93 std::string path(AppendPathToUrl(cloud_print_server_url, "update")); 93 std::string path(AppendPathToUrl(cloud_print_server_url, "update"));
94 GURL::Replacements replacements; 94 GURL::Replacements replacements;
95 replacements.SetPathStr(path); 95 replacements.SetPathStr(path);
96 std::string query = StringPrintf("printerid=%s", printer_id.c_str()); 96 std::string query = base::StringPrintf("printerid=%s", printer_id.c_str());
97 replacements.SetQueryStr(query); 97 replacements.SetQueryStr(query);
98 return cloud_print_server_url.ReplaceComponents(replacements); 98 return cloud_print_server_url.ReplaceComponents(replacements);
99 } 99 }
100 100
101 GURL GetUrlForPrinterDelete(const GURL& cloud_print_server_url, 101 GURL GetUrlForPrinterDelete(const GURL& cloud_print_server_url,
102 const std::string& printer_id, 102 const std::string& printer_id,
103 const std::string& reason) { 103 const std::string& reason) {
104 std::string path(AppendPathToUrl(cloud_print_server_url, "delete")); 104 std::string path(AppendPathToUrl(cloud_print_server_url, "delete"));
105 GURL::Replacements replacements; 105 GURL::Replacements replacements;
106 replacements.SetPathStr(path); 106 replacements.SetPathStr(path);
107 std::string query = StringPrintf( 107 std::string query = base::StringPrintf(
108 "printerid=%s&reason=%s", printer_id.c_str(), reason.c_str()); 108 "printerid=%s&reason=%s", printer_id.c_str(), reason.c_str());
109 replacements.SetQueryStr(query); 109 replacements.SetQueryStr(query);
110 return cloud_print_server_url.ReplaceComponents(replacements); 110 return cloud_print_server_url.ReplaceComponents(replacements);
111 } 111 }
112 112
113 GURL GetUrlForJobFetch(const GURL& cloud_print_server_url, 113 GURL GetUrlForJobFetch(const GURL& cloud_print_server_url,
114 const std::string& printer_id, 114 const std::string& printer_id,
115 const std::string& reason) { 115 const std::string& reason) {
116 std::string path(AppendPathToUrl(cloud_print_server_url, "fetch")); 116 std::string path(AppendPathToUrl(cloud_print_server_url, "fetch"));
117 GURL::Replacements replacements; 117 GURL::Replacements replacements;
118 replacements.SetPathStr(path); 118 replacements.SetPathStr(path);
119 std::string query = StringPrintf( 119 std::string query = base::StringPrintf(
120 "printerid=%s&deb=%s", printer_id.c_str(), reason.c_str()); 120 "printerid=%s&deb=%s", printer_id.c_str(), reason.c_str());
121 replacements.SetQueryStr(query); 121 replacements.SetQueryStr(query);
122 return cloud_print_server_url.ReplaceComponents(replacements); 122 return cloud_print_server_url.ReplaceComponents(replacements);
123 } 123 }
124 124
125 125
126 GURL GetUrlForJobDelete(const GURL& cloud_print_server_url, 126 GURL GetUrlForJobDelete(const GURL& cloud_print_server_url,
127 const std::string& job_id) { 127 const std::string& job_id) {
128 std::string path(AppendPathToUrl(cloud_print_server_url, "deletejob")); 128 std::string path(AppendPathToUrl(cloud_print_server_url, "deletejob"));
129 GURL::Replacements replacements; 129 GURL::Replacements replacements;
130 replacements.SetPathStr(path); 130 replacements.SetPathStr(path);
131 std::string query = StringPrintf("jobid=%s", job_id.c_str()); 131 std::string query = base::StringPrintf("jobid=%s", job_id.c_str());
132 replacements.SetQueryStr(query); 132 replacements.SetQueryStr(query);
133 return cloud_print_server_url.ReplaceComponents(replacements); 133 return cloud_print_server_url.ReplaceComponents(replacements);
134 } 134 }
135 135
136 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url, 136 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
137 const std::string& job_id, 137 const std::string& job_id,
138 const std::string& status_string) { 138 const std::string& status_string) {
139 std::string path(AppendPathToUrl(cloud_print_server_url, "control")); 139 std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
140 GURL::Replacements replacements; 140 GURL::Replacements replacements;
141 replacements.SetPathStr(path); 141 replacements.SetPathStr(path);
142 std::string query = StringPrintf( 142 std::string query = base::StringPrintf(
143 "jobid=%s&status=%s", job_id.c_str(), status_string.c_str()); 143 "jobid=%s&status=%s", job_id.c_str(), status_string.c_str());
144 replacements.SetQueryStr(query); 144 replacements.SetQueryStr(query);
145 return cloud_print_server_url.ReplaceComponents(replacements); 145 return cloud_print_server_url.ReplaceComponents(replacements);
146 } 146 }
147 147
148 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url, 148 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url,
149 const std::string& message_id) { 149 const std::string& message_id) {
150 std::string path(AppendPathToUrl(cloud_print_server_url, "message")); 150 std::string path(AppendPathToUrl(cloud_print_server_url, "message"));
151 GURL::Replacements replacements; 151 GURL::Replacements replacements;
152 replacements.SetPathStr(path); 152 replacements.SetPathStr(path);
153 std::string query = StringPrintf("code=%s", message_id.c_str()); 153 std::string query = base::StringPrintf("code=%s", message_id.c_str());
154 replacements.SetQueryStr(query); 154 replacements.SetQueryStr(query);
155 return cloud_print_server_url.ReplaceComponents(replacements); 155 return cloud_print_server_url.ReplaceComponents(replacements);
156 } 156 }
157 157
158 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url, 158 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url,
159 const std::string& oauth_client_id, 159 const std::string& oauth_client_id,
160 const std::string& proxy_id) { 160 const std::string& proxy_id) {
161 // We use the internal API "createrobot" instead of "getauthcode". This API 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. 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")); 163 std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot"));
164 GURL::Replacements replacements; 164 GURL::Replacements replacements;
165 replacements.SetPathStr(path); 165 replacements.SetPathStr(path);
166 std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", 166 std::string query = base::StringPrintf("oauth_client_id=%s&proxy=%s",
167 oauth_client_id.c_str(), 167 oauth_client_id.c_str(),
168 proxy_id.c_str()); 168 proxy_id.c_str());
169 replacements.SetQueryStr(query); 169 replacements.SetQueryStr(query);
170 return cloud_print_server_url.ReplaceComponents(replacements); 170 return cloud_print_server_url.ReplaceComponents(replacements);
171 } 171 }
172 172
173 scoped_ptr<base::DictionaryValue> ParseResponseJSON( 173 scoped_ptr<base::DictionaryValue> ParseResponseJSON(
174 const std::string& response_data, 174 const std::string& response_data,
175 bool* succeeded) { 175 bool* succeeded) {
176 scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data)); 176 scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data));
177 if (!message_value.get()) 177 if (!message_value.get())
178 return scoped_ptr<base::DictionaryValue>(); 178 return scoped_ptr<base::DictionaryValue>();
(...skipping 11 matching lines...) Expand all
190 190
191 void AddMultipartValueForUpload(const std::string& value_name, 191 void AddMultipartValueForUpload(const std::string& value_name,
192 const std::string& value, 192 const std::string& value,
193 const std::string& mime_boundary, 193 const std::string& mime_boundary,
194 const std::string& content_type, 194 const std::string& content_type,
195 std::string* post_data) { 195 std::string* post_data) {
196 DCHECK(post_data); 196 DCHECK(post_data);
197 // First line is the boundary 197 // First line is the boundary
198 post_data->append("--" + mime_boundary + "\r\n"); 198 post_data->append("--" + mime_boundary + "\r\n");
199 // Next line is the Content-disposition 199 // Next line is the Content-disposition
200 post_data->append(StringPrintf("Content-Disposition: form-data; " 200 post_data->append(base::StringPrintf("Content-Disposition: form-data; "
201 "name=\"%s\"\r\n", value_name.c_str())); 201 "name=\"%s\"\r\n", value_name.c_str()));
202 if (!content_type.empty()) { 202 if (!content_type.empty()) {
203 // If Content-type is specified, the next line is that 203 // If Content-type is specified, the next line is that
204 post_data->append(StringPrintf("Content-Type: %s\r\n", 204 post_data->append(base::StringPrintf("Content-Type: %s\r\n",
205 content_type.c_str())); 205 content_type.c_str()));
206 } 206 }
207 // Leave an empty line and append the value. 207 // Leave an empty line and append the value.
208 post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); 208 post_data->append(base::StringPrintf("\r\n%s\r\n", value.c_str()));
209 } 209 }
210 210
211 std::string GetMultipartMimeType(const std::string& mime_boundary) { 211 std::string GetMultipartMimeType(const std::string& mime_boundary) {
212 return std::string("multipart/form-data; boundary=") + mime_boundary; 212 return std::string("multipart/form-data; boundary=") + mime_boundary;
213 } 213 }
214 214
215 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). 215 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
216 void CreateMimeBoundaryForUpload(std::string* out) { 216 void CreateMimeBoundaryForUpload(std::string* out) {
217 int r1 = base::RandInt(0, kint32max); 217 int r1 = base::RandInt(0, kint32max);
218 int r2 = base::RandInt(0, kint32max); 218 int r2 = base::RandInt(0, kint32max);
(...skipping 13 matching lines...) Expand all
232 std::string post_data; 232 std::string post_data;
233 for (PrinterTags::const_iterator it = printer_tags_prepared.begin(); 233 for (PrinterTags::const_iterator it = printer_tags_prepared.begin();
234 it != printer_tags_prepared.end(); ++it) { 234 it != printer_tags_prepared.end(); ++it) {
235 // TODO(gene) Escape '=' char from name. Warning for now. 235 // TODO(gene) Escape '=' char from name. Warning for now.
236 if (it->first.find('=') != std::string::npos) { 236 if (it->first.find('=') != std::string::npos) {
237 LOG(WARNING) << 237 LOG(WARNING) <<
238 "CP_PROXY: Printer option name contains '=' character"; 238 "CP_PROXY: Printer option name contains '=' character";
239 NOTREACHED(); 239 NOTREACHED();
240 } 240 }
241 // All our tags have a special prefix to identify them as such. 241 // All our tags have a special prefix to identify them as such.
242 std::string msg = StringPrintf("%s%s=%s", 242 std::string msg = base::StringPrintf("%s%s=%s",
243 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str()); 243 proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
244 AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary, 244 AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
245 std::string(), &post_data); 245 std::string(), &post_data);
246 } 246 }
247 std::string tags_hash_msg = StringPrintf("%s=%s", 247 std::string tags_hash_msg = base::StringPrintf("%s=%s",
248 tags_hash_tag_name.c_str(), 248 tags_hash_tag_name.c_str(),
249 HashPrinterTags(printer_tags_prepared).c_str()); 249 HashPrinterTags(printer_tags_prepared).c_str());
250 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary, 250 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary,
251 std::string(), &post_data); 251 std::string(), &post_data);
252 return post_data; 252 return post_data;
253 } 253 }
254 254
255 std::string GetCloudPrintAuthHeader(const std::string& auth_token) { 255 std::string GetCloudPrintAuthHeader(const std::string& auth_token) {
256 return StringPrintf("Authorization: OAuth %s", auth_token.c_str()); 256 return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str());
257 } 257 }
258 258
259 } // namespace cloud_print 259 } // namespace cloud_print
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698