1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/files/file_enumerator.h"
6#include "base/files/file_util.h"
7#include "base/threading/worker_pool.h"
8#include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
9#include "chrome/browser/extensions/api/image_writer_private/operation.h"
10#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
11#include "content/public/browser/browser_thread.h"
12
13namespace extensions {
14namespace image_writer {
15
16using content::BrowserThread;
17
18void Operation::Write(const base::Closure& continuation) {
19  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
20  if (IsCancelled()) {
21    return;
22  }
23
24  SetStage(image_writer_api::STAGE_WRITE);
25  StartUtilityClient();
26
27  int64 file_size;
28  if (!base::GetFileSize(image_path_, &file_size)) {
29    Error(error::kImageReadError);
30    return;
31  }
32
33  BrowserThread::PostTask(
34      BrowserThread::IO,
35      FROM_HERE,
36      base::Bind(
37          &ImageWriterUtilityClient::Write,
38          image_writer_client_,
39          base::Bind(&Operation::WriteImageProgress, this, file_size),
40          base::Bind(&Operation::CompleteAndContinue, this, continuation),
41          base::Bind(&Operation::Error, this),
42          image_path_,
43          device_path_));
44}
45
46void Operation::VerifyWrite(const base::Closure& continuation) {
47  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
48
49  if (IsCancelled()) {
50    return;
51  }
52
53  SetStage(image_writer_api::STAGE_VERIFYWRITE);
54  StartUtilityClient();
55
56  int64 file_size;
57  if (!base::GetFileSize(image_path_, &file_size)) {
58    Error(error::kImageReadError);
59    return;
60  }
61
62  BrowserThread::PostTask(
63      BrowserThread::IO,
64      FROM_HERE,
65      base::Bind(
66          &ImageWriterUtilityClient::Verify,
67          image_writer_client_,
68          base::Bind(&Operation::WriteImageProgress, this, file_size),
69          base::Bind(&Operation::CompleteAndContinue, this, continuation),
70          base::Bind(&Operation::Error, this),
71          image_path_,
72          device_path_));
73}
74
75}  // namespace image_writer
76}  // namespace extensions
77