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"
11 namespace local_discovery
{
14 const char kPrinterListURLFormat
[] = "%s/search";
17 CloudPrintPrinterList::CloudPrintPrinterList(
18 net::URLRequestContextGetter
* request_context
,
19 const std::string
& cloud_print_url
,
20 OAuth2TokenService
* token_service
,
21 const std::string
& account_id
,
23 : request_context_(request_context
),
24 url_(base::StringPrintf(kPrinterListURLFormat
, cloud_print_url
.c_str())),
26 api_flow_(request_context_
,
34 CloudPrintPrinterList::~CloudPrintPrinterList() {
37 void CloudPrintPrinterList::Start() {
41 const CloudPrintPrinterList::PrinterDetails
*
42 CloudPrintPrinterList::GetDetailsFor(const std::string
& id
) {
43 PrinterIDMap::iterator found
= printer_id_map_
.find(id
);
44 if (found
!= printer_id_map_
.end()) {
45 return &printer_list_
[found
->second
];
51 void CloudPrintPrinterList::OnCloudPrintAPIFlowError(
52 CloudPrintBaseApiFlow
* flow
,
53 CloudPrintBaseApiFlow::Status status
) {
54 delegate_
->OnCloudPrintPrinterListUnavailable();
58 void CloudPrintPrinterList::OnCloudPrintAPIFlowComplete(
59 CloudPrintBaseApiFlow
* flow
,
60 const base::DictionaryValue
* value
) {
61 const base::ListValue
* printers
;
63 if (!value
->GetList(cloud_print::kPrinterListValue
, &printers
)) {
64 delegate_
->OnCloudPrintPrinterListUnavailable();
68 for (base::ListValue::const_iterator i
= printers
->begin();
71 base::DictionaryValue
* printer
;
72 PrinterDetails printer_details
;
74 if (!(*i
)->GetAsDictionary(&printer
))
77 if (!FillPrinterDetails(printer
, &printer_details
)) continue;
79 std::pair
<PrinterIDMap::iterator
, bool> inserted
=
80 printer_id_map_
.insert(
81 make_pair(printer_details
.id
, printer_list_
.size()) );
83 if (inserted
.second
) { // ID is new.
84 printer_list_
.push_back(printer_details
);
88 delegate_
->OnCloudPrintPrinterListReady();
91 bool CloudPrintPrinterList::FillPrinterDetails(
92 const base::DictionaryValue
* printer_value
,
93 PrinterDetails
* printer_details
) {
94 if (!printer_value
->GetString(cloud_print::kIdValue
, &printer_details
->id
))
97 if (!printer_value
->GetString(cloud_print::kDisplayNameValue
,
98 &printer_details
->display_name
)) {
103 printer_value
->GetString(cloud_print::kPrinterDescValue
,
104 &printer_details
->description
);
109 CloudPrintPrinterList::PrinterDetails::PrinterDetails() {
112 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() {
115 } // namespace local_discovery