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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" 6 7 #include <utility> 8 9 #include "base/strings/stringprintf.h" 10 #include "chrome/common/cloud_print/cloud_print_constants.h" 11 #include "components/cloud_devices/common/cloud_devices_urls.h" 12 13 namespace local_discovery { 14 CloudPrintPrinterList(CloudDeviceListDelegate * delegate)15CloudPrintPrinterList::CloudPrintPrinterList(CloudDeviceListDelegate* delegate) 16 : delegate_(delegate) { 17 } 18 ~CloudPrintPrinterList()19CloudPrintPrinterList::~CloudPrintPrinterList() { 20 } 21 OnGCDAPIFlowError(GCDApiFlow::Status status)22void CloudPrintPrinterList::OnGCDAPIFlowError(GCDApiFlow::Status status) { 23 delegate_->OnDeviceListUnavailable(); 24 } 25 OnGCDAPIFlowComplete(const base::DictionaryValue & value)26void CloudPrintPrinterList::OnGCDAPIFlowComplete( 27 const base::DictionaryValue& value) { 28 const base::ListValue* printers; 29 30 if (!value.GetList(cloud_print::kPrinterListValue, &printers)) { 31 delegate_->OnDeviceListUnavailable(); 32 return; 33 } 34 35 std::vector<CloudDeviceListDelegate::Device> devices; 36 for (base::ListValue::const_iterator i = printers->begin(); 37 i != printers->end(); 38 i++) { 39 base::DictionaryValue* printer; 40 CloudDeviceListDelegate::Device printer_details; 41 42 if (!(*i)->GetAsDictionary(&printer)) 43 continue; 44 45 if (!FillPrinterDetails(*printer, &printer_details)) 46 continue; 47 48 devices.push_back(printer_details); 49 } 50 51 delegate_->OnDeviceListReady(devices); 52 } 53 GetURL()54GURL CloudPrintPrinterList::GetURL() { 55 return cloud_devices::GetCloudPrintRelativeURL("search"); 56 } 57 FillPrinterDetails(const base::DictionaryValue & printer_value,CloudDeviceListDelegate::Device * printer_details)58bool CloudPrintPrinterList::FillPrinterDetails( 59 const base::DictionaryValue& printer_value, 60 CloudDeviceListDelegate::Device* printer_details) { 61 if (!printer_value.GetString(cloud_print::kIdValue, &printer_details->id)) 62 return false; 63 64 if (!printer_value.GetString(cloud_print::kDisplayNameValue, 65 &printer_details->display_name)) { 66 return false; 67 } 68 69 // Non-essential. 70 printer_value.GetString(cloud_print::kPrinterDescValue, 71 &printer_details->description); 72 73 printer_details->type = CloudDeviceListDelegate::kDeviceTypePrinter; 74 75 return true; 76 } 77 78 } // namespace local_discovery 79