File: shell_integration_mac.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (309 lines) | stat: -rw-r--r-- 10,826 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/shell_integration.h"

#include <AppKit/AppKit.h>
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

#include "base/apple/bridging.h"
#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "build/branding_buildflags.h"
#include "chrome/common/channel_info.h"
#include "components/version_info/version_info.h"
#import "net/base/apple/url_conversions.h"

namespace shell_integration {

namespace {

// Returns the bundle id of the default client application for the given
// scheme or nil on failure.
NSString* GetBundleIdForDefaultAppForScheme(NSString* scheme) {
  NSURL* scheme_url =
      [NSURL URLWithString:[scheme stringByAppendingString:@":"]];

  NSURL* default_app_url =
      [NSWorkspace.sharedWorkspace URLForApplicationToOpenURL:scheme_url];
  if (!default_app_url) {
    return nil;
  }

  NSBundle* default_app_bundle = [NSBundle bundleWithURL:default_app_url];
  return default_app_bundle.bundleIdentifier;
}

// Returns the bundle ID of the default client application for `type`, or nil on
// failure.
NSString* GetBundleIdForDefaultAppForUTType(NSString* type) {
  UTType* uttype = [UTType typeWithIdentifier:type];
  if (!uttype) {
    return nil;
  }
  NSURL* default_app_url =
      [NSWorkspace.sharedWorkspace URLForApplicationToOpenContentType:uttype];
  if (!default_app_url) {
    return nil;
  }
  return [NSBundle bundleWithURL:default_app_url].bundleIdentifier;
}

}  // namespace

bool SetAsDefaultBrowser() {
  // We really do want the outer bundle here, not the main bundle since
  // setting a shortcut to Chrome as the default browser doesn't make sense.
  NSURL* app_bundle = base::apple::OuterBundleURL();
  if (!app_bundle) {
    return false;
  }

  [NSWorkspace.sharedWorkspace setDefaultApplicationAtURL:app_bundle
                                     toOpenURLsWithScheme:@"http"
                                        completionHandler:^(NSError*){
                                        }];
  [NSWorkspace.sharedWorkspace setDefaultApplicationAtURL:app_bundle
                                     toOpenURLsWithScheme:@"https"
                                        completionHandler:^(NSError*){
                                        }];
  [NSWorkspace.sharedWorkspace setDefaultApplicationAtURL:app_bundle
                                        toOpenContentType:UTTypeHTML
                                        completionHandler:^(NSError*){
                                        }];
  // TODO(https://crbug.com/40248220): Passing empty completion handlers,
  // above, is kinda broken, but given that this API is synchronous, nothing
  // better can be done. This entire API should be rebuilt.

  // The CoreServicesUIAgent presents a dialog asking the user to confirm their
  // new default browser choice, but the agent sometimes orders the dialog
  // behind the Chrome window. The user never sees the dialog, and therefore
  // never confirms the change. Make the CoreServicesUIAgent active so the
  // confirmation dialog comes to the front.
  NSString* const kCoreServicesUIAgentBundleID =
      @"com.apple.coreservices.uiagent";

  for (NSRunningApplication* application in NSWorkspace.sharedWorkspace
           .runningApplications) {
    if ([application.bundleIdentifier
            isEqualToString:kCoreServicesUIAgentBundleID]) {
      [application activateWithOptions:NSApplicationActivateAllWindows];
      break;
    }
  }

  return true;
}

bool SetAsDefaultClientForScheme(const std::string& scheme) {
  if (scheme.empty()) {
    return false;
  }

  if (GetDefaultSchemeClientSetPermission() != SET_DEFAULT_UNATTENDED) {
    return false;
  }

  // We really do want the main bundle here since it makes sense to set an
  // app shortcut as a default scheme handler.
  NSURL* app_bundle = base::apple::MainBundleURL();
  if (!app_bundle) {
    return false;
  }

  [NSWorkspace.sharedWorkspace
      setDefaultApplicationAtURL:app_bundle
            toOpenURLsWithScheme:base::SysUTF8ToNSString(scheme)
               completionHandler:^(NSError*){
               }];

  // TODO(https://crbug.com/40248220): Passing empty completion handlers,
  // above, is kinda broken, but given that this API is synchronous, nothing
  // better can be done. This entire API should be rebuilt.
  return true;
}

bool SetAsDefaultHandlerForUTType(const std::string& type) {
  if (type.empty()) {
    return false;
  }
  UTType* uttype = [UTType typeWithIdentifier:base::SysUTF8ToNSString(type)];
  if (!uttype) {
    return false;
  }
  NSURL* app_bundle = base::apple::OuterBundleURL();
  if (!app_bundle) {
    return false;
  }
  [NSWorkspace.sharedWorkspace setDefaultApplicationAtURL:app_bundle
                                        toOpenContentType:uttype
                                        completionHandler:^(NSError*){
                                        }];
  return true;
}

std::u16string GetApplicationNameForScheme(const GURL& url) {
  NSURL* ns_url = net::NSURLWithGURL(url);
  if (!ns_url) {
    return {};
  }

  NSURL* app_url =
      [NSWorkspace.sharedWorkspace URLForApplicationToOpenURL:ns_url];
  if (!app_url) {
    return std::u16string();
  }

  NSString* app_display_name =
      [NSFileManager.defaultManager displayNameAtPath:app_url.path];
  return base::SysNSStringToUTF16(app_display_name);
}

std::vector<base::FilePath> GetAllApplicationPathsForURL(const GURL& url) {
  NSURL* ns_url = net::NSURLWithGURL(url);
  if (!ns_url) {
    return {};
  }

  NSArray* app_urls =
      [NSWorkspace.sharedWorkspace URLsForApplicationsToOpenURL:ns_url];
  if (app_urls.count == 0) {
    return {};
  }

  std::vector<base::FilePath> app_paths;
  app_paths.reserve(app_urls.count);
  for (NSURL* app_url in app_urls) {
    app_paths.push_back(base::apple::NSURLToFilePath(app_url));
  }
  return app_paths;
}

bool CanApplicationHandleURL(const base::FilePath& app_path, const GURL& url) {
  NSURL* ns_item_url = net::NSURLWithGURL(url);
  NSURL* ns_app_url = base::apple::FilePathToNSURL(app_path);
  Boolean result = FALSE;
  LSCanURLAcceptURL(base::apple::NSToCFPtrCast(ns_item_url),
                    base::apple::NSToCFPtrCast(ns_app_url), kLSRolesAll,
                    kLSAcceptDefault, &result);
  return result;
}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Returns true if `other_identifier` is another instance (or channel) of
// Chrome, given Chrome bundle ID `chrome_identifier`.
bool IsAnotherChromeChannel(NSString* chrome_identifier,
                            NSString* other_identifier) {
  // Flavors of Chrome are of the constructions "com.google.Chrome" and
  // "com.google.Chrome.beta". If the first three components match, then these
  // are variant flavors.
  auto three_components_only_lopper = [](NSString* bundle_id) {
    NSMutableArray<NSString*>* parts =
        [[bundle_id componentsSeparatedByString:@"."] mutableCopy];
    while (parts.count > 3) {
      [parts removeLastObject];
    }
    return [parts componentsJoinedByString:@"."];
  };
  NSString* chrome_identifier_lopped =
      three_components_only_lopper(chrome_identifier);
  NSString* other_identifier_lopped =
      three_components_only_lopper(other_identifier);
  return [chrome_identifier_lopped isEqualToString:other_identifier_lopped];
}
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

// Attempt to determine if this instance of Chrome is the default browser and
// return the appropriate state. (Defined as being the handler for HTTP/HTTPS
// schemes; we don't want to report "no" here if the user has simply chosen
// to open HTML files in a text editor and FTP links with an FTP client.)
DefaultWebClientState GetDefaultBrowser() {
  // We really do want the outer bundle here, since this we want to know the
  // status of the main Chrome bundle and not a shortcut.
  NSString* my_identifier = base::apple::OuterBundle().bundleIdentifier;
  if (!my_identifier) {
    return UNKNOWN_DEFAULT;
  }

  NSString* default_browser = GetBundleIdForDefaultAppForScheme(@"http");
  if ([default_browser isEqualToString:my_identifier]) {
    return IS_DEFAULT;
  }

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (IsAnotherChromeChannel(my_identifier, default_browser)) {
    return OTHER_MODE_IS_DEFAULT;
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  return NOT_DEFAULT;
}

// Returns true if Firefox is the default browser for the current user.
bool IsFirefoxDefaultBrowser() {
  return [GetBundleIdForDefaultAppForScheme(@"http")
      isEqualToString:@"org.mozilla.firefox"];
}

// Attempt to determine if this instance of Chrome is the default client
// application for the given scheme and return the appropriate state.
DefaultWebClientState IsDefaultClientForScheme(const std::string& scheme) {
  if (scheme.empty()) {
    return UNKNOWN_DEFAULT;
  }

  // We really do want the main bundle here since it makes sense to set an
  // app shortcut as a default scheme handler.
  NSString* my_identifier = base::apple::MainBundle().bundleIdentifier;
  if (!my_identifier) {
    return UNKNOWN_DEFAULT;
  }

  NSString* default_browser =
      GetBundleIdForDefaultAppForScheme(base::SysUTF8ToNSString(scheme));
  return [default_browser isEqualToString:my_identifier] ? IS_DEFAULT
                                                         : NOT_DEFAULT;
}

DefaultWebClientState IsDefaultHandlerForUTType(const std::string& type) {
  if (type.empty()) {
    return UNKNOWN_DEFAULT;
  }
  NSString* my_identifier = base::apple::OuterBundle().bundleIdentifier;
  if (!my_identifier) {
    return UNKNOWN_DEFAULT;
  }
  NSString* default_app =
      GetBundleIdForDefaultAppForUTType(base::SysUTF8ToNSString(type));
  if (!default_app) {
    return UNKNOWN_DEFAULT;
  }
  if ([default_app isEqualToString:my_identifier]) {
    return IS_DEFAULT;
  }
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (IsAnotherChromeChannel(my_identifier, default_app)) {
    return OTHER_MODE_IS_DEFAULT;
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  return NOT_DEFAULT;
}

namespace internal {

DefaultWebClientSetPermission GetPlatformSpecificDefaultWebClientSetPermission(
    WebClientSetMethod method) {
  // This should be `SET_DEFAULT_INTERACTIVE`, but that changes how
  // `DefaultBrowserWorker` and `DefaultSchemeClientWorker` work.
  // TODO(https://crbug.com/40248220): Migrate all callers to the new API,
  // migrate all the Mac code to integrate with it, and change this to return
  // the correct value.
  return SET_DEFAULT_UNATTENDED;
}

}  // namespace internal

}  // namespace shell_integration