1 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #import <Cocoa/Cocoa.h>
9 #include "nsMacFinderProgress.h"
10 #include "nsProxyRelease.h"
11 #include "nsThreadUtils.h"
13 #include "nsObjCExceptions.h"
15 NS_IMPL_ISUPPORTS(nsMacFinderProgress, nsIMacFinderProgress)
17 nsMacFinderProgress::nsMacFinderProgress() : mProgress(nil) {}
19 nsMacFinderProgress::~nsMacFinderProgress() {
21 [mProgress unpublish];
27 nsMacFinderProgress::Init(const nsAString& path,
28 nsIMacFinderProgressCanceledCallback* cancellationCallback) {
29 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
31 NSURL* pathUrl = [NSURL
32 fileURLWithPath:[NSString
33 stringWithCharacters:reinterpret_cast<const unichar*>(path.BeginReading())
34 length:path.Length()]];
35 NSDictionary* userInfo = @{
36 @"NSProgressFileOperationKindKey" : @"NSProgressFileOperationKindDownloading",
37 @"NSProgressFileURLKey" : pathUrl
40 mProgress = [[NSProgress alloc] initWithParent:nil userInfo:userInfo];
41 mProgress.kind = NSProgressKindFile;
42 mProgress.cancellable = YES;
44 nsMainThreadPtrHandle<nsIMacFinderProgressCanceledCallback> cancellationCallbackHandle(
45 new nsMainThreadPtrHolder<nsIMacFinderProgressCanceledCallback>(
46 "MacFinderProgress::CancellationCallback", cancellationCallback));
48 mProgress.cancellationHandler = ^{
49 NS_DispatchToMainThread(
50 NS_NewRunnableFunction("MacFinderProgress::Canceled", [cancellationCallbackHandle] {
51 MOZ_ASSERT(NS_IsMainThread());
52 cancellationCallbackHandle->Canceled();
60 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
64 nsMacFinderProgress::UpdateProgress(uint64_t currentProgress, uint64_t totalProgress) {
65 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
67 mProgress.totalUnitCount = totalProgress;
68 mProgress.completedUnitCount = currentProgress;
73 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
77 nsMacFinderProgress::End() {
78 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
81 [mProgress unpublish];
86 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);