summaryrefslogtreecommitdiffstats
path: root/widget/cocoa/nsMacDockSupport.mm
blob: e69c0b625985b1954ba8e127c7f46bcb69e079b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#import <Cocoa/Cocoa.h>
#include <CoreFoundation/CoreFoundation.h>
#include <signal.h>

#include "nsCocoaUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsMacDockSupport.h"
#include "nsObjCExceptions.h"
#include "nsNativeThemeColors.h"
#include "nsString.h"

NS_IMPL_ISUPPORTS(nsMacDockSupport, nsIMacDockSupport, nsITaskbarProgress)

// This view is used in the dock tile when we're downloading a file.
// It draws a progress bar that looks similar to the native progress bar on
// 10.12. This style of progress bar is not animated, unlike the pre-10.10
// progress bar look which had to redrawn multiple times per second.
@interface MOZProgressDockOverlayView : NSView {
  double mFractionValue;
}
@property double fractionValue;

@end

@implementation MOZProgressDockOverlayView

@synthesize fractionValue = mFractionValue;

- (void)drawRect:(NSRect)aRect {
  // Erase the background behind this view, i.e. cut a rectangle hole in the icon.
  [[NSColor clearColor] set];
  NSRectFill(self.bounds);

  // Split the height of this view into four quarters. The middle two quarters
  // will be covered by the actual progress bar.
  CGFloat radius = self.bounds.size.height / 4;
  NSRect barBounds = NSInsetRect(self.bounds, 0, radius);

  NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:barBounds
                                                       xRadius:radius
                                                       yRadius:radius];

  // Draw a grayish background first.
  [[NSColor colorWithDeviceWhite:0 alpha:0.1] setFill];
  [path fill];

  // Draw a fill in the control accent color for the progress part.
  NSRect progressFillRect = self.bounds;
  progressFillRect.size.width *= mFractionValue;
  [NSGraphicsContext saveGraphicsState];
  [NSBezierPath clipRect:progressFillRect];
  [ControlAccentColor() setFill];
  [path fill];
  [NSGraphicsContext restoreGraphicsState];

  // Add a shadowy stroke on top.
  [NSGraphicsContext saveGraphicsState];
  [path addClip];
  [[NSColor colorWithDeviceWhite:0 alpha:0.2] setStroke];
  path.lineWidth = barBounds.size.height / 10;
  [path stroke];
  [NSGraphicsContext restoreGraphicsState];
}

@end

nsMacDockSupport::nsMacDockSupport()
    : mDockTileWrapperView(nil),
      mProgressDockOverlayView(nil),
      mProgressState(STATE_NO_PROGRESS),
      mProgressFraction(0.0) {}

nsMacDockSupport::~nsMacDockSupport() {
  if (mDockTileWrapperView) {
    [mDockTileWrapperView release];
    mDockTileWrapperView = nil;
  }
  if (mProgressDockOverlayView) {
    [mProgressDockOverlayView release];
    mProgressDockOverlayView = nil;
  }
}

NS_IMETHODIMP
nsMacDockSupport::GetDockMenu(nsIStandaloneNativeMenu** aDockMenu) {
  nsCOMPtr<nsIStandaloneNativeMenu> dockMenu(mDockMenu);
  dockMenu.forget(aDockMenu);
  return NS_OK;
}

NS_IMETHODIMP
nsMacDockSupport::SetDockMenu(nsIStandaloneNativeMenu* aDockMenu) {
  mDockMenu = aDockMenu;
  return NS_OK;
}

NS_IMETHODIMP
nsMacDockSupport::ActivateApplication(bool aIgnoreOtherApplications) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  [[NSApplication sharedApplication] activateIgnoringOtherApps:aIgnoreOtherApplications];
  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

