Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(969)

Side by Side Diff: chrome/browser/shell_integration_mac.mm

Issue 6961013: Allow chrome to become the os default handler for arbitrary protocols on mac/win. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More changes for comments; fix for a bug introduced with DefaultWebClientWorker refactoring Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 the current user. Returns false if this cannot be done, or
14 // if 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 // We really do want the main bundle here, not base::mac::MainAppBundle(),
47 NSString* myIdentifier = [[NSBundle mainBundle] bundleIdentifier]; 94 // which is the bundle for the framework.
48 if (!myIdentifier) 95 NSString* my_identifier = [[NSBundle mainBundle] bundleIdentifier];
49 return UNKNOWN_DEFAULT_BROWSER; 96 if (!my_identifier)
50 return IsIdentifierDefaultBrowser(myIdentifier) ? IS_DEFAULT_BROWSER 97 return UNKNOWN_DEFAULT_WEB_CLIENT;
51 : NOT_DEFAULT_BROWSER; 98
99 return IsIdentifierDefaultBrowser(my_identifier) ? IS_DEFAULT_WEB_CLIENT
100 : NOT_DEFAULT_WEB_CLIENT;
52 } 101 }
53 102
54 // Returns true if Firefox is the default browser for the current user. 103 // Returns true if Firefox is the default browser for the current user.
55 bool ShellIntegration::IsFirefoxDefaultBrowser() { 104 bool ShellIntegration::IsFirefoxDefaultBrowser() {
56 return IsIdentifierDefaultBrowser(@"org.mozilla.firefox"); 105 return IsIdentifierDefaultBrowser(@"org.mozilla.firefox");
57 } 106 }
107
108 // Attempt to determine if this instance of Chrome is the default client
109 // application for the given protocol and return the appropriate state.
110 ShellIntegration::DefaultWebClientState
111 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
112 if (protocol.empty())
113 return UNKNOWN_DEFAULT_WEB_CLIENT;
114
115 // We really do want the main bundle here, not base::mac::MainAppBundle(),
116 // which is the bundle for the framework.
117 NSString* my_identifier = [[NSBundle mainBundle] bundleIdentifier];
118 if (!my_identifier)
119 return UNKNOWN_DEFAULT_WEB_CLIENT;
120
121 NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
122 return IsIdentifierDefaultProtocolClient(my_identifier, protocol_ns) ?
123 IS_DEFAULT_WEB_CLIENT : NOT_DEFAULT_WEB_CLIENT;
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698