Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_printer_list.cc
blob1f3feea11101769150130f05789d8c76f0fd64b6
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 <utility>
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 {
13 namespace {
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,
22 Delegate* delegate)
23 : request_context_(request_context),
24 url_(base::StringPrintf(kPrinterListURLFormat, cloud_print_url.c_str())),
25 delegate_(delegate),
26 api_flow_(request_context_,
27 token_service,
28 account_id,
29 url_,
30 this) {
34 CloudPrintPrinterList::~CloudPrintPrinterList() {
37 void CloudPrintPrinterList::Start() {
38 api_flow_.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];
48 return NULL;
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();
65 return;
68 for (base::ListValue::const_iterator i = printers->begin();
69 i != printers->end();
70 i++) {
71 base::DictionaryValue* printer;
72 PrinterDetails printer_details;
74 if (!(*i)->GetAsDictionary(&printer))
75 continue;
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))
95 return false;
97 if (!printer_value->GetString(cloud_print::kDisplayNameValue,
98 &printer_details->display_name)) {
99 return false;
102 // Non-essential.
103 printer_value->GetString(cloud_print::kPrinterDescValue,
104 &printer_details->description);
106 return true;
109 CloudPrintPrinterList::PrinterDetails::PrinterDetails() {
112 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() {
115 } // namespace local_discovery