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

Side by Side Diff: chrome/browser/printing/cloud_print/cloud_print_helpers.cc

Issue 1566047: First cut of Cloud Print Proxy implementation. The code is not enabled for no... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Final review changes Created 10 years, 8 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/browser/printing/cloud_print/cloud_print_helpers.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/rand_util.h"
9 #include "base/scoped_ptr.h"
10 #include "base/string_util.h"
11 #include "base/task.h"
12 #include "base/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/net/url_fetcher.h"
15 #include "chrome/browser/printing/cloud_print/cloud_print_consts.h"
16 #include "chrome/browser/profile.h"
17
18 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) {
19 std::string ret;
20 switch (status) {
21 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS:
22 ret = "in_progress";
23 break;
24 case cloud_print::PRINT_JOB_STATUS_ERROR:
25 ret = "error";
26 break;
27 case cloud_print::PRINT_JOB_STATUS_COMPLETED:
28 ret = "done";
29 break;
30 default:
31 ret = "unknown";
32 NOTREACHED();
33 break;
34 }
35 return ret;
36 }
37
38 GURL CloudPrintHelpers::GetUrlForPrinterRegistration() {
39 return GURL(StringPrintf("%s/printing/register", kCloudPrintServerUrl));
40 }
41
42 GURL CloudPrintHelpers::GetUrlForPrinterUpdate(const std::string& printer_id) {
43 return GURL(StringPrintf("%s/printing/update?printerid=%s",
44 kCloudPrintServerUrl, printer_id.c_str()));
45 }
46
47 GURL CloudPrintHelpers::GetUrlForPrinterDelete(const std::string& printer_id) {
48 return GURL(StringPrintf("%s/printing/delete?printerid=%s",
49 kCloudPrintServerUrl, printer_id.c_str()));
50 }
51
52 GURL CloudPrintHelpers::GetUrlForPrinterList(const std::string& proxy_id) {
53 return GURL(StringPrintf(
54 "%s/printing/list?proxy=%s",
55 kCloudPrintServerUrl, proxy_id.c_str()));
56 }
57
58 GURL CloudPrintHelpers::GetUrlForJobFetch(const std::string& printer_id) {
59 return GURL(StringPrintf(
60 "%s/printing/fetch?printerid=%s",
61 kCloudPrintServerUrl, printer_id.c_str()));
62 }
63
64 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate(
65 const std::string& job_id, cloud_print::PrintJobStatus status) {
66 std::string status_string = StringFromJobStatus(status);
67 return GURL(StringPrintf(
68 "%s/printing/control?jobid=%s&status=%s",
69 kCloudPrintServerUrl, job_id.c_str(), status_string.c_str()));
70 }
71
72 GURL CloudPrintHelpers::GetUrlForJobStatusUpdate(
73 const std::string& job_id, const cloud_print::PrintJobDetails& details) {
74 std::string status_string = StringFromJobStatus(details.status);
75 return GURL(StringPrintf(
76 "%s/printing/control?jobid=%s&status=%s&code=%d&message=%s"
77 "&numpages=%d&pagesprinted=%d",
78 kCloudPrintServerUrl, job_id.c_str(), status_string.c_str(),
79 details.platform_status_flags, details.status_message.c_str(),
80 details.total_pages, details.pages_printed));
81 }
82
83 bool CloudPrintHelpers::ParseResponseJSON(
84 const std::string& response_data, bool* succeeded,
85 DictionaryValue** response_dict) {
86 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false));
87 DCHECK(message_value.get());
88 if (!message_value.get()) {
89 return false;
90 }
91 if (!message_value->IsType(Value::TYPE_DICTIONARY)) {
92 NOTREACHED();
93 return false;
94 }
95 DictionaryValue* response_dict_local =
96 static_cast<DictionaryValue*>(message_value.get());
97 if (succeeded) {
98 response_dict_local->GetBoolean(kSuccessValue, succeeded);
99 }
100 if (response_dict) {
101 *response_dict = response_dict_local;
102 message_value.release();
103 }
104 return true;
105 }
106
107 void CloudPrintHelpers::PrepCloudPrintRequest(URLFetcher* request,
108 const std::string& auth_token) {
109 request->set_request_context(
110 Profile::GetDefaultRequestContext());
111 std::string headers = "Authorization: GoogleLogin auth=";
112 headers += auth_token;
113 request->set_extra_request_headers(headers);
114 }
115
116 void CloudPrintHelpers::HandleServerError(int* error_count, int max_retry_count,
117 int64 max_retry_interval,
118 int64 base_retry_interval,
119 Task* task_to_retry,
120 Task* task_on_give_up) {
121 (*error_count)++;
122 if ((-1 != max_retry_count) && (*error_count > max_retry_count)) {
123 if (task_on_give_up) {
124 MessageLoop::current()->PostTask(FROM_HERE, task_on_give_up);
125 }
126 } else {
127 int64 retry_interval = base_retry_interval * (*error_count);
128 if ((-1 != max_retry_interval) && (retry_interval > max_retry_interval)) {
129 retry_interval = max_retry_interval;
130 }
131 MessageLoop::current()->PostDelayedTask(FROM_HERE, task_to_retry,
132 retry_interval);
133 }
134 }
135
136 void CloudPrintHelpers::AddMultipartValueForUpload(
137 const std::string& value_name, const std::string& value,
138 const std::string& mime_boundary, const std::string& content_type,
139 std::string* post_data) {
140 DCHECK(post_data);
141 // First line is the boundary
142 post_data->append("--" + mime_boundary + "\r\n");
143 // Next line is the Content-disposition
144 post_data->append(StringPrintf("Content-Disposition: form-data; "
145 "name=\"%s\"\r\n", value_name.c_str()));
146 if (!content_type.empty()) {
147 // If Content-type is specified, the next line is that
148 post_data->append(StringPrintf("Content-Type: %s\r\n",
149 content_type.c_str()));
150 }
151 // Leave an empty line and append the value.
152 post_data->append(StringPrintf("\r\n%s\r\n", value.c_str()));
153 }
154
155 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
156 void CloudPrintHelpers::CreateMimeBoundaryForUpload(std::string* out) {
157 int r1 = base::RandInt(0, kint32max);
158 int r2 = base::RandInt(0, kint32max);
159 SStringPrintf(out, "---------------------------%08X%08X", r1, r2);
160 }
161
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698