OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_connector.h" | 5 #include "chrome/service/cloud_print/cloud_print_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/md5.h" | 9 #include "base/md5.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 cloud_print_server_url_(cloud_print_server_url), | 29 cloud_print_server_url_(cloud_print_server_url), |
30 next_response_handler_(NULL) { | 30 next_response_handler_(NULL) { |
31 if (print_system_settings) { | 31 if (print_system_settings) { |
32 // It is possible to have no print settings specified. | 32 // It is possible to have no print settings specified. |
33 print_system_settings_.reset(print_system_settings->DeepCopy()); | 33 print_system_settings_.reset(print_system_settings->DeepCopy()); |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 bool CloudPrintConnector::Start() { | 37 bool CloudPrintConnector::Start() { |
38 DCHECK(!print_system_.get()); | 38 DCHECK(!print_system_.get()); |
39 VLOG(1) << "CP_CONNECTOR: Starting connector, id: " << proxy_id_; | 39 VLOG(1) << "CP_CONNECTOR: Starting connector" |
| 40 << ", proxy id: " << proxy_id_; |
40 | 41 |
41 pending_tasks_.clear(); | 42 pending_tasks_.clear(); |
42 | 43 |
43 print_system_ = | 44 print_system_ = |
44 cloud_print::PrintSystem::CreateInstance(print_system_settings_.get()); | 45 cloud_print::PrintSystem::CreateInstance(print_system_settings_.get()); |
45 if (!print_system_.get()) { | 46 if (!print_system_.get()) { |
46 NOTREACHED(); | 47 NOTREACHED(); |
47 return false; // No print system available, fail initalization. | 48 return false; // No print system available, fail initalization. |
48 } | 49 } |
49 cloud_print::PrintSystem::PrintSystemResult result = print_system_->Init(); | 50 cloud_print::PrintSystem::PrintSystemResult result = print_system_->Init(); |
50 if (!result.succeeded()) { | 51 if (!result.succeeded()) { |
51 // We could not initialize the print system. We need to notify the server. | 52 // We could not initialize the print system. We need to notify the server. |
52 ReportUserMessage(kPrintSystemFailedMessageId, result.message()); | 53 ReportUserMessage(kPrintSystemFailedMessageId, result.message()); |
53 print_system_.release(); | 54 print_system_.release(); |
54 return false; | 55 return false; |
55 } | 56 } |
56 | 57 |
57 // Start watching for updates from the print system. | 58 // Start watching for updates from the print system. |
58 print_server_watcher_ = print_system_->CreatePrintServerWatcher(); | 59 print_server_watcher_ = print_system_->CreatePrintServerWatcher(); |
59 print_server_watcher_->StartWatching(this); | 60 print_server_watcher_->StartWatching(this); |
60 | 61 |
61 // Get list of registered printers. | 62 // Get list of registered printers. |
62 AddPendingAvailableTask(); | 63 AddPendingAvailableTask(); |
63 return true; | 64 return true; |
64 } | 65 } |
65 | 66 |
66 void CloudPrintConnector::Stop() { | 67 void CloudPrintConnector::Stop() { |
67 VLOG(1) << "CP_CONNECTOR: Stopping connector, id: " << proxy_id_; | 68 VLOG(1) << "CP_CONNECTOR: Stopping connector" |
| 69 << ", proxy id: " << proxy_id_; |
68 DCHECK(print_system_.get()); | 70 DCHECK(print_system_.get()); |
69 if (print_system_.get()) { | 71 if (print_system_.get()) { |
70 // Do uninitialization here. | 72 // Do uninitialization here. |
71 pending_tasks_.clear(); | 73 pending_tasks_.clear(); |
72 print_server_watcher_.release(); | 74 print_server_watcher_.release(); |
73 print_system_.release(); | 75 print_system_.release(); |
74 } | 76 } |
75 request_ = NULL; | 77 request_ = NULL; |
76 } | 78 } |
77 | 79 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 ContinuePendingTaskProcessing(); // Continue processing background tasks. | 231 ContinuePendingTaskProcessing(); // Continue processing background tasks. |
230 return CloudPrintURLFetcher::STOP_PROCESSING; | 232 return CloudPrintURLFetcher::STOP_PROCESSING; |
231 } | 233 } |
232 | 234 |
233 CloudPrintURLFetcher::ResponseAction | 235 CloudPrintURLFetcher::ResponseAction |
234 CloudPrintConnector::HandlePrinterDeleteResponse( | 236 CloudPrintConnector::HandlePrinterDeleteResponse( |
235 const net::URLFetcher* source, | 237 const net::URLFetcher* source, |
236 const GURL& url, | 238 const GURL& url, |
237 DictionaryValue* json_data, | 239 DictionaryValue* json_data, |
238 bool succeeded) { | 240 bool succeeded) { |
239 VLOG(1) << "CP_CONNECTOR: Handler printer delete response, succeeded:" | 241 VLOG(1) << "CP_CONNECTOR: Handler printer delete response" |
240 << succeeded << " url: " << url; | 242 << ", succeeded: " << succeeded |
| 243 << ", url: " << url; |
241 ContinuePendingTaskProcessing(); // Continue processing background tasks. | 244 ContinuePendingTaskProcessing(); // Continue processing background tasks. |
242 return CloudPrintURLFetcher::STOP_PROCESSING; | 245 return CloudPrintURLFetcher::STOP_PROCESSING; |
243 } | 246 } |
244 | 247 |
245 CloudPrintURLFetcher::ResponseAction | 248 CloudPrintURLFetcher::ResponseAction |
246 CloudPrintConnector::HandleRegisterPrinterResponse( | 249 CloudPrintConnector::HandleRegisterPrinterResponse( |
247 const net::URLFetcher* source, | 250 const net::URLFetcher* source, |
248 const GURL& url, | 251 const GURL& url, |
249 DictionaryValue* json_data, | 252 DictionaryValue* json_data, |
250 bool succeeded) { | 253 bool succeeded) { |
251 VLOG(1) << "CP_CONNECTOR: Handler printer register response, succeeded:" | 254 VLOG(1) << "CP_CONNECTOR: Handler printer register response" |
252 << succeeded << " url: " << url; | 255 << ", succeeded: " << succeeded |
| 256 << ", url: " << url; |
253 if (succeeded) { | 257 if (succeeded) { |
254 ListValue* printer_list = NULL; | 258 ListValue* printer_list = NULL; |
255 // There should be a "printers" value in the JSON | 259 // There should be a "printers" value in the JSON |
256 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list)) { | 260 if (json_data->GetList(cloud_print::kPrinterListValue, &printer_list)) { |
257 DictionaryValue* printer_data = NULL; | 261 DictionaryValue* printer_data = NULL; |
258 if (printer_list->GetDictionary(0, &printer_data)) | 262 if (printer_list->GetDictionary(0, &printer_data)) |
259 InitJobHandlerForPrinter(printer_data); | 263 InitJobHandlerForPrinter(printer_data); |
260 } | 264 } |
261 } | 265 } |
262 ContinuePendingTaskProcessing(); // Continue processing background tasks. | 266 ContinuePendingTaskProcessing(); // Continue processing background tasks. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 319 } |
316 return false; | 320 return false; |
317 } | 321 } |
318 | 322 |
319 void CloudPrintConnector::InitJobHandlerForPrinter( | 323 void CloudPrintConnector::InitJobHandlerForPrinter( |
320 DictionaryValue* printer_data) { | 324 DictionaryValue* printer_data) { |
321 DCHECK(printer_data); | 325 DCHECK(printer_data); |
322 PrinterJobHandler::PrinterInfoFromCloud printer_info_cloud; | 326 PrinterJobHandler::PrinterInfoFromCloud printer_info_cloud; |
323 printer_data->GetString(kIdValue, &printer_info_cloud.printer_id); | 327 printer_data->GetString(kIdValue, &printer_info_cloud.printer_id); |
324 DCHECK(!printer_info_cloud.printer_id.empty()); | 328 DCHECK(!printer_info_cloud.printer_id.empty()); |
325 VLOG(1) << "CP_CONNECTOR: Init job handler for printer id: " | 329 VLOG(1) << "CP_CONNECTOR: Init job handler" |
326 << printer_info_cloud.printer_id; | 330 << ", printer id: " << printer_info_cloud.printer_id; |
327 JobHandlerMap::iterator index = job_handler_map_.find( | 331 JobHandlerMap::iterator index = job_handler_map_.find( |
328 printer_info_cloud.printer_id); | 332 printer_info_cloud.printer_id); |
329 if (index != job_handler_map_.end()) | 333 if (index != job_handler_map_.end()) |
330 return; // Nothing to do if we already have a job handler for this printer. | 334 return; // Nothing to do if we already have a job handler for this printer. |
331 | 335 |
332 printing::PrinterBasicInfo printer_info; | 336 printing::PrinterBasicInfo printer_info; |
333 printer_data->GetString(kNameValue, &printer_info.printer_name); | 337 printer_data->GetString(kNameValue, &printer_info.printer_name); |
334 DCHECK(!printer_info.printer_name.empty()); | 338 DCHECK(!printer_info.printer_name.empty()); |
335 printer_data->GetString(kPrinterDescValue, | 339 printer_data->GetString(kPrinterDescValue, |
336 &printer_info.printer_description); | 340 &printer_info.printer_description); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 void CloudPrintConnector::OnReceivePrinterCaps( | 484 void CloudPrintConnector::OnReceivePrinterCaps( |
481 bool succeeded, | 485 bool succeeded, |
482 const std::string& printer_name, | 486 const std::string& printer_name, |
483 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 487 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
484 if (!IsRunning()) | 488 if (!IsRunning()) |
485 return; // Orphant call. | 489 return; // Orphant call. |
486 DCHECK(pending_tasks_.size() > 0 && | 490 DCHECK(pending_tasks_.size() > 0 && |
487 pending_tasks_.front().type == PENDING_PRINTER_REGISTER); | 491 pending_tasks_.front().type == PENDING_PRINTER_REGISTER); |
488 | 492 |
489 if (!succeeded) { | 493 if (!succeeded) { |
490 LOG(ERROR) << "CP_CONNECTOR: Failed to get printer info for: " << | 494 LOG(ERROR) << "CP_CONNECTOR: Failed to get printer info" |
491 printer_name; | 495 << ", printer name: " << printer_name; |
492 // This printer failed to register, notify the server of this failure. | 496 // This printer failed to register, notify the server of this failure. |
493 string16 printer_name_utf16 = UTF8ToUTF16(printer_name); | 497 string16 printer_name_utf16 = UTF8ToUTF16(printer_name); |
494 std::string status_message = l10n_util::GetStringFUTF8( | 498 std::string status_message = l10n_util::GetStringFUTF8( |
495 IDS_CLOUD_PRINT_REGISTER_PRINTER_FAILED, | 499 IDS_CLOUD_PRINT_REGISTER_PRINTER_FAILED, |
496 printer_name_utf16, | 500 printer_name_utf16, |
497 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); | 501 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); |
498 ReportUserMessage(kGetPrinterCapsFailedMessageId, status_message); | 502 ReportUserMessage(kGetPrinterCapsFailedMessageId, status_message); |
499 | 503 |
500 ContinuePendingTaskProcessing(); // Skip this printer registration. | 504 ContinuePendingTaskProcessing(); // Skip this printer registration. |
501 return; | 505 return; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 mime_type, | 550 mime_type, |
547 post_data, | 551 post_data, |
548 &CloudPrintConnector::HandleRegisterPrinterResponse); | 552 &CloudPrintConnector::HandleRegisterPrinterResponse); |
549 } | 553 } |
550 | 554 |
551 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, | 555 bool CloudPrintConnector::IsSamePrinter(const std::string& name1, |
552 const std::string& name2) const { | 556 const std::string& name2) const { |
553 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); | 557 return (0 == base::strcasecmp(name1.c_str(), name2.c_str())); |
554 } | 558 } |
555 | 559 |
OLD | NEW |