1 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset:
3 /* vim: set ts=2 et sw=2 tw=80: */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #import <Cocoa/Cocoa.h>
10 #include "nsMacFinderProgress.h"
11 #include "nsProxyRelease.h"
12 #include "nsThreadUtils.h"
14 #include "nsObjCExceptions.h"
16 NS_IMPL_ISUPPORTS(nsMacFinderProgress, nsIMacFinderProgress)
18 nsMacFinderProgress::nsMacFinderProgress() : mProgress(nil) {}
20 nsMacFinderProgress::~nsMacFinderProgress() {
22 [mProgress unpublish];
28 nsMacFinderProgress::Init(
29 const nsAString& path,
30 nsIMacFinderProgressCanceledCallback* cancellationCallback) {
31 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
33 NSURL* pathUrl = [NSURL
34 fileURLWithPath:[NSString
35 stringWithCharacters:reinterpret_cast<const unichar*>(
37 length:path.Length()]];
38 NSDictionary* userInfo = @{
39 @"NSProgressFileOperationKindKey" :
40 @"NSProgressFileOperationKindDownloading",
41 @"NSProgressFileURLKey" : pathUrl
44 mProgress = [[NSProgress alloc] initWithParent:nil userInfo:userInfo];
45 mProgress.kind = NSProgressKindFile;
46 mProgress.cancellable = YES;
48 nsMainThreadPtrHandle<nsIMacFinderProgressCanceledCallback>
49 cancellationCallbackHandle(
50 new nsMainThreadPtrHolder<nsIMacFinderProgressCanceledCallback>(
51 "MacFinderProgress::CancellationCallback", cancellationCallback));
53 mProgress.cancellationHandler = ^{
54 NS_DispatchToMainThread(NS_NewRunnableFunction(
55 "MacFinderProgress::Canceled", [cancellationCallbackHandle] {
56 MOZ_ASSERT(NS_IsMainThread());
57 cancellationCallbackHandle->Canceled();
65 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
69 nsMacFinderProgress::UpdateProgress(uint64_t currentProgress,
70 uint64_t totalProgress) {
71 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
73 mProgress.totalUnitCount = totalProgress;
74 mProgress.completedUnitCount = currentProgress;
79 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
83 nsMacFinderProgress::End() {
84 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
87 [mProgress unpublish];
92 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);