blob: 7d641eb6113efffceba34dce2d2ab2481b57eb76 [file] [log] [blame]
[email protected]3d78cbe2014-02-27 13:19:301// 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
[email protected]4e100e82014-06-18 23:47:295#include "chrome/utility/image_writer/image_writer_handler.h"
6
[email protected]3d78cbe2014-02-27 13:19:307#include "base/files/file_path.h"
8#include "chrome/common/chrome_utility_messages.h"
9#include "chrome/utility/image_writer/error_messages.h"
[email protected]3d78cbe2014-02-27 13:19:3010#include "content/public/utility/utility_thread.h"
11
[email protected]3d78cbe2014-02-27 13:19:3012namespace image_writer {
13
[email protected]01fe23ce2014-05-07 13:18:5514ImageWriterHandler::ImageWriterHandler() {}
[email protected]3d78cbe2014-02-27 13:19:3015ImageWriterHandler::~ImageWriterHandler() {}
16
17void ImageWriterHandler::SendSucceeded() {
18 Send(new ChromeUtilityHostMsg_ImageWriter_Succeeded());
19 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
20}
21
22void ImageWriterHandler::SendCancelled() {
23 Send(new ChromeUtilityHostMsg_ImageWriter_Cancelled());
24 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
25}
26
27void ImageWriterHandler::SendFailed(const std::string& message) {
28 Send(new ChromeUtilityHostMsg_ImageWriter_Failed(message));
29 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
30}
31
32void ImageWriterHandler::SendProgress(int64 progress) {
33 Send(new ChromeUtilityHostMsg_ImageWriter_Progress(progress));
34}
35
36void ImageWriterHandler::Send(IPC::Message* msg) {
37 content::UtilityThread::Get()->Send(msg);
38}
39
40bool 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
51void ImageWriterHandler::OnWriteStart(const base::FilePath& image,
52 const base::FilePath& device) {
[email protected]01fe23ce2014-05-07 13:18:5553 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);
[email protected]3d78cbe2014-02-27 13:19:3060 return;
61 }
62
[email protected]01fe23ce2014-05-07 13:18:5563 if (!image_writer_->IsValidDevice()) {
64 SendFailed(error::kInvalidDevice);
[email protected]3d78cbe2014-02-27 13:19:3065 return;
66 }
[email protected]01fe23ce2014-05-07 13:18:5567
[email protected]4e100e82014-06-18 23:47:2968 image_writer_->UnmountVolumes(
69 base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr()));
[email protected]3d78cbe2014-02-27 13:19:3070}
71
72void ImageWriterHandler::OnVerifyStart(const base::FilePath& image,
73 const base::FilePath& device) {
[email protected]01fe23ce2014-05-07 13:18:5574 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);
[email protected]3d78cbe2014-02-27 13:19:3081 return;
82 }
83
[email protected]01fe23ce2014-05-07 13:18:5584 if (!image_writer_->IsValidDevice()) {
85 SendFailed(error::kInvalidDevice);
[email protected]3d78cbe2014-02-27 13:19:3086 return;
87 }
[email protected]01fe23ce2014-05-07 13:18:5588
89 image_writer_->Verify();
[email protected]3d78cbe2014-02-27 13:19:3090}
91
92void ImageWriterHandler::OnCancel() {
[email protected]01fe23ce2014-05-07 13:18:5593 if (image_writer_.get()) {
94 image_writer_->Cancel();
[email protected]3d78cbe2014-02-27 13:19:3095 } else {
96 SendCancelled();
97 }
98}
99
[email protected]3d78cbe2014-02-27 13:19:30100} // namespace image_writer