Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: chrome/browser/chromeos/printing/cups_print_job_manager_impl.cc

Issue 2943843002: Convert to CupsPrintJobManagerImpl to TaskScheduler. (Closed)
Patch Set: notification must happen on the browser thread Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/printing/cups_print_job_manager_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/chromeos/printing/cups_print_job_manager_impl.h" 5 #include "chrome/browser/chromeos/printing/cups_print_job_manager_impl.h"
6 6
7 #include <cups/cups.h> 7 #include <cups/cups.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
17 #include "base/sequenced_task_runner.h"
17 #include "base/stl_util.h" 18 #include "base/stl_util.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/sequenced_task_runner_handle.h" 20 #include "base/task_runner_util.h"
20 #include "base/threading/sequenced_worker_pool.h" 21 #include "base/task_scheduler/post_task.h"
22 #include "base/threading/thread_task_runner_handle.h"
21 #include "chrome/browser/chrome_notification_types.h" 23 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/chromeos/printing/cups_print_job.h" 24 #include "chrome/browser/chromeos/printing/cups_print_job.h"
23 #include "chrome/browser/chromeos/printing/printers_manager.h" 25 #include "chrome/browser/chromeos/printing/printers_manager.h"
24 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" 26 #include "chrome/browser/chromeos/printing/printers_manager_factory.h"
25 #include "chrome/browser/printing/print_job.h" 27 #include "chrome/browser/printing/print_job.h"
26 #include "chrome/browser/profiles/profile.h" 28 #include "chrome/browser/profiles/profile.h"
27 #include "content/public/browser/browser_context.h" 29 #include "content/public/browser/browser_context.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/notification_registrar.h" 30 #include "content/public/browser/notification_registrar.h"
30 #include "content/public/browser/notification_service.h" 31 #include "content/public/browser/notification_service.h"
31 #include "printing/backend/cups_connection.h" 32 #include "printing/backend/cups_connection.h"
32 #include "printing/printed_document.h" 33 #include "printing/printed_document.h"
33 34
34 namespace { 35 namespace {
35 36
36 // The rate in milliseconds at which we will poll CUPS for print job updates. 37 // The rate in milliseconds at which we will poll CUPS for print job updates.
37 const int kPollRate = 1000; 38 const int kPollRate = 1000;
38 39
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 break; 98 break;
98 } 99 }
99 100
100 NOTREACHED(); 101 NOTREACHED();
101 102
102 return State::STATE_NONE; 103 return State::STATE_NONE;
103 } 104 }
104 105
105 chromeos::QueryResult QueryCups(::printing::CupsConnection* connection, 106 chromeos::QueryResult QueryCups(::printing::CupsConnection* connection,
106 const std::vector<std::string>& printer_ids) { 107 const std::vector<std::string>& printer_ids) {
108 base::ThreadRestrictions::AssertIOAllowed();
107 chromeos::QueryResult result; 109 chromeos::QueryResult result;
108 result.success = connection->GetJobs(printer_ids, &result.queues); 110 result.success = connection->GetJobs(printer_ids, &result.queues);
109 return result; 111 return result;
110 } 112 }
111 113
112 // Returns true if |printer_status|.reasons contains |reason|. 114 // Returns true if |printer_status|.reasons contains |reason|.
113 bool ContainsReason(const printing::PrinterStatus printer_status, 115 bool ContainsReason(const printing::PrinterStatus printer_status,
114 PrinterReason::Reason reason) { 116 PrinterReason::Reason reason) {
115 return std::find_if(printer_status.reasons.begin(), 117 return std::find_if(printer_status.reasons.begin(),
116 printer_status.reasons.end(), 118 printer_status.reasons.end(),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } // namespace 177 } // namespace
176 178
177 namespace chromeos { 179 namespace chromeos {
178 180
179 QueryResult::QueryResult() = default; 181 QueryResult::QueryResult() = default;
180 182
181 QueryResult::QueryResult(const QueryResult& other) = default; 183 QueryResult::QueryResult(const QueryResult& other) = default;
182 184
183 QueryResult::~QueryResult() = default; 185 QueryResult::~QueryResult() = default;
184 186
185 CupsPrintJobManagerImpl::CupsPrintJobManagerImpl(Profile* profile) 187 CupsPrintJobManagerImpl::CupsPrintJobManagerImpl(
188 Profile* profile,
189 scoped_refptr<base::SingleThreadTaskRunner> browser_thread_runner)
186 : CupsPrintJobManager(profile), 190 : CupsPrintJobManager(profile),
187 cups_connection_(GURL(), HTTP_ENCRYPT_NEVER, false), 191 cups_connection_(GURL(), HTTP_ENCRYPT_NEVER, false),
192 browser_thread_runner_(browser_thread_runner),
gab 2017/06/20 21:04:49 std::move
skau 2017/06/24 01:03:44 It's not injected anymore.
193 query_runner_(base::CreateSequencedTaskRunnerWithTraits(
194 base::TaskTraits(base::TaskPriority::BACKGROUND,
195 base::MayBlock(),
196 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN))),
188 weak_ptr_factory_(this) { 197 weak_ptr_factory_(this) {
189 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, 198 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
190 content::NotificationService::AllSources()); 199 content::NotificationService::AllSources());
191 } 200 }
192 201
193 CupsPrintJobManagerImpl::~CupsPrintJobManagerImpl() {} 202 CupsPrintJobManagerImpl::~CupsPrintJobManagerImpl() {}
194 203
195 // Must be run from the UI thread.
196 void CupsPrintJobManagerImpl::CancelPrintJob(CupsPrintJob* job) { 204 void CupsPrintJobManagerImpl::CancelPrintJob(CupsPrintJob* job) {
197 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 205 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
198 206
199 // Copy job_id and printer_id. |job| is about to be freed. 207 // Copy job_id and printer_id. |job| is about to be freed.
200 const int job_id = job->job_id(); 208 const int job_id = job->job_id();
201 const std::string printer_id = job->printer().id(); 209 const std::string printer_id = job->printer().id();
202 210
203 // Stop montioring jobs after we cancel them. The user no longer cares. 211 // Stop montioring jobs after we cancel them. The user no longer cares.
204 jobs_.erase(job->GetUniqueId()); 212 jobs_.erase(job->GetUniqueId());
205 213
206 // Be sure to copy out all relevant fields. |job| may be destroyed after we 214 // Be sure to copy out all relevant fields. |job| may be destroyed after we
207 // exit this scope. 215 // exit this scope.
208 content::BrowserThread::GetBlockingPool()->PostTask( 216 base::PostTaskWithTraits(
209 FROM_HERE, 217 FROM_HERE,
218 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
210 base::Bind(&CupsPrintJobManagerImpl::CancelJobImpl, 219 base::Bind(&CupsPrintJobManagerImpl::CancelJobImpl,
211 weak_ptr_factory_.GetWeakPtr(), printer_id, job_id)); 220 weak_ptr_factory_.GetWeakPtr(), printer_id, job_id));
212 } 221 }
213 222
214 bool CupsPrintJobManagerImpl::SuspendPrintJob(CupsPrintJob* job) { 223 bool CupsPrintJobManagerImpl::SuspendPrintJob(CupsPrintJob* job) {
215 NOTREACHED() << "Pause printer is not implemented"; 224 NOTREACHED() << "Pause printer is not implemented";
216 return false; 225 return false;
217 } 226 }
218 227
219 bool CupsPrintJobManagerImpl::ResumePrintJob(CupsPrintJob* job) { 228 bool CupsPrintJobManagerImpl::ResumePrintJob(CupsPrintJob* job) {
(...skipping 17 matching lines...) Expand all
237 CreatePrintJob(base::UTF16ToUTF8(document->settings().device_name()), 246 CreatePrintJob(base::UTF16ToUTF8(document->settings().device_name()),
238 base::UTF16ToUTF8(document->settings().title()), 247 base::UTF16ToUTF8(document->settings().title()),
239 job_details->job_id(), document->page_count()); 248 job_details->job_id(), document->page_count());
240 } 249 }
241 } 250 }
242 251
243 bool CupsPrintJobManagerImpl::CreatePrintJob(const std::string& printer_name, 252 bool CupsPrintJobManagerImpl::CreatePrintJob(const std::string& printer_name,
244 const std::string& title, 253 const std::string& title,
245 int job_id, 254 int job_id,
246 int total_page_number) { 255 int total_page_number) {
256 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
257
247 auto printer = 258 auto printer =
248 PrintersManagerFactory::GetForBrowserContext(profile_)->GetPrinter( 259 PrintersManagerFactory::GetForBrowserContext(profile_)->GetPrinter(
249 printer_name); 260 printer_name);
250 if (!printer) { 261 if (!printer) {
251 LOG(WARNING) << "Printer was removed while job was in progress. It cannot " 262 LOG(WARNING) << "Printer was removed while job was in progress. It cannot "
252 "be tracked"; 263 "be tracked";
253 return false; 264 return false;
254 } 265 }
255 266
256 // Records the number of jobs we're currently tracking when a new job is 267 // Records the number of jobs we're currently tracking when a new job is
(...skipping 20 matching lines...) Expand all
277 } 288 }
278 289
279 void CupsPrintJobManagerImpl::ScheduleQuery() { 290 void CupsPrintJobManagerImpl::ScheduleQuery() {
280 ScheduleQuery(base::TimeDelta::FromMilliseconds(kPollRate)); 291 ScheduleQuery(base::TimeDelta::FromMilliseconds(kPollRate));
281 } 292 }
282 293
283 void CupsPrintJobManagerImpl::ScheduleQuery(const base::TimeDelta& delay) { 294 void CupsPrintJobManagerImpl::ScheduleQuery(const base::TimeDelta& delay) {
284 if (!in_query_) { 295 if (!in_query_) {
285 in_query_ = true; 296 in_query_ = true;
286 297
287 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( 298 browser_thread_runner_->PostDelayedTask(
288 FROM_HERE, 299 FROM_HERE,
289 base::Bind(&CupsPrintJobManagerImpl::PostQuery, 300 base::Bind(&CupsPrintJobManagerImpl::PostQuery,
290 weak_ptr_factory_.GetWeakPtr()), 301 weak_ptr_factory_.GetWeakPtr()),
291 delay); 302 delay);
292 } 303 }
293 } 304 }
294 305
295 void CupsPrintJobManagerImpl::PostQuery() { 306 void CupsPrintJobManagerImpl::PostQuery() {
307 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
308
296 // The set of active printers is expected to be small. 309 // The set of active printers is expected to be small.
297 std::set<std::string> printer_ids; 310 std::set<std::string> printer_ids;
298 for (const auto& entry : jobs_) { 311 for (const auto& entry : jobs_) {
299 printer_ids.insert(entry.second->printer().id()); 312 printer_ids.insert(entry.second->printer().id());
300 } 313 }
301 std::vector<std::string> ids{printer_ids.begin(), printer_ids.end()}; 314 std::vector<std::string> ids{printer_ids.begin(), printer_ids.end()};
302 315
303 content::BrowserThread::PostTaskAndReplyWithResult( 316 base::PostTaskAndReplyWithResult(
304 content::BrowserThread::FILE_USER_BLOCKING, FROM_HERE, 317 query_runner_.get(), FROM_HERE,
305 base::Bind(&QueryCups, &cups_connection_, ids), 318 base::Bind(&QueryCups, &cups_connection_, ids),
306 base::Bind(&CupsPrintJobManagerImpl::UpdateJobs, 319 base::Bind(&CupsPrintJobManagerImpl::UpdateJobs,
307 weak_ptr_factory_.GetWeakPtr())); 320 weak_ptr_factory_.GetWeakPtr()));
308 } 321 }
309 322
310 bool CupsPrintJobManagerImpl::UpdatePrintJob( 323 bool CupsPrintJobManagerImpl::UpdatePrintJob(
311 const ::printing::PrinterStatus& printer_status, 324 const ::printing::PrinterStatus& printer_status,
312 const ::printing::CupsJob& job, 325 const ::printing::CupsJob& job,
313 CupsPrintJob* print_job) { 326 CupsPrintJob* print_job) {
314 DCHECK_EQ(job.id, print_job->job_id()); 327 DCHECK_EQ(job.id, print_job->job_id());
(...skipping 25 matching lines...) Expand all
340 break; 353 break;
341 } 354 }
342 355
343 return print_job->state() != old_state || pages_updated; 356 return print_job->state() != old_state || pages_updated;
344 } 357 }
345 358
346 // Use job information to update local job states. Previously completed jobs 359 // Use job information to update local job states. Previously completed jobs
347 // could be in |jobs| but those are ignored as we will not emit updates for them 360 // could be in |jobs| but those are ignored as we will not emit updates for them
348 // after they are completed. 361 // after they are completed.
349 void CupsPrintJobManagerImpl::UpdateJobs(const QueryResult& result) { 362 void CupsPrintJobManagerImpl::UpdateJobs(const QueryResult& result) {
363 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
364
350 const std::vector<::printing::QueueStatus>& queues = result.queues; 365 const std::vector<::printing::QueueStatus>& queues = result.queues;
351 366
352 // Query has completed. Allow more queries. 367 // Query has completed. Allow more queries.
353 in_query_ = false; 368 in_query_ = false;
354 369
355 // If the query failed, either retry or purge. 370 // If the query failed, either retry or purge.
356 if (!result.success) { 371 if (!result.success) {
357 retry_count_++; 372 retry_count_++;
358 LOG(WARNING) << "Failed to query CUPS for queue status. Schedule retry (" 373 LOG(WARNING) << "Failed to query CUPS for queue status. Schedule retry ("
359 << retry_count_ << ")"; 374 << retry_count_ << ")";
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 ScheduleQuery(); 423 ScheduleQuery();
409 } else if (!jobs_.empty()) { 424 } else if (!jobs_.empty()) {
410 // We're tracking jobs that we didn't receive an update for. Something bad 425 // We're tracking jobs that we didn't receive an update for. Something bad
411 // has happened. 426 // has happened.
412 LOG(ERROR) << "Lost track of (" << jobs_.size() << ") jobs"; 427 LOG(ERROR) << "Lost track of (" << jobs_.size() << ") jobs";
413 PurgeJobs(); 428 PurgeJobs();
414 } 429 }
415 } 430 }
416 431
417 void CupsPrintJobManagerImpl::PurgeJobs() { 432 void CupsPrintJobManagerImpl::PurgeJobs() {
433 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
434
418 for (const auto& entry : jobs_) { 435 for (const auto& entry : jobs_) {
419 // Declare all lost jobs errors. 436 // Declare all lost jobs errors.
420 RecordJobResult(LOST); 437 RecordJobResult(LOST);
421 CupsPrintJob* job = entry.second.get(); 438 CupsPrintJob* job = entry.second.get();
422 job->set_state(CupsPrintJob::State::STATE_ERROR); 439 job->set_state(CupsPrintJob::State::STATE_ERROR);
423 NotifyJobStateUpdate(job); 440 NotifyJobStateUpdate(job);
424 } 441 }
425 442
426 jobs_.clear(); 443 jobs_.clear();
427 } 444 }
428 445
429 void CupsPrintJobManagerImpl::CancelJobImpl(const std::string& printer_id, 446 void CupsPrintJobManagerImpl::CancelJobImpl(const std::string& printer_id,
430 const int job_id) { 447 const int job_id) {
448 base::ThreadRestrictions::AssertIOAllowed();
449
431 std::unique_ptr<::printing::CupsPrinter> printer = 450 std::unique_ptr<::printing::CupsPrinter> printer =
432 cups_connection_.GetPrinter(printer_id); 451 cups_connection_.GetPrinter(printer_id);
433 if (!printer) { 452 if (!printer) {
434 LOG(WARNING) << "Printer not found: " << printer_id; 453 LOG(WARNING) << "Printer not found: " << printer_id;
435 return; 454 return;
436 } 455 }
437 456
438 if (!printer->CancelJob(job_id)) { 457 if (!printer->CancelJob(job_id)) {
439 // This is not expected to fail but log it if it does. 458 // This is not expected to fail but log it if it does.
440 LOG(WARNING) << "Cancelling job failed. Job may be stuck in queue."; 459 LOG(WARNING) << "Cancelling job failed. Job may be stuck in queue.";
441 } 460 }
442 } 461 }
443 462
444 void CupsPrintJobManagerImpl::NotifyJobStateUpdate(CupsPrintJob* job) { 463 void CupsPrintJobManagerImpl::NotifyJobStateUpdate(CupsPrintJob* job) {
464 DCHECK(browser_thread_runner_->RunsTasksInCurrentSequence());
465
445 switch (job->state()) { 466 switch (job->state()) {
446 case State::STATE_NONE: 467 case State::STATE_NONE:
447 // State does not require notification. 468 // State does not require notification.
448 break; 469 break;
449 case State::STATE_WAITING: 470 case State::STATE_WAITING:
450 NotifyJobUpdated(job); 471 NotifyJobUpdated(job);
451 break; 472 break;
452 case State::STATE_STARTED: 473 case State::STATE_STARTED:
453 NotifyJobStarted(job); 474 NotifyJobStarted(job);
454 break; 475 break;
(...skipping 13 matching lines...) Expand all
468 NotifyJobError(job); 489 NotifyJobError(job);
469 break; 490 break;
470 case State::STATE_DOCUMENT_DONE: 491 case State::STATE_DOCUMENT_DONE:
471 NotifyJobDone(job); 492 NotifyJobDone(job);
472 break; 493 break;
473 } 494 }
474 } 495 }
475 496
476 // static 497 // static
477 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) { 498 CupsPrintJobManager* CupsPrintJobManager::CreateInstance(Profile* profile) {
478 return new CupsPrintJobManagerImpl(profile); 499 return new CupsPrintJobManagerImpl(profile,
500 base::ThreadTaskRunnerHandle::Get());
479 } 501 }
480 502
481 } // namespace chromeos 503 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/printing/cups_print_job_manager_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698