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.
5 #ifndef CHROME_UTILITY_IMAGE_WRITER_DISK_UNMOUNTER_MAC_H_
6 #define CHROME_UTILITY_IMAGE_WRITER_DISK_UNMOUNTER_MAC_H_
8 #include <CoreFoundation/CoreFoundation.h>
9 #include <DiskArbitration/DiskArbitration.h>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/mac/foundation_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread.h"
17 namespace image_writer
{
21 // Manages the unmounting of disks through Disk Arbitration. Disk Arbitration
22 // has to be run on a thread with a CFRunLoop. In the utility process neither
23 // the main or IO thread have one by default, so we need to manage a new thread
24 // which will explicitly have a CFRunLoop-based message pump. Note that this
25 // class can only handle one unmount operation at a time and calling Unmount
26 // again before the continuation returns will cause undefined behavior.
27 class DiskUnmounterMac
{
32 // Claims and unmounts the device described by |device_path| and then calls
33 // the |continuation| when complete. This can be called from any thread.
34 // The continuation will be run on the thread this object was created on.
35 void Unmount(const std::string
& device_path
,
36 const base::Closure
& success_continuation
,
37 const base::Closure
& failure_continuation
);
40 // Handles disk-claimed callbacks.
41 static void DiskClaimed(DADiskRef disk
,
42 DADissenterRef dissenter
,
44 // Handles when we fail to claim a disk.
45 static DADissenterRef
DiskClaimRevoked(DADiskRef disk
, void* context
);
46 // Handles the disk-unmounted callback.
47 static void DiskUnmounted(DADiskRef disk
,
48 DADissenterRef dissenter
,
51 // A |MessagePumpFactory| for creating the thread.
52 static scoped_ptr
<base::MessagePump
> CreateMessagePump();
54 // Starts the unmount process. Should be posted to the |cf_thread_|.
55 void UnmountOnWorker(const std::string
& device_path
);
57 // A convenience method that triggers the failure continuation.
60 scoped_refptr
<base::MessageLoopProxy
> original_thread_
;
61 base::Closure success_continuation_
;
62 base::Closure failure_continuation_
;
64 base::ScopedCFTypeRef
<DADiskRef
> disk_
;
65 base::ScopedCFTypeRef
<DASessionRef
> session_
;
67 // Thread is last to ensure it is stopped before the data members are
69 base::Thread cf_thread_
;
72 } // namespace image_writer
74 #endif // CHROME_UTILITY_IMAGE_WRITER_DISK_UNMOUNTER_MAC_H_