- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / cloud_print / cloud_print_helpers.cc
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.
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|.
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
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
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
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
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
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 = base::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 = 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
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
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 = base::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                               int connector_code) {
141   std::string path(AppendPathToUrl(cloud_print_server_url, "control"));
142   GURL::Replacements replacements;
143   replacements.SetPathStr(path);
144   std::string query = base::StringPrintf(
145       "jobid=%s&status=%s&connector_code=%d", job_id.c_str(),
146       status_string.c_str(), connector_code);
147   replacements.SetQueryStr(query);
148   return cloud_print_server_url.ReplaceComponents(replacements);
149 }
150
151 GURL GetUrlForUserMessage(const GURL& cloud_print_server_url,
152                           const std::string& message_id) {
153   std::string path(AppendPathToUrl(cloud_print_server_url, "message"));
154   GURL::Replacements replacements;
155   replacements.SetPathStr(path);
156   std::string query = base::StringPrintf("code=%s", message_id.c_str());
157   replacements.SetQueryStr(query);
158   return cloud_print_server_url.ReplaceComponents(replacements);
159 }
160
161 GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url,
162                           const std::string& oauth_client_id,
163                           const std::string& proxy_id) {
164   // We use the internal API "createrobot" instead of "getauthcode". This API
165   // will add the robot as owner to all the existing printers for this user.
166   std::string path(AppendPathToUrl(cloud_print_server_url, "createrobot"));
167   GURL::Replacements replacements;
168   replacements.SetPathStr(path);
169   std::string query = base::StringPrintf("oauth_client_id=%s&proxy=%s",
170                                           oauth_client_id.c_str(),
171                                           proxy_id.c_str());
172   replacements.SetQueryStr(query);
173   return cloud_print_server_url.ReplaceComponents(replacements);
174 }
175
176 scoped_ptr<base::DictionaryValue> ParseResponseJSON(
177     const std::string& response_data,
178     bool* succeeded) {
179   scoped_ptr<base::Value> message_value(base::JSONReader::Read(response_data));
180   if (!message_value.get())
181     return scoped_ptr<base::DictionaryValue>();
182
183   if (!message_value->IsType(base::Value::TYPE_DICTIONARY))
184     return scoped_ptr<base::DictionaryValue>();
185
186   scoped_ptr<base::DictionaryValue> response_dict(
187       static_cast<base::DictionaryValue*>(message_value.release()));
188   if (succeeded &&
189       !response_dict->GetBoolean(kSuccessValue, succeeded))
190     *succeeded = false;
191   return response_dict.Pass();
192 }
193
194 std::string GetMultipartMimeType(const std::string& mime_boundary) {
195   return std::string("multipart/form-data; boundary=") + mime_boundary;
196 }
197
198 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
199 void CreateMimeBoundaryForUpload(std::string* out) {
200   int r1 = base::RandInt(0, kint32max);
201   int r2 = base::RandInt(0, kint32max);
202   base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2);
203 }
204
205 std::string GetHashOfPrinterTags(const PrinterTags& printer_tags) {
206   return HashPrinterTags(PreparePrinterTags(printer_tags));
207 }
208
209 std::string GetPostDataForPrinterTags(
210     const PrinterTags& printer_tags,
211     const std::string& mime_boundary,
212     const std::string& proxy_tag_prefix,
213     const std::string& tags_hash_tag_name) {
214   PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags);
215   std::string post_data;
216   for (PrinterTags::const_iterator it = printer_tags_prepared.begin();
217        it != printer_tags_prepared.end(); ++it) {
218     // TODO(gene) Escape '=' char from name. Warning for now.
219     if (it->first.find('=') != std::string::npos) {
220       LOG(WARNING) <<
221           "CP_PROXY: Printer option name contains '=' character";
222       NOTREACHED();
223     }
224     // All our tags have a special prefix to identify them as such.
225     std::string msg = base::StringPrintf("%s%s=%s",
226         proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
227     net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
228         std::string(), &post_data);
229   }
230   std::string tags_hash_msg = base::StringPrintf("%s=%s",
231       tags_hash_tag_name.c_str(),
232       HashPrinterTags(printer_tags_prepared).c_str());
233   net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg,
234       mime_boundary, std::string(), &post_data);
235   return post_data;
236 }
237
238 std::string GetCloudPrintAuthHeader(const std::string& auth_token) {
239   return base::StringPrintf("Authorization: OAuth %s", auth_token.c_str());
240 }
241
242 }  // namespace cloud_print