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/single_thread_task_runner.h"
16 #include "base/threading/thread.h"
18 namespace image_writer
{
22 // Manages the unmounting of disks through Disk Arbitration. Disk Arbitration
23 // has to be run on a thread with a CFRunLoop. In the utility process neither
24 // the main or IO thread have one by default, so we need to manage a new thread
25 // which will explicitly have a CFRunLoop-based message pump. Note that this
26 // class can only handle one unmount operation at a time and calling Unmount
27 // again before the continuation returns will cause undefined behavior.
28 class DiskUnmounterMac
{
33 // Claims and unmounts the device described by |device_path| and then calls
34 // the |continuation| when complete. This can be called from any thread.
35 // The continuation will be run on the thread this object was created on.
36 void Unmount(const std::string
& device_path
,
37 const base::Closure
& success_continuation
,
38 const base::Closure
& failure_continuation
);
41 // Handles disk-claimed callbacks.
42 static void DiskClaimed(DADiskRef disk
,
43 DADissenterRef dissenter
,
45 // Handles when we fail to claim a disk.
46 static DADissenterRef
DiskClaimRevoked(DADiskRef disk
, void* context
);
47 // Handles the disk-unmounted callback.
48 static void DiskUnmounted(DADiskRef disk
,
49 DADissenterRef dissenter
,
52 // A |MessagePumpFactory| for creating the thread.
53 static scoped_ptr
<base::MessagePump
> CreateMessagePump();
55 // Starts the unmount process. Should be posted to the |cf_thread_|.
56 void UnmountOnWorker(const std::string
& device_path
);
58 // A convenience method that triggers the failure continuation.
61 scoped_refptr
<base::SingleThreadTaskRunner
> original_thread_
;
62 base::Closure success_continuation_
;
63 base::Closure failure_continuation_
;
65 base::ScopedCFTypeRef
<DADiskRef
> disk_
;
66 base::ScopedCFTypeRef
<DASessionRef
> session_
;
68 // Thread is last to ensure it is stopped before the data members are
70 base::Thread cf_thread_
;
73 } // namespace image_writer
75 #endif // CHROME_UTILITY_IMAGE_WRITER_DISK_UNMOUNTER_MAC_H_