Refactor SharedMemory::Create and fix a rare file leak.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_printer_list.cc
bloba8de56a3580fe426e074219d8637ae09c00db92d
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"
7 #include <utility>
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();
32 return;
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;
42 if (!(*i)->GetAsDictionary(&printer))
43 continue;
45 if (!FillPrinterDetails(*printer, &printer_details))
46 continue;
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))
62 return false;
64 if (!printer_value.GetString(cloud_print::kDisplayNameValue,
65 &printer_details->display_name)) {
66 return false;
69 // Non-essential.
70 printer_value.GetString(cloud_print::kPrinterDescValue,
71 &printer_details->description);
73 printer_details->type = CloudDeviceListDelegate::kDeviceTypePrinter;
75 return true;
78 } // namespace local_discovery