Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/mac/install_from_dmg.mm

Issue 7273052: Install from disk image: Create persistent Dock icon (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/mac/launchd.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 present in 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
415 // immediately before the first existing tile for Google Chrome Canary,
416 // and a new tile for Google Chrome Canary will be placed immediately after
417 // the last existing tile for Google Chrome.
418 // - The new tile will be placed immediately after the last tile for another
419 // browser application already in the Dock.
420 // - The new tile will be placed last in the Dock.
421 // For the purposes of these comparisons, applications are identified by the
422 // last component in their path. For example, any application named Safari.app
423 // will be treated as a browser. If the user renames an application on disk,
424 // it will alter the result. Looking up the bundle ID could be slightly more
425 // robust in the presence of such alterations, but it's not thought to be a
426 // large enough problem to warrant such lookups.
427 //
428 // The changes made to the Dock's configuration are the minimal changes
429 // necessary to cause the desired behavior. Although it's possible to set
430 // additional properties on the dock tile added to the Dock's plist, this
431 // is not done. Upon relaunch, Dock.app will determine the correct values for
432 // the properties it requires and add them to its configuration.
433 //
434 // ApplicationServices.framework/Frameworks/HIServices.framework contains an
435 // undocumented function, CoreDockAddFileToDock, that is able to add items to
436 // the Dock "live" without requiring a Dock restart. Under the hood, it
437 // communicates with the Dock via Mach IPC. It is available as of Mac OS X
438 // 10.6. AddDockIcon could call CoreDockAddFileToDock if available, but
439 // CoreDockAddFileToDock seems to always to add the new Dock icon last, where
440 // AddDockIcon takes care to position the icon appropriately. Based on
441 // disassembly, the signature of the undocumented function appears to be
442 // extern "C" OSStatus CoreDockAddFileToDock(CFURLRef url, int);
443 // The int argument doesn't appear to have any effect. It's not used as the
444 // position to place the icon as hoped.
445 void AddDockIcon(NSString* installed_path, NSString* dmg_app_path) {
446 // There's enough potential allocation in this function to justify a
447 // distinct pool.
448 base::mac::ScopedNSAutoreleasePool autorelease_pool;
449
450 NSString* const kDockDomain = @"com.apple.dock";
451 NSUserDefaults* user_defaults = [NSUserDefaults standardUserDefaults];
452
453 NSDictionary* dock_plist_const =
454 [user_defaults persistentDomainForName:kDockDomain];
455 if (![dock_plist_const isKindOfClass:[NSDictionary class]]) {
456 LOG(ERROR) << "dock_plist_const not NSDictionary";
457 return;
458 }
459 NSMutableDictionary* dock_plist =
460 [NSMutableDictionary dictionaryWithDictionary:dock_plist_const];
461
462 NSString* const kDockPersistentAppsKey = @"persistent-apps";
463 NSArray* persistent_apps_const =
464 [dock_plist objectForKey:kDockPersistentAppsKey];
465 if (![persistent_apps_const isKindOfClass:[NSArray class]]) {
466 LOG(ERROR) << "persistent_apps_const not NSArray";
467 return;
468 }
469 NSMutableArray* persistent_apps =
470 [NSMutableArray arrayWithArray:persistent_apps_const];
471
472 NSMutableArray* persistent_app_paths = PersistentAppPaths(persistent_apps);
473 if (!persistent_app_paths) {
474 return;
475 }
476
477 // Directories in the Dock's plist are given with trailing slashes. Since
478 // installed_path and dmg_app_path both refer to application bundles,
479 // they're directories and will show up with trailing slashes. This is an
480 // artifact of the Dock's internal use of CFURL. Look for paths that match,
481 // and when adding an item to the Dock's plist, keep it in the form that the
482 // Dock likes.
483 NSString* installed_path_dock = [installed_path stringByAppendingString:@"/"];
484 NSString* dmg_app_path_dock = [dmg_app_path stringByAppendingString:@"/"];
485
486 NSUInteger already_installed_app_index = NSNotFound;
487 NSUInteger app_index = NSNotFound;
488 for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
489 NSString* app_path = [persistent_app_paths objectAtIndex:index];
490 if ([app_path isEqualToString:installed_path_dock]) {
491 // If the Dock already contains a reference to the newly installed
492 // application, don't add another one.
493 already_installed_app_index = index;
494 } else if ([app_path isEqualToString:dmg_app_path_dock]) {
495 // If the Dock contains a reference to the application on the disk
496 // image, replace it with a reference to the newly installed
497 // application. However, if the Dock contains a reference to both the
498 // application on the disk image and the newly installed application,
499 // just remove the one referencing the disk image.
500 //
501 // This case is only encountered when the user drags the icon from the
502 // disk image volume window in the Finder directly into the Dock.
503 app_index = index;
504 }
505 }
506
507 bool made_change = false;
508
509 if (app_index != NSNotFound) {
510 // Remove the Dock's reference to the application on the disk image.
511 [persistent_apps removeObjectAtIndex:app_index];
512 [persistent_app_paths removeObjectAtIndex:app_index];
513 made_change = true;
514 }
515
516 if (already_installed_app_index == NSNotFound) {
517 // The Dock doesn't yet have a reference to the icon at the
518 // newly installed path. Figure out where to put the new icon.
519 NSString* app_name = [installed_path lastPathComponent];
520
521 if (app_index == NSNotFound) {
522 // If an application with this name is already in the Dock, put the new
523 // one right before it.
524 for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
525 NSString* dock_app_name =
526 [[persistent_app_paths objectAtIndex:index] lastPathComponent];
527 if ([dock_app_name isEqualToString:app_name]) {
528 app_index = index;
529 break;
530 }
531 }
532 }
533
534 #if defined(GOOGLE_CHROME_BUILD)
535 if (app_index == NSNotFound) {
536 // If this is an officially-branded Chrome (including Canary) and an
537 // application matching the "other" flavor is already in the Dock, put
538 // them next to each other. Google Chrome will precede Google Chrome
539 // Canary in the Dock.
540 NSString* chrome_name = @"Google Chrome.app";
541 NSString* canary_name = @"Google Chrome Canary.app";
542 for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
543 NSString* dock_app_name =
544 [[persistent_app_paths objectAtIndex:index] lastPathComponent];
545 if ([dock_app_name isEqualToString:canary_name] &&
546 [app_name isEqualToString:chrome_name]) {
547 app_index = index;
548
549 // Break: put Google Chrome.app before the first Google Chrome
550 // Canary.app.
551 break;
552 } else if ([dock_app_name isEqualToString:chrome_name] &&
553 [app_name isEqualToString:canary_name]) {
554 app_index = index + 1;
555
556 // No break: put Google Chrome Canary.app after the last Google
557 // Chrome.app.
558 }
559 }
560 }
561 #endif // GOOGLE_CHROME_BUILD
562
563 if (app_index == NSNotFound) {
564 // Put the new application after the last browser application already
565 // present in the Dock.
566 NSArray* other_browser_app_names =
567 [NSArray arrayWithObjects:
568 #if defined(GOOGLE_CHROME_BUILD)
569 @"Chromium.app", // Unbranded Google Chrome
570 #else
571 @"Google Chrome.app",
572 @"Google Chrome Canary.app",
573 #endif
574 @"Safari.app",
575 @"Firefox.app",
576 @"Camino.app",
577 @"Opera.app",
578 @"OmniWeb.app",
579 @"WebKit.app", // Safari nightly
580 @"Aurora.app", // Firefox dev
581 @"Nightly.app", // Firefox nightly
582 nil];
583 for (NSUInteger index = 0; index < [persistent_apps count]; ++index) {
584 NSString* dock_app_name =
585 [[persistent_app_paths objectAtIndex:index] lastPathComponent];
586 if ([other_browser_app_names containsObject:dock_app_name]) {
587 app_index = index + 1;
588 }
589 }
590 }
591
592 if (app_index == NSNotFound) {
593 // Put the new application last in the Dock.
594 app_index = [persistent_apps count];
595 }
596
597 // Set up the new Dock tile.
598 NSDictionary* new_tile_file_data =
599 [NSDictionary dictionaryWithObjectsAndKeys:
600 installed_path_dock, kDockCFURLStringKey,
601 [NSNumber numberWithInt:0], kDockCFURLStringTypeKey,
602 nil];
603 NSDictionary* new_tile_data =
604 [NSDictionary dictionaryWithObject:new_tile_file_data
605 forKey:kDockFileDataKey];
606 NSDictionary* new_tile =
607 [NSDictionary dictionaryWithObject:new_tile_data
608 forKey:kDockTileDataKey];
609
610 // Add the new tile to the Dock.
611 [persistent_apps insertObject:new_tile atIndex:app_index];
612 [persistent_app_paths insertObject:installed_path_dock atIndex:app_index];
613 made_change = true;
614 }
615
616 // Verify that the arrays are still parallel.
617 DCHECK_EQ([persistent_apps count], [persistent_app_paths count]);
618
619 if (!made_change) {
620 // If no changes were made, there's no point in rewriting the Dock's
621 // plist or restarting the Dock.
622 return;
623 }
624
625 // Rewrite the plist.
626 [dock_plist setObject:persistent_apps forKey:kDockPersistentAppsKey];
627 [user_defaults setPersistentDomain:dock_plist forName:kDockDomain];
628
629 // Restart the Dock. Doing this via launchd using the proper job label is
630 // the safest way to handle the restart. Unlike "killall Dock", looking this
631 // up via launchd guarantees that only the right process will be targeted.
632 // Sending a SIGHUP to the Dock seems to be a more reliable way to get the
633 // replacement Dock process to read the newly written plist than using the
634 // equivalent of "launchctl stop" (even if followed by "launchctl start.")
635 launchd::SignalJob("com.apple.Dock.agent", SIGHUP);
636 }
637
343 // Launches the application at installed_path. The helper application 638 // Launches the application at installed_path. The helper application
344 // contained within install_path will be used for the relauncher process. This 639 // 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 640 // 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 641 // application on the disk image. The relauncher process will be asked to
347 // call EjectAndTrashDiskImage on dmg_bsd_device_name. 642 // call EjectAndTrashDiskImage on dmg_bsd_device_name.
348 bool LaunchInstalledApp(NSString* installed_path, 643 bool LaunchInstalledApp(NSString* installed_path,
349 const std::string& dmg_bsd_device_name) { 644 const std::string& dmg_bsd_device_name) {
350 FilePath browser_path([installed_path fileSystemRepresentation]); 645 FilePath browser_path([installed_path fileSystemRepresentation]);
351 646
352 FilePath helper_path = browser_path.Append("Contents/Versions"); 647 FilePath helper_path = browser_path.Append("Contents/Versions");
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 736
442 ScopedAuthorizationRef authorization( 737 ScopedAuthorizationRef authorization(
443 MaybeShowAuthorizationDialog(application_directory)); 738 MaybeShowAuthorizationDialog(application_directory));
444 // authorization will be NULL if it's deemed unnecessary or if 739 // authorization will be NULL if it's deemed unnecessary or if
445 // authentication fails. In either case, try to install without privilege 740 // authentication fails. In either case, try to install without privilege
446 // escalation. 741 // escalation.
447 742
448 if (!InstallFromDiskImage(authorization.release(), 743 if (!InstallFromDiskImage(authorization.release(),
449 installer_path, 744 installer_path,
450 source_path, 745 source_path,
451 target_path) || 746 target_path)) {
452 !LaunchInstalledApp(target_path, dmg_bsd_device_name)) { 747 ShowErrorDialog();
748 return false;
749 }
750
751 AddDockIcon(target_path, source_path);
752
753 if (!LaunchInstalledApp(target_path, dmg_bsd_device_name)) {
453 ShowErrorDialog(); 754 ShowErrorDialog();
454 return false; 755 return false;
455 } 756 }
456 757
457 return true; 758 return true;
458 } 759 }
459 760
460 namespace { 761 namespace {
461 762
462 // A simple scoper that calls DASessionScheduleWithRunLoop when created and 763 // A simple scoper that calls DASessionScheduleWithRunLoop when created and
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 const UInt8* trash_path_u8 = reinterpret_cast<const UInt8*>( 957 const UInt8* trash_path_u8 = reinterpret_cast<const UInt8*>(
657 trash_path.value().c_str()); 958 trash_path.value().c_str());
658 status = FNNotifyByPath(trash_path_u8, 959 status = FNNotifyByPath(trash_path_u8,
659 kFNDirectoryModifiedMessage, 960 kFNDirectoryModifiedMessage,
660 kNilOptions); 961 kNilOptions);
661 if (status != noErr) { 962 if (status != noErr) {
662 LOG(ERROR) << "FNNotifyByPath: " << status; 963 LOG(ERROR) << "FNNotifyByPath: " << status;
663 return; 964 return;
664 } 965 }
665 } 966 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/mac/launchd.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698