NS_IMETHODIMP
nsMacDockSupport::SetBadgeText(const nsAString& aBadgeText) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  NSDockTile* tile = [[NSApplication sharedApplication] dockTile];
  mBadgeText = aBadgeText;
  if (aBadgeText.IsEmpty())
    [tile setBadgeLabel:nil];
  else
    [tile setBadgeLabel:[NSString
                            stringWithCharacters:reinterpret_cast<const unichar*>(mBadgeText.get())
                                          length:mBadgeText.Length()]];
  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

NS_IMETHODIMP
nsMacDockSupport::GetBadgeText(nsAString& aBadgeText) {
  aBadgeText = mBadgeText;
  return NS_OK;
}

NS_IMETHODIMP
nsMacDockSupport::SetProgressState(nsTaskbarProgressState aState, uint64_t aCurrentValue,
                                   uint64_t aMaxValue) {
  NS_ENSURE_ARG_RANGE(aState, 0, STATE_PAUSED);
  if (aState == STATE_NO_PROGRESS || aState == STATE_INDETERMINATE) {
    NS_ENSURE_TRUE(aCurrentValue == 0, NS_ERROR_INVALID_ARG);
    NS_ENSURE_TRUE(aMaxValue == 0, NS_ERROR_INVALID_ARG);
  }
  if (aCurrentValue > aMaxValue) {
    return NS_ERROR_ILLEGAL_VALUE;
  }

  mProgressState = aState;
  if (aMaxValue == 0) {
    mProgressFraction = 0;
  } else {
    mProgressFraction = (double)aCurrentValue / aMaxValue;
  }

  return UpdateDockTile();
}

nsresult nsMacDockSupport::UpdateDockTile() {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  if (mProgressState == STATE_NORMAL || mProgressState == STATE_INDETERMINATE) {
    if (!mDockTileWrapperView) {
      // Create the following NSView hierarchy:
      // * mDockTileWrapperView (NSView)
      //    * imageView (NSImageView) <- has the application icon
      //    * mProgressDockOverlayView (MOZProgressDockOverlayView) <- draws the progress bar

      mDockTileWrapperView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 32, 32)];
      mDockTileWrapperView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

      NSImageView* imageView = [[NSImageView alloc] initWithFrame:[mDockTileWrapperView bounds]];
      imageView.image = [NSImage imageNamed:@"NSApplicationIcon"];
      imageView.imageScaling = NSImageScaleAxesIndependently;
      imageView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
      [mDockTileWrapperView addSubview:imageView];

      mProgressDockOverlayView =
          [[MOZProgressDockOverlayView alloc] initWithFrame:NSMakeRect(1, 3, 30, 4)];
      mProgressDockOverlayView.autoresizingMask = NSViewMinXMargin | NSViewWidthSizable |
                                                  NSViewMaxXMargin | NSViewMinYMargin |
                                                  NSViewHeightSizable | NSViewMaxYMargin;
      [mDockTileWrapperView addSubview:mProgressDockOverlayView];
    }
    if (NSApp.dockTile.contentView != mDockTileWrapperView) {
      NSApp.dockTile.contentView = mDockTileWrapperView;
    }

    if (mProgressState == STATE_NORMAL) {
      mProgressDockOverlayView.fractionValue = mProgressFraction;
    } else {
      // Indeterminate states are rare. Just fill the entire progress bar in
      // that case.
      mProgressDockOverlayView.fractionValue = 1.0;
    }
    [NSApp.dockTile display];
  } else if (NSApp.dockTile.contentView) {
    NSApp.dockTile.contentView = nil;
    [NSApp.dockTile display];
  }

  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

extern "C" {
// Private CFURL API used by the Dock.
CFPropertyListRef _CFURLCopyPropertyListRepresentation(CFURLRef url);
CFURLRef _CFURLCreateFromPropertyListRepresentation(CFAllocatorRef alloc,
                                                    CFPropertyListRef pListRepresentation);
}  // extern "C"

