Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / local_discovery / cloud_print_printer_list.cc
blob8849073ae2f0bd8ab10d03005ca14ceb847a5f47
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"
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,
18 Delegate* delegate)
19 : request_context_(request_context),
20 delegate_(delegate),
21 api_flow_(request_context_,
22 token_service,
23 account_id,
24 cloud_devices::GetCloudPrintRelativeURL("search"),
25 this) {
28 CloudPrintPrinterList::~CloudPrintPrinterList() {
31 void CloudPrintPrinterList::Start() {
32 api_flow_.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];
42 return NULL;
45 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow* flow,
46 GCDBaseApiFlow::Status status) {
47 delegate_->OnCloudPrintPrinterListUnavailable();
50 void CloudPrintPrinterList::OnGCDAPIFlowComplete(
51 GCDBaseApiFlow* flow,
52 const base::DictionaryValue* value) {
53 const base::ListValue* printers;
55 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) {
56 delegate_->OnCloudPrintPrinterListUnavailable();
57 return;
60 for (base::ListValue::const_iterator i = printers->begin();
61 i != printers->end();
62 i++) {
63 base::DictionaryValue* printer;
64 PrinterDetails printer_details;
66 if (!(*i)->GetAsDictionary(&printer))
67 continue;
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))
89 return false;
91 if (!printer_value->GetString(cloud_print::kDisplayNameValue,
92 &printer_details->display_name)) {
93 return false;
96 // Non-essential.
97 printer_value->GetString(cloud_print::kPrinterDescValue,
98 &printer_details->description);
100 return true;
103 CloudPrintPrinterList::PrinterDetails::PrinterDetails() {
106 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() {
109 } // namespace local_discovery