summaryrefslogtreecommitdiffstats
path: root/chrome/browser/mac/dock.mm
blob: 200e6b05bb84e24ed62822353708e74f3869d704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// 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