namespace {

const NSArray* const browserAppNames =
    [NSArray arrayWithObjects:@"Firefox.app", @"Firefox Beta.app", @"Firefox Nightly.app",
                              @"Safari.app", @"WebKit.app", @"Google Chrome.app",
                              @"Google Chrome Canary.app", @"Chromium.app", @"Opera.app", nil];

constexpr NSString* const kDockDomainName = @"com.apple.dock";
// See https://developer.apple.com/documentation/devicemanagement/dock
constexpr NSString* const kDockPersistentAppsKey = @"persistent-apps";
// See https://developer.apple.com/documentation/devicemanagement/dock/staticitem
constexpr NSString* const kDockTileDataKey = @"tile-data";
constexpr NSString* const kDockFileDataKey = @"file-data";

NSArray* GetPersistentAppsFromDockPlist(NSDictionary* aDockPlist) {
  if (!aDockPlist) {
    return nil;
  }
  NSArray* persistentApps = [aDockPlist objectForKey:kDockPersistentAppsKey];
  if (![persistentApps isKindOfClass:[NSArray class]]) {
    return nil;
  }
  return persistentApps;
}

NSString* GetPathForApp(NSDictionary* aPersistantApp) {
  if (![aPersistantApp isKindOfClass:[NSDictionary class]]) {
    return nil;
  }
  NSDictionary* tileData = aPersistantApp[kDockTileDataKey];
  if (![tileData isKindOfClass:[NSDictionary class]]) {
    return nil;
  }
  NSDictionary* fileData = tileData[kDockFileDataKey];
  if (![fileData isKindOfClass:[NSDictionary class]]) {
    // Some special tiles may not have DockFileData but we can ignore those.
    return nil;
  }
  NSURL* url = CFBridgingRelease(_CFURLCreateFromPropertyListRepresentation(NULL, fileData));
  if (!url) {
    return nil;
  }
  return [url isFileURL] ? [url path] : nullptr;
}

// The only reliable way to get our changes to take effect seems to be to use
// `kill`.
void RefreshDock(NSDictionary* aDockPlist) {
  [[NSUserDefaults standardUserDefaults] setPersistentDomain:aDockPlist forName:kDockDomainName];
  NSRunningApplication* dockApp = [[NSRunningApplication
      runningApplicationsWithBundleIdentifier:@"com.apple.dock"] firstObject];
  if (!dockApp) {
    return;
  }
  pid_t pid = [dockApp processIdentifier];
  if (pid > 0) {
    kill(pid, SIGTERM);
  }
}

}  // namespace

nsresult nsMacDockSupport::GetIsAppInDock(bool* aIsInDock) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  *aIsInDock = false;

  NSDictionary* dockPlist =
      [[NSUserDefaults standardUserDefaults] persistentDomainForName:kDockDomainName];
  if (!dockPlist) {
    return NS_ERROR_FAILURE;
  }

  NSArray* persistentApps = GetPersistentAppsFromDockPlist(dockPlist);
  if (!persistentApps) {
    return NS_ERROR_FAILURE;
  }

  NSString* appPath = [[NSBundle mainBundle] bundlePath];

  for (id app in persistentApps) {
    NSString* persistentAppPath = GetPathForApp(app);
    if (persistentAppPath && [appPath isEqual:persistentAppPath]) {
      *aIsInDock = true;
      break;
    }
  }

  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}

