1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/shell_integration.h"
7 #include "base/mac/bundle_locations.h"
8 #include "base/mac/foundation_util.h"
9 #include "chrome/common/chrome_version_info.h"
10 #import "third_party/mozilla/NSWorkspace+Utils.h"
12 ShellIntegration::DefaultWebClientSetPermission
13 ShellIntegration::CanSetAsDefaultBrowser() {
14 if (chrome::VersionInfo::GetChannel() !=
15 chrome::VersionInfo::CHANNEL_CANARY) {
16 return SET_DEFAULT_UNATTENDED;
19 return SET_DEFAULT_NOT_ALLOWED;
22 // Sets Chromium as default browser to be used by the operating system. This
23 // applies only for the current user. Returns false if this cannot be done, or
24 // if the operation fails.
25 bool ShellIntegration::SetAsDefaultBrowser() {
26 if (CanSetAsDefaultBrowser() != SET_DEFAULT_UNATTENDED)
29 // We really do want the outer bundle here, not the main bundle since setting
30 // a shortcut to Chrome as the default browser doesn't make sense.
31 NSString* identifier = [base::mac::OuterBundle() bundleIdentifier];
35 [[NSWorkspace sharedWorkspace] setDefaultBrowserWithIdentifier:identifier];
39 // Sets Chromium as the default application to be used by the operating system
40 // for the given protocol. This applies only for the current user. Returns false
41 // if this cannot be done, or if the operation fails.
42 bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
46 if (CanSetAsDefaultProtocolClient() != SET_DEFAULT_UNATTENDED)
49 // We really do want the main bundle here since it makes sense to set an
50 // app shortcut as a default protocol handler.
51 NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
55 NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
56 OSStatus return_code =
57 LSSetDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns),
58 base::mac::NSToCFCast(identifier));
59 return return_code == noErr;
64 // Returns true if |identifier| is the bundle id of the default browser.
65 bool IsIdentifierDefaultBrowser(NSString* identifier) {
66 NSString* default_browser =
67 [[NSWorkspace sharedWorkspace] defaultBrowserIdentifier];
71 // We need to ensure we do the comparison case-insensitive as LS doesn't
72 // persist the case of our bundle id.
73 NSComparisonResult result =
74 [default_browser caseInsensitiveCompare:identifier];
75 return result == NSOrderedSame;
78 // Returns true if |identifier| is the bundle id of the default client
79 // application for the given protocol.
80 bool IsIdentifierDefaultProtocolClient(NSString* identifier,
82 CFStringRef default_client_cf =
83 LSCopyDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol));
84 NSString* default_client = static_cast<NSString*>(
85 base::mac::CFTypeRefToNSObjectAutorelease(default_client_cf));
89 // We need to ensure we do the comparison case-insensitive as LS doesn't
90 // persist the case of our bundle id.
91 NSComparisonResult result =
92 [default_client caseInsensitiveCompare:identifier];
93 return result == NSOrderedSame;
98 // Attempt to determine if this instance of Chrome is the default browser and
99 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS
100 // protocols; we don't want to report "no" here if the user has simply chosen
101 // to open HTML files in a text editor and FTP links with an FTP client.)
102 ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
103 // We really do want the outer bundle here, since this we want to know the
104 // status of the main Chrome bundle and not a shortcut.
105 NSString* my_identifier = [base::mac::OuterBundle() bundleIdentifier];
107 return UNKNOWN_DEFAULT;
109 return IsIdentifierDefaultBrowser(my_identifier) ? IS_DEFAULT : NOT_DEFAULT;
112 // Returns true if Firefox is the default browser for the current user.
113 bool ShellIntegration::IsFirefoxDefaultBrowser() {
114 return IsIdentifierDefaultBrowser(@"org.mozilla.firefox");
117 // Attempt to determine if this instance of Chrome is the default client
118 // application for the given protocol and return the appropriate state.
119 ShellIntegration::DefaultWebClientState
120 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
121 if (protocol.empty())
122 return UNKNOWN_DEFAULT;
124 // We really do want the main bundle here since it makes sense to set an
125 // app shortcut as a default protocol handler.
126 NSString* my_identifier = [base::mac::MainBundle() bundleIdentifier];
128 return UNKNOWN_DEFAULT;
130 NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
131 return IsIdentifierDefaultProtocolClient(my_identifier, protocol_ns) ?
132 IS_DEFAULT : NOT_DEFAULT;