11bd154502d15a88fc265ad4ab3d45eca9cff30a
[platform/framework/web/crosswalk.git] / src / chrome / utility / image_writer / image_writer.h
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 #ifndef CHROME_UTILITY_IMAGE_WRITER_IMAGE_WRITER_H_
6 #define CHROME_UTILITY_IMAGE_WRITER_IMAGE_WRITER_H_
7
8 #if defined(OS_WIN)
9 #include <windows.h>
10 #endif
11
12 #include <string>
13 #include <vector>
14
15 #include "base/bind.h"
16 #include "base/callback.h"
17 #include "base/files/file.h"
18 #include "base/files/file_path.h"
19 #include "base/memory/weak_ptr.h"
20
21 namespace image_writer {
22
23 class ImageWriterHandler;
24
25 // Manages a write within the utility thread.  This class holds all the state
26 // around the writing and communicates with the ImageWriterHandler to dispatch
27 // messages.
28 class ImageWriter : public base::SupportsWeakPtr<ImageWriter> {
29  public:
30   explicit ImageWriter(ImageWriterHandler* handler,
31                        const base::FilePath& image_path,
32                        const base::FilePath& device_path);
33   virtual ~ImageWriter();
34
35   // Starts a write from |image_path_| to |device_path_|.
36   void Write();
37   // Starts verifying that |image_path_| and |device_path_| have the same size
38   // and contents.
39   void Verify();
40   // Cancels any pending writes or verifications.
41   void Cancel();
42
43   // Returns whether an operation is in progress.
44   bool IsRunning() const;
45   // Checks if a path is a valid target device.
46   // This method has OS-specific implementations.
47   bool IsValidDevice();
48   // Unmounts all volumes on the target device.
49   // This method has OS-specific implementations.
50   bool UnmountVolumes();
51
52   // Return the current image path.
53   const base::FilePath& GetImagePath();
54   // Return the current device path.
55   const base::FilePath& GetDevicePath();
56
57  private:
58   // Convenience wrappers.
59   void PostTask(const base::Closure& task);
60   void PostProgress(int64 progress);
61   void Error(const std::string& message);
62
63   // Initializes the files.
64   bool InitializeFiles();
65
66   // Work loops.
67   void WriteChunk();
68   void VerifyChunk();
69
70   base::FilePath image_path_;
71   base::FilePath device_path_;
72
73   base::File image_file_;
74   base::File device_file_;
75   int64 bytes_processed_;
76   bool running_;
77
78 #if defined(OS_WIN)
79   std::vector<HANDLE> volume_handles_;
80 #endif
81
82   ImageWriterHandler* handler_;
83 };
84
85 }  // namespace image_writer
86
87 #endif  // CHROME_UTILITY_IMAGE_WRITER_IMAGE_WRITER_H_