Backed out changeset b4a0f8afc02e (bug 1857946) for causing bc failures at browser...
[gecko.git] / widget / cocoa / nsMacFinderProgress.mm
blobe2b0401313e8eedb65c710793edaff23215d49df
1 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset:
2  * 2 -*- */
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"
13 #include "nsString.h"
14 #include "nsObjCExceptions.h"
16 NS_IMPL_ISUPPORTS(nsMacFinderProgress, nsIMacFinderProgress)
18 nsMacFinderProgress::nsMacFinderProgress() : mProgress(nil) {}
20 nsMacFinderProgress::~nsMacFinderProgress() {
21   if (mProgress) {
22     [mProgress unpublish];
23     [mProgress release];
24   }
27 NS_IMETHODIMP
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*>(
36                                                    path.BeginReading())
37                                         length:path.Length()]];
38   NSDictionary* userInfo = @{
39     @"NSProgressFileOperationKindKey" :
40         @"NSProgressFileOperationKindDownloading",
41     @"NSProgressFileURLKey" : pathUrl
42   };
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();
58         }));
59   };
61   [mProgress publish];
63   return NS_OK;
65   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
68 NS_IMETHODIMP
69 nsMacFinderProgress::UpdateProgress(uint64_t currentProgress,
70                                     uint64_t totalProgress) {
71   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
72   if (mProgress) {
73     mProgress.totalUnitCount = totalProgress;
74     mProgress.completedUnitCount = currentProgress;
75   }
77   return NS_OK;
79   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
82 NS_IMETHODIMP
83 nsMacFinderProgress::End() {
84   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
86   if (mProgress) {
87     [mProgress unpublish];
88   }
90   return NS_OK;
92   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);