nsresult nsMacDockSupport::EnsureAppIsPinnedToDock(const nsAString& aAppPath,
                                                   const nsAString& aAppToReplacePath,
                                                   bool* aIsInDock) {
  NS_OBJC_BEGIN_TRY_BLOCK_RETURN;

  MOZ_ASSERT(aAppPath != aAppToReplacePath || !aAppPath.IsEmpty());

  *aIsInDock = false;

  NSString* appPath =
      !aAppPath.IsEmpty() ? nsCocoaUtils::ToNSString(aAppPath) : [[NSBundle mainBundle] bundlePath];
  NSString* appToReplacePath = nsCocoaUtils::ToNSString(aAppToReplacePath);

  NSMutableDictionary* dockPlist =
      [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults]
                                                        persistentDomainForName:kDockDomainName]];
  if (!dockPlist) {
    return NS_ERROR_FAILURE;
  }

  NSMutableArray* persistentApps =
      [NSMutableArray arrayWithArray:GetPersistentAppsFromDockPlist(dockPlist)];
  if (!persistentApps) {
    return NS_ERROR_FAILURE;
  }

  // See the comment for this method in the .idl file for the strategy that we
  // use here to determine where to pin the app.
  NSUInteger preexistingAppIndex = NSNotFound;  // full path matches
  NSUInteger sameNameAppIndex = NSNotFound;     // app name matches only
  NSUInteger toReplaceAppIndex = NSNotFound;
  NSUInteger lastBrowserAppIndex = NSNotFound;
  for (NSUInteger index = 0; index < [persistentApps count]; ++index) {
    NSString* persistentAppPath = GetPathForApp([persistentApps objectAtIndex:index]);

    if ([persistentAppPath isEqualToString:appPath]) {
      preexistingAppIndex = index;
    } else if (appToReplacePath && [persistentAppPath isEqualToString:appToReplacePath]) {
      toReplaceAppIndex = index;
    } else {
      NSString* appName = [appPath lastPathComponent];
      NSString* persistentAppName = [persistentAppPath lastPathComponent];

      if ([persistentAppName isEqual:appName]) {
        if ([appToReplacePath hasPrefix:@"/private/var/folders/"] &&
            [appToReplacePath containsString:@"/AppTranslocation/"] &&
            [persistentAppPath hasPrefix:@"/Volumes/"]) {
          // This is a special case when an app with the same name was
          // previously dragged and pinned from a quarantined DMG straight to
          // the Dock and an attempt is now made to pin the same named app to
          // the Dock. In this case we want to replace the currently pinned app
          // icon.
          toReplaceAppIndex = index;
        } else {
          sameNameAppIndex = index;
        }
      } else {
        if ([browserAppNames containsObject:persistentAppName]) {
          lastBrowserAppIndex = index;
        }
      }
    }
  }

  // Special cases where we're not going to add a new Dock tile:
  if (preexistingAppIndex != NSNotFound) {
    if (toReplaceAppIndex != NSNotFound) {
      [persistentApps removeObjectAtIndex:toReplaceAppIndex];
      [dockPlist setObject:persistentApps forKey:kDockPersistentAppsKey];
      RefreshDock(dockPlist);
    }
    *aIsInDock = true;
    return NS_OK;
  }

  // Create new tile:
  NSDictionary* newDockTile = nullptr;
  {
    NSURL* appUrl = [NSURL fileURLWithPath:appPath isDirectory:YES];
    NSDictionary* dict =
        CFBridgingRelease(_CFURLCopyPropertyListRepresentation((__bridge CFURLRef)appUrl));
    if (!dict) {
      return NS_ERROR_FAILURE;
    }
    NSDictionary* dockTileData = [NSDictionary dictionaryWithObject:dict forKey:kDockFileDataKey];
    if (dockTileData) {
      newDockTile = [NSDictionary dictionaryWithObject:dockTileData forKey:kDockTileDataKey];
    }
    if (!newDockTile) {
      return NS_ERROR_FAILURE;
    }
  }

  // Update the Dock:
  if (toReplaceAppIndex != NSNotFound) {
    [persistentApps replaceObjectAtIndex:toReplaceAppIndex withObject:newDockTile];
  } else {
    NSUInteger index;
    if (sameNameAppIndex != NSNotFound) {
      index = sameNameAppIndex + 1;
    } else if (lastBrowserAppIndex != NSNotFound) {
      index = lastBrowserAppIndex + 1;
    } else {
      index = [persistentApps count];
    }
    [persistentApps insertObject:newDockTile atIndex:index];
  }
  [dockPlist setObject:persistentApps forKey:kDockPersistentAppsKey];
  RefreshDock(dockPlist);

  *aIsInDock = true;
  return NS_OK;

  NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}