Chromium Code Reviews| Index: chrome/common/cloud_print/cloud_print_helpers.cc |
| diff --git a/chrome/common/cloud_print/cloud_print_helpers.cc b/chrome/common/cloud_print/cloud_print_helpers.cc |
| index dbe7544e4b7fd3c51618c3243daee7fc466e957a..a096ee8bdb83903156587889194d4a2a9b2b34c9 100644 |
| --- a/chrome/common/cloud_print/cloud_print_helpers.cc |
| +++ b/chrome/common/cloud_print/cloud_print_helpers.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -6,19 +6,48 @@ |
| #include "base/json/json_reader.h" |
| #include "base/logging.h" |
| +#include "base/md5.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/rand_util.h" |
| #include "base/stringprintf.h" |
| +#include "base/sys_info.h" |
| #include "base/values.h" |
| #include "googleurl/src/gurl.h" |
| +#include "chrome/common/chrome_version_info.h" |
| +#include "chrome/common/cloud_print/cloud_print_constants.h" |
| -namespace cloud_print { |
| +namespace { |
| + |
| +// Returns printer tags generated from |printer_tags| and the default tags |
| +// required by cloud print server. |
| +cloud_print::PrinterTags PreparePrinterTags( |
|
msw
2012/11/17 00:22:30
nit: move the anon namespace into the cloud_print
Chen Yu
2012/11/26 12:07:06
Done.
|
| + const cloud_print::PrinterTags& printer_tags) { |
| + cloud_print::PrinterTags printer_tags_out = printer_tags; |
| + chrome::VersionInfo version_info; |
| + DCHECK(version_info.is_valid()); |
| + printer_tags_out[cloud_print::kChromeVersionTagName] = |
| + version_info.CreateVersionString(); |
| + using base::SysInfo; |
|
msw
2012/11/17 00:22:30
nit: this doesn't help much, just specify base:: i
Chen Yu
2012/11/26 12:07:06
Done.
|
| + printer_tags_out[cloud_print::kSystemNameTagName] = |
| + SysInfo::OperatingSystemName(); |
| + printer_tags_out[cloud_print::kSystemVersionTagName] = |
| + SysInfo::OperatingSystemVersion(); |
| + return printer_tags_out; |
| +} |
| -const char kPrinterListValue[] = "printers"; |
| -const char kSuccessValue[] = "success"; |
| +std::string HashPrinterTags(const cloud_print::PrinterTags& strings) { |
|
msw
2012/11/17 00:22:30
nit: comment.
Chen Yu
2012/11/26 12:07:06
Done.
|
| + std::string values_list; |
| + cloud_print::PrinterTags::const_iterator it; |
| + for (it = strings.begin(); it != strings.end(); ++it) { |
| + values_list.append(it->first); |
| + values_list.append(it->second); |
| + } |
| + return base::MD5String(values_list); |
| +} |
| -// Certain cloud print requests require Chrome's X-CloudPrint-Proxy header. |
| -const char kChromeCloudPrintProxyHeader[] = "X-CloudPrint-Proxy: Chrome"; |
| +} // namespace |
| + |
| +namespace cloud_print { |
| std::string AppendPathToUrl(const GURL& url, const std::string& path) { |
| DCHECK_NE(path[0], '/'); |
| @@ -43,6 +72,114 @@ GURL GetUrlForSubmit(const GURL& cloud_print_server_url) { |
| return cloud_print_server_url.ReplaceComponents(replacements); |
| } |
| +GURL GetUrlForPrinterList(const GURL& cloud_print_server_url, |
| + const std::string& proxy_id) { |
| + std::string path(cloud_print::AppendPathToUrl( |
|
msw
2012/11/17 00:22:30
nit: nix all the cloud_print:: specifiers.
Chen Yu
2012/11/26 12:07:06
Done.
|
| + cloud_print_server_url, "list")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf("proxy=%s", proxy_id.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForPrinterRegistration(const GURL& cloud_print_server_url) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "register")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForPrinterUpdate(const GURL& cloud_print_server_url, |
| + const std::string& printer_id) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "update")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf("printerid=%s", printer_id.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForPrinterDelete(const GURL& cloud_print_server_url, |
| + const std::string& printer_id, |
| + const std::string& reason) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "delete")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf( |
| + "printerid=%s&reason=%s", printer_id.c_str(), reason.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForJobFetch(const GURL& cloud_print_server_url, |
| + const std::string& printer_id, |
| + const std::string& reason) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "fetch")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf( |
| + "printerid=%s&deb=%s", printer_id.c_str(), reason.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| + |
| +GURL GetUrlForJobDelete(const GURL& cloud_print_server_url, |
| + const std::string& job_id) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "deletejob")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf("jobid=%s", job_id.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForJobStatusUpdate(const GURL& cloud_print_server_url, |
| + const std::string& job_id, |
| + const std::string& status_string) { |
| + std::string path(cloud_print::AppendPathToUrl( |
| + cloud_print_server_url, "control")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf( |
| + "jobid=%s&status=%s", job_id.c_str(), status_string.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForUserMessage(const GURL& cloud_print_server_url, |
| + const std::string& message_id) { |
| + std::string path( |
| + cloud_print::AppendPathToUrl(cloud_print_server_url, "message")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf("code=%s", message_id.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| +GURL GetUrlForGetAuthCode(const GURL& cloud_print_server_url, |
| + const std::string& oauth_client_id, |
| + const std::string& proxy_id) { |
| + // We use the internal API "createrobot" instead of "getauthcode". This API |
| + // will add the robot as owner to all the existing printers for this user. |
| + std::string path( |
| + cloud_print::AppendPathToUrl(cloud_print_server_url, "createrobot")); |
| + GURL::Replacements replacements; |
| + replacements.SetPathStr(path); |
| + std::string query = StringPrintf("oauth_client_id=%s&proxy=%s", |
| + oauth_client_id.c_str(), |
| + proxy_id.c_str()); |
| + replacements.SetQueryStr(query); |
| + return cloud_print_server_url.ReplaceComponents(replacements); |
| +} |
| + |
| bool ParseResponseJSON(const std::string& response_data, |
| bool* succeeded, |
| DictionaryValue** response_dict) { |
| @@ -83,6 +220,10 @@ void AddMultipartValueForUpload(const std::string& value_name, |
| post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); |
| } |
| +std::string GetMultipartMimeType(const std::string& mime_boundary) { |
| + return std::string("multipart/form-data; boundary=") += mime_boundary; |
|
msw
2012/11/17 00:22:30
Why +=? shouldn't this just be +?
Chen Yu
2012/11/26 12:07:06
Done.
|
| +} |
| + |
| // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). |
| void CreateMimeBoundaryForUpload(std::string* out) { |
| int r1 = base::RandInt(0, kint32max); |
| @@ -90,4 +231,65 @@ void CreateMimeBoundaryForUpload(std::string* out) { |
| base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); |
| } |
| +std::string GetHashOfPrinterTags(const PrinterTags& printer_tags) { |
| + PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags); |
| + std::string values_list; |
| + for (PrinterTags::const_iterator it = printer_tags_prepared.begin(); |
| + it != printer_tags_prepared.end(); ++it) { |
|
msw
2012/11/17 00:22:30
nit: I prefer wrapping within the parens (indent o
Chen Yu
2012/11/26 12:07:06
Changed to call HashPrinterTags()
On 2012/11/17 0
|
| + values_list.append(it->first); |
| + values_list.append(it->second); |
| + } |
| + return base::MD5String(values_list); |
| +} |
| + |
| +std::string GetPostDataForPrinterTags( |
| + const PrinterTags& printer_tags, |
| + const std::string& mime_boundary, |
| + const std::string& proxy_tag_prefix, |
| + const std::string& tags_hash_tag_name) { |
| + PrinterTags printer_tags_prepared = PreparePrinterTags(printer_tags); |
| + std::string post_data; |
| + for (PrinterTags::const_iterator it = printer_tags.begin(); |
| + it != printer_tags.end(); ++it) { |
|
msw
2012/11/17 00:22:30
ditto nit: indent one more space.
Chen Yu
2012/11/26 12:07:06
Done.
|
| + // TODO(gene) Escape '=' char from name. Warning for now. |
| + if (it->first.find('=') != std::string::npos) { |
| + LOG(WARNING) << |
| + "CP_PROXY: Printer option name contains '=' character"; |
| + NOTREACHED(); |
| + } |
| + // All our tags have a special prefix to identify them as such. |
| + std::string msg(proxy_tag_prefix); |
|
msw
2012/11/17 00:22:30
nit: use stringprintf here
Chen Yu
2012/11/26 12:07:06
Done.
|
| + msg += it->first; |
| + msg += "="; |
| + msg += it->second; |
| + cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterTagValue, msg, |
| + mime_boundary, std::string(), &post_data); |
| + } |
| + std::string tags_hash_msg(tags_hash_tag_name); |
|
msw
2012/11/17 00:22:30
nit: use stringprintf here
Chen Yu
2012/11/26 12:07:06
Done.
|
| + tags_hash_msg += "="; |
| + tags_hash_msg += HashPrinterTags(printer_tags); |
| + cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterTagValue, |
| + tags_hash_msg, |
| + mime_boundary, std::string(), |
| + &post_data); |
| + return post_data; |
| +} |
| + |
| +bool IsDryRunJob(const std::vector<std::string>& tags, |
| + const std::string& tag_dry_run_flag) { |
| + std::vector<std::string>::const_iterator it; |
| + for (it = tags.begin(); it != tags.end(); ++it) { |
|
msw
2012/11/17 00:22:30
return std::find(tags.begin(), tags.end(), tag_dry
Chen Yu
2012/11/26 12:07:06
Done.
|
| + if (*it == tag_dry_run_flag) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +std::string GetCloudPrintAuthHeader(const std::string& auth_token) { |
| + std::string header; |
|
msw
2012/11/17 00:22:30
nit: use stringprintf here
Chen Yu
2012/11/26 12:07:06
Done.
|
| + header = "Authorization: OAuth "; |
| + header += auth_token; |
| + return header; |
| +} |
| + |
| } // namespace cloud_print |