Index: chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.cc |
diff --git a/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.cc b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..feed23c6c225cd8f02c34a8331583d386ca0bac2 |
--- /dev/null |
+++ b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.cc |
@@ -0,0 +1,291 @@ |
+// 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. |
+ |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_util.h" |
+ |
+#include "base/logging.h" |
+#include "base/md5.h" |
+#include "base/values.h" |
+#include "chrome/common/chrome_version_info.h" |
+#include "chrome/common/cloud_print/cloud_print_helpers.h" |
+#include "chrome/common/cloud_print/cloud_print_consts.h" |
+#include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h" |
+#include "googleurl/src/gurl.h" |
+ |
+namespace { |
+ |
+const char* kCloudPrintServerUrl = "http://www.google.com/cloudprint/"; |
+const char* kCloudPrintSandboxUrl = |
+ "http://cloudprint.sandbox.google.com/cloudprint/"; |
+ |
+const char kDefaultCapabilities[] = |
+ "*PPD-Adobe: \"4.3\"" |
+ "*OpenUI *LanguageLevel/Language Level: PickOne" |
+ "*DefaultLanguageLevel: 2" |
+ "*LanguageLevel 2/Two 2:" |
+ "*CloseUI *LanguageLevel" |
+ |
+ "*OpenUI *ColorDevice/Color Device: PickOne" |
+ "*DefaultColorDevice: True" |
+ "*ColorDevice True/True:" |
+ "*CloseUI *ColorDevice" |
+ |
+ "*OpenUI *ColorSpace/Color Space: PickOne" |
+ "*DefaultColorSpace: CMYK" |
+ "*ColorSpace CMYK/CMYK:" |
+ "*CloseUI *ColorSpace" |
+ |
+ "*OpenUI *TTRasterizer/TT Rasterizer: PickOne" |
+ "*DefaultTTRasterizer: Type42" |
+ "*TTRasterizer Type42/Type42:" |
+ "*CloseUI *TTRasterizer" |
+ |
+ "*OpenUI *FileSystem/File System: PickOne" |
+ "*DefaultFileSystem: False" |
+ "*FileSystem False/False:" |
+ "*CloseUI *FileSystem" |
+ |
+ "*OpenUI *Throughput/Throughput: PickOne" |
+ "*DefaultThroughput: 10" |
+ "*Throughput 10/10:" |
+ "*CloseUI *Throughput"; |
+ |
+const char kPrinterStatusOK[] = "\0"; |
+ |
+} // namespace |
+ |
+namespace chrome_to_mobile_receive { |
+ |
+const char kSnapshotIdTag[] = "__snapshot_id"; |
+const char kSnapshotTypeTag[] = "__snapshot_type"; |
+const char kSnapshotTypeURL[] = "url"; |
+const char kSnapshotTypeURLWithDelayedSnapshot[] = "url_with_delayed_snapshot"; |
+const char kSnapshotTypeSnapshot[] = "snapshot"; |
+ |
+GURL GetCloudPrintServerURL() { |
+ chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
+ if (channel == chrome::VersionInfo::CHANNEL_STABLE || |
+ channel == chrome::VersionInfo::CHANNEL_CANARY || |
+ channel == chrome::VersionInfo::CHANNEL_BETA) { |
+ return GURL(kCloudPrintServerUrl); |
+ } else { |
+ return GURL(kCloudPrintSandboxUrl); |
+ } |
+} |
+ |
+std::string GeneratePrinterRegistrationPostData( |
+ const std::map<std::string, std::string>& printer_tags, |
+ std::string* post_mime_type) { |
+ std::string post_mime_boundary; |
+ cloud_print::CreateMimeBoundaryForUpload(&post_mime_boundary); |
+ *post_mime_type = cloud_print::GetMultipartMimeType(post_mime_boundary); |
+ |
+ std::string post_data = cloud_print::GetPostDataForPrinterTags( |
+ printer_tags, post_mime_boundary, kProxyTagPrefix, kTagsHashTagName); |
+ |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterNameValue, |
+ GenreatePrinterName(), |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kProxyIdValue, |
+ GenerateProxyIdValue(), |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDescValue, |
+ GeneratePrinterDescription(), |
+ post_mime_boundary, |
+ std::string() , |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterTypeValue, |
+ GeneratePrinterType(), |
+ post_mime_boundary, |
+ std::string() , |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterStatusValue, |
+ std::string(kPrinterStatusOK), |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsValue, |
+ kDefaultCapabilities, |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterDefaultsValue, |
+ kDefaultCapabilities, |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ cloud_print::AddMultipartValueForUpload(cloud_print::kPrinterCapsHashValue, |
+ base::MD5String( |
+ kDefaultCapabilities), |
+ post_mime_boundary, |
+ std::string(), |
+ &post_data); |
+ |
+ return post_data; |
+} |
+ |
+std::string GeneratePrinterUpdatePostData( |
+ const std::map<std::string, std::string>& printer_tags, |
+ std::string* post_mime_type) { |
+ std::string post_mime_boundary; |
+ cloud_print::CreateMimeBoundaryForUpload(&post_mime_boundary); |
+ *post_mime_type = cloud_print::GetMultipartMimeType(post_mime_boundary); |
+ |
+ std::string post_data = cloud_print::GetPostDataForPrinterTags( |
+ printer_tags, post_mime_boundary, kProxyTagPrefix, kTagsHashTagName); |
+ |
+ std::string tag_wildcard(kProxyTagPrefix); |
+ tag_wildcard += ".*"; |
+ |
+ cloud_print::AddMultipartValueForUpload( |
+ cloud_print::kPrinterRemoveTagValue, tag_wildcard, post_mime_boundary, |
+ std::string(), &post_data); |
+ |
+ return post_data; |
+} |
+ |
+bool ParseSnapshotURLJobInformationFromCloudPrintFetchJobList( |
+ const ListValue* job_list, |
+ int index, |
+ std::string* job_id, |
+ std::string* snapshot_id, |
+ std::string* snapshot_type, |
+ std::string* original_url, |
+ std::string* title, |
+ std::string* job_creation_time) { |
+ DCHECK(job_id); |
+ DCHECK(snapshot_id); |
+ DCHECK(snapshot_type); |
+ DCHECK(original_url); |
+ DCHECK(title); |
+ DCHECK(job_creation_time); |
+ |
+ const DictionaryValue* job_data = NULL; |
+ if (!job_list->GetDictionary(index, &job_data) || !job_data) |
+ return false; |
+ // job id is required. |
+ if (!job_data->GetString(cloud_print::kIdValue, job_id) || job_id->empty()) |
+ return false; |
+ const ListValue* tags = NULL; |
+ if (!job_data->GetList(cloud_print::kTagsValue, &tags) || !tags) |
+ return false; |
+ |
+ for (size_t i = 0; i < tags->GetSize(); ++i) { |
+ std::string value; |
+ if (tags->GetString(i, &value)) { |
+ if (value.find(kSnapshotIdTag) == 0) |
+ *snapshot_id = value.substr(value.find("=") + 1); |
+ else if (value.find(kSnapshotTypeTag) == 0) |
+ *snapshot_type = value.substr(value.find("=") + 1); |
+ else if (value.find(kSnapshotOriginalUrlTag) == 0 && original_url) |
+ *original_url = value.substr(value.find("=") + 1); |
+ |
+ if (!original_url->empty() && |
+ !snapshot_id->empty() && |
+ !snapshot_type->empty()) { |
+ break; |
+ } |
+ } |
+ } |
+ // snapshot id and type and the original url are required. |
+ if (snapshot_id->empty() || snapshot_type->empty() || original_url->empty()) |
+ return false; |
+ // Returns false if it is not a snapshot url job. |
+ if (!(snapshot_type->compare(kSnapshotTypeURL) == 0) && |
+ !(snapshot_type->compare(kSnapshotTypeURLWithDelayedSnapshot) == 0)) { |
+ return false; |
+ } |
+ |
+ job_data->GetString(cloud_print::kTitleValue, title); |
+ job_data->GetString(cloud_print::kCreateTimeValue, job_creation_time); |
+ return true; |
+} |
+ |
+bool ParseSnapshotOfflineDataJobInformationFromCloudPrintFetchJobList( |
+ const ListValue* job_list, |
+ int index, |
+ std::string* job_id, |
+ std::string* snapshot_id, |
+ std::string* offline_data_download_url) { |
+ DCHECK(job_id); |
+ DCHECK(snapshot_id); |
+ DCHECK(offline_data_download_url); |
+ |
+ std::string snapshot_type; |
+ |
+ const DictionaryValue* job_data = NULL; |
+ if (!job_list->GetDictionary(index, &job_data) || !job_data) |
+ return false; |
+ // job id is required. |
+ if (!job_data->GetString(cloud_print::kIdValue, job_id) || job_id->empty()) |
+ return false; |
+ const ListValue* tags = NULL; |
+ if (!job_data->GetList(cloud_print::kTagsValue, &tags) || !tags) |
+ return false; |
+ |
+ for (size_t i = 0; i < tags->GetSize(); ++i) { |
+ std::string value; |
+ if (tags->GetString(i, &value)) { |
+ if (value.find(kSnapshotIdTag) == 0) |
+ *snapshot_id = value.substr(value.find("=") + 1); |
+ else if (value.find(kSnapshotTypeTag) == 0) |
+ snapshot_type = value.substr(value.find("=") + 1); |
+ if (!snapshot_id->empty() && !snapshot_type.empty()) |
+ break; |
+ } |
+ } |
+ if (snapshot_id->empty() || snapshot_type.empty() || |
+ snapshot_type.compare(kSnapshotTypeSnapshot) != 0) |
+ return false; |
+ |
+ if (!job_data->GetString(cloud_print::kFileUrlValue, |
+ offline_data_download_url) || |
+ offline_data_download_url->empty()) { |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool ParseQueuedPrintToPhoneJobFromFetchJobList( |
+ const ListValue* job_list, |
+ int index, |
+ std::string* job_id, |
+ std::string* title, |
+ std::string* data_download_url) { |
+ DCHECK(job_list); |
+ DCHECK(job_id); |
+ DCHECK(title); |
+ DCHECK(data_download_url); |
+ |
+ const DictionaryValue* job_data = NULL; |
+ if (!job_list->GetDictionary(index, &job_data) || !job_data) |
+ return false; |
+ job_data->GetString(cloud_print::kIdValue, job_id); |
+ |
+ std::string status; |
+ job_data->GetString("status", &status); |
+ if (status.compare("QUEUED") != 0) |
+ return false; |
+ |
+ // Checks if it is a print-to-phone job, which should contain no snapshot id |
+ // tag. |
+ const ListValue* tags = NULL; |
+ if (job_data->GetList(cloud_print::kTagsValue, &tags) && tags) { |
+ for (size_t i = 0; i < tags->GetSize(); ++i) { |
+ std::string value; |
+ if (tags->GetString(i, &value) && value.find(kSnapshotIdTag) == 0) |
+ return false; |
+ } |
+ } |
+ job_data->GetString(cloud_print::kTitleValue, title); |
+ job_data->GetString(cloud_print::kFileUrlValue, data_download_url); |
+ return true; |
+} |
+ |
+} // namespace chrome_to_mobile_receive |