Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/shell_integration.h" | 5 #include "chrome/browser/shell_integration.h" |
| 6 | 6 |
| 7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #include "base/mac/foundation_util.h" | |
| 8 #include "chrome/browser/platform_util.h" | 9 #include "chrome/browser/platform_util.h" |
| 9 #import "third_party/mozilla/NSWorkspace+Utils.h" | 10 #import "third_party/mozilla/NSWorkspace+Utils.h" |
| 10 | 11 |
| 11 // Sets Chromium as default browser (only for current user). Returns false if | 12 // Sets Chromium as default browser to be used by the operating system. This |
| 12 // this operation fails. | 13 // applies only for current user. Returns false if this cannot be done, or if |
|
Mark Mentovai
2011/05/24 16:28:53
for the current user
benwells
2011/05/25 08:07:19
Done.
| |
| 14 // the operation fails. | |
| 13 bool ShellIntegration::SetAsDefaultBrowser() { | 15 bool ShellIntegration::SetAsDefaultBrowser() { |
| 14 if (!platform_util::CanSetAsDefaultBrowser()) | 16 if (!platform_util::CanSetAsDefaultBrowser()) |
| 15 return false; | 17 return false; |
| 16 | 18 |
| 17 // We really do want the main bundle here, not base::mac::MainAppBundle(), | 19 // We really do want the main bundle here, not base::mac::MainAppBundle(), |
| 18 // which is the bundle for the framework. | 20 // which is the bundle for the framework. |
| 19 NSString* identifier = [[NSBundle mainBundle] bundleIdentifier]; | 21 NSString* identifier = [[NSBundle mainBundle] bundleIdentifier]; |
| 22 if (!identifier) | |
| 23 return false; | |
| 24 | |
| 20 [[NSWorkspace sharedWorkspace] setDefaultBrowserWithIdentifier:identifier]; | 25 [[NSWorkspace sharedWorkspace] setDefaultBrowserWithIdentifier:identifier]; |
| 21 return true; | 26 return true; |
| 22 } | 27 } |
| 23 | 28 |
| 29 // Sets Chromium as the default application to be used by the operating system | |
| 30 // for the given protocol. This applies only for the current user. Returns false | |
| 31 // if this cannot be done, or if the operation fails. | |
| 32 bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) { | |
| 33 if (protocol.empty()) | |
| 34 return false; | |
| 35 | |
| 36 if (!platform_util::CanSetAsDefaultProtocolClient(protocol)) | |
| 37 return false; | |
| 38 | |
| 39 // We really do want the main bundle here, not base::mac::MainAppBundle(), | |
| 40 // which is the bundle for the framework. | |
| 41 NSString* identifier = [[NSBundle mainBundle] bundleIdentifier]; | |
| 42 if (!identifier) | |
| 43 return false; | |
| 44 | |
| 45 NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()]; | |
| 46 OSStatus return_code = | |
| 47 LSSetDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns), | |
| 48 base::mac::NSToCFCast(identifier)); | |
| 49 return return_code == noErr; | |
| 50 } | |
| 51 | |
| 24 namespace { | 52 namespace { |
| 25 | 53 |
| 26 // Returns true if |identifier| is the bundle id of the default browser. | 54 // Returns true if |identifier| is the bundle id of the default browser. |
| 27 bool IsIdentifierDefaultBrowser(NSString* identifier) { | 55 bool IsIdentifierDefaultBrowser(NSString* identifier) { |
| 28 NSString* defaultBrowser = | 56 NSString* default_browser = |
| 29 [[NSWorkspace sharedWorkspace] defaultBrowserIdentifier]; | 57 [[NSWorkspace sharedWorkspace] defaultBrowserIdentifier]; |
| 30 if (!defaultBrowser) | 58 if (!default_browser) |
| 31 return false; | 59 return false; |
| 60 | |
| 32 // We need to ensure we do the comparison case-insensitive as LS doesn't | 61 // We need to ensure we do the comparison case-insensitive as LS doesn't |
| 33 // persist the case of our bundle id. | 62 // persist the case of our bundle id. |
| 34 NSComparisonResult result = | 63 NSComparisonResult result = |
| 35 [defaultBrowser caseInsensitiveCompare:identifier]; | 64 [default_browser caseInsensitiveCompare:identifier]; |
| 36 return result == NSOrderedSame; | 65 return result == NSOrderedSame; |
| 37 } | 66 } |
| 38 | 67 |
| 68 // Returns true if |identifier| is the bundle id of the default client | |
| 69 // application for the given protocol. | |
| 70 bool IsIdentifierDefaultProtocolClient(NSString* identifier, | |
| 71 NSString* protocol) { | |
| 72 CFStringRef default_client_cf = | |
| 73 LSCopyDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol)); | |
| 74 NSString* default_client = static_cast<NSString*>( | |
| 75 base::mac::CFTypeRefToNSObjectAutorelease(default_client_cf)); | |
| 76 if (!default_client) | |
| 77 return false; | |
| 78 | |
| 79 // We need to ensure we do the comparison case-insensitive as LS doesn't | |
| 80 // persist the case of our bundle id. | |
| 81 NSComparisonResult result = | |
| 82 [default_client caseInsensitiveCompare:identifier]; | |
| 83 return result == NSOrderedSame; | |
| 84 } | |
| 85 | |
| 39 } // namespace | 86 } // namespace |
| 40 | 87 |
| 41 // Attempt to determine if this instance of Chrome is the default browser and | 88 // Attempt to determine if this instance of Chrome is the default browser and |
| 42 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS | 89 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS |
| 43 // protocols; we don't want to report "no" here if the user has simply chosen | 90 // protocols; we don't want to report "no" here if the user has simply chosen |
| 44 // to open HTML files in a text editor and FTP links with an FTP client.) | 91 // to open HTML files in a text editor and FTP links with an FTP client.) |
| 45 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { | 92 ShellIntegration::DefaultWebClientState ShellIntegration::IsDefaultBrowser() { |
| 46 // As above, we want to use the real main bundle. | 93 |
|
Mark Mentovai
2011/05/24 16:28:53
No blank line here.
benwells
2011/05/25 08:07:19
Done.
| |
| 47 NSString* myIdentifier = [[NSBundle mainBundle] bundleIdentifier]; | 94 // We really do want the main bundle here, not base::mac::MainAppBundle(), |
| 48 if (!myIdentifier) | 95 // which is the bundle for the framework. |
| 49 return UNKNOWN_DEFAULT_BROWSER; | 96 NSString* my_identifier = [[NSBundle mainBundle] bundleIdentifier]; |
| 50 return IsIdentifierDefaultBrowser(myIdentifier) ? IS_DEFAULT_BROWSER | 97 if (!my_identifier) |
| 51 : NOT_DEFAULT_BROWSER; | 98 return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 99 | |
| 100 return IsIdentifierDefaultBrowser(my_identifier) ? IS_DEFAULT_WEB_CLIENT | |
| 101 : NOT_DEFAULT_WEB_CLIENT; | |
| 52 } | 102 } |
| 53 | 103 |
| 54 // Returns true if Firefox is the default browser for the current user. | 104 // Returns true if Firefox is the default browser for the current user. |
| 55 bool ShellIntegration::IsFirefoxDefaultBrowser() { | 105 bool ShellIntegration::IsFirefoxDefaultBrowser() { |
| 56 return IsIdentifierDefaultBrowser(@"org.mozilla.firefox"); | 106 return IsIdentifierDefaultBrowser(@"org.mozilla.firefox"); |
| 57 } | 107 } |
| 108 | |
| 109 // Attempt to determine if this instance of Chrome is the default client | |
| 110 // application for the given protocol and return the appropriate state. | |
| 111 ShellIntegration::DefaultWebClientState | |
| 112 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) { | |
| 113 if (protocol.empty()) | |
| 114 return UNKNOWN_DEFAULT_WEB_CLIENT; | |
| 115 | |
| 116 // We really do want the main bundle here, not base::mac::MainAppBundle(), | |
| 117 // which is the bundle for the framework. | |
| 118 NSString* my_identifier = [[NSBundle mainBundle] bundleIdentifier]; | |
| 119 if (!my_identifier) | |
| 120 return UNKNOWN_DEFAULT_WEB_CLIENT; | |
| 121 | |
| 122 NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()]; | |
| 123 return IsIdentifierDefaultProtocolClient(my_identifier, protocol_ns) ? | |
| 124 IS_DEFAULT_WEB_CLIENT : NOT_DEFAULT_WEB_CLIENT; | |
| 125 } | |
| OLD | NEW |