| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/local_discovery/cloud_print_printer_list.h" |
| 6 |
| 5 #include <utility> | 7 #include <utility> |
| 6 | 8 |
| 7 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" | |
| 9 #include "chrome/common/cloud_print/cloud_print_constants.h" | 10 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 10 #include "components/cloud_devices/common/cloud_devices_urls.h" | 11 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 11 | 12 |
| 12 namespace local_discovery { | 13 namespace local_discovery { |
| 13 | 14 |
| 14 CloudPrintPrinterList::CloudPrintPrinterList( | 15 CloudPrintPrinterList::CloudPrintPrinterList( |
| 15 net::URLRequestContextGetter* request_context, | 16 net::URLRequestContextGetter* request_context, |
| 16 OAuth2TokenService* token_service, | 17 OAuth2TokenService* token_service, |
| 17 const std::string& account_id, | 18 const std::string& account_id, |
| 18 Delegate* delegate) | 19 Delegate* delegate) |
| 19 : request_context_(request_context), | 20 : request_context_(request_context), |
| 20 delegate_(delegate), | 21 delegate_(delegate), |
| 21 api_flow_(request_context_, | 22 api_flow_(request_context_, |
| 22 token_service, | 23 token_service, |
| 23 account_id, | 24 account_id, |
| 24 cloud_devices::GetCloudPrintRelativeURL("search"), | 25 cloud_devices::GetCloudPrintRelativeURL("search"), |
| 25 this) { | 26 this) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 CloudPrintPrinterList::~CloudPrintPrinterList() { | 29 CloudPrintPrinterList::~CloudPrintPrinterList() { |
| 29 } | 30 } |
| 30 | 31 |
| 31 void CloudPrintPrinterList::Start() { | 32 void CloudPrintPrinterList::Start() { |
| 32 api_flow_.Start(); | 33 api_flow_.Start(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 const CloudPrintPrinterList::PrinterDetails* | 36 const CloudPrintPrinterList::PrinterDetails* |
| 36 CloudPrintPrinterList::GetDetailsFor(const std::string& id) { | 37 CloudPrintPrinterList::GetDetailsFor(const std::string& id) { |
| 37 PrinterIDMap::iterator found = printer_id_map_.find(id); | 38 for (iterator i = printer_list_.begin(); i != printer_list_.end(); ++i) { |
| 38 if (found != printer_id_map_.end()) { | 39 if (i->id == id) |
| 39 return &printer_list_[found->second]; | 40 return &(*i); |
| 40 } | 41 } |
| 42 return NULL; |
| 41 | 43 |
| 42 return NULL; | 44 return NULL; |
| 43 } | 45 } |
| 44 | 46 |
| 45 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, | 47 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, |
| 46 GCDBaseApiFlow::Status status) { | 48 GCDBaseApiFlow::Status status) { |
| 47 delegate_->OnCloudPrintPrinterListUnavailable(); | 49 delegate_->OnCloudPrintPrinterListUnavailable(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 void CloudPrintPrinterList::OnGCDAPIFlowComplete( | 52 void CloudPrintPrinterList::OnGCDAPIFlowComplete( |
| 51 GCDBaseApiFlow* flow, | 53 GCDBaseApiFlow* flow, |
| 52 const base::DictionaryValue* value) { | 54 const base::DictionaryValue* value) { |
| 53 const base::ListValue* printers; | 55 const base::ListValue* printers; |
| 54 | 56 |
| 55 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) { | 57 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) { |
| 56 delegate_->OnCloudPrintPrinterListUnavailable(); | 58 delegate_->OnCloudPrintPrinterListUnavailable(); |
| 57 return; | 59 return; |
| 58 } | 60 } |
| 59 | 61 |
| 60 for (base::ListValue::const_iterator i = printers->begin(); | 62 for (base::ListValue::const_iterator i = printers->begin(); |
| 61 i != printers->end(); | 63 i != printers->end(); |
| 62 i++) { | 64 i++) { |
| 63 base::DictionaryValue* printer; | 65 base::DictionaryValue* printer; |
| 64 PrinterDetails printer_details; | 66 PrinterDetails printer_details; |
| 65 | 67 |
| 66 if (!(*i)->GetAsDictionary(&printer)) | 68 if (!(*i)->GetAsDictionary(&printer)) |
| 67 continue; | 69 continue; |
| 68 | 70 |
| 69 if (!FillPrinterDetails(printer, &printer_details)) continue; | 71 if (!FillPrinterDetails(printer, &printer_details)) |
| 72 continue; |
| 70 | 73 |
| 71 std::pair<PrinterIDMap::iterator, bool> inserted = | 74 printer_list_.push_back(printer_details); |
| 72 printer_id_map_.insert( | |
| 73 make_pair(printer_details.id, printer_list_.size()) ); | |
| 74 | |
| 75 if (inserted.second) { // ID is new. | |
| 76 printer_list_.push_back(printer_details); | |
| 77 } | |
| 78 } | 75 } |
| 79 | 76 |
| 80 delegate_->OnCloudPrintPrinterListReady(); | 77 delegate_->OnCloudPrintPrinterListReady(); |
| 81 } | 78 } |
| 82 | 79 |
| 83 bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; } | 80 bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; } |
| 84 | 81 |
| 85 bool CloudPrintPrinterList::FillPrinterDetails( | 82 bool CloudPrintPrinterList::FillPrinterDetails( |
| 86 const base::DictionaryValue* printer_value, | 83 const base::DictionaryValue* printer_value, |
| 87 PrinterDetails* printer_details) { | 84 PrinterDetails* printer_details) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 100 return true; | 97 return true; |
| 101 } | 98 } |
| 102 | 99 |
| 103 CloudPrintPrinterList::PrinterDetails::PrinterDetails() { | 100 CloudPrintPrinterList::PrinterDetails::PrinterDetails() { |
| 104 } | 101 } |
| 105 | 102 |
| 106 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() { | 103 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() { |
| 107 } | 104 } |
| 108 | 105 |
| 109 } // namespace local_discovery | 106 } // namespace local_discovery |
| OLD | NEW |