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.
5 #include "chrome/browser/local_discovery/cloud_print_printer_list.h"
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"
13 namespace local_discovery
{
15 CloudPrintPrinterList::CloudPrintPrinterList(CloudDeviceListDelegate
* delegate
)
16 : delegate_(delegate
) {
19 CloudPrintPrinterList::~CloudPrintPrinterList() {
22 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDApiFlow::Status status
) {
23 delegate_
->OnDeviceListUnavailable();
26 void CloudPrintPrinterList::OnGCDAPIFlowComplete(
27 const base::DictionaryValue
& value
) {
28 const base::ListValue
* printers
;
30 if (!value
.GetList(cloud_print::kPrinterListValue
, &printers
)) {
31 delegate_
->OnDeviceListUnavailable();
35 std::vector
<CloudDeviceListDelegate::Device
> devices
;
36 for (base::ListValue::const_iterator i
= printers
->begin();
39 base::DictionaryValue
* printer
;
40 CloudDeviceListDelegate::Device printer_details
;
42 if (!(*i
)->GetAsDictionary(&printer
))
45 if (!FillPrinterDetails(*printer
, &printer_details
))
48 devices
.push_back(printer_details
);
51 delegate_
->OnDeviceListReady(devices
);
54 GURL
CloudPrintPrinterList::GetURL() {
55 return cloud_devices::GetCloudPrintRelativeURL("search");
58 bool CloudPrintPrinterList::FillPrinterDetails(
59 const base::DictionaryValue
& printer_value
,
60 CloudDeviceListDelegate::Device
* printer_details
) {
61 if (!printer_value
.GetString(cloud_print::kIdValue
, &printer_details
->id
))
64 if (!printer_value
.GetString(cloud_print::kDisplayNameValue
,
65 &printer_details
->display_name
)) {
70 printer_value
.GetString(cloud_print::kPrinterDescValue
,
71 &printer_details
->description
);
73 printer_details
->type
= CloudDeviceListDelegate::kDeviceTypePrinter
;
78 } // namespace local_discovery