OLD | NEW |
(Empty) | |
| 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/disk_unmounter_mac.h" |
| 6 |
| 7 #include <sys/socket.h> |
| 8 #include <IOKit/storage/IOStorageProtocolCharacteristics.h> |
| 9 |
| 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/message_loop/message_pump_mac.h" |
| 12 #include "base/posix/eintr_wrapper.h" |
| 13 #include "chrome/utility/image_writer/error_messages.h" |
| 14 #include "chrome/utility/image_writer/image_writer.h" |
| 15 |
| 16 namespace image_writer { |
| 17 |
| 18 DiskUnmounterMac::DiskUnmounterMac(base::WeakPtr<ImageWriter> image_writer) |
| 19 : image_writer_(image_writer), |
| 20 original_thread_(base::MessageLoopProxy::current()), |
| 21 cf_thread_("ImageWriterDiskArb") { |
| 22 } |
| 23 |
| 24 DiskUnmounterMac::~DiskUnmounterMac() { |
| 25 if (disk_) |
| 26 DADiskUnclaim(disk_); |
| 27 } |
| 28 |
| 29 void DiskUnmounterMac::Unmount(const std::string& device_path, |
| 30 const base::Closure& continuation) { |
| 31 unmount_continuation_closure_ = continuation; |
| 32 |
| 33 base::Thread::Options options; |
| 34 options.message_pump_factory = base::Bind(&CreateMessagePump); |
| 35 |
| 36 cf_thread_.StartWithOptions(options); |
| 37 |
| 38 cf_thread_.message_loop()->PostTask( |
| 39 FROM_HERE, |
| 40 base::Bind(&DiskUnmounterMac::UnmountOnWorker, |
| 41 base::Unretained(this), |
| 42 device_path)); |
| 43 } |
| 44 |
| 45 // static |
| 46 void DiskUnmounterMac::DiskClaimed(DADiskRef disk, |
| 47 DADissenterRef dissenter, |
| 48 void* context) { |
| 49 DiskUnmounterMac* disk_unmounter = static_cast<DiskUnmounterMac*>(context); |
| 50 |
| 51 if (dissenter) { |
| 52 LOG(ERROR) << "Unable to claim disk."; |
| 53 disk_unmounter->Error(error::kUnmountVolumes); |
| 54 return; |
| 55 } |
| 56 |
| 57 DADiskUnmount(disk, |
| 58 kDADiskUnmountOptionForce | kDADiskUnmountOptionWhole, |
| 59 DiskUnmounted, |
| 60 disk_unmounter); |
| 61 } |
| 62 |
| 63 // static |
| 64 DADissenterRef DiskUnmounterMac::DiskClaimRevoked(DADiskRef disk, |
| 65 void* context) { |
| 66 CFStringRef reason = CFSTR( |
| 67 "Hi. Sorry to bother you, but I'm busy overwriting the entire disk " |
| 68 "here. There's nothing to claim but the smoldering ruins of bytes " |
| 69 "that were in flash memory. Trust me, it's nothing that you want. " |
| 70 "All the best. Toodles!"); |
| 71 return DADissenterCreate(kCFAllocatorDefault, kDAReturnBusy, reason); |
| 72 } |
| 73 |
| 74 // static |
| 75 void DiskUnmounterMac::DiskUnmounted(DADiskRef disk, |
| 76 DADissenterRef dissenter, |
| 77 void* context) { |
| 78 DiskUnmounterMac* disk_unmounter = static_cast<DiskUnmounterMac*>(context); |
| 79 |
| 80 if (dissenter) { |
| 81 LOG(ERROR) << "Unable to unmount disk."; |
| 82 disk_unmounter->Error(error::kUnmountVolumes); |
| 83 return; |
| 84 } |
| 85 |
| 86 disk_unmounter->original_thread_->PostTask( |
| 87 FROM_HERE, disk_unmounter->unmount_continuation_closure_); |
| 88 } |
| 89 |
| 90 void DiskUnmounterMac::UnmountOnWorker(const std::string& device_path) { |
| 91 DCHECK(cf_thread_.message_loop() == base::MessageLoop::current()); |
| 92 |
| 93 session_.reset(DASessionCreate(NULL)); |
| 94 |
| 95 DASessionScheduleWithRunLoop( |
| 96 session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); |
| 97 |
| 98 disk_.reset(DADiskCreateFromBSDName( |
| 99 kCFAllocatorDefault, session_, device_path.c_str())); |
| 100 |
| 101 if (!disk_) { |
| 102 LOG(ERROR) << "Unable to get disk reference."; |
| 103 Error(error::kUnmountVolumes); |
| 104 return; |
| 105 } |
| 106 |
| 107 DADiskClaim(disk_, |
| 108 kDADiskClaimOptionDefault, |
| 109 DiskClaimRevoked, |
| 110 this, |
| 111 DiskClaimed, |
| 112 this); |
| 113 } |
| 114 |
| 115 // static |
| 116 scoped_ptr<base::MessagePump> DiskUnmounterMac::CreateMessagePump() { |
| 117 return scoped_ptr<base::MessagePump>(new base::MessagePumpCFRunLoop); |
| 118 } |
| 119 |
| 120 void DiskUnmounterMac::Error(const std::string& message) { |
| 121 original_thread_->PostTask( |
| 122 FROM_HERE, base::Bind(&ImageWriter::Error, image_writer_, message)); |
| 123 } |
| 124 |
| 125 } // namespace image_writer |
OLD | NEW |