diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-18 21:45:52 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-18 21:45:52 +0000 |
commit | 3dd0e20bd3477cdb195fd5ef4c7f682438d2156c (patch) | |
tree | f0608ff623e4b9ab61d4df95e147c3690c347ddc | |
parent | 89a5616f0bdd3842854f559de57ad84a13690210 (diff) | |
download | chromium_src-3dd0e20bd3477cdb195fd5ef4c7f682438d2156c.zip chromium_src-3dd0e20bd3477cdb195fd5ef4c7f682438d2156c.tar.gz chromium_src-3dd0e20bd3477cdb195fd5ef4c7f682438d2156c.tar.bz2 |
Make Dock restart work on 10.5.
On Mac OS X 10.5, the Dock isn't launched from a launch agent as it is on 10.6
and 10.7. There is no fixed name to use to look up the Dock job with launchd
(via launchd::PIDForJob). Instead of communicating with launchd, on 10.5,
locate the Dock process by calling into the Process Manager and looking for
a process whose bundle identifier matches the value expected for the Dock.
Dock restart was introduced in r91156. This bug is present on 10.5 since that
revision.
This change moves the Dock-related code in install_from_dmg.mm out into its
own file, dock.mm. With this change, there's certainly enough code to justify
a distinct file.
BUG=88247
TEST=Install from disk image without any persistent tiles for Chrome in the
Dock. Following installation, the Dock should restart, with a new
persistent tile for Chrome present. This should work on 10.5 as well as
newer OS releases. No error messages from install_from_dmg.mm, dock.cc,
or launchd.cc should be logged.
Review URL: http://codereview.chromium.org/7285038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92898 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/mac/dock.h | 57 | ||||
-rw-r--r-- | chrome/browser/mac/dock.mm | 355 | ||||
-rw-r--r-- | chrome/browser/mac/install_from_dmg.mm | 304 | ||||
-rw-r--r-- | chrome/browser/mac/launchd.cc | 24 | ||||
-rw-r--r-- | chrome/browser/mac/launchd.h | 6 | ||||
-rw-r--r-- | chrome/browser/mac/relauncher.cc | 6 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 10 |
7 files changed, 442 insertions, 320 deletions
diff --git a/chrome/browser/mac/dock.h b/chrome/browser/mac/dock.h new file mode 100644 index 0000000..f39ffc5 --- /dev/null +++ b/chrome/browser/mac/dock.h @@ -0,0 +1,57 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_MAC_DOCK_H_ +#define CHROME_BROWSER_MAC_DOCK_H_ +#pragma once + +#if defined(__OBJC__) +@class NSString; +#else +class NSString; +#endif + +namespace dock { + +// Adds an icon to the Dock pointing to |installed_path| if one is not already +// present. |dmg_app_path| is the path to the install source. Its tile will be +// removed if present. If any changes are made to the Dock's configuration, +// the Dock process is restarted so that the changes become visible in the UI. +// +// Various heuristics are used to determine where to place the new icon +// relative to other items already present in the Dock: +// - If installed_path is already in the Dock, no new tiles for this path +// will be added. +// - If dmg_app_path is present in the Dock, it will be removed. If +// installed_path is not already present, the new tile referencing +// installed_path will be placed where the dmg_app_path tile was. This +// keeps the tile where a user expects it if they dragged the application +// icon from a disk image into the Dock and then clicked on the new icon +// in the Dock. +// - The new tile will precede any application with the same name already +// in the Dock. +// - In an official build, a new tile for Google Chrome will be placed +// immediately before the first existing tile for Google Chrome Canary, +// and a new tile for Google Chrome Canary will be placed immediately after +// the last existing tile for Google Chrome. +// - The new tile will be placed immediately after the last tile for another +// browser application already in the Dock. +// - The new tile will be placed last in the Dock. +// For the purposes of these comparisons, applications are identified by the +// last component in their path. For example, any application named Safari.app +// will be treated as a browser. If the user renames an application on disk, +// it will alter the result. Looking up the bundle ID could be slightly more +// robust in the presence of such alterations, but it's not thought to be a +// large enough problem to warrant such lookups. +// +// The changes made to the Dock's configuration are the minimal changes +// necessary to cause the desired behavior. Although it's possible to set +// additional properties on the dock tile added to the Dock's plist, this +// is not done. Upon relaunch, Dock.app will determine the correct values for +// the properties it requires and add them to its configuration. +void AddIcon(NSString* installed_path, NSString* dmg_app_path); + +} // namespace dock + +#endif // CHROME_BROWSER_MAC_DOCK_H_ diff --git a/chrome/browser/mac/dock.mm b/chrome/browser/mac/dock.mm new file mode 100644 index 0000000..200e6b0 --- /dev/null +++ b/chrome/browser/mac/dock.mm @@ -0,0 +1,355 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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> +#import <Foundation/Foundation.h> +#include <CoreFoundation/CoreFoundation.h> +#include <signal.h> + +#include "base/logging.h" +#include "base/mac/mac_util.h" +#include "base/mac/scoped_cftyperef.h" +#include "base/mac/scoped_nsautorelease_pool.h" +#include "base/sys_string_conversions.h" +#include "chrome/browser/mac/launchd.h" + +namespace dock { +namespace { + +NSString* const kDockTileDataKey = @"tile-data"; +NSString* const kDockFileDataKey = @"file-data"; +NSString* const kDockCFURLStringKey = @"_CFURLString"; +NSString* const kDockCFURLStringTypeKey = @"_CFURLStringType"; + +// 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) { + 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 objectForKey:kDockTileDataKey]; + if (![tile_data isKindOfClass:[NSDictionary class]]) { + LOG(ERROR) << "tile_data not NSDictionary"; + return nil; + } + + NSDictionary* file_data = [tile_data objectForKey:kDockFileDataKey]; + if (![file_data isKindOfClass:[NSDictionary class]]) { + LOG(ERROR) << "file_data not NSDictionary"; + return nil; + } + + NSNumber* type = [file_data objectForKey:kDockCFURLStringTypeKey]; + if (![type isKindOfClass:[NSNumber class]]) { + LOG(ERROR) << "type not NSNumber"; + return nil; + } + if ([type intValue] != 0) { + LOG(ERROR) << "type not 0"; + return nil; + } + + NSString* path = [file_data objectForKey:kDockCFURLStringKey]; + if (![path isKindOfClass:[NSString class]]) { + LOG(ERROR) << "path not NSString"; + return nil; + } + + [app_paths addObject:path]; + } + + return app_paths; +} + +// Returns the process ID for a process whose bundle identifier is bundle_id. +// The process is looked up using the Process Manager. Returns -1 on error, +// including when no process matches the bundle identifier. +pid_t PIDForProcessBundleID(const std::string& bundle_id) { + // This technique is racy: what happens if |psn| becomes invalid before a + // subsequent call to GetNextProcess, or if the Process Manager's internal + // order of processes changes? Tolerate the race by allowing failure. Since + // this function is only used on Leopard to find the Dock process so that it + // can be restarted, the worst that can happen here is that the Dock won't + // be restarted. + ProcessSerialNumber psn = {0, kNoProcess}; + OSStatus status; + while ((status = GetNextProcess(&psn)) == noErr) { + pid_t process_pid; + if ((status = GetProcessPID(&psn, &process_pid)) != noErr) { + LOG(ERROR) << "GetProcessPID: " << status; + continue; + } + + base::mac::ScopedCFTypeRef<CFDictionaryRef> process_dictionary( + ProcessInformationCopyDictionary(&psn, + kProcessDictionaryIncludeAllInformationMask)); + if (!process_dictionary.get()) { + LOG(ERROR) << "ProcessInformationCopyDictionary"; + continue; + } + + CFStringRef process_bundle_id_cf = static_cast<CFStringRef>( + CFDictionaryGetValue(process_dictionary, kCFBundleIdentifierKey)); + if (!process_bundle_id_cf) { + // Not all processes have a bundle ID. + continue; + } else if (CFGetTypeID(process_bundle_id_cf) != CFStringGetTypeID()) { + LOG(ERROR) << "process_bundle_id_cf not CFStringRef"; + continue; + } + + std::string process_bundle_id = + base::SysCFStringRefToUTF8(process_bundle_id_cf); + if (process_bundle_id == bundle_id) { + // Found it! + return process_pid; + } + } + + // status will be procNotFound (-600) if the process wasn't found. + LOG(ERROR) << "GetNextProcess: " << status; + + return -1; +} + +// Restart the Dock process by sending it a SIGHUP. +void Restart() { + pid_t pid; + + if (base::mac::IsOSSnowLeopardOrLater()) { + // 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 = launchd::PIDForJob("com.apple.Dock.agent"); + } else { + // On Leopard, the Dock doesn't have a known fixed job label name as it + // does on Snow Leopard and Lion because it's not launched as a launch + // agent. Look the PID up by finding a process with the expected bundle + // identifier using the Process Manager. + pid = PIDForProcessBundleID("com.apple.dock"); + } + + if (pid <= 0) { + return; + } + + // Sending a SIGHUP 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, SIGHUP); +} + +} // namespace + +void 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. + + // There's enough potential allocation in this function to justify a + // distinct pool. + base::mac::ScopedNSAutoreleasePool autorelease_pool; + + NSString* const kDockDomain = @"com.apple.dock"; + NSUserDefaults* user_defaults = [NSUserDefaults standardUserDefaults]; + + NSDictionary* dock_plist_const = + [user_defaults persistentDomainForName:kDockDomain]; + if (![dock_plist_const isKindOfClass:[NSDictionary class]]) { + LOG(ERROR) << "dock_plist_const not NSDictionary"; + return; + } + NSMutableDictionary* dock_plist = + [NSMutableDictionary dictionaryWithDictionary:dock_plist_const]; + + NSString* const kDockPersistentAppsKey = @"persistent-apps"; + NSArray* persistent_apps_const = + [dock_plist objectForKey:kDockPersistentAppsKey]; + if (![persistent_apps_const isKindOfClass:[NSArray class]]) { + LOG(ERROR) << "persistent_apps_const not NSArray"; + return; + } + NSMutableArray* persistent_apps = + [NSMutableArray arrayWithArray:persistent_apps_const]; + + NSMutableArray* persistent_app_paths = PersistentAppPaths(persistent_apps); + if (!persistent_app_paths) { + return; + } + + // Directories in the Dock's plist are given with trailing slashes. Since + // installed_path and dmg_app_path both refer to application bundles, + // they're directories and will show up with trailing slashes. This is an + // artifact of the Dock's internal use of CFURL. Look for paths that match, + // and when adding an item to the Dock's plist, keep it in the form that the + // Dock likes. + NSString* installed_path_dock = [installed_path stringByAppendingString:@"/"]; + NSString* dmg_app_path_dock = [dmg_app_path stringByAppendingString:@"/"]; + + 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 objectAtIndex:index]; + if ([app_path isEqualToString:installed_path_dock]) { + // 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_dock]) { + // 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 objectAtIndex:index] lastPathComponent]; + if ([dock_app_name isEqualToString:app_name]) { + app_index = index; + break; + } + } + } + +#if defined(GOOGLE_CHROME_BUILD) + if (app_index == NSNotFound) { + // If this is an officially-branded Chrome (including Canary) and an + // application matching the "other" flavor is already in the Dock, put + // them next to each other. Google Chrome will precede Google Chrome + // Canary in the Dock. + NSString* chrome_name = @"Google Chrome.app"; + NSString* canary_name = @"Google Chrome Canary.app"; + for (NSUInteger index = 0; index < [persistent_apps count]; ++index) { + NSString* dock_app_name = + [[persistent_app_paths objectAtIndex:index] lastPathComponent]; + if ([dock_app_name isEqualToString:canary_name] && + [app_name isEqualToString:chrome_name]) { + app_index = index; + + // Break: put Google Chrome.app before the first Google Chrome + // Canary.app. + break; + } else if ([dock_app_name isEqualToString:chrome_name] && + [app_name isEqualToString:canary_name]) { + app_index = index + 1; + + // No break: put Google Chrome Canary.app after the last Google + // Chrome.app. + } + } + } +#endif // GOOGLE_CHROME_BUILD + + if (app_index == NSNotFound) { + // Put the new application after the last browser application already + // present in the Dock. + NSArray* other_browser_app_names = + [NSArray arrayWithObjects: +#if defined(GOOGLE_CHROME_BUILD) + @"Chromium.app", // Unbranded Google Chrome +#else + @"Google Chrome.app", + @"Google Chrome Canary.app", +#endif + @"Safari.app", + @"Firefox.app", + @"Camino.app", + @"Opera.app", + @"OmniWeb.app", + @"WebKit.app", // Safari nightly + @"Aurora.app", // Firefox dev + @"Nightly.app", // Firefox nightly + nil]; + for (NSUInteger index = 0; index < [persistent_apps count]; ++index) { + NSString* dock_app_name = + [[persistent_app_paths objectAtIndex:index] lastPathComponent]; + if ([other_browser_app_names containsObject:dock_app_name]) { + app_index = index + 1; + } + } + } + + if (app_index == NSNotFound) { + // Put the new application last in the Dock. + app_index = [persistent_apps count]; + } + + // Set up the new Dock tile. + NSDictionary* new_tile_file_data = + [NSDictionary dictionaryWithObjectsAndKeys: + installed_path_dock, kDockCFURLStringKey, + [NSNumber numberWithInt:0], kDockCFURLStringTypeKey, + nil]; + NSDictionary* new_tile_data = + [NSDictionary dictionaryWithObject:new_tile_file_data + forKey:kDockFileDataKey]; + NSDictionary* new_tile = + [NSDictionary dictionaryWithObject:new_tile_data + forKey:kDockTileDataKey]; + + // Add the new tile to the Dock. + [persistent_apps insertObject:new_tile atIndex:app_index]; + [persistent_app_paths insertObject:installed_path_dock 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; + } + + // Rewrite the plist. + [dock_plist setObject:persistent_apps forKey:kDockPersistentAppsKey]; + [user_defaults setPersistentDomain:dock_plist forName:kDockDomain]; + + Restart(); +} + +} // namespace dock diff --git a/chrome/browser/mac/install_from_dmg.mm b/chrome/browser/mac/install_from_dmg.mm index 07e316f..b263491 100644 --- a/chrome/browser/mac/install_from_dmg.mm +++ b/chrome/browser/mac/install_from_dmg.mm @@ -27,10 +27,10 @@ #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "chrome/browser/mac/authorization_util.h" +#include "chrome/browser/mac/dock.h" #include "chrome/browser/mac/scoped_authorizationref.h" #include "chrome/browser/mac/scoped_ioobject.h" #import "chrome/browser/mac/keystone_glue.h" -#include "chrome/browser/mac/launchd.h" #include "chrome/browser/mac/relauncher.h" #include "chrome/common/chrome_constants.h" #include "grit/chromium_strings.h" @@ -49,11 +49,6 @@ namespace { -NSString* const kDockTileDataKey = @"tile-data"; -NSString* const kDockFileDataKey = @"file-data"; -NSString* const kDockCFURLStringKey = @"_CFURLString"; -NSString* const kDockCFURLStringTypeKey = @"_CFURLStringType"; - // Given an io_service_t (expected to be of class IOMedia), walks the ancestor // chain, returning the closest ancestor that implements class IOHDIXHDDrive, // if any. If no such ancestor is found, returns NULL. Following the "copy" @@ -139,7 +134,7 @@ bool MediaResidesOnDiskImage(io_service_t media, std::string* image_path) { observed_type.assign(", observed "); observed_type.append(base::SysCFStringRefToUTF8(observed_type_cf)); } - LOG(ERROR) << "image-path: expected CFData" << observed_type; + LOG(ERROR) << "image-path: expected CFData, observed " << observed_type; return true; } @@ -347,294 +342,6 @@ bool InstallFromDiskImage(AuthorizationRef authorization_arg, return true; } -// 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) { - 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 objectForKey:kDockTileDataKey]; - if (![tile_data isKindOfClass:[NSDictionary class]]) { - LOG(ERROR) << "tile_data not NSDictionary"; - return nil; - } - - NSDictionary* file_data = [tile_data objectForKey:kDockFileDataKey]; - if (![file_data isKindOfClass:[NSDictionary class]]) { - LOG(ERROR) << "file_data not NSDictionary"; - return nil; - } - - NSNumber* type = [file_data objectForKey:kDockCFURLStringTypeKey]; - if (![type isKindOfClass:[NSNumber class]]) { - LOG(ERROR) << "type not NSNumber"; - return nil; - } - if ([type intValue] != 0) { - LOG(ERROR) << "type not 0"; - return nil; - } - - NSString* path = [file_data objectForKey:kDockCFURLStringKey]; - if (![path isKindOfClass:[NSString class]]) { - LOG(ERROR) << "path not NSString"; - return nil; - } - - [app_paths addObject:path]; - } - - return app_paths; -} - -// Adds an icon to the Dock pointing to |installed_path| if one is not already -// present. |dmg_app_path| is the path to the install source. Its tile will be -// removed if present. If any changes are made to the Dock's configuration, -// the Dock process is restarted to reflect those changes. -// -// Various heruistics are used to determine where to place the new icon -// relative to other items already present in the Dock: -// - If installed_path is already in the Dock, no new tiles for this path -// will be added. -// - If dmg_app_path is present in the Dock, it will be removed. If -// installed_path is not already present, the new tile referencing -// installed_path will be placed where the dmg_app_path tile was. This -// keeps the tile where a user expects it if they dragged the application -// icon from a disk image into the Dock and then clicked on the new icon -// in the Dock. -// - The new tile will precede any application with the same name already -// in the Dock. -// - In an official build, a new tile for Google Chrome will be placed -// immediately before the first existing tile for Google Chrome Canary, -// and a new tile for Google Chrome Canary will be placed immediately after -// the last existing tile for Google Chrome. -// - The new tile will be placed immediately after the last tile for another -// browser application already in the Dock. -// - The new tile will be placed last in the Dock. -// For the purposes of these comparisons, applications are identified by the -// last component in their path. For example, any application named Safari.app -// will be treated as a browser. If the user renames an application on disk, -// it will alter the result. Looking up the bundle ID could be slightly more -// robust in the presence of such alterations, but it's not thought to be a -// large enough problem to warrant such lookups. -// -// The changes made to the Dock's configuration are the minimal changes -// necessary to cause the desired behavior. Although it's possible to set -// additional properties on the dock tile added to the Dock's plist, this -// is not done. Upon relaunch, Dock.app will determine the correct values for -// the properties it requires and add them to its configuration. -// -// 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. AddDockIcon could call CoreDockAddFileToDock if available, but -// CoreDockAddFileToDock seems to always to add the new Dock icon last, where -// AddDockIcon 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. -void AddDockIcon(NSString* installed_path, NSString* dmg_app_path) { - // There's enough potential allocation in this function to justify a - // distinct pool. - base::mac::ScopedNSAutoreleasePool autorelease_pool; - - NSString* const kDockDomain = @"com.apple.dock"; - NSUserDefaults* user_defaults = [NSUserDefaults standardUserDefaults]; - - NSDictionary* dock_plist_const = - [user_defaults persistentDomainForName:kDockDomain]; - if (![dock_plist_const isKindOfClass:[NSDictionary class]]) { - LOG(ERROR) << "dock_plist_const not NSDictionary"; - return; - } - NSMutableDictionary* dock_plist = - [NSMutableDictionary dictionaryWithDictionary:dock_plist_const]; - - NSString* const kDockPersistentAppsKey = @"persistent-apps"; - NSArray* persistent_apps_const = - [dock_plist objectForKey:kDockPersistentAppsKey]; - if (![persistent_apps_const isKindOfClass:[NSArray class]]) { - LOG(ERROR) << "persistent_apps_const not NSArray"; - return; - } - NSMutableArray* persistent_apps = - [NSMutableArray arrayWithArray:persistent_apps_const]; - - NSMutableArray* persistent_app_paths = PersistentAppPaths(persistent_apps); - if (!persistent_app_paths) { - return; - } - - // Directories in the Dock's plist are given with trailing slashes. Since - // installed_path and dmg_app_path both refer to application bundles, - // they're directories and will show up with trailing slashes. This is an - // artifact of the Dock's internal use of CFURL. Look for paths that match, - // and when adding an item to the Dock's plist, keep it in the form that the - // Dock likes. - NSString* installed_path_dock = [installed_path stringByAppendingString:@"/"]; - NSString* dmg_app_path_dock = [dmg_app_path stringByAppendingString:@"/"]; - - 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 objectAtIndex:index]; - if ([app_path isEqualToString:installed_path_dock]) { - // 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_dock]) { - // 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 objectAtIndex:index] lastPathComponent]; - if ([dock_app_name isEqualToString:app_name]) { - app_index = index; - break; - } - } - } - -#if defined(GOOGLE_CHROME_BUILD) - if (app_index == NSNotFound) { - // If this is an officially-branded Chrome (including Canary) and an - // application matching the "other" flavor is already in the Dock, put - // them next to each other. Google Chrome will precede Google Chrome - // Canary in the Dock. - NSString* chrome_name = @"Google Chrome.app"; - NSString* canary_name = @"Google Chrome Canary.app"; - for (NSUInteger index = 0; index < [persistent_apps count]; ++index) { - NSString* dock_app_name = - [[persistent_app_paths objectAtIndex:index] lastPathComponent]; - if ([dock_app_name isEqualToString:canary_name] && - [app_name isEqualToString:chrome_name]) { - app_index = index; - - // Break: put Google Chrome.app before the first Google Chrome - // Canary.app. - break; - } else if ([dock_app_name isEqualToString:chrome_name] && - [app_name isEqualToString:canary_name]) { - app_index = index + 1; - - // No break: put Google Chrome Canary.app after the last Google - // Chrome.app. - } - } - } -#endif // GOOGLE_CHROME_BUILD - - if (app_index == NSNotFound) { - // Put the new application after the last browser application already - // present in the Dock. - NSArray* other_browser_app_names = - [NSArray arrayWithObjects: -#if defined(GOOGLE_CHROME_BUILD) - @"Chromium.app", // Unbranded Google Chrome -#else - @"Google Chrome.app", - @"Google Chrome Canary.app", -#endif - @"Safari.app", - @"Firefox.app", - @"Camino.app", - @"Opera.app", - @"OmniWeb.app", - @"WebKit.app", // Safari nightly - @"Aurora.app", // Firefox dev - @"Nightly.app", // Firefox nightly - nil]; - for (NSUInteger index = 0; index < [persistent_apps count]; ++index) { - NSString* dock_app_name = - [[persistent_app_paths objectAtIndex:index] lastPathComponent]; - if ([other_browser_app_names containsObject:dock_app_name]) { - app_index = index + 1; - } - } - } - - if (app_index == NSNotFound) { - // Put the new application last in the Dock. - app_index = [persistent_apps count]; - } - - // Set up the new Dock tile. - NSDictionary* new_tile_file_data = - [NSDictionary dictionaryWithObjectsAndKeys: - installed_path_dock, kDockCFURLStringKey, - [NSNumber numberWithInt:0], kDockCFURLStringTypeKey, - nil]; - NSDictionary* new_tile_data = - [NSDictionary dictionaryWithObject:new_tile_file_data - forKey:kDockFileDataKey]; - NSDictionary* new_tile = - [NSDictionary dictionaryWithObject:new_tile_data - forKey:kDockTileDataKey]; - - // Add the new tile to the Dock. - [persistent_apps insertObject:new_tile atIndex:app_index]; - [persistent_app_paths insertObject:installed_path_dock 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; - } - - // Rewrite the plist. - [dock_plist setObject:persistent_apps forKey:kDockPersistentAppsKey]; - [user_defaults setPersistentDomain:dock_plist forName:kDockDomain]; - - // Restart the Dock. 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. - // Sending a SIGHUP 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.") - launchd::SignalJob("com.apple.Dock.agent", SIGHUP); -} - // Launches the application at installed_path. The helper application // contained within install_path will be used for the relauncher process. This // keeps Launch Services from ever having to see or think about the helper @@ -748,7 +455,12 @@ bool MaybeInstallFromDiskImage() { return false; } - AddDockIcon(target_path, source_path); + dock::AddIcon(target_path, source_path); + + if (dmg_bsd_device_name.empty()) { + // Not fatal, just diagnostic. + LOG(ERROR) << "Could not determine disk image BSD device name"; + } if (!LaunchInstalledApp(target_path, dmg_bsd_device_name)) { ShowErrorDialog(); diff --git a/chrome/browser/mac/launchd.cc b/chrome/browser/mac/launchd.cc index aa3506f..3a55c3b 100644 --- a/chrome/browser/mac/launchd.cc +++ b/chrome/browser/mac/launchd.cc @@ -5,7 +5,6 @@ #include "chrome/browser/mac/launchd.h" #include <launch.h> -#include <signal.h> #include "base/logging.h" #include "chrome/browser/mac/scoped_launch_data.h" @@ -46,15 +45,21 @@ launch_data_t MessageForJob(const std::string& job_label, return launch_msg(message); } -// Returns the process ID for |job_label|, or -1 on error. +} // namespace + pid_t PIDForJob(const std::string& job_label) { ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB)); if (!response) { return -1; } - if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) { - LOG(ERROR) << "PIDForJob: expected dictionary"; + launch_data_type_t response_type = launch_data_get_type(response); + if (response_type != LAUNCH_DATA_DICTIONARY) { + if (response_type == LAUNCH_DATA_ERRNO) { + LOG(ERROR) << "PIDForJob: error " << launch_data_get_errno(response); + } else { + LOG(ERROR) << "PIDForJob: expected dictionary, got " << response_type; + } return -1; } @@ -73,15 +78,4 @@ pid_t PIDForJob(const std::string& job_label) { return launch_data_get_integer(pid_data); } -} // namespace - -void SignalJob(const std::string& job_name, int signal) { - pid_t pid = PIDForJob(job_name); - if (pid <= 0) { - return; - } - - kill(pid, signal); -} - } // namespace launchd diff --git a/chrome/browser/mac/launchd.h b/chrome/browser/mac/launchd.h index f2e9bdd..a65e354 100644 --- a/chrome/browser/mac/launchd.h +++ b/chrome/browser/mac/launchd.h @@ -6,12 +6,14 @@ #define CHROME_BROWSER_MAC_LAUNCHD_H_ #pragma once +#include <sys/types.h> + #include <string> namespace launchd { -// Sends a signal to the job at |job_label|. -void SignalJob(const std::string& job_label, int signal); +// Returns the process ID for |job_label|, or -1 on error. +pid_t PIDForJob(const std::string& job_label); } // namespace launchd diff --git a/chrome/browser/mac/relauncher.cc b/chrome/browser/mac/relauncher.cc index d5ce323..fdbad51 100644 --- a/chrome/browser/mac/relauncher.cc +++ b/chrome/browser/mac/relauncher.cc @@ -368,9 +368,9 @@ int RelauncherMain(const MainFunctionParams& main_parameters) { NULL // initialEvent }; - OSStatus err = LSOpenApplication(&ls_parameters, NULL); - if (err != noErr) { - LOG(ERROR) << "LSOpenApplication: " << err; + OSStatus status = LSOpenApplication(&ls_parameters, NULL); + if (status != noErr) { + LOG(ERROR) << "LSOpenApplication: " << status; return 1; } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 53b2686..965ed0b 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1287,8 +1287,8 @@ 'browser/importer/importer_progress_observer.h', 'browser/importer/importer_type.cc', 'browser/importer/importer_type.h', - 'browser/importer/importer_util.cc', - 'browser/importer/importer_util.h', + 'browser/importer/importer_util.cc', + 'browser/importer/importer_util.h', 'browser/importer/in_process_importer_bridge.cc', 'browser/importer/in_process_importer_bridge.h', 'browser/importer/mork_reader.cc', @@ -1349,6 +1349,8 @@ 'browser/language_order_table_model.h', 'browser/mac/authorization_util.h', 'browser/mac/authorization_util.mm', + 'browser/mac/dock.h', + 'browser/mac/dock.mm', 'browser/mac/install_from_dmg.h', 'browser/mac/install_from_dmg.mm', 'browser/mac/keystone_glue.h', @@ -2824,8 +2826,8 @@ 'browser/ui/gtk/notifications/balloon_view_gtk.h', 'browser/ui/gtk/notifications/balloon_view_host_gtk.cc', 'browser/ui/gtk/notifications/balloon_view_host_gtk.h', - 'browser/ui/gtk/omnibox/omnibox_popup_view_gtk.cc', - 'browser/ui/gtk/omnibox/omnibox_popup_view_gtk.h', + 'browser/ui/gtk/omnibox/omnibox_popup_view_gtk.cc', + 'browser/ui/gtk/omnibox/omnibox_popup_view_gtk.h', 'browser/ui/gtk/omnibox/omnibox_view_gtk.cc', 'browser/ui/gtk/omnibox/omnibox_view_gtk.h', 'browser/ui/gtk/overflow_button.cc', |