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.
7 #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 "components/cloud_devices/common/cloud_devices_urls.h"
12 namespace local_discovery
{
14 CloudPrintPrinterList::CloudPrintPrinterList(
15 net::URLRequestContextGetter
* request_context
,
16 OAuth2TokenService
* token_service
,
17 const std::string
& account_id
,
19 : request_context_(request_context
),
21 api_flow_(request_context_
,
24 cloud_devices::GetCloudPrintRelativeURL("search"),
28 CloudPrintPrinterList::~CloudPrintPrinterList() {
31 void CloudPrintPrinterList::Start() {
35 const CloudPrintPrinterList::PrinterDetails
*
36 CloudPrintPrinterList::GetDetailsFor(const std::string
& id
) {
37 PrinterIDMap::iterator found
= printer_id_map_
.find(id
);
38 if (found
!= printer_id_map_
.end()) {
39 return &printer_list_
[found
->second
];
45 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow
* flow
,
46 GCDBaseApiFlow::Status status
) {
47 delegate_
->OnCloudPrintPrinterListUnavailable();
50 void CloudPrintPrinterList::OnGCDAPIFlowComplete(
52 const base::DictionaryValue
* value
) {
53 const base::ListValue
* printers
;
55 if (!value
->GetList(cloud_print::kPrinterListValue
, &printers
)) {
56 delegate_
->OnCloudPrintPrinterListUnavailable();
60 for (base::ListValue::const_iterator i
= printers
->begin();
63 base::DictionaryValue
* printer
;
64 PrinterDetails printer_details
;
66 if (!(*i
)->GetAsDictionary(&printer
))
69 if (!FillPrinterDetails(printer
, &printer_details
)) continue;
71 std::pair
<PrinterIDMap::iterator
, bool> inserted
=
72 printer_id_map_
.insert(
73 make_pair(printer_details
.id
, printer_list_
.size()) );
75 if (inserted
.second
) { // ID is new.
76 printer_list_
.push_back(printer_details
);
80 delegate_
->OnCloudPrintPrinterListReady();
83 bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; }
85 bool CloudPrintPrinterList::FillPrinterDetails(
86 const base::DictionaryValue
* printer_value
,
87 PrinterDetails
* printer_details
) {
88 if (!printer_value
->GetString(cloud_print::kIdValue
, &printer_details
->id
))
91 if (!printer_value
->GetString(cloud_print::kDisplayNameValue
,
92 &printer_details
->display_name
)) {
97 printer_value
->GetString(cloud_print::kPrinterDescValue
,
98 &printer_details
->description
);
103 CloudPrintPrinterList::PrinterDetails::PrinterDetails() {
106 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() {
109 } // namespace local_discovery