• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "chrome/utility/image_writer/image_writer_handler.h"
6 
7 #include "base/files/file_path.h"
8 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
9 #include "chrome/utility/image_writer/error_messages.h"
10 #include "content/public/utility/utility_thread.h"
11 
12 namespace image_writer {
13 
ImageWriterHandler()14 ImageWriterHandler::ImageWriterHandler() {}
~ImageWriterHandler()15 ImageWriterHandler::~ImageWriterHandler() {}
16 
SendSucceeded()17 void ImageWriterHandler::SendSucceeded() {
18   Send(new ChromeUtilityHostMsg_ImageWriter_Succeeded());
19   content::UtilityThread::Get()->ReleaseProcessIfNeeded();
20 }
21 
SendCancelled()22 void ImageWriterHandler::SendCancelled() {
23   Send(new ChromeUtilityHostMsg_ImageWriter_Cancelled());
24   content::UtilityThread::Get()->ReleaseProcessIfNeeded();
25 }
26 
SendFailed(const std::string & message)27 void ImageWriterHandler::SendFailed(const std::string& message) {
28   Send(new ChromeUtilityHostMsg_ImageWriter_Failed(message));
29   content::UtilityThread::Get()->ReleaseProcessIfNeeded();
30 }
31 
SendProgress(int64 progress)32 void ImageWriterHandler::SendProgress(int64 progress) {
33   Send(new ChromeUtilityHostMsg_ImageWriter_Progress(progress));
34 }
35 
Send(IPC::Message * msg)36 void ImageWriterHandler::Send(IPC::Message* msg) {
37   content::UtilityThread::Get()->Send(msg);
38 }
39 
OnMessageReceived(const IPC::Message & message)40 bool ImageWriterHandler::OnMessageReceived(const IPC::Message& message) {
41   bool handled = true;
42   IPC_BEGIN_MESSAGE_MAP(ImageWriterHandler, message)
43   IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Write, OnWriteStart)
44   IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Verify, OnVerifyStart)
45   IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Cancel, OnCancel)
46   IPC_MESSAGE_UNHANDLED(handled = false)
47   IPC_END_MESSAGE_MAP()
48   return handled;
49 }
50 
OnWriteStart(const base::FilePath & image,const base::FilePath & device)51 void ImageWriterHandler::OnWriteStart(const base::FilePath& image,
52                                       const base::FilePath& device) {
53   if (!image_writer_.get() || image != image_writer_->GetImagePath() ||
54       device != image_writer_->GetDevicePath()) {
55     image_writer_.reset(new ImageWriter(this, image, device));
56   }
57 
58   if (image_writer_->IsRunning()) {
59     SendFailed(error::kOperationAlreadyInProgress);
60     return;
61   }
62 
63   if (!image_writer_->IsValidDevice()) {
64     SendFailed(error::kInvalidDevice);
65     return;
66   }
67 
68   image_writer_->UnmountVolumes(
69       base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr()));
70 }
71 
OnVerifyStart(const base::FilePath & image,const base::FilePath & device)72 void ImageWriterHandler::OnVerifyStart(const base::FilePath& image,
73                                        const base::FilePath& device) {
74   if (!image_writer_.get() || image != image_writer_->GetImagePath() ||
75       device != image_writer_->GetDevicePath()) {
76     image_writer_.reset(new ImageWriter(this, image, device));
77   }
78 
79   if (image_writer_->IsRunning()) {
80     SendFailed(error::kOperationAlreadyInProgress);
81     return;
82   }
83 
84   if (!image_writer_->IsValidDevice()) {
85     SendFailed(error::kInvalidDevice);
86     return;
87   }
88 
89   image_writer_->Verify();
90 }
91 
OnCancel()92 void ImageWriterHandler::OnCancel() {
93   if (image_writer_.get()) {
94     image_writer_->Cancel();
95   } else {
96     SendCancelled();
97   }
98 }
99 
100 }  // namespace image_writer
101