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