OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <utility> | |
6 | |
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 | |
11 namespace local_discovery { | |
12 | |
13 namespace { | |
14 const char kPrinterListURLFormat[] = "%s/search"; | |
15 } | |
16 | |
17 CloudPrintPrinterList::CloudPrintPrinterList( | |
18 net::URLRequestContextGetter* request_context, | |
19 const std::string& cloud_print_url, | |
20 OAuth2TokenService* token_service, | |
21 Delegate* delegate) | |
22 : request_context_(request_context), | |
23 url_(base::StringPrintf(kPrinterListURLFormat, cloud_print_url.c_str())), | |
24 delegate_(delegate), | |
25 api_flow_(request_context_, | |
26 token_service, | |
27 url_, | |
28 this) { | |
29 } | |
30 | |
31 | |
32 CloudPrintPrinterList::~CloudPrintPrinterList() { | |
33 } | |
34 | |
35 void CloudPrintPrinterList::Start() { | |
36 api_flow_.Start(); | |
37 } | |
38 | |
39 const CloudPrintPrinterList::PrinterDetails* | |
40 CloudPrintPrinterList::GetDetailsFor(const std::string& id) { | |
41 PrinterIDMap::iterator found = printer_id_map_.find(id); | |
42 if (found != printer_id_map_.end()) { | |
43 return &printer_list_[found->second]; | |
44 } | |
45 | |
46 return NULL; | |
47 } | |
48 | |
49 void CloudPrintPrinterList::OnCloudPrintAPIFlowError( | |
50 CloudPrintBaseApiFlow* flow, | |
51 CloudPrintBaseApiFlow::Status status) { | |
52 delegate_->OnCloudPrintPrinterListUnavailable(); | |
53 } | |
54 | |
55 | |
56 void CloudPrintPrinterList::OnCloudPrintAPIFlowComplete( | |
57 CloudPrintBaseApiFlow* flow, | |
58 const base::DictionaryValue* value) { | |
59 const base::ListValue* printers; | |
60 | |
61 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) { | |
62 delegate_->OnCloudPrintPrinterListUnavailable(); | |
63 return; | |
64 } | |
65 | |
66 for (base::ListValue::const_iterator i = printers->begin(); | |
67 i != printers->end(); | |
68 i++) { | |
69 base::DictionaryValue* printer; | |
70 PrinterDetails printer_details; | |
71 | |
72 if (!(*i)->GetAsDictionary(&printer)) | |
73 continue; | |
74 | |
75 if (!FillPrinterDetails(printer, &printer_details)) continue; | |
76 | |
77 std::pair<PrinterIDMap::iterator, bool> inserted = | |
78 printer_id_map_.insert( | |
79 make_pair(printer_details.id, printer_list_.size()) ); | |
80 | |
81 if (inserted.second) { // ID is new. | |
82 printer_list_.push_back(printer_details); | |
83 } | |
84 } | |
85 | |
86 delegate_->OnCloudPrintPrinterListReady(); | |
87 } | |
88 | |
89 bool CloudPrintPrinterList::FillPrinterDetails( | |
90 const base::DictionaryValue* printer_value, | |
91 PrinterDetails* printer_details) { | |
92 if (!printer_value->GetString(cloud_print::kIdValue, &printer_details->id)) | |
93 return false; | |
94 | |
95 if (!printer_value->GetString(cloud_print::kDisplayNameValue, | |
Vitaly Buka (NO REVIEWS)
2013/09/04 21:46:25
misaligned and needs {}
Noam Samuel
2013/09/05 23:08:31
Done.
| |
96 &printer_details->display_name)) | |
97 return false; | |
98 | |
99 // Non-essential. | |
100 printer_value->GetString(cloud_print::kPrinterDescValue, | |
101 &printer_details->description); | |
102 | |
103 return true; | |
104 } | |
105 | |
106 CloudPrintPrinterList::PrinterDetails::PrinterDetails() { | |
107 } | |
108 | |
109 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() { | |
110 } | |
111 | |
112 } // namespace local_discovery | |
OLD | NEW |