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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/mac/dock.h"
#include <ApplicationServices/ApplicationServices.h>
#include <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#include <signal.h>
#include <tuple>
#include "base/apple/bridging.h"
#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/logging.h"
#include "base/mac/launchd.h"
#include "build/branding_buildflags.h"
extern "C" {
// Undocumented private internal CFURL functions. The Dock uses these to
// serialize and deserialize CFURLs for use in its plist's file-data keys. See
// 10.5.8 CF-476.19 and 10.7.2 CF-635.15's CFPriv.h and CFURL.c. The property
// list representation will contain, at the very least, the _CFURLStringType
// and _CFURLString keys. _CFURLStringType is a number that defines the
// interpretation of the _CFURLString. It may be a CFURLPathStyle value, or
// the CFURL-internal FULL_URL_REPRESENTATION value (15). Prior to Mac OS X
// 10.7.2, the Dock plist always used kCFURLPOSIXPathStyle (0), formatting
// _CFURLString as a POSIX path. In Mac OS X 10.7.2 (CF-635.15), it uses
// FULL_URL_REPRESENTATION along with a file:/// URL. This is due to a change
// in _CFURLInit.
CFPropertyListRef _CFURLCopyPropertyListRepresentation(CFURLRef url);
CFURLRef _CFURLCreateFromPropertyListRepresentation(
CFAllocatorRef allocator, CFPropertyListRef property_list_representation);
} // extern "C"
namespace dock {
namespace {
NSString* const kDockTileDataKey = @"tile-data";
NSString* const kDockFileDataKey = @"file-data";
NSString* const kDockDomain = @"com.apple.dock";
NSString* const kDockPersistentAppsKey = @"persistent-apps";
// A wrapper around _CFURLCopyPropertyListRepresentation that operates on
// Foundation data types and returns an autoreleased NSDictionary.
NSDictionary* DockFileDataDictionaryForURL(NSURL* url) {
base::apple::ScopedCFTypeRef<CFPropertyListRef> property_list(
_CFURLCopyPropertyListRepresentation(base::apple::NSToCFPtrCast(url)));
CFDictionaryRef dictionary =
base::apple::CFCast<CFDictionaryRef>(property_list.get());
if (!dictionary)
return nil;
return base::apple::CFToNSOwnershipCast(
(CFDictionaryRef)property_list.release());
}
// A wrapper around _CFURLCreateFromPropertyListRepresentation that operates
// on Foundation data types and returns an autoreleased NSURL.
NSURL* URLFromDockFileDataDictionary(NSDictionary* dictionary) {
base::apple::ScopedCFTypeRef<CFURLRef> url(
_CFURLCreateFromPropertyListRepresentation(
kCFAllocatorDefault, base::apple::NSToCFPtrCast(dictionary)));
if (!url)
return nil;
return base::apple::CFToNSOwnershipCast(url.release());
}
// Returns an array parallel to |persistent_apps| containing only the
// pathnames of the Dock tiles contained therein. Returns nil on failure, such
// as when the structure of |persistent_apps| is not understood.
NSMutableArray* PersistentAppPaths(NSArray* persistent_apps) {
if (!persistent_apps) {
return nil;
}
NSMutableArray* app_paths =
[NSMutableArray arrayWithCapacity:[persistent_apps count]];
for (NSDictionary* app in persistent_apps) {
if (![app isKindOfClass:[NSDictionary class]]) {
LOG(ERROR) << "app not NSDictionary";
return nil;
}
NSDictionary* tile_data = app[kDockTileDataKey];
if (![tile_data isKindOfClass:[NSDictionary class]]) {
LOG(ERROR) << "tile_data not NSDictionary";
return nil;
}
NSDictionary* file_data = tile_data[kDockFileDataKey];
if (![file_data isKindOfClass:[NSDictionary class]]) {
// Some apps (e.g. Dashboard) have no file data, but instead have a
// special value for the tile-type key. For these, add an empty string to
// align indexes with the source array.
[app_paths addObject:@""];
continue;
}
NSURL* url = URLFromDockFileDataDictionary(file_data);
if (!url) {
LOG(ERROR) << "no URL";
return nil;
}
if (![url isFileURL]) {
LOG(ERROR) << "non-file URL";
return nil;
}
NSString* path = [url path];
[app_paths addObject:path];
}
return app_paths;
}
BOOL IsAppAtPathAWebBrowser(NSString* app_path) {
NSBundle* app_bundle = [NSBundle bundleWithPath:app_path];
if (!app_bundle)
return NO;
NSArray* activities = base::apple::ObjCCast<NSArray>(
[app_bundle objectForInfoDictionaryKey:@"NSUserActivityTypes"]);
if (!activities)
return NO;
return [activities containsObject:NSUserActivityTypeBrowsingWeb];
}
// Restart the Dock process by sending it a SIGTERM.
void Restart() {
// Doing this via launchd using the proper job label is the safest way to
// handle the restart. Unlike "killall Dock", looking this up via launchd
// guarantees that only the right process will be targeted.
pid_t pid = base::mac::PIDForJob("com.apple.Dock.agent");
if (pid <= 0) {
return;
}
// Sending a SIGTERM to the Dock seems to be a more reliable way to get the
// replacement Dock process to read the newly written plist than using the
// equivalent of "launchctl stop" (even if followed by "launchctl start.")
// Note that this is a potential race in that pid may no longer be valid or
// may even have been reused.
kill(pid, SIGTERM);
}
NSDictionary* DockPlistFromUserDefaults() {
NSDictionary* dock_plist = [[NSUserDefaults standardUserDefaults]
persistentDomainForName:kDockDomain];
if (![dock_plist isKindOfClass:[NSDictionary class]]) {
LOG(ERROR) << "dock_plist is not an NSDictionary";
return nil;
}
return dock_plist;
}
NSArray* PersistentAppsFromDockPlist(NSDictionary* dock_plist) {
if (!dock_plist) {
return nil;
}
NSArray* persistent_apps = dock_plist[kDockPersistentAppsKey];
if (![persistent_apps isKindOfClass:[NSArray class]]) {
LOG(ERROR) << "persistent_apps is not an NSArray";
return nil;
}
return persistent_apps;
}
} // namespace
ChromeInDockStatus ChromeIsInTheDock() {
NSDictionary* dock_plist = DockPlistFromUserDefaults();
NSArray* persistent_apps = PersistentAppsFromDockPlist(dock_plist);
if (!persistent_apps) {
return ChromeInDockFailure;
}
NSString* launch_path = [base::apple::OuterBundle() bundlePath];
return [PersistentAppPaths(persistent_apps) containsObject:launch_path]
? ChromeInDockTrue
: ChromeInDockFalse;
}
AddIconStatus AddIcon(NSString* installed_path, NSString* dmg_app_path) {
// ApplicationServices.framework/Frameworks/HIServices.framework contains an
// undocumented function, CoreDockAddFileToDock, that is able to add items
// to the Dock "live" without requiring a Dock restart. Under the hood, it
// communicates with the Dock via Mach IPC. It is available as of Mac OS X
// 10.6. AddIcon could call CoreDockAddFileToDock if available, but
// CoreDockAddFileToDock seems to always to add the new Dock icon last,
// where AddIcon takes care to position the icon appropriately. Based on
// disassembly, the signature of the undocumented function appears to be
// extern "C" OSStatus CoreDockAddFileToDock(CFURLRef url, int);
// The int argument doesn't appear to have any effect. It's not used as the
// position to place the icon as hoped.
@autoreleasepool {
NSMutableDictionary* dock_plist = [NSMutableDictionary
dictionaryWithDictionary:DockPlistFromUserDefaults()];
NSMutableArray* persistent_apps =
[NSMutableArray arrayWithArray:PersistentAppsFromDockPlist(dock_plist)];
NSMutableArray* persistent_app_paths = PersistentAppPaths(persistent_apps);
if (!persistent_app_paths) {
return IconAddFailure;
}
NSUInteger already_installed_app_index = NSNotFound;
NSUInteger app_index = NSNotFound;
for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
NSString* app_path = persistent_app_paths[index];
if ([app_path isEqualToString:installed_path]) {
// If the Dock already contains a reference to the newly installed
// application, don't add another one.
already_installed_app_index = index;
} else if ([app_path isEqualToString:dmg_app_path]) {
// If the Dock contains a reference to the application on the disk
// image, replace it with a reference to the newly installed
// application. However, if the Dock contains a reference to both the
// application on the disk image and the newly installed application,
// just remove the one referencing the disk image.
//
// This case is only encountered when the user drags the icon from the
// disk image volume window in the Finder directly into the Dock.
app_index = index;
}
}
bool made_change = false;
if (app_index != NSNotFound) {
// Remove the Dock's reference to the application on the disk image.
[persistent_apps removeObjectAtIndex:app_index];
[persistent_app_paths removeObjectAtIndex:app_index];
made_change = true;
}
if (already_installed_app_index == NSNotFound) {
// The Dock doesn't yet have a reference to the icon at the
// newly installed path. Figure out where to put the new icon.
NSString* app_name = [installed_path lastPathComponent];
if (app_index == NSNotFound) {
// If an application with this name is already in the Dock, put the new
// one right before it.
for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
NSString* dock_app_name =
[persistent_app_paths[index] lastPathComponent];
if ([dock_app_name isEqualToString:app_name]) {
app_index = index;
break;
}
}
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
if (app_index == NSNotFound) {
// If this is an officially-branded Chrome and another flavor is already
// in the Dock, put them next to each other. With side-by-side, there
// can be multiple Chromes already in the dock; pick the first one found
// in order, and put this Chrome next to it.
NSArray* app_name_order = @[
@"Google Chrome.app", @"Google Chrome Beta.app",
@"Google Chrome Dev.app", @"Google Chrome Canary.app"
];
NSUInteger app_name_index = [app_name_order indexOfObject:app_name];
if (app_name_index != NSNotFound) {
for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
NSString* dock_app_name =
[persistent_app_paths[index] lastPathComponent];
NSUInteger dock_app_name_index =
[app_name_order indexOfObject:dock_app_name];
if (dock_app_name_index == NSNotFound)
continue;
if (app_name_index < dock_app_name_index)
app_index = index;
else
app_index = index + 1;
break;
}
}
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
if (app_index == NSNotFound) {
// Put the new application after the last browser application already
// present in the Dock.
NSUInteger last_browser = [persistent_app_paths
indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^(NSString* app_path, NSUInteger idx,
BOOL* stop) {
return IsAppAtPathAWebBrowser(app_path);
}];
if (last_browser != NSNotFound)
app_index = last_browser + 1;
}
if (app_index == NSNotFound) {
// Put the new application last in the Dock.
app_index = [persistent_apps count];
}
// Set up the new Dock tile.
NSURL* url = [NSURL fileURLWithPath:installed_path isDirectory:YES];
NSDictionary* url_dict = DockFileDataDictionaryForURL(url);
if (!url_dict) {
LOG(ERROR) << "couldn't create url_dict";
return IconAddFailure;
}
NSDictionary* new_tile_data = @{kDockFileDataKey : url_dict};
NSDictionary* new_tile = @{kDockTileDataKey : new_tile_data};
// Add the new tile to the Dock.
[persistent_apps insertObject:new_tile atIndex:app_index];
[persistent_app_paths insertObject:installed_path atIndex:app_index];
made_change = true;
}
// Verify that the arrays are still parallel.
DCHECK_EQ([persistent_apps count], [persistent_app_paths count]);
if (!made_change) {
// If no changes were made, there's no point in rewriting the Dock's
// plist or restarting the Dock.
return IconAlreadyPresent;
}
// Rewrite the plist.
dock_plist[kDockPersistentAppsKey] = persistent_apps;
[[NSUserDefaults standardUserDefaults] setPersistentDomain:dock_plist
forName:kDockDomain];
Restart();
return IconAddSuccess;
}
}
} // namespace dock
|