| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/service/cloud_print/printer_info.h" | 5 #include "chrome/service/cloud_print/printer_info.h" |
| 6 | 6 |
| 7 #include <cups/cups.h> | 7 #include <cups/cups.h> |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static const char kCUPSPrinterInfoOpt[] = "printer-info"; | 25 static const char kCUPSPrinterInfoOpt[] = "printer-info"; |
| 26 static const char kCUPSPrinterStateOpt[] = "printer-state"; | 26 static const char kCUPSPrinterStateOpt[] = "printer-state"; |
| 27 | 27 |
| 28 void EnumeratePrinters(PrinterList* printer_list) { | 28 void EnumeratePrinters(PrinterList* printer_list) { |
| 29 DCHECK(printer_list); | 29 DCHECK(printer_list); |
| 30 printer_list->clear(); | 30 printer_list->clear(); |
| 31 | 31 |
| 32 cups_dest_t* destinations = NULL; | 32 cups_dest_t* destinations = NULL; |
| 33 int num_dests = cupsGetDests(&destinations); | 33 int num_dests = cupsGetDests(&destinations); |
| 34 | 34 |
| 35 for (int i = 0; i < num_dests; i++) { | 35 for (int printer_index = 0; printer_index < num_dests; printer_index++) { |
| 36 const cups_dest_t& printer = destinations[printer_index]; |
| 37 |
| 36 PrinterBasicInfo printer_info; | 38 PrinterBasicInfo printer_info; |
| 37 printer_info.printer_name = destinations[i].name; | 39 printer_info.printer_name = printer.name; |
| 38 | 40 |
| 39 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, | 41 const char* info = cupsGetOption(kCUPSPrinterInfoOpt, |
| 40 destinations[i].num_options, destinations[i].options); | 42 printer.num_options, printer.options); |
| 41 if (info != NULL) | 43 if (info != NULL) |
| 42 printer_info.printer_description = info; | 44 printer_info.printer_description = info; |
| 43 | 45 |
| 44 const char* state = cupsGetOption(kCUPSPrinterStateOpt, | 46 const char* state = cupsGetOption(kCUPSPrinterStateOpt, |
| 45 destinations[i].num_options, destinations[i].options); | 47 printer.num_options, printer.options); |
| 46 if (state != NULL) | 48 if (state != NULL) |
| 47 StringToInt(state, &printer_info.printer_status); | 49 StringToInt(state, &printer_info.printer_status); |
| 48 | 50 |
| 51 // Store printer options. |
| 52 for (int opt_index = 0; opt_index < printer.num_options; opt_index++) { |
| 53 printer_info.options[printer.options[opt_index].name] = |
| 54 printer.options[opt_index].value; |
| 55 } |
| 56 |
| 49 printer_list->push_back(printer_info); | 57 printer_list->push_back(printer_info); |
| 50 } | 58 } |
| 51 | 59 |
| 52 cupsFreeDests(num_dests, destinations); | 60 cupsFreeDests(num_dests, destinations); |
| 61 |
| 62 DLOG(INFO) << "Enumerated " << printer_list->size() << " printers."; |
| 53 } | 63 } |
| 54 | 64 |
| 55 bool GetPrinterCapsAndDefaults(const std::string& printer_name, | 65 bool GetPrinterCapsAndDefaults(const std::string& printer_name, |
| 56 PrinterCapsAndDefaults* printer_info) { | 66 PrinterCapsAndDefaults* printer_info) { |
| 57 DCHECK(printer_info); | 67 DCHECK(printer_info); |
| 58 | 68 |
| 69 DLOG(INFO) << "Getting Caps and Defaults for: " << printer_name; |
| 70 |
| 59 static Lock ppd_lock; | 71 static Lock ppd_lock; |
| 60 // cupsGetPPD returns a filename stored in a static buffer in CUPS. | 72 // cupsGetPPD returns a filename stored in a static buffer in CUPS. |
| 61 // Protect this code with lock. | 73 // Protect this code with lock. |
| 62 ppd_lock.Acquire(); | 74 ppd_lock.Acquire(); |
| 63 FilePath ppd_path(cupsGetPPD(printer_name.c_str())); | 75 FilePath ppd_path(cupsGetPPD(printer_name.c_str())); |
| 64 ppd_lock.Release(); | 76 ppd_lock.Release(); |
| 65 | 77 |
| 66 std::string content; | 78 std::string content; |
| 67 bool res = file_util::ReadFileToString(ppd_path, &content); | 79 bool res = file_util::ReadFileToString(ppd_path, &content); |
| 68 | 80 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 131 } |
| 120 | 132 |
| 121 bool SpoolPrintJob(const std::string& print_ticket, | 133 bool SpoolPrintJob(const std::string& print_ticket, |
| 122 const FilePath& print_data_file_path, | 134 const FilePath& print_data_file_path, |
| 123 const std::string& print_data_mime_type, | 135 const std::string& print_data_mime_type, |
| 124 const std::string& printer_name, | 136 const std::string& printer_name, |
| 125 const std::string& job_title, | 137 const std::string& job_title, |
| 126 PlatformJobId* job_id_ret) { | 138 PlatformJobId* job_id_ret) { |
| 127 DCHECK(job_id_ret); | 139 DCHECK(job_id_ret); |
| 128 | 140 |
| 141 DLOG(INFO) << "Spooling print job for: " << printer_name; |
| 142 |
| 129 // We need to store options as char* string for the duration of the | 143 // We need to store options as char* string for the duration of the |
| 130 // cupsPrintFile call. We'll use map here to store options, since | 144 // cupsPrintFile call. We'll use map here to store options, since |
| 131 // Dictionary value from JSON parser returns wchat_t. | 145 // Dictionary value from JSON parser returns wchat_t. |
| 132 std::map<std::string, std::string> options; | 146 std::map<std::string, std::string> options; |
| 133 bool res = ParsePrintTicket(print_ticket, &options); | 147 bool res = ParsePrintTicket(print_ticket, &options); |
| 134 DCHECK(res); // If print ticket is invalid we still print using defaults. | 148 DCHECK(res); // If print ticket is invalid we still print using defaults. |
| 135 | 149 |
| 136 std::vector<cups_option_t> cups_options; | 150 std::vector<cups_option_t> cups_options; |
| 137 std::map<std::string, std::string>::iterator it; | 151 std::map<std::string, std::string>::iterator it; |
| 138 for (it = options.begin(); it != options.end(); ++it) { | 152 for (it = options.begin(); it != options.end(); ++it) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 return false; | 166 return false; |
| 153 | 167 |
| 154 *job_id_ret = job_id; | 168 *job_id_ret = job_id; |
| 155 return true; | 169 return true; |
| 156 } | 170 } |
| 157 | 171 |
| 158 bool GetJobDetails(const std::string& printer_name, | 172 bool GetJobDetails(const std::string& printer_name, |
| 159 PlatformJobId job_id, | 173 PlatformJobId job_id, |
| 160 PrintJobDetails *job_details) { | 174 PrintJobDetails *job_details) { |
| 161 DCHECK(job_details); | 175 DCHECK(job_details); |
| 176 |
| 177 DLOG(INFO) << "Getting job details for: " << printer_name << |
| 178 " job_id: " << job_id; |
| 179 |
| 162 cups_job_t* jobs = NULL; | 180 cups_job_t* jobs = NULL; |
| 163 int num_jobs = cupsGetJobs(&jobs, printer_name.c_str(), 1, -1); | 181 int num_jobs = cupsGetJobs(&jobs, printer_name.c_str(), 1, -1); |
| 164 | 182 |
| 165 bool found = false; | 183 bool found = false; |
| 166 for (int i = 0; i < num_jobs; i++) { | 184 for (int i = 0; i < num_jobs; i++) { |
| 167 if (jobs[i].id == job_id) { | 185 if (jobs[i].id == job_id) { |
| 168 found = true; | 186 found = true; |
| 169 switch (jobs[i].state) { | 187 switch (jobs[i].state) { |
| 170 case IPP_JOB_PENDING : | 188 case IPP_JOB_PENDING : |
| 171 case IPP_JOB_HELD : | 189 case IPP_JOB_HELD : |
| (...skipping 18 matching lines...) Expand all Loading... |
| 190 } | 208 } |
| 191 } | 209 } |
| 192 | 210 |
| 193 cupsFreeJobs(num_jobs, jobs); | 211 cupsFreeJobs(num_jobs, jobs); |
| 194 return found; | 212 return found; |
| 195 } | 213 } |
| 196 | 214 |
| 197 bool GetPrinterInfo(const std::string& printer_name, PrinterBasicInfo* info) { | 215 bool GetPrinterInfo(const std::string& printer_name, PrinterBasicInfo* info) { |
| 198 DCHECK(info); | 216 DCHECK(info); |
| 199 | 217 |
| 218 DLOG(INFO) << "Getting printer info for: " << printer_name; |
| 219 |
| 200 // This is not very efficient way to get specific printer info. CUPS 1.4 | 220 // This is not very efficient way to get specific printer info. CUPS 1.4 |
| 201 // supports cupsGetNamedDest() function. However, CUPS 1.4 is not available | 221 // supports cupsGetNamedDest() function. However, CUPS 1.4 is not available |
| 202 // everywhere (for example, it supported from Mac OS 10.6 only). | 222 // everywhere (for example, it supported from Mac OS 10.6 only). |
| 203 PrinterList printer_list; | 223 PrinterList printer_list; |
| 204 EnumeratePrinters(&printer_list); | 224 EnumeratePrinters(&printer_list); |
| 205 | 225 |
| 206 PrinterList::iterator it; | 226 PrinterList::iterator it; |
| 207 for (it = printer_list.begin(); it != printer_list.end(); ++it) { | 227 for (it = printer_list.begin(); it != printer_list.end(); ++it) { |
| 208 if (it->printer_name == printer_name) { | 228 if (it->printer_name == printer_name) { |
| 209 *info = *it; | 229 *info = *it; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 bool PrinterChangeNotifier::GetCurrentPrinterInfo( | 322 bool PrinterChangeNotifier::GetCurrentPrinterInfo( |
| 303 PrinterBasicInfo* printer_info) { | 323 PrinterBasicInfo* printer_info) { |
| 304 if (!state_) { | 324 if (!state_) { |
| 305 return false; | 325 return false; |
| 306 } | 326 } |
| 307 DCHECK(printer_info); | 327 DCHECK(printer_info); |
| 308 return GetPrinterInfo(state_->printer_name(), printer_info); | 328 return GetPrinterInfo(state_->printer_name(), printer_info); |
| 309 } | 329 } |
| 310 } // namespace cloud_print | 330 } // namespace cloud_print |
| 311 | 331 |
| OLD | NEW |