summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/printer_job_handler.cc
blob: 8e765b6be4d8dfc17b3ea1578562a3fb6c068656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/service/cloud_print/printer_job_handler.h"

#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/md5.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/service/cloud_print/cloud_print_consts.h"
#include "chrome/service/cloud_print/cloud_print_helpers.h"
#include "chrome/service/cloud_print/job_status_updater.h"
#include "googleurl/src/gurl.h"
#include "net/http/http_response_headers.h"

PrinterJobHandler::PrinterJobHandler(
    const cloud_print::PrinterBasicInfo& printer_info,
    const std::string& printer_id,
    const std::string& caps_hash,
    const std::string& auth_token,
    const GURL& cloud_print_server_url,
    cloud_print::PrintSystem* print_system,
    Delegate* delegate)
    : print_system_(print_system),
      printer_info_(printer_info),
      printer_id_(printer_id),
      auth_token_(auth_token),
      last_caps_hash_(caps_hash),
      cloud_print_server_url_(cloud_print_server_url),
      delegate_(delegate),
      local_job_id_(-1),
      next_response_handler_(NULL),
      next_failure_handler_(NULL),
      server_error_count_(0),
      print_thread_("Chrome_CloudPrintJobPrintThread"),
      shutting_down_(false),
      server_job_available_(false),
      printer_update_pending_(true),
      printer_delete_pending_(false),
      task_in_progress_(false) {
}

bool PrinterJobHandler::Initialize() {
  if (print_system_->IsValidPrinter(printer_info_.printer_name)) {
    printer_watcher_ = print_system_->CreatePrinterWatcher(
        printer_info_.printer_name);
    printer_watcher_->StartWatching(this);
    NotifyJobAvailable();
  } else {
    // This printer does not exist any more. Delete it from the server.
    OnPrinterDeleted();
  }
  return true;
}

PrinterJobHandler::~PrinterJobHandler() {
  printer_watcher_->StopWatching();
}

void PrinterJobHandler::Reset() {
  print_data_url_.clear();
  job_details_.Clear();
  request_.reset();
  print_thread_.Stop();
}

void PrinterJobHandler::Start() {
  if (task_in_progress_) {
    // Multiple Starts can get posted because of multiple notifications
    // We want to ignore the other ones that happen when a task is in progress.
    return;
  }
  Reset();
  if (!shutting_down_) {
    // Check if we have work to do.
    if (HavePendingTasks()) {
      if (printer_delete_pending_) {
        printer_delete_pending_ = false;
        task_in_progress_ = true;
        MakeServerRequest(
            CloudPrintHelpers::GetUrlForPrinterDelete(cloud_print_server_url_,
                                                      printer_id_),
            &PrinterJobHandler::HandlePrinterDeleteResponse,
            &PrinterJobHandler::Stop);
      }
      if (!task_in_progress_ && printer_update_pending_) {
        printer_update_pending_ = false;
        task_in_progress_ = UpdatePrinterInfo();
      }
      if (!task_in_progress_ && server_job_available_) {
        task_in_progress_ = true;
        server_job_available_ = false;
        // We need to fetch any pending jobs for this printer
        MakeServerRequest(
            CloudPrintHelpers::GetUrlForJobFetch(
                cloud_print_server_url_, printer_id_),
            &PrinterJobHandler::HandleJobMetadataResponse,
            &PrinterJobHandler::Stop);
      }
    }
  }
}

void PrinterJobHandler::Stop() {
  task_in_progress_ = false;
  Reset();
  if (HavePendingTasks()) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start));
  }
}

void PrinterJobHandler::NotifyJobAvailable() {
  server_job_available_ = true;
  if (!task_in_progress_) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start));
  }
}

