OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" |
| 6 |
5 #include <utility> | 7 #include <utility> |
6 | 8 |
7 #include "base/strings/stringprintf.h" | 9 #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 "chrome/common/cloud_print/cloud_print_constants.h" |
10 #include "components/cloud_devices/common/cloud_devices_urls.h" | 11 #include "components/cloud_devices/common/cloud_devices_urls.h" |
11 | 12 |
12 namespace local_discovery { | 13 namespace local_discovery { |
13 | 14 |
14 CloudPrintPrinterList::CloudPrintPrinterList( | 15 CloudPrintPrinterList::CloudPrintPrinterList( |
15 net::URLRequestContextGetter* request_context, | 16 net::URLRequestContextGetter* request_context, |
16 OAuth2TokenService* token_service, | 17 OAuth2TokenService* token_service, |
17 const std::string& account_id, | 18 const std::string& account_id, |
18 Delegate* delegate) | 19 CloudDeviceListDelegate* delegate) |
19 : request_context_(request_context), | 20 : request_context_(request_context), |
20 delegate_(delegate), | 21 delegate_(delegate), |
21 api_flow_(request_context_, | 22 api_flow_(request_context_, |
22 token_service, | 23 token_service, |
23 account_id, | 24 account_id, |
24 cloud_devices::GetCloudPrintRelativeURL("search"), | 25 cloud_devices::GetCloudPrintRelativeURL("search"), |
25 this) { | 26 this) { |
26 } | 27 } |
27 | 28 |
28 CloudPrintPrinterList::~CloudPrintPrinterList() { | 29 CloudPrintPrinterList::~CloudPrintPrinterList() { |
29 } | 30 } |
30 | 31 |
31 void CloudPrintPrinterList::Start() { | 32 void CloudPrintPrinterList::Start() { |
32 api_flow_.Start(); | 33 api_flow_.Start(); |
33 } | 34 } |
34 | 35 |
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]; | |
40 } | |
41 | |
42 return NULL; | |
43 } | |
44 | |
45 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, | 36 void CloudPrintPrinterList::OnGCDAPIFlowError(GCDBaseApiFlow* flow, |
46 GCDBaseApiFlow::Status status) { | 37 GCDBaseApiFlow::Status status) { |
47 delegate_->OnCloudPrintPrinterListUnavailable(); | 38 delegate_->OnDeviceListUnavailable(); |
48 } | 39 } |
49 | 40 |
50 void CloudPrintPrinterList::OnGCDAPIFlowComplete( | 41 void CloudPrintPrinterList::OnGCDAPIFlowComplete( |
51 GCDBaseApiFlow* flow, | 42 GCDBaseApiFlow* flow, |
52 const base::DictionaryValue* value) { | 43 const base::DictionaryValue* value) { |
53 const base::ListValue* printers; | 44 const base::ListValue* printers; |
54 | 45 |
55 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) { | 46 if (!value->GetList(cloud_print::kPrinterListValue, &printers)) { |
56 delegate_->OnCloudPrintPrinterListUnavailable(); | 47 delegate_->OnDeviceListUnavailable(); |
57 return; | 48 return; |
58 } | 49 } |
59 | 50 |
60 for (base::ListValue::const_iterator i = printers->begin(); | 51 for (base::ListValue::const_iterator i = printers->begin(); |
61 i != printers->end(); | 52 i != printers->end(); |
62 i++) { | 53 i++) { |
63 base::DictionaryValue* printer; | 54 base::DictionaryValue* printer; |
64 PrinterDetails printer_details; | 55 CloudDeviceListDelegate::Device printer_details; |
65 | 56 |
66 if (!(*i)->GetAsDictionary(&printer)) | 57 if (!(*i)->GetAsDictionary(&printer)) |
67 continue; | 58 continue; |
68 | 59 |
69 if (!FillPrinterDetails(printer, &printer_details)) continue; | 60 if (!FillPrinterDetails(printer, &printer_details)) |
| 61 continue; |
70 | 62 |
71 std::pair<PrinterIDMap::iterator, bool> inserted = | 63 printer_list_.push_back(printer_details); |
72 printer_id_map_.insert( | |
73 make_pair(printer_details.id, printer_list_.size()) ); | |
74 | |
75 if (inserted.second) { // ID is new. | |
76 printer_list_.push_back(printer_details); | |
77 } | |
78 } | 64 } |
79 | 65 |
80 delegate_->OnCloudPrintPrinterListReady(); | 66 delegate_->OnDeviceListReady(); |
81 } | 67 } |
82 | 68 |
83 bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; } | 69 bool CloudPrintPrinterList::GCDIsCloudPrint() { return true; } |
84 | 70 |
85 bool CloudPrintPrinterList::FillPrinterDetails( | 71 bool CloudPrintPrinterList::FillPrinterDetails( |
86 const base::DictionaryValue* printer_value, | 72 const base::DictionaryValue* printer_value, |
87 PrinterDetails* printer_details) { | 73 CloudDeviceListDelegate::Device* printer_details) { |
88 if (!printer_value->GetString(cloud_print::kIdValue, &printer_details->id)) | 74 if (!printer_value->GetString(cloud_print::kIdValue, &printer_details->id)) |
89 return false; | 75 return false; |
90 | 76 |
91 if (!printer_value->GetString(cloud_print::kDisplayNameValue, | 77 if (!printer_value->GetString(cloud_print::kDisplayNameValue, |
92 &printer_details->display_name)) { | 78 &printer_details->display_name)) { |
93 return false; | 79 return false; |
94 } | 80 } |
95 | 81 |
96 // Non-essential. | 82 // Non-essential. |
97 printer_value->GetString(cloud_print::kPrinterDescValue, | 83 printer_value->GetString(cloud_print::kPrinterDescValue, |
98 &printer_details->description); | 84 &printer_details->description); |
99 | 85 |
| 86 printer_details->type = CloudDeviceListDelegate::kDeviceTypePrinter; |
| 87 |
100 return true; | 88 return true; |
101 } | 89 } |
102 | 90 |
103 CloudPrintPrinterList::PrinterDetails::PrinterDetails() { | |
104 } | |
105 | |
106 CloudPrintPrinterList::PrinterDetails::~PrinterDetails() { | |
107 } | |
108 | |
109 } // namespace local_discovery | 91 } // namespace local_discovery |
OLD | NEW |