OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/mac/install_from_dmg.h" | 5 #include "chrome/browser/mac/install_from_dmg.h" |
6 | 6 |
7 #include <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
8 #import <AppKit/AppKit.h> | 8 #import <AppKit/AppKit.h> |
9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
10 #include <CoreServices/CoreServices.h> | 10 #include <CoreServices/CoreServices.h> |
11 #include <DiskArbitration/DiskArbitration.h> | 11 #include <DiskArbitration/DiskArbitration.h> |
12 #include <IOKit/IOKitLib.h> | 12 #include <IOKit/IOKitLib.h> |
13 #include <signal.h> | |
13 #include <stdlib.h> | 14 #include <stdlib.h> |
14 #include <string.h> | 15 #include <string.h> |
15 #include <sys/param.h> | 16 #include <sys/param.h> |
16 #include <sys/mount.h> | 17 #include <sys/mount.h> |
17 | 18 |
18 #include "base/auto_reset.h" | 19 #include "base/auto_reset.h" |
19 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
20 #include "base/command_line.h" | 21 #include "base/command_line.h" |
21 #include "base/file_path.h" | 22 #include "base/file_path.h" |
22 #include "base/logging.h" | 23 #include "base/logging.h" |
23 #import "base/mac/mac_util.h" | 24 #import "base/mac/mac_util.h" |
24 #include "base/mac/scoped_cftyperef.h" | 25 #include "base/mac/scoped_cftyperef.h" |
25 #include "base/mac/scoped_nsautorelease_pool.h" | 26 #include "base/mac/scoped_nsautorelease_pool.h" |
26 #include "base/string_util.h" | 27 #include "base/string_util.h" |
27 #include "base/sys_string_conversions.h" | 28 #include "base/sys_string_conversions.h" |
28 #include "chrome/browser/mac/authorization_util.h" | 29 #include "chrome/browser/mac/authorization_util.h" |
29 #include "chrome/browser/mac/scoped_authorizationref.h" | 30 #include "chrome/browser/mac/scoped_authorizationref.h" |
30 #include "chrome/browser/mac/scoped_ioobject.h" | 31 #include "chrome/browser/mac/scoped_ioobject.h" |
31 #import "chrome/browser/mac/keystone_glue.h" | 32 #import "chrome/browser/mac/keystone_glue.h" |
33 #include "chrome/browser/mac/launchd.h" | |
32 #include "chrome/browser/mac/relauncher.h" | 34 #include "chrome/browser/mac/relauncher.h" |
33 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
34 #include "grit/chromium_strings.h" | 36 #include "grit/chromium_strings.h" |
35 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
36 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
37 #include "ui/base/l10n/l10n_util_mac.h" | 39 #include "ui/base/l10n/l10n_util_mac.h" |
38 | 40 |
39 // When C++ exceptions are disabled, the C++ library defines |try| and | 41 // When C++ exceptions are disabled, the C++ library defines |try| and |
40 // |catch| so as to allow exception-expecting C++ code to build properly when | 42 // |catch| so as to allow exception-expecting C++ code to build properly when |
41 // language support for exceptions is not present. These macros interfere | 43 // language support for exceptions is not present. These macros interfere |
42 // with the use of |@try| and |@catch| in Objective-C files such as this one. | 44 // with the use of |@try| and |@catch| in Objective-C files such as this one. |
43 // Undefine these macros here, after everything has been #included, since | 45 // Undefine these macros here, after everything has been #included, since |
44 // there will be no C++ uses and only Objective-C uses from this point on. | 46 // there will be no C++ uses and only Objective-C uses from this point on. |
45 #undef try | 47 #undef try |
46 #undef catch | 48 #undef catch |
47 | 49 |
48 namespace { | 50 namespace { |
49 | 51 |
52 NSString* const kDockTileDataKey = @"tile-data"; | |
53 NSString* const kDockFileDataKey = @"file-data"; | |
54 NSString* const kDockCFURLStringKey = @"_CFURLString"; | |
55 NSString* const kDockCFURLStringTypeKey = @"_CFURLStringType"; | |
56 | |
50 // Given an io_service_t (expected to be of class IOMedia), walks the ancestor | 57 // Given an io_service_t (expected to be of class IOMedia), walks the ancestor |
51 // chain, returning the closest ancestor that implements class IOHDIXHDDrive, | 58 // chain, returning the closest ancestor that implements class IOHDIXHDDrive, |
52 // if any. If no such ancestor is found, returns NULL. Following the "copy" | 59 // if any. If no such ancestor is found, returns NULL. Following the "copy" |
53 // rule, the caller assumes ownership of the returned value. | 60 // rule, the caller assumes ownership of the returned value. |
54 // | 61 // |
55 // Note that this looks for a class that inherits from IOHDIXHDDrive, but it | 62 // Note that this looks for a class that inherits from IOHDIXHDDrive, but it |
56 // will not likely find a concrete IOHDIXHDDrive. It will be | 63 // will not likely find a concrete IOHDIXHDDrive. It will be |
57 // IOHDIXHDDriveOutKernel for disk images mounted "out-of-kernel" or | 64 // IOHDIXHDDriveOutKernel for disk images mounted "out-of-kernel" or |
58 // IOHDIXHDDriveInKernel for disk images mounted "in-kernel." Out-of-kernel is | 65 // IOHDIXHDDriveInKernel for disk images mounted "in-kernel." Out-of-kernel is |
59 // the default as of Mac OS X 10.5. See the documentation for "hdiutil attach | 66 // the default as of Mac OS X 10.5. See the documentation for "hdiutil attach |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 // ticket. Inform KeystoneGlue of the new path to use. | 340 // ticket. Inform KeystoneGlue of the new path to use. |
334 KeystoneGlue* keystone_glue = [KeystoneGlue defaultKeystoneGlue]; | 341 KeystoneGlue* keystone_glue = [KeystoneGlue defaultKeystoneGlue]; |
335 [keystone_glue setAppPath:target_path]; | 342 [keystone_glue setAppPath:target_path]; |
336 [keystone_glue promoteTicketWithAuthorization:authorization.release() | 343 [keystone_glue promoteTicketWithAuthorization:authorization.release() |
337 synchronous:YES]; | 344 synchronous:YES]; |
338 } | 345 } |
339 | 346 |
340 return true; | 347 return true; |
341 } | 348 } |
342 | 349 |
350 // Returns an array parallel to |persistent_apps| containing only the | |
351 // pathnames of the Dock tiles contained therein. Returns nil on failure, such | |
352 // as when the structure of |persistent_apps| is not understood. | |
353 NSMutableArray* PersistentAppPaths(NSArray* persistent_apps) { | |
354 NSMutableArray* app_paths = | |
355 [NSMutableArray arrayWithCapacity:[persistent_apps count]]; | |
356 | |
357 for (NSDictionary* app in persistent_apps) { | |
358 if (![app isKindOfClass:[NSDictionary class]]) { | |
359 LOG(ERROR) << "app not NSDictionary"; | |
360 return nil; | |
361 } | |
362 | |
363 NSDictionary* tile_data = [app objectForKey:kDockTileDataKey]; | |
364 if (![tile_data isKindOfClass:[NSDictionary class]]) { | |
365 LOG(ERROR) << "tile_data not NSDictionary"; | |
366 return nil; | |
367 } | |
368 | |
369 NSDictionary* file_data = [tile_data objectForKey:kDockFileDataKey]; | |
370 if (![file_data isKindOfClass:[NSDictionary class]]) { | |
371 LOG(ERROR) << "file_data not NSDictionary"; | |
372 return nil; | |
373 } | |
374 | |
375 NSNumber* type = [file_data objectForKey:kDockCFURLStringTypeKey]; | |
376 if (![type isKindOfClass:[NSNumber class]]) { | |
377 LOG(ERROR) << "type not NSNumber"; | |
378 return nil; | |
379 } | |
380 if ([type intValue] != 0) { | |
381 LOG(ERROR) << "type not 0"; | |
382 return nil; | |
383 } | |
384 | |
385 NSString* path = [file_data objectForKey:kDockCFURLStringKey]; | |
386 if (![path isKindOfClass:[NSString class]]) { | |
387 LOG(ERROR) << "path not NSString"; | |
388 return nil; | |
389 } | |
390 | |
391 [app_paths addObject:path]; | |
392 } | |
393 | |
394 return app_paths; | |
395 } | |
396 | |
397 // Adds an icon to the Dock pointing to |installed_path| if one is not already | |
398 // present. |dmg_app_path| is the path to the install source. Its tile will be | |
399 // removed if present. If any changes are made to the Dock's configuration, | |
400 // the Dock process is restarted to reflect those changes. | |
401 // | |
402 // Various heruistics are used to determine where to place the new icon | |
403 // relative to other items already existing within the Dock: | |
404 // - If installed_path is already in the Dock, no new tiles for this path | |
405 // will be added. | |
406 // - If dmg_app_path is present in the Dock, it will be removed. If | |
407 // installed_path is not already present, the new tile referencing | |
408 // installed_path will be placed where the dmg_app_path tile was. This | |
409 // keeps the tile where a user expects it if they dragged the application | |
410 // icon from a disk image into the Dock and then clicked on the new icon | |
411 // in the Dock. | |
412 // - The new tile will precede any application with the same name already | |
413 // in the Dock. | |
414 // - In an official build, a new tile for Google Chrome will be placed before | |
415 // any existing tiles for Google Chrome Canary, and a new tile for Google | |
416 // Chrome Canary will be placed after any existing tiles for Google Chrome. | |
417 // - The new tile will be placed immediately after the last tile for another | |
418 // browser application already in the Dock. | |
419 // - The new tile will be placed last in the Dock. | |
420 // For the purposes of these comparisons, applications are identified by the | |
421 // last component in their path. For example, any application named Safari.app | |
422 // will be treated as a browser. | |
423 // | |
424 // The changes made to the Dock's configuration are the minimal changes | |
425 // necessary to cause the desired behavior. Although it's possible to set | |
426 // additional properties on the dock tile added to the Dock's plist, this | |
427 // is not done. Upon relaunch, Dock.app will determine the correct values for | |
428 // the properties it requires and add them to its configuration. | |
429 void AddDockIcon(NSString* installed_path, NSString* dmg_app_path) { | |
430 NSString* dock_domain = @"com.apple.dock"; | |
431 NSUserDefaults* user_defaults = [NSUserDefaults standardUserDefaults]; | |
432 | |
433 NSDictionary* dock_plist_const = | |
434 [user_defaults persistentDomainForName:dock_domain]; | |
435 if (![dock_plist_const isKindOfClass:[NSDictionary class]]) { | |
436 LOG(ERROR) << "dock_plist_const not NSDictionary"; | |
437 return; | |
438 } | |
439 NSMutableDictionary* dock_plist = | |
440 [NSMutableDictionary dictionaryWithDictionary:dock_plist_const]; | |
441 | |
442 NSString* persistent_apps_key = @"persistent-apps"; | |
443 NSArray* persistent_apps_const = | |
444 [dock_plist objectForKey:persistent_apps_key]; | |
445 if (![persistent_apps_const isKindOfClass:[NSArray class]]) { | |
446 LOG(ERROR) << "persistent_apps_const not NSArray"; | |
447 return; | |
448 } | |
449 NSMutableArray* persistent_apps = | |
450 [NSMutableArray arrayWithArray:persistent_apps_const]; | |
451 | |
452 NSMutableArray* persistent_app_paths = PersistentAppPaths(persistent_apps); | |
453 if (!persistent_app_paths) { | |
454 return; | |
455 } | |
456 | |
457 int count = static_cast<int>([persistent_app_paths count]); | |
Mark Mentovai
2011/06/29 19:29:29
I only had “count” in a separate variable to elimi
| |
458 if (count < 0) { | |
459 LOG(ERROR) << "Too many items in the Dock"; | |
Robert Sesek
2011/06/29 18:26:42
srsly?
Mark Mentovai
2011/06/29 19:29:29
rsesek wrote:
| |
460 return; | |
461 } | |
462 | |
463 NSString* installed_path_dock = [installed_path stringByAppendingString:@"/"]; | |
Robert Sesek
2011/06/29 18:26:42
I think I understand why this is necessary, but pl
| |
464 NSString* dmg_app_path_dock = [dmg_app_path stringByAppendingString:@"/"]; | |
465 | |
466 int already_installed_app_index = -1; | |
467 int app_index = -1; | |
468 for (int index = 0; index < count; ++index) { | |
Robert Sesek
2011/06/29 18:26:42
Pinkerton in the past has told me to use fast enum
Mark Mentovai
2011/06/29 19:29:29
rsesek wrote:
| |
469 NSString* app_path = [persistent_app_paths objectAtIndex:index]; | |
470 if ([app_path isEqualToString:installed_path_dock]) { | |
471 // If the Dock already contains a reference to the newly-installed | |
Robert Sesek
2011/06/29 18:26:42
nit: don't hyphenate compound adjectives if the le
Mark Mentovai
2011/06/29 19:29:29
rsesek wrote:
| |
472 // application, don't add another one. | |
473 already_installed_app_index = index; | |
474 } else if ([app_path isEqualToString:dmg_app_path_dock]) { | |
475 // If the Dock contains a reference to the application on the disk | |
476 // image, replace it with a reference to the newly-installed | |
477 // application. However, if the Dock contains a reference to both the | |
478 // application on the disk image and the newly-installed application, | |
479 // just remove the one referencing the disk image. | |
480 app_index = index; | |
481 } | |
482 } | |
483 | |
484 bool made_change = false; | |
485 | |
486 if (app_index >= 0) { | |
487 // Remove the Dock's reference to the application on the disk image. | |
488 [persistent_apps removeObjectAtIndex:app_index]; | |
489 [persistent_app_paths removeObjectAtIndex:app_index]; | |
490 --count; | |
491 made_change = true; | |
492 } | |
493 | |
494 if (already_installed_app_index < 0) { | |
495 // The Dock doesn't yet have a reference to the icon at the | |
496 // newly-installed path. Figure out where to put the new icon. | |
497 NSString* app_name = [installed_path lastPathComponent]; | |
498 | |
499 if (app_index < 0) { | |
Robert Sesek
2011/06/29 18:26:42
This case is confusing me; shouldn't app_index be
Mark Mentovai
2011/06/29 19:29:29
rsesek wrote:
| |
500 // If an application with this name is already in the Dock, put the new | |
501 // one right before it. | |
502 for (int index = 0; index < count; ++index) { | |
503 NSString* dock_app_name = | |
504 [[persistent_app_paths objectAtIndex:index] lastPathComponent]; | |
505 if ([dock_app_name isEqualToString:app_name]) { | |
506 app_index = index; | |
507 break; | |
508 } | |
509 } | |
510 } | |
511 | |
512 #if defined(GOOGLE_CHROME_BUILD) | |
513 if (app_index < 0) { | |
514 // If this is an officially-branded Chrome (including Canary) and an | |
515 // application matching the "other" flavor is already in the Dock, put | |
516 // them next to each other. Google Chrome will precede Google Chrome | |
517 // Canary in the Dock. | |
518 NSString* chrome_name = @"Google Chrome.app"; | |
519 NSString* canary_name = @"Google Chrome Canary.app"; | |
520 for (int index = 0; index < count; ++index) { | |
Robert Sesek
2011/06/29 18:26:42
Same comment about fast enum.
| |
521 NSString* dock_app_name = | |
522 [[persistent_app_paths objectAtIndex:index] lastPathComponent]; | |
523 if ([dock_app_name isEqualToString:canary_name] && | |
524 [app_name isEqualToString:chrome_name]) { | |
525 app_index = index; | |
526 | |
527 // Break: put Google Chrome.app before the first Google Chrome | |
528 // Canary.app. | |
529 break; | |
530 } else if ([dock_app_name isEqualToString:chrome_name] && | |
531 [app_name isEqualToString:canary_name]) { | |
532 app_index = index + 1; | |
533 | |
534 // No break: put Google Chrome Canary.app after the last Google | |
535 // Chrome.app. | |
536 } | |
537 } | |
538 } | |
539 #endif // GOOGLE_CHROME_BUILD | |
540 | |
541 if (app_index < 0) { | |
542 // Put the new application after the last browser application already | |
543 // present in the Dock. | |
544 NSArray* other_browser_app_names = | |
545 [NSArray arrayWithObjects: | |
546 #if defined(GOOGLE_CHROME_BUILD) | |
547 @"Chromium.app", // Unbranded Google Chrome | |
548 #else | |
549 @"Google Chrome.app", | |
550 @"Google Chrome Canary.app", | |
551 #endif | |
552 @"Safari.app", | |
553 @"Firefox.app", | |
554 @"Camino.app", | |
555 @"Opera.app", | |
556 @"OmniWeb.app", | |
557 @"WebKit.app", // Safari nightly | |
558 @"Aurora.app", // Firefox dev | |
559 @"Nightly.app", // Firefox nightly | |
560 nil]; | |
561 for (int index = 0; index < count; ++index) { | |
562 NSString* dock_app_name = | |
563 [[persistent_app_paths objectAtIndex:index] lastPathComponent]; | |
564 if ([other_browser_app_names containsObject:dock_app_name]) { | |
565 app_index = index + 1; | |
566 } | |
567 } | |
568 } | |
569 | |
570 if (app_index < 0) { | |
571 // Put the new application last in the Dock. | |
572 app_index = count; | |
573 } | |
574 | |
575 // Set up the new Dock tile. | |
576 NSDictionary* new_tile_file_data = | |
577 [NSDictionary dictionaryWithObjectsAndKeys: | |
578 installed_path_dock, kDockCFURLStringKey, | |
579 [NSNumber numberWithInt:0], kDockCFURLStringTypeKey, | |
580 nil]; | |
581 NSDictionary* new_tile_data = | |
582 [NSDictionary dictionaryWithObject:new_tile_file_data | |
583 forKey:kDockFileDataKey]; | |
584 NSDictionary* new_tile = | |
585 [NSDictionary dictionaryWithObject:new_tile_data | |
586 forKey:kDockTileDataKey]; | |
587 | |
588 // Add the new tile to the Dock. | |
589 [persistent_apps insertObject:new_tile atIndex:app_index]; | |
590 [persistent_app_paths insertObject:installed_path_dock atIndex:app_index]; | |
591 ++count; | |
592 made_change = true; | |
593 } | |
594 | |
595 if (!made_change) { | |
596 // If no changes were made, there's no point in rewriting the Dock's | |
597 // plist or restarting the Dock. | |
598 return; | |
599 } | |
600 | |
601 // Rewrite the plist. | |
602 [dock_plist setObject:persistent_apps forKey:persistent_apps_key]; | |
603 [user_defaults setPersistentDomain:dock_plist forName:dock_domain]; | |
604 | |
605 // Restart the Dock. Doing this via launchd using the proper job label is | |
606 // the safest way to handle the restart. Unlike "killall Dock", looking this | |
607 // up via launchd guarantees that only the right process will be targeted. | |
608 // Sending a SIGHUP to the Dock seems to be a more reliable way to get the | |
609 // replacement Dock process to read the newly-written plist than using the | |
610 // equivalent of "launchctl stop" (even if followed by "launchctl start.") | |
611 launchd::SignalJob("com.apple.Dock.agent", SIGHUP); | |
612 } | |
613 | |
343 // Launches the application at installed_path. The helper application | 614 // Launches the application at installed_path. The helper application |
344 // contained within install_path will be used for the relauncher process. This | 615 // contained within install_path will be used for the relauncher process. This |
345 // keeps Launch Services from ever having to see or think about the helper | 616 // keeps Launch Services from ever having to see or think about the helper |
346 // application on the disk image. The relauncher process will be asked to | 617 // application on the disk image. The relauncher process will be asked to |
347 // call EjectAndTrashDiskImage on dmg_bsd_device_name. | 618 // call EjectAndTrashDiskImage on dmg_bsd_device_name. |
348 bool LaunchInstalledApp(NSString* installed_path, | 619 bool LaunchInstalledApp(NSString* installed_path, |
349 const std::string& dmg_bsd_device_name) { | 620 const std::string& dmg_bsd_device_name) { |
350 FilePath browser_path([installed_path fileSystemRepresentation]); | 621 FilePath browser_path([installed_path fileSystemRepresentation]); |
351 | 622 |
352 FilePath helper_path = browser_path.Append("Contents/Versions"); | 623 FilePath helper_path = browser_path.Append("Contents/Versions"); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 | 712 |
442 ScopedAuthorizationRef authorization( | 713 ScopedAuthorizationRef authorization( |
443 MaybeShowAuthorizationDialog(application_directory)); | 714 MaybeShowAuthorizationDialog(application_directory)); |
444 // authorization will be NULL if it's deemed unnecessary or if | 715 // authorization will be NULL if it's deemed unnecessary or if |
445 // authentication fails. In either case, try to install without privilege | 716 // authentication fails. In either case, try to install without privilege |
446 // escalation. | 717 // escalation. |
447 | 718 |
448 if (!InstallFromDiskImage(authorization.release(), | 719 if (!InstallFromDiskImage(authorization.release(), |
449 installer_path, | 720 installer_path, |
450 source_path, | 721 source_path, |
451 target_path) || | 722 target_path)) { |
452 !LaunchInstalledApp(target_path, dmg_bsd_device_name)) { | 723 ShowErrorDialog(); |
724 return false; | |
725 } | |
726 | |
727 AddDockIcon(target_path, source_path); | |
728 | |
729 if (!LaunchInstalledApp(target_path, dmg_bsd_device_name)) { | |
453 ShowErrorDialog(); | 730 ShowErrorDialog(); |
454 return false; | 731 return false; |
455 } | 732 } |
456 | 733 |
457 return true; | 734 return true; |
458 } | 735 } |
459 | 736 |
460 namespace { | 737 namespace { |
461 | 738 |
462 // A simple scoper that calls DASessionScheduleWithRunLoop when created and | 739 // A simple scoper that calls DASessionScheduleWithRunLoop when created and |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
656 const UInt8* trash_path_u8 = reinterpret_cast<const UInt8*>( | 933 const UInt8* trash_path_u8 = reinterpret_cast<const UInt8*>( |
657 trash_path.value().c_str()); | 934 trash_path.value().c_str()); |
658 status = FNNotifyByPath(trash_path_u8, | 935 status = FNNotifyByPath(trash_path_u8, |
659 kFNDirectoryModifiedMessage, | 936 kFNDirectoryModifiedMessage, |
660 kNilOptions); | 937 kNilOptions); |
661 if (status != noErr) { | 938 if (status != noErr) { |
662 LOG(ERROR) << "FNNotifyByPath: " << status; | 939 LOG(ERROR) << "FNNotifyByPath: " << status; |
663 return; | 940 return; |
664 } | 941 } |
665 } | 942 } |
OLD | NEW |