bool PrinterJobHandler::UpdatePrinterInfo() {
  // We need to update the parts of the printer info that have changed
  // (could be printer name, description, status or capabilities).
  cloud_print::PrinterBasicInfo printer_info;
  printer_watcher_->GetCurrentPrinterInfo(&printer_info);
  cloud_print::PrinterCapsAndDefaults printer_caps;
  std::string post_data;
  std::string mime_boundary;
  if (print_system_->GetPrinterCapsAndDefaults(printer_info.printer_name,
                                               &printer_caps)) {
    std::string caps_hash = MD5String(printer_caps.printer_capabilities);
    CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary);
    if (caps_hash != last_caps_hash_) {
      // Hashes don't match, we need to upload new capabilities (the defaults
      // go for free along with the capabilities)
      last_caps_hash_ = caps_hash;
      CloudPrintHelpers::AddMultipartValueForUpload(
          kPrinterCapsValue, printer_caps.printer_capabilities,
          mime_boundary, printer_caps.caps_mime_type, &post_data);
      CloudPrintHelpers::AddMultipartValueForUpload(
          kPrinterDefaultsValue, printer_caps.printer_defaults,
          mime_boundary, printer_caps.defaults_mime_type,
          &post_data);
      CloudPrintHelpers::AddMultipartValueForUpload(
          WideToUTF8(kPrinterCapsHashValue).c_str(), caps_hash, mime_boundary,
          std::string(), &post_data);
    }
  }
  if (printer_info.printer_name != printer_info_.printer_name) {
    CloudPrintHelpers::AddMultipartValueForUpload(kPrinterNameValue,
                                                  printer_info.printer_name,
                                                  mime_boundary,
                                                  std::string(), &post_data);
  }
  if (printer_info.printer_description != printer_info_.printer_description) {
    CloudPrintHelpers::AddMultipartValueForUpload(
        kPrinterDescValue, printer_info.printer_description, mime_boundary,
        std::string() , &post_data);
  }
  if (printer_info.printer_status != printer_info_.printer_status) {
    CloudPrintHelpers::AddMultipartValueForUpload(
        kPrinterStatusValue, StringPrintf("%d", printer_info.printer_status),
        mime_boundary, std::string(), &post_data);
  }
  printer_info_ = printer_info;
  bool ret = false;
  if (!post_data.empty()) {
    // Terminate the request body
    post_data.append("--" + mime_boundary + "--\r\n");
    std::string mime_type("multipart/form-data; boundary=");
    mime_type += mime_boundary;
    request_.reset(
        new URLFetcher(CloudPrintHelpers::GetUrlForPrinterUpdate(
                           cloud_print_server_url_, printer_id_),
                       URLFetcher::POST, this));
    CloudPrintHelpers::PrepCloudPrintRequest(request_.get(), auth_token_);
    request_->set_upload_data(mime_type, post_data);
    next_response_handler_ = &PrinterJobHandler::HandlePrinterUpdateResponse;
    next_failure_handler_ = &PrinterJobHandler::Stop;
    request_->Start();
    ret = true;
  }
  return ret;
}

// URLFetcher::Delegate implementation.
void PrinterJobHandler::OnURLFetchComplete(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  if (!shutting_down_) {
    DCHECK(source == request_.get());
    // We need a next response handler because we are strictly a sequential
    // state machine. We need each response handler to tell us which state to
    //  advance to next.
    DCHECK(next_response_handler_);
    if (!(this->*next_response_handler_)(source, url, status,
                                         response_code, cookies, data)) {
      // By contract, if the response handler returns false, it wants us to
      // retry the request (upto the usual limit after which we give up and
      // send the state machine to the Stop state);
      HandleServerError(url);
    }
  }
}

// JobStatusUpdater::Delegate implementation
bool PrinterJobHandler::OnJobCompleted(JobStatusUpdater* updater) {
  bool ret = false;
  for (JobStatusUpdaterList::iterator index = job_status_updater_list_.begin();
       index != job_status_updater_list_.end(); index++) {
    if (index->get() == updater) {
      job_status_updater_list_.erase(index);
      ret = true;
      break;
    }
  }
  return ret;
}

void PrinterJobHandler::OnPrinterDeleted() {
  printer_delete_pending_ = true;
  if (!task_in_progress_) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start));
  }
}

void PrinterJobHandler::OnPrinterChanged() {
  printer_update_pending_ = true;
  if (!task_in_progress_) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start));
  }
}

void PrinterJobHandler::OnJobChanged() {
  // Some job on the printer changed. Loop through all our JobStatusUpdaters
  // and have them check for updates.
  for (JobStatusUpdaterList::iterator index = job_status_updater_list_.begin();
       index != job_status_updater_list_.end(); index++) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(index->get(),
                                     &JobStatusUpdater::UpdateStatus));
  }
}

bool PrinterJobHandler::HandlePrinterUpdateResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  bool ret = false;
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (status.is_success() && (response_code == 200)) {
    bool succeeded = false;
    DictionaryValue* response_dict = NULL;
    CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict);
    // If we get valid JSON back, we are done.
    if (NULL != response_dict) {
      ret = true;
    }
  }
  if (ret) {
    // We are done here. Go to the Stop state
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
  } else {
    // Since we failed to update the server, set the flag again.
    printer_update_pending_ = true;
  }
  return ret;
}

bool PrinterJobHandler::HandlePrinterDeleteResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  bool ret = false;
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (status.is_success() && (response_code == 200)) {
    bool succeeded = false;
    DictionaryValue* response_dict = NULL;
    CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict);
    // If we get valid JSON back, we are done.
    if (NULL != response_dict) {
      ret = true;
    }
  }
  if (ret) {
    // The printer has been deleted. Shutdown the handler class.
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Shutdown));
  } else {
    // Since we failed to update the server, set the flag again.
    printer_delete_pending_ = true;
  }
  return ret;
}

