OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 5 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/md5.h" | 8 #include "base/md5.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/task.h" | 12 #include "base/task.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/service/cloud_print/cloud_print_consts.h" | 15 #include "chrome/service/cloud_print/cloud_print_consts.h" |
16 #include "chrome/common/net/url_fetcher.h" | 16 #include "chrome/common/net/url_fetcher.h" |
17 #include "chrome/service/net/service_url_request_context.h" | |
18 #include "chrome/service/service_process.h" | 17 #include "chrome/service/service_process.h" |
19 | 18 |
20 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { | 19 std::string StringFromJobStatus(cloud_print::PrintJobStatus status) { |
21 std::string ret; | 20 std::string ret; |
22 switch (status) { | 21 switch (status) { |
23 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: | 22 case cloud_print::PRINT_JOB_STATUS_IN_PROGRESS: |
24 ret = "in_progress"; | 23 ret = "in_progress"; |
25 break; | 24 break; |
26 case cloud_print::PRINT_JOB_STATUS_ERROR: | 25 case cloud_print::PRINT_JOB_STATUS_ERROR: |
27 ret = "error"; | 26 ret = "error"; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 details.total_pages, | 130 details.total_pages, |
132 details.pages_printed); | 131 details.pages_printed); |
133 replacements.SetQueryStr(query); | 132 replacements.SetQueryStr(query); |
134 return cloud_print_server_url.ReplaceComponents(replacements); | 133 return cloud_print_server_url.ReplaceComponents(replacements); |
135 } | 134 } |
136 | 135 |
137 bool CloudPrintHelpers::ParseResponseJSON( | 136 bool CloudPrintHelpers::ParseResponseJSON( |
138 const std::string& response_data, bool* succeeded, | 137 const std::string& response_data, bool* succeeded, |
139 DictionaryValue** response_dict) { | 138 DictionaryValue** response_dict) { |
140 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); | 139 scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); |
141 if (!message_value.get()) { | 140 if (!message_value.get()) |
142 NOTREACHED(); | |
143 return false; | 141 return false; |
144 } | 142 |
145 if (!message_value->IsType(Value::TYPE_DICTIONARY)) { | 143 if (!message_value->IsType(Value::TYPE_DICTIONARY)) |
146 NOTREACHED(); | |
147 return false; | 144 return false; |
148 } | 145 |
149 scoped_ptr<DictionaryValue> response_dict_local( | 146 scoped_ptr<DictionaryValue> response_dict_local( |
150 static_cast<DictionaryValue*>(message_value.release())); | 147 static_cast<DictionaryValue*>(message_value.release())); |
151 if (succeeded) | 148 if (succeeded) |
152 response_dict_local->GetBoolean(kSuccessValue, succeeded); | 149 response_dict_local->GetBoolean(kSuccessValue, succeeded); |
153 if (response_dict) | 150 if (response_dict) |
154 *response_dict = response_dict_local.release(); | 151 *response_dict = response_dict_local.release(); |
155 return true; | 152 return true; |
156 } | 153 } |
157 | 154 |
158 void CloudPrintHelpers::PrepCloudPrintRequest(URLFetcher* request, | |
159 const std::string& auth_token) { | |
160 DCHECK(g_service_process); | |
161 request->set_request_context(new ServiceURLRequestContextGetter()); | |
162 std::string headers = "Authorization: GoogleLogin auth="; | |
163 headers += auth_token; | |
164 headers += "\r\n"; | |
165 headers += kChromeCloudPrintProxyHeader; | |
166 request->set_extra_request_headers(headers); | |
167 } | |
168 | |
169 void CloudPrintHelpers::HandleServerError(int* error_count, int max_retry_count, | |
170 int64 max_retry_interval, | |
171 int64 base_retry_interval, | |
172 Task* task_to_retry, | |
173 Task* task_on_give_up) { | |
174 (*error_count)++; | |
175 if ((-1 != max_retry_count) && (*error_count > max_retry_count)) { | |
176 if (task_on_give_up) { | |
177 MessageLoop::current()->PostTask(FROM_HERE, task_on_give_up); | |
178 } | |
179 } else { | |
180 int64 retry_interval = base_retry_interval * (*error_count); | |
181 if ((-1 != max_retry_interval) && (retry_interval > max_retry_interval)) { | |
182 retry_interval = max_retry_interval; | |
183 } | |
184 MessageLoop::current()->PostDelayedTask(FROM_HERE, task_to_retry, | |
185 retry_interval); | |
186 } | |
187 } | |
188 | |
189 void CloudPrintHelpers::AddMultipartValueForUpload( | 155 void CloudPrintHelpers::AddMultipartValueForUpload( |
190 const std::string& value_name, const std::string& value, | 156 const std::string& value_name, const std::string& value, |
191 const std::string& mime_boundary, const std::string& content_type, | 157 const std::string& mime_boundary, const std::string& content_type, |
192 std::string* post_data) { | 158 std::string* post_data) { |
193 DCHECK(post_data); | 159 DCHECK(post_data); |
194 // First line is the boundary | 160 // First line is the boundary |
195 post_data->append("--" + mime_boundary + "\r\n"); | 161 post_data->append("--" + mime_boundary + "\r\n"); |
196 // Next line is the Content-disposition | 162 // Next line is the Content-disposition |
197 post_data->append(StringPrintf("Content-Disposition: form-data; " | 163 post_data->append(StringPrintf("Content-Disposition: form-data; " |
198 "name=\"%s\"\r\n", value_name.c_str())); | 164 "name=\"%s\"\r\n", value_name.c_str())); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 std::string(), post_data); | 220 std::string(), post_data); |
255 } | 221 } |
256 std::string tags_hash = MD5String(tags_list); | 222 std::string tags_hash = MD5String(tags_list); |
257 std::string tags_hash_msg(kTagsHashTagName); | 223 std::string tags_hash_msg(kTagsHashTagName); |
258 tags_hash_msg += "="; | 224 tags_hash_msg += "="; |
259 tags_hash_msg += tags_hash; | 225 tags_hash_msg += tags_hash; |
260 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary, | 226 AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary, |
261 std::string(), post_data); | 227 std::string(), post_data); |
262 } | 228 } |
263 | 229 |
OLD | NEW |