• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "chrome/common/cloud_print/cloud_print_helpers.h"
6 
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/md5.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/rand_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/sys_info.h"
14 #include "base/values.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/common/cloud_print/cloud_print_constants.h"
17 #include "net/base/mime_util.h"
18 #include "url/gurl.h"
19 
20 namespace cloud_print {
21 
22 namespace {
23 
24 // Returns printer tags generated from |printer_tags| and the default tags
25 // required by cloud print server.
PreparePrinterTags(const PrinterTags & printer_tags)26 PrinterTags PreparePrinterTags(const PrinterTags& printer_tags) {
27   PrinterTags printer_tags_out = printer_tags;
28   chrome::VersionInfo version_info;
29   DCHECK(version_info.is_valid());
30   printer_tags_out[kChromeVersionTagName] =
31       version_info.CreateVersionString();
32   printer_tags_out[kSystemNameTagName] =
33       base::SysInfo::OperatingSystemName();
34   printer_tags_out[kSystemVersionTagName] =
35       base::SysInfo::OperatingSystemVersion();
36   return printer_tags_out;
37 }
38 
39 // Returns the hash of |printer_tags|.
HashPrinterTags(const PrinterTags & printer_tags)40 std::string HashPrinterTags(const PrinterTags& printer_tags) {
41   std::string values_list;
42   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
51 
AppendPathToUrl(const GURL & url,const std::string & path)52 std::string AppendPathToUrl(const GURL& url, const std::string& path) {
53   DCHECK_NE(path[0], '/');
54   std::string ret = url.path();
55   if (url.has_path() && (ret[ret.length() - 1] != '/'))
56     ret += '/';
57   ret += path;
58   return ret;
59 }
60 
GetUrlForSearch(const GURL & cloud_print_server_url)61 GURL GetUrlForSearch(const GURL& cloud_print_server_url) {
62   std::string path(AppendPathToUrl(cloud_print_server_url, "search"));
63   GURL::Replacements replacements;
64   replacements.SetPathStr(path);
65   return cloud_print_server_url.ReplaceComponents(replacements);
66 }
67 
GetUrlForSubmit(const GURL & cloud_print_server_url)68 GURL GetUrlForSubmit(const GURL& cloud_print_server_url) {
69   std::string path(AppendPathToUrl(cloud_print_server_url, "submit"));
70   GURL::Replacements replacements;
71   replacements.SetPathStr(path);
72   return cloud_print_server_url.ReplaceComponents(replacements);
73 }
74 
GetUrlForPrinterList(const GURL & cloud_print_server_url,const std::string & proxy_id)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 = base::StringPrintf("proxy=%s", proxy_id.c_str());
81   replacements.SetQueryStr(query);
82   return cloud_print_server_url.ReplaceComponents(replacements);
83 }
84 
GetUrlForPrinterRegistration(const GURL & cloud_print_server_url)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 
GetUrlForPrinterUpdate(const GURL & cloud_print_server_url,const std::string & printer_id)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 = base::StringPrintf("printerid=%s", printer_id.c_str());
98   replacements.SetQueryStr(query);
99   return cloud_print_server_url.ReplaceComponents(replacements);
100 }
101 
GetUrlForPrinterDelete(const GURL & cloud_print_server_url,const std::string & printer_id,const std::string & reason)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 = base::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 
GetUrlForJobFetch(const GURL & cloud_print_server_url,const std::string & printer_id,const std::string & reason)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 = base::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 
GetUrlForJobCjt(const GURL & cloud_print_server_url,const std::string & job_id,const std::string & reason)126 GURL GetUrlForJobCjt(const GURL& cloud_print_server_url,
127                      const std::string& job_id,
128                      const std::string& reason) {
129   std::string path(AppendPathToUrl(cloud_print_server_url, "ticket"));
130   GURL::Replacements replacements;
131   replacements.SetPathStr(path);
132   std::string query = base::StringPrintf(
133       "jobid=%s&deb=%s&use_cjt=true", job_id.c_str(), reason.c_str());
134   replacements.SetQueryStr(query);
135   return cloud_print_server_url.ReplaceComponents(replacements);
136 }
137 
GetUrlForJobDelete(const GURL & cloud_print_server_url,const std::string & job_id)138 GURL GetUrlForJobDelete(const GURL& cloud_print_server_url,
139                         const std::string& job_id) {
140   std::string path(AppendPathToUrl(cloud_print_server_url, "deletejob"));
141   GURL::Replacements replacements;
142   replacements.SetPathStr(path);
143   std::string query = base::StringPrintf("jobid=%s", job_id.c_str());
144   replacements.SetQueryStr(query);
145   return cloud_print_server_url.ReplaceComponents(replacements);
146 }
147 
GetUrlForJobStatusUpdate(const GURL & cloud_print_server_url,const std::string & job_id,const std::string & status_string,int connector_code)148 GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url,
149                               const std::string& job_id,
150                               const std::string& status_string,
151                               int connector_code) {
152   std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
153   GURL::Replacements replacements;
154   replacements.SetPathStr(path);
155   std::string query = base::StringPrintf(
156       "jobid=%s&status=%s&connector_code=%d", job_id.c_str(),
157       status_string.c_str(), connector_code);
158   replacements.SetQueryStr(query);
159   return cloud_print_server_url.ReplaceComponents(replacements);
160 }
161 
GetUrlForUserMessage(const GURL & cloud_print_server_url,const std::string & message_id)162 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url,
163                           const std::string& message_id) {
164   std::string path(AppendPathToUrl(cloud_print_server_url, "message"));
165   GURL::Replacements replacements;
166   replacements.SetPathStr(path);
167   std::string query = base::StringPrintf("code=%s", message_id.c_str());
168   replacements.SetQueryStr(query);
169   return cloud_print_server_url.ReplaceComponents(replacements);
170 }
171 
GetUrlForGetAuthCode(const GURL & cloud_print_server_url,const std::string & oauth_client_id,const std::string & proxy_id)172 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url,
173                           const std::string& oauth_client_id,
174                           const std::string& proxy_id) {
175   // We use the internal API "createrobot" instead of "getauthcode". This API
176   // will add the robot as owner to all the existing printers for this user.
177   std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot"));
178   GURL::Replacements replacements;
179   replacements.SetPathStr(path);
180   std::string query = base::StringPrintf("oauth_client_id=%s&proxy=%s",
181                                           oauth_client_id.c_str(),
182                                           proxy_id.c_str());
183   replacements.SetQueryStr(query);
184   return cloud_print_server_url.ReplaceComponents(replacements);
185 }
186 
ParseResponseJSON(const std::string & response_data,bool * succeeded)187 scoped_ptr<base::DictionaryValue> ParseResponseJSON(
188     const std::string& response_data,
189     bool* succeeded) {
190   scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data));
191   if (!message_value.get())
192     return scoped_ptr<base::DictionaryValue>();
193 
194   if (!message_value->IsType(base::Value::TYPE_DICTIONARY))
195     return scoped_ptr<base::DictionaryValue>();
196 
197   scoped_ptr<base::DictionaryValue> response_dict(
198       static_cast<base::DictionaryValue*>(message_value.release()));
199   if (succeeded &&
200       !response_dict->GetBoolean(kSuccessValue, succeeded))
201     *succeeded = false;
202   return response_dict.Pass();
203 }
204 
GetMultipartMimeType(const std::string & mime_boundary)205 std::string GetMultipartMimeType(const std::string& mime_boundary) {
206   return std::string("multipart/form-data; boundary=") + mime_boundary;
207 }
208 
209 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
CreateMimeBoundaryForUpload(std::string * out)210 void CreateMimeBoundaryForUpload(std::string* out) {
211   int r1 = base::RandInt(0, kint32max);
212   int r2 = base::RandInt(0, kint32max);
213   base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2);
214 }
215 
GetHashOfPrinterTags(const PrinterTags & printer_tags)216 std::string GetHashOfPrinterTags(const PrinterTags& printer_tags) {
217   return HashPrinterTags(PreparePrinterTags(printer_tags));
218 }
219 
GetPostDataForPrinterTags(const PrinterTags & printer_tags,const std::string & mime_boundary,const std::string & proxy_tag_prefix,const std::string & tags_hash_tag_name)220 std::string GetPostDataForPrinterTags(
221     const PrinterTags& printer_tags,
222     const std::string& mime_boundary,
223     const std::string& proxy_tag_prefix,
224     const std::string& tags_hash_tag_name) {
225   PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags);
226   std::string post_data;
227   for (PrinterTags::const_iterator it = printer_tags_prepared.begin();
228        it != printer_tags_prepared.end(); ++it) {
229     // TODO(gene) Escape '=' char from name. Warning for now.
230     if (it->first.find('=') != std::string::npos) {
231       LOG(WARNING) <<
232           "CP_PROXY: Printer option name contains '=' character";
233       NOTREACHED();
234     }
235     // All our tags have a special prefix to identify them as such.
236     std::string msg = base::StringPrintf("%s%s=%s",
237         proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
238     net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
239         std::string(), &post_data);
240   }
241   std::string tags_hash_msg = base::StringPrintf("%s=%s",
242       tags_hash_tag_name.c_str(),
243       HashPrinterTags(printer_tags_prepared).c_str());
244   net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg,
245       mime_boundary, std::string(), &post_data);
246   return post_data;
247 }
248 
GetCloudPrintAuthHeader(const std::string & auth_token)249 std::string GetCloudPrintAuthHeader(const std::string& auth_token) {
250   return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str());
251 }
252 
253 }  // namespace cloud_print
254