bool PrinterJobHandler::HandleJobMetadataResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (!status.is_success() || (response_code != 200)) {
    return false;
  }
  bool succeeded = false;
  DictionaryValue* response_dict = NULL;
  CloudPrintHelpers::ParseResponseJSON(data, &succeeded, &response_dict);
  if (NULL == response_dict) {
    // If we did not get a valid JSON response, we need to retry.
    return false;
  }
  Task* next_task = NULL;
  if (succeeded) {
    ListValue* job_list = NULL;
    response_dict->GetList(kJobListValue, &job_list);
    if (job_list) {
      // Even though it is a job list, for now we are only interested in the
      // first job
      DictionaryValue* job_data = NULL;
      if (job_list->GetDictionary(0, &job_data)) {
        job_data->GetString(kIdValue, &job_details_.job_id_);
        job_data->GetString(kTitleValue, &job_details_.job_title_);
        std::string print_ticket_url;
        job_data->GetString(kTicketUrlValue, &print_ticket_url);
        job_data->GetString(kFileUrlValue, &print_data_url_);
        next_task = NewRunnableMethod(
              this, &PrinterJobHandler::MakeServerRequest,
              GURL(print_ticket_url.c_str()),
              &PrinterJobHandler::HandlePrintTicketResponse,
              &PrinterJobHandler::FailedFetchingJobData);
      }
    }
  }
  if (!next_task) {
    // If we got a valid JSON but there were no jobs, we are done
    next_task = NewRunnableMethod(this, &PrinterJobHandler::Stop);
  }
  delete response_dict;
  DCHECK(next_task);
  MessageLoop::current()->PostTask(FROM_HERE, next_task);
  return true;
}

bool PrinterJobHandler::HandlePrintTicketResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (!status.is_success() || (response_code != 200)) {
    return false;
  }
  if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) {
    job_details_.print_ticket_ = data;
    MessageLoop::current()->PostTask(
        FROM_HERE,
        NewRunnableMethod(this,
                          &PrinterJobHandler::MakeServerRequest,
                          GURL(print_data_url_.c_str()),
                          &PrinterJobHandler::HandlePrintDataResponse,
                          &PrinterJobHandler::FailedFetchingJobData));
  } else {
    // The print ticket was not valid. We are done here.
    FailedFetchingJobData();
  }
  return true;
}

bool PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source,
                                                const GURL& url,
                                                const URLRequestStatus& status,
                                                int response_code,
                                                const ResponseCookies& cookies,
                                                const std::string& data) {
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (!status.is_success() || (response_code != 200)) {
    return false;
  }
  Task* next_task = NULL;
  if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) {
    int ret = file_util::WriteFile(job_details_.print_data_file_path_,
                                   data.c_str(),
                                   data.length());
    source->response_headers()->GetMimeType(
        &job_details_.print_data_mime_type_);
    DCHECK(ret == static_cast<int>(data.length()));
    if (ret == static_cast<int>(data.length())) {
      next_task = NewRunnableMethod(this, &PrinterJobHandler::StartPrinting);
    }
  }
  // If there was no task allocated above, then there was an error in
  // saving the print data, bail out here.
  if (!next_task) {
    next_task = NewRunnableMethod(this, &PrinterJobHandler::JobFailed,
                                  JOB_DOWNLOAD_FAILED);
  }
  MessageLoop::current()->PostTask(FROM_HERE, next_task);
  return true;
}

void PrinterJobHandler::StartPrinting() {
  // We are done with the request object for now.
  request_.reset();
  if (!shutting_down_) {
    if (!print_thread_.Start()) {
      JobFailed(PRINT_FAILED);
    } else {
      print_thread_.message_loop()->PostTask(
          FROM_HERE, NewRunnableFunction(&PrinterJobHandler::DoPrint,
                                         job_details_,
                                         printer_info_.printer_name,
                                         print_system_, this,
                                         MessageLoop::current()));
    }
  }
}

void PrinterJobHandler::JobFailed(PrintJobError error) {
  if (!shutting_down_) {
    UpdateJobStatus(cloud_print::PRINT_JOB_STATUS_ERROR, error);
  }
}

void PrinterJobHandler::JobSpooled(cloud_print::PlatformJobId local_job_id) {
  if (!shutting_down_) {
    local_job_id_ = local_job_id;
    UpdateJobStatus(cloud_print::PRINT_JOB_STATUS_IN_PROGRESS, SUCCESS);
    print_thread_.Stop();
  }
}

void PrinterJobHandler::Shutdown() {
  Reset();
  shutting_down_ = true;
  while (!job_status_updater_list_.empty()) {
    // Calling Stop() will cause the OnJobCompleted to be called which will
    // remove the updater object from the list.
    job_status_updater_list_.front()->Stop();
  }
  if (delegate_) {
    delegate_->OnPrinterJobHandlerShutdown(this, printer_id_);
  }
}

void PrinterJobHandler::HandleServerError(const GURL& url) {
  Task* task_to_retry = NewRunnableMethod(this,
                                          &PrinterJobHandler::FetchURL, url);
  Task* task_on_give_up = NewRunnableMethod(this, next_failure_handler_);
  CloudPrintHelpers::HandleServerError(&server_error_count_, kMaxRetryCount,
                                       -1, kBaseRetryInterval, task_to_retry,
                                       task_on_give_up);
}

void PrinterJobHandler::UpdateJobStatus(cloud_print::PrintJobStatus status,
                                        PrintJobError error) {
  if (!shutting_down_) {
    if (!job_details_.job_id_.empty()) {
      LOG(INFO) << "CP: Updating status, jod id: " << job_details_.job_id_ <<
          ", status: " << status;

      ResponseHandler response_handler = NULL;
      if (error == SUCCESS) {
        response_handler =
            &PrinterJobHandler::HandleSuccessStatusUpdateResponse;
      } else {
        response_handler =
            &PrinterJobHandler::HandleFailureStatusUpdateResponse;
      }
      MakeServerRequest(
          CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_,
                                                      job_details_.job_id_,
                                                      status),
          response_handler,
          &PrinterJobHandler::Stop);
    }
  }
}

bool PrinterJobHandler::HandleSuccessStatusUpdateResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (!status.is_success() || (response_code != 200)) {
    return false;
  }
  // The print job has been spooled locally. We now need to create an object
  // that monitors the status of the job and updates the server.
  scoped_refptr<JobStatusUpdater> job_status_updater =
      new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_,
                           local_job_id_, auth_token_, cloud_print_server_url_,
                           print_system_.get(), this);
  job_status_updater_list_.push_back(job_status_updater);
  MessageLoop::current()->PostTask(
      FROM_HERE, NewRunnableMethod(job_status_updater.get(),
                                   &JobStatusUpdater::UpdateStatus));
  bool succeeded = false;
  CloudPrintHelpers::ParseResponseJSON(data, &succeeded, NULL);
  if (succeeded) {
    // Since we just printed successfully, we want to look for more jobs.
    server_job_available_ = true;
  }
  MessageLoop::current()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
  return true;
}

bool PrinterJobHandler::HandleFailureStatusUpdateResponse(
    const URLFetcher* source, const GURL& url, const URLRequestStatus& status,
    int response_code, const ResponseCookies& cookies,
    const std::string& data) {
  // If there was a network error or a non-200 response (which, for our purposes
  // is the same as a network error), we want to retry.
  if (!status.is_success() || (response_code != 200)) {
    return false;
  }
  MessageLoop::current()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop));
  return true;
}

void PrinterJobHandler::MakeServerRequest(const GURL& url,
                                          ResponseHandler response_handler,
                                          FailureHandler failure_handler) {
  if (!shutting_down_) {
    server_error_count_ = 0;
    // Set up the next response handler
    next_response_handler_ = response_handler;
    next_failure_handler_ = failure_handler;
    FetchURL(url);
  }
}

void PrinterJobHandler::FetchURL(const GURL& url) {
  request_.reset(new URLFetcher(url, URLFetcher::GET, this));
  CloudPrintHelpers::PrepCloudPrintRequest(request_.get(), auth_token_);
  request_->Start();
}

bool PrinterJobHandler::HavePendingTasks() {
  return server_job_available_ || printer_update_pending_ ||
         printer_delete_pending_;
}

void PrinterJobHandler::FailedFetchingJobData() {
  if (!shutting_down_) {
    LOG(ERROR) << "CP: Failed fetching job data for printer: " <<
        printer_info_.printer_name << ", job id: " << job_details_.job_id_;
    JobFailed(INVALID_JOB_DATA);
  }
}

void PrinterJobHandler::DoPrint(const JobDetails& job_details,
                          const std::string& printer_name,
                          scoped_refptr<cloud_print::PrintSystem> print_system,
                          PrinterJobHandler* job_handler,
                          MessageLoop* job_message_loop) {
  DCHECK(job_handler);
  DCHECK(job_message_loop);
  cloud_print::PlatformJobId job_id = -1;
  if (print_system->SpoolPrintJob(job_details.print_ticket_,
                                  job_details.print_data_file_path_,
                                  job_details.print_data_mime_type_,
                                  printer_name,
                                  job_details.job_title_, &job_id)) {
    job_message_loop->PostTask(FROM_HERE,
                               NewRunnableMethod(job_handler,
                                                 &PrinterJobHandler::JobSpooled,
                                                 job_id));
  } else {
    job_message_loop->PostTask(FROM_HERE,
                               NewRunnableMethod(job_handler,
                                                 &PrinterJobHandler::JobFailed,
                                                 PRINT_FAILED));
  }
}