Bug 1755924 [wpt PR 32876] - Handle resumed blocks that get sliced by floats correctl...
[gecko.git] / widget / cocoa / nsMacFinderProgress.mm
blob2e518fe0122abe774ec9548ae8ae4d0df5e32d13
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"
12 #include "nsString.h"
13 #include "nsObjCExceptions.h"
15 NS_IMPL_ISUPPORTS(nsMacFinderProgress, nsIMacFinderProgress)
17 nsMacFinderProgress::nsMacFinderProgress() : mProgress(nil) {}
19 nsMacFinderProgress::~nsMacFinderProgress() {
20   if (mProgress) {
21     [mProgress unpublish];
22     [mProgress release];
23   }
26 NS_IMETHODIMP
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
38   };
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();
53         }));
54   };
56   [mProgress publish];
58   return NS_OK;
60   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
63 NS_IMETHODIMP
64 nsMacFinderProgress::UpdateProgress(uint64_t currentProgress, uint64_t totalProgress) {
65   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
66   if (mProgress) {
67     mProgress.totalUnitCount = totalProgress;
68     mProgress.completedUnitCount = currentProgress;
69   }
71   return NS_OK;
73   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
76 NS_IMETHODIMP
77 nsMacFinderProgress::End() {
78   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
80   if (mProgress) {
81     [mProgress unpublish];
82   }
84   return NS_OK;
86   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);