| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/print_system.h" | 5 #include "chrome/service/cloud_print/print_system.h" |
| 6 | 6 |
| 7 #include <cups/cups.h> | 7 #include <cups/cups.h> |
| 8 #include <dlfcn.h> | 8 #include <dlfcn.h> |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 return update_timeout_; | 120 return update_timeout_; |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool NotifyDelete() const { | 123 bool NotifyDelete() const { |
| 124 // Notify about deleted printers only when we | 124 // Notify about deleted printers only when we |
| 125 // fetched printers list without errors. | 125 // fetched printers list without errors. |
| 126 return notify_delete_ && printer_enum_succeeded_; | 126 return notify_delete_ && printer_enum_succeeded_; |
| 127 } | 127 } |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 virtual ~PrintSystemCUPS() {} |
| 131 |
| 130 // Following functions are wrappers around corresponding CUPS functions. | 132 // Following functions are wrappers around corresponding CUPS functions. |
| 131 // <functions>2() are called when print server is specified, and plain | 133 // <functions>2() are called when print server is specified, and plain |
| 132 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT | 134 // version in another case. There is an issue specifing CUPS_HTTP_DEFAULT |
| 133 // in the <functions>2(), it does not work in CUPS prior to 1.4. | 135 // in the <functions>2(), it does not work in CUPS prior to 1.4. |
| 134 int GetJobs(cups_job_t** jobs, const GURL& url, const char* name, | 136 int GetJobs(cups_job_t** jobs, const GURL& url, const char* name, |
| 135 int myjobs, int whichjobs); | 137 int myjobs, int whichjobs); |
| 136 int PrintFile(const GURL& url, const char* name, const char* filename, | 138 int PrintFile(const GURL& url, const char* name, const char* filename, |
| 137 const char* title, int num_options, cups_option_t* options); | 139 const char* title, int num_options, cups_option_t* options); |
| 138 | 140 |
| 139 void InitPrintBackends(const DictionaryValue* print_system_settings); | 141 void InitPrintBackends(const DictionaryValue* print_system_settings); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 166 bool notify_delete_; | 168 bool notify_delete_; |
| 167 }; | 169 }; |
| 168 | 170 |
| 169 class PrintServerWatcherCUPS | 171 class PrintServerWatcherCUPS |
| 170 : public PrintSystem::PrintServerWatcher { | 172 : public PrintSystem::PrintServerWatcher { |
| 171 public: | 173 public: |
| 172 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) | 174 explicit PrintServerWatcherCUPS(PrintSystemCUPS* print_system) |
| 173 : print_system_(print_system), | 175 : print_system_(print_system), |
| 174 delegate_(NULL) { | 176 delegate_(NULL) { |
| 175 } | 177 } |
| 176 ~PrintServerWatcherCUPS() { | |
| 177 StopWatching(); | |
| 178 } | |
| 179 | 178 |
| 180 // PrintSystem::PrintServerWatcher implementation. | 179 // PrintSystem::PrintServerWatcher implementation. |
| 181 virtual bool StartWatching( | 180 virtual bool StartWatching( |
| 182 PrintSystem::PrintServerWatcher::Delegate* delegate) OVERRIDE { | 181 PrintSystem::PrintServerWatcher::Delegate* delegate) OVERRIDE { |
| 183 delegate_ = delegate; | 182 delegate_ = delegate; |
| 184 printers_hash_ = GetPrintersHash(); | 183 printers_hash_ = GetPrintersHash(); |
| 185 MessageLoop::current()->PostDelayedTask( | 184 MessageLoop::current()->PostDelayedTask( |
| 186 FROM_HERE, | 185 FROM_HERE, |
| 187 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this), | 186 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this), |
| 188 print_system_->GetUpdateTimeout()); | 187 print_system_->GetUpdateTimeout()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 203 printers_hash_ = new_hash; | 202 printers_hash_ = new_hash; |
| 204 delegate_->OnPrinterAdded(); | 203 delegate_->OnPrinterAdded(); |
| 205 } | 204 } |
| 206 MessageLoop::current()->PostDelayedTask( | 205 MessageLoop::current()->PostDelayedTask( |
| 207 FROM_HERE, | 206 FROM_HERE, |
| 208 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this), | 207 base::Bind(&PrintServerWatcherCUPS::CheckForUpdates, this), |
| 209 print_system_->GetUpdateTimeout()); | 208 print_system_->GetUpdateTimeout()); |
| 210 } | 209 } |
| 211 | 210 |
| 212 private: | 211 private: |
| 212 virtual ~PrintServerWatcherCUPS() { |
| 213 StopWatching(); |
| 214 } |
| 215 |
| 213 std::string GetPrintersHash() { | 216 std::string GetPrintersHash() { |
| 214 printing::PrinterList printer_list; | 217 printing::PrinterList printer_list; |
| 215 print_system_->EnumeratePrinters(&printer_list); | 218 print_system_->EnumeratePrinters(&printer_list); |
| 216 | 219 |
| 217 // Sort printer names. | 220 // Sort printer names. |
| 218 std::vector<std::string> printers; | 221 std::vector<std::string> printers; |
| 219 printing::PrinterList::iterator it; | 222 printing::PrinterList::iterator it; |
| 220 for (it = printer_list.begin(); it != printer_list.end(); ++it) | 223 for (it = printer_list.begin(); it != printer_list.end(); ++it) |
| 221 printers.push_back(it->printer_name); | 224 printers.push_back(it->printer_name); |
| 222 std::sort(printers.begin(), printers.end()); | 225 std::sort(printers.begin(), printers.end()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 238 class PrinterWatcherCUPS | 241 class PrinterWatcherCUPS |
| 239 : public PrintSystem::PrinterWatcher { | 242 : public PrintSystem::PrinterWatcher { |
| 240 public: | 243 public: |
| 241 PrinterWatcherCUPS(PrintSystemCUPS* print_system, | 244 PrinterWatcherCUPS(PrintSystemCUPS* print_system, |
| 242 const std::string& printer_name) | 245 const std::string& printer_name) |
| 243 : printer_name_(printer_name), | 246 : printer_name_(printer_name), |
| 244 delegate_(NULL), | 247 delegate_(NULL), |
| 245 print_system_(print_system) { | 248 print_system_(print_system) { |
| 246 } | 249 } |
| 247 | 250 |
| 248 ~PrinterWatcherCUPS() { | |
| 249 StopWatching(); | |
| 250 } | |
| 251 | |
| 252 // PrintSystem::PrinterWatcher implementation. | 251 // PrintSystem::PrinterWatcher implementation. |
| 253 virtual bool StartWatching( | 252 virtual bool StartWatching( |
| 254 PrintSystem::PrinterWatcher::Delegate* delegate) OVERRIDE{ | 253 PrintSystem::PrinterWatcher::Delegate* delegate) OVERRIDE{ |
| 255 scoped_refptr<printing::PrintBackend> print_backend( | 254 scoped_refptr<printing::PrintBackend> print_backend( |
| 256 printing::PrintBackend::CreateInstance(NULL)); | 255 printing::PrintBackend::CreateInstance(NULL)); |
| 257 child_process_logging::ScopedPrinterInfoSetter prn_info( | 256 child_process_logging::ScopedPrinterInfoSetter prn_info( |
| 258 print_backend->GetPrinterDriverInfo(printer_name_)); | 257 print_backend->GetPrinterDriverInfo(printer_name_)); |
| 259 if (delegate_ != NULL) | 258 if (delegate_ != NULL) |
| 260 StopWatching(); | 259 StopWatching(); |
| 261 delegate_ = delegate; | 260 delegate_ = delegate; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 VLOG(1) << "CP_CUPS: Printer update detected for: " << printer_name_; | 313 VLOG(1) << "CP_CUPS: Printer update detected for: " << printer_name_; |
| 315 } | 314 } |
| 316 } | 315 } |
| 317 MessageLoop::current()->PostDelayedTask( | 316 MessageLoop::current()->PostDelayedTask( |
| 318 FROM_HERE, | 317 FROM_HERE, |
| 319 base::Bind(&PrinterWatcherCUPS::PrinterUpdate, this), | 318 base::Bind(&PrinterWatcherCUPS::PrinterUpdate, this), |
| 320 print_system_->GetUpdateTimeout()); | 319 print_system_->GetUpdateTimeout()); |
| 321 } | 320 } |
| 322 | 321 |
| 323 private: | 322 private: |
| 323 virtual ~PrinterWatcherCUPS() { |
| 324 StopWatching(); |
| 325 } |
| 326 |
| 324 std::string GetSettingsHash() { | 327 std::string GetSettingsHash() { |
| 325 printing::PrinterBasicInfo info; | 328 printing::PrinterBasicInfo info; |
| 326 if (!print_system_->GetPrinterInfo(printer_name_, &info)) | 329 if (!print_system_->GetPrinterInfo(printer_name_, &info)) |
| 327 return std::string(); | 330 return std::string(); |
| 328 | 331 |
| 329 printing::PrinterCapsAndDefaults caps; | 332 printing::PrinterCapsAndDefaults caps; |
| 330 if (!print_system_->GetPrinterCapsAndDefaults(printer_name_, &caps)) | 333 if (!print_system_->GetPrinterCapsAndDefaults(printer_name_, &caps)) |
| 331 return std::string(); | 334 return std::string(); |
| 332 | 335 |
| 333 std::string to_hash(info.printer_name); | 336 std::string to_hash(info.printer_name); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 | 384 |
| 382 static void NotifyDelegate(JobSpooler::Delegate* delegate, | 385 static void NotifyDelegate(JobSpooler::Delegate* delegate, |
| 383 int job_id, bool dry_run) { | 386 int job_id, bool dry_run) { |
| 384 if (dry_run || job_id) | 387 if (dry_run || job_id) |
| 385 delegate->OnJobSpoolSucceeded(job_id); | 388 delegate->OnJobSpoolSucceeded(job_id); |
| 386 else | 389 else |
| 387 delegate->OnJobSpoolFailed(); | 390 delegate->OnJobSpoolFailed(); |
| 388 } | 391 } |
| 389 | 392 |
| 390 private: | 393 private: |
| 394 virtual ~JobSpoolerCUPS() {} |
| 395 |
| 391 scoped_refptr<PrintSystemCUPS> print_system_; | 396 scoped_refptr<PrintSystemCUPS> print_system_; |
| 392 | 397 |
| 393 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); | 398 DISALLOW_COPY_AND_ASSIGN(JobSpoolerCUPS); |
| 394 }; | 399 }; |
| 395 | 400 |
| 396 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) | 401 PrintSystemCUPS::PrintSystemCUPS(const DictionaryValue* print_system_settings) |
| 397 : update_timeout_(base::TimeDelta::FromMinutes( | 402 : update_timeout_(base::TimeDelta::FromMinutes( |
| 398 kCheckForPrinterUpdatesMinutes)), | 403 kCheckForPrinterUpdatesMinutes)), |
| 399 initialized_(false), | 404 initialized_(false), |
| 400 printer_enum_succeeded_(false), | 405 printer_enum_succeeded_(false), |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 | 834 |
| 830 void PrintSystemCUPS::RunCapsCallback( | 835 void PrintSystemCUPS::RunCapsCallback( |
| 831 const PrinterCapsAndDefaultsCallback& callback, | 836 const PrinterCapsAndDefaultsCallback& callback, |
| 832 bool succeeded, | 837 bool succeeded, |
| 833 const std::string& printer_name, | 838 const std::string& printer_name, |
| 834 const printing::PrinterCapsAndDefaults& printer_info) { | 839 const printing::PrinterCapsAndDefaults& printer_info) { |
| 835 callback.Run(succeeded, printer_name, printer_info); | 840 callback.Run(succeeded, printer_name, printer_info); |
| 836 } | 841 } |
| 837 | 842 |
| 838 } // namespace cloud_print | 843 } // namespace cloud_print |
| OLD